Sure -- this behavior happens automatically. The important thing is that your BarClass have a parameterless constructor (I think this is common to serialization classes in general as well). The JavaScriptSerializer deserializing method takes in your type for use in reconstituting the object and goes through all subtypes in your class.
From the documentation on MSDN: "During deserialization, the serializer’s current type resolver is referenced, which determines the managed type to use when converting elements that are nested inside arrays and dictionary types. As a result, the deserialization process iterates through all nested elements of input."
using System.Collections.Generic;
using System.Web.Script.Serialization;
namespace ConsoleApplication1
{
class Program
{
private static void Main(string[] args)
{
var person = new Person();
person.Name = "reacher gilt";
person.Address = "100 East Way";
person.Age = 74;
person.Aliases = new List<string>(new []{"teddy", "freddy", "eddy", "Betty"});
person.Bars = new List<BarClass>(new[]{
new BarClass("beep","boop"),
new BarClass("meep","moop"),
new BarClass("feep","foop"),
});
JavaScriptSerializer serializer = new JavaScriptSerializer();
string jsonString = serializer.Serialize(person);
var rehydrated = serializer.Deserialize<Person>(jsonString);
}
}
class Person
{
public string Name { get; set; }
public string Address { get; set; }
public int Age { get; set; }
public List<string> Aliases;
public List<BarClass> Bars { get; set; }
}
class BarClass
{
public string Sub_Property1 { get; set; }
public string Sub_Property2 { get; set; }
public BarClass() { }
public BarClass (string one, string two)
{
Sub_Property1 = one;
Sub_Property2 = two;
}
}
}
edit: After coach rob's clarifying comment:
using System.Collections.Generic;
using System.Web.Script.Serialization;
namespace ConsoleApplication1
{
class Program
{
private static void Main(string[] args)
{
var person = new Person();
person.Name = "reacher gilt";
person.Address = "100 East Way";
person.Age = 74;
person.Aliases = new List<string>(new []{"teddy", "freddy", "eddy", "Betty"});
person.Bars = new Dictionary<string, List<BarClass>>();
person.Bars.Add("items",
new List<BarClass>(new[]{
new BarClass("beep","boop"),
new BarClass("meep","moop"),
new BarClass("feep","foop"),
}));
JavaScriptSerializer serializer = new JavaScriptSerializer();
string jsonString = serializer.Serialize(person);
var rehydrated = serializer.Deserialize<Person>(jsonString);
}
}
class BarClass
{
public string Sub_Property1 { get; set; }
public string Sub_Property2 { get; set; }
public BarClass() { }
public BarClass (string one, string two)
{
Sub_Property1 = one;
Sub_Property2 = two;
}
}
class Person
{
public string Name { get; set; }
public string Address { get; set; }
public int Age { get; set; }
public List<string> Aliases;
public Dictionary <string, List<BarClass> >Bars { get; set; }
}
}
which gets you json like :
{
"Aliases":
[
"teddy",
"freddy",
"eddy",
"Betty"
],
"Name":"reacher gilt",
"Address":"100 East Way",
"Age":74,
"Bars":
{
"items":
[
{
"Sub_Property1":"beep",
"Sub_Property2":"boop"
},
{
"Sub_Property1":"meep",
"Sub_Property2":"moop"
},
{
"Sub_Property1":"feep",
"Sub_Property2":"foop"
}
]
}
}
I also see people using the DataContractJsonSerializer, which is indeed one way to serialize to json, but System.Web.Script.Serialization.JavaScriptSerializer was specified in the question.