- Posted by Ian Suttle on July 30, 2007
- Filed under .NET 3.5 | JSON
The JsonQueryStringConverter class comes equipped with the .Net
Framework 3.5. The basic premise here is
you can serialize an object into a JSON-like string which can be passed via
query string. Typically this class is to
interact via Windows Communication Foundation although it’s interesting to see
how it works with a simple application.
I can see using this as a means to serialize and transport object data
without going through web services and using SOAP. You may find a practical application for this
today.
Let’s start with a very basic type with some properties to
show how this works. We’ll have one of
our properties be another type with properties to show a bit of hierarchy. Note – your classes must be
serializable. I’ve marked both of my
objects with the [Serializable] attribute.
The ToString methods I’ve added are for demonstration purposes to show
the data in the objects. We’ll use them
below, but these are not requirements by any means.
[Serializable]
public class MyName
{
public string
First { get; set; }
public string Last
{ get; set; }
public override
string ToString()
{
return
string.Format("First: {0}, Last: {1}", First, Last);
}
}
[Serializable]
public class MyClass
{
public MyName
MyName { get; set; }
public string
MyAddress { get; set; }
public int MyAge {
get; set; }
public bool IsOld
{ get; set; }
public override
string ToString()
{
return
string.Format("MyName: {0}, MyAddress: {1}, MeAge: {2}, IsOld: {3}",
MyName.ToString(), MyAddress, MyAge, IsOld);
}
}
As you can see
we’ve create two types; MyClass and MyName, with MyName being a type used
within MyClass. We’re also using
auto-implemented properties. If you’d
like a bit more info on those, check out this short explanation. Next, we’re going to instantiate an object of
MyClass, and convert it to a JSON string for ease of passing over the web. We’ll do this using the
System.ServiceModel.Dispatcher.JsonQueryStringConverter class (you must
reference the System.ServiceModel.Web assembly first).
void JsonQuerystringConversion()
{
//setup object to test with
var me = new MyClass();
me.IsOld = false;
me.MyAddress = "123 My Way";
me.MyAge = 29;
me.MyName = new MyName { First = "ian",
Last = "suttle" };
//convert object to JSON value
JsonQueryStringConverter jsonQ = new
JsonQueryStringConverter();
string value = jsonQ.ConvertValueToString(me,
typeof(MyClass));
Response.Write(value);
}
If you compile and run
it you’ll see this method create a string as follows:
{"_x003C_IsOld_x003E_k__BackingField":false,"_x003C_MyAddress_x003E_k__BackingField":"123
My
Way","_x003C_MyAge_x003E_k__BackingField":29,"_x003C_MyName_x003E_k__BackingField":{"_x003C_First_x003E_k__BackingField":
"ian","_x003C_Last_x003E_k__BackingField":"suttle"}}
That’s great, but we
must also be able to revert this data to an object to get the best bang for the
buck. That’s just as easy, if not easier, than what we just did.
void
JsonQuerystringToObject()
{
//convert querystring value to object
JsonQueryStringConverter jsonQ = new
JsonQueryStringConverter();
//naturally this would come from the querystring and
never as a hard-coded string here
string value =
"{\"_x003C_IsOld_x003E_k__BackingField\":false,\"_x003C_MyAddress_x003E_k__BackingField\":\"123
My
Way\",\"_x003C_MyAge_x003E_k__BackingField\":29,\"_x003C_MyName_x003E_k__BackingField\":{\"_x003C_First_x003E_k__BackingField\":
\"ian\",\"_x003C_Last_x003E_k__BackingField\":\"suttle\"}}";
var fromString = jsonQ.ConvertStringToValue(value,
typeof(MyClass));
Response.Write(fromString.ToString()); //This is why we added the ToString method
}
A couple of items to
point out as the inline documentation already does:a) we’d never have value
in hard-coding the JSON string into the code like this and would likely grab it
from the Request object
b) the last line of this
method uses the ToString method we added to display the values in the MyClass
object.
And the result:
MyName: First: ian,
Last: suttle, MyAddress: 123 My Way, MeAge: 29, IsOld: False
Bingo – the JSON string
was successfully converted back into a MyClass object, with the MyName property
being created properly as well.