Created
July 13, 2012 22:10
-
-
Save bobbychopra/3107875 to your computer and use it in GitHub Desktop.
JSON Deserialize
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[DataContract] | |
public class PortfolioManagementUnit | |
{ | |
[DataMember] | |
public string pmu { get; set; } | |
[DataMember] | |
public IList<Classification> list { get; set; } | |
} | |
[DataContract] | |
public class Classification | |
{ | |
[DataMember] | |
public string fund { get; set; } | |
[DataMember] | |
public string strategy { get; set; } | |
[DataMember] | |
public string folder { get; set; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Uses System.Web.Extensions | |
public static IList<PortfolioManagementUnit> Deserialize(string jsonFilePath) | |
{ | |
//NOTE: Don't use JSONDataContractSerializer... it doesn't work without a root element | |
var ser = new System.Web.Script.Serialization.JavaScriptSerializer(); | |
var reader = new StreamReader(jsonFilePath); | |
return ser.Deserialize<List<PortfolioManagementUnit>>(reader.ReadToEnd()); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#region Example JSON Serialization | |
var pmuCapMarkets = new PortfolioManagementUnit() | |
{ | |
pmu = "Capital Markets", | |
list = new List<Classification>() | |
{ | |
new Classification() {fund = "PRMF", strategy = "Capital Markets"}, | |
new Classification() {fund = "Nisswa Acq", strategy = "Capital Markets"} | |
} | |
}; | |
var pmuFinServices = new PortfolioManagementUnit() | |
{ | |
pmu = "FinServices", | |
list = new List<Classification>() | |
{ | |
new Classification() {fund = "FinServices"}, | |
new Classification() {fund = "PRMF", strategy = "FinEqLongShort"}, | |
new Classification() {fund = "LiquidMtge", strategy = "FinEqLongShort"} | |
} | |
}; | |
var pmus = new List<PortfolioManagementUnit>() {pmuCapMarkets, pmuFinServices}; | |
//NOTE: Don't use JSONDataContractSerializer... it doesn't work without a root element | |
var ser = new System.Web.Script.Serialization.JavaScriptSerializer(); | |
Debug.WriteLine(ser.Serialize(pmus)); | |
#endregion |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"pmu":"Capital Markets", | |
"list":[ | |
{ | |
"fund":"AAA", | |
"strategy":"US Capital Markets" | |
}, | |
{ | |
"fund":"BBB", | |
"strategy":"UK Capital Markets" | |
} | |
] | |
}, | |
{ | |
"pmu":"Event Driven", | |
"list":[ | |
{ | |
"fund":"AAA", | |
"strategy":"Asia Event Driven" | |
} | |
] | |
} | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment