Skip to content

Instantly share code, notes, and snippets.

@bobbychopra
Created July 13, 2012 22:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bobbychopra/3107875 to your computer and use it in GitHub Desktop.
Save bobbychopra/3107875 to your computer and use it in GitHub Desktop.
JSON Deserialize
[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; }
}
//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());
}
#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
[
{
"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