navigation
 Wednesday, July 16, 2008

By the time I found the handy JavaScriptSerializer class, it had been introduced, deprecated and brought back from the hellish purgatory where Microsoft keeps objects that have outlived their usefulness.  JavaScriptSerializer is used internally by MS AJAX to move .NET types to JSON strings and back again. A public version of the class is available in the System.Web.Script.Serialization namespace and can be used to shuttle your own objects between .NET code on the server and JavaScript/JSON/AJAX on the client. Consider the ubiquitous Foo class:

public class Foo
{
  private string _bar;
  public string Bar
  {
    get { return _bar; }
    set { _bar = value;}
  }
}

To make Foo into a JSON string, create an instance of the JavaScriptSerializer and pass a SimpleTypeResolver instance in the constructor. You can omit the SimpleTypeResolverif you want a very simplified set of property : value pairs, e.g. {"Bar":"123"}.  Note: you can create your own resolver types by implementing a JavaScriptTypeResolver descendant.

This next code example creates and populates an instance of Foo, serializes the object into a JSON string, displays it in a TextBox and finally completes the round trip by deserializing the JSON string back into its .NET Foo object:

Foo foo = new Foo(); foo.Bar = "123"; JavaScriptSerializer jss = new JavaScriptSerializer(new SimpleTypeResolver()); string serializedFoo = jss.Serialize(foo); tbOutput.Text = serializedFoo; Foo newFoo = jss.DeserializeObject(serializedFoo) as Foo;

The serialized JSON string displayed in the text box takes this form:

{"__type":"Falafel.Foo, JSSerializeTest, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null","Bar":"123"}

Likewise, you can serialize a series of objects like this generic list of Foo:

List<Foo> foos = new List<Foo>(); for (int i = 0; i < 10; i++) { Foo foo = new Foo(); foo.Bar = i.ToString(); foos.Add(foo); } JavaScriptSerializer jss = new JavaScriptSerializer(new SimpleTypeResolver()); string serializedFoos = jss.Serialize(foos); tbOutput.Text = serializedFoos; List<Foo> newFoos = jss.DeserializeObject(serializedFoos) as List<Foo>;

In this case the output looks like the following:

[{"__type":"Falafel.Foo, JSSerializeTest, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null","Bar":"0"},
{"__type":"Falafel.Foo, JSSerializeTest, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null","Bar":"1"},
{"__type":"Falafel.Foo, JSSerializeTest, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null","Bar":"2"},
{"__type":"Falafel.Foo, JSSerializeTest, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null","Bar":"3"},
{"__type":"Falafel.Foo, JSSerializeTest, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null","Bar":"4"},
{"__type":"Falafel.Foo, JSSerializeTest, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null","Bar":"5"},
{"__type":"Falafel.Foo, JSSerializeTest, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null","Bar":"6"},
{"__type":"Falafel.Foo, JSSerializeTest, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null","Bar":"7"},
{"__type":"Falafel.Foo, JSSerializeTest, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null","Bar":"8"},
{"__type":"Falafel.Foo, JSSerializeTest, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null","Bar":"9"}]
A fun and useful tool that helps bridge the gap between .NET and JSON/AJAX.
 |  |