- Posted by Ian Suttle on January 6, 2008
- Filed under .NET 3.5 | C# 3.0 | ASP.Net | JSON | .Net Framework
Download the sample code for this post.
In October of 2007 The Gu wrote a ToJSON extension method using the JavaScriptSerializer class. Since that time the JavaScriptSerializer class has been marked obsolete in favor of DataContractJsonSerializer. In this post we’ll write an updated ToJSON method to satisfy every JSON extension method fantasy you have. Quick agenda… the goal is to take a .Net object and convert it in to JSON text which would likely be consumed be a remote client. Let’s start with the object to convert… an Address class.
using System.Runtime.Serialization;
...
[DataContract(Name = "Address", Namespace = "")]
public class PersonAddress
{
[DataMember(Name = "City", Order = 1)]
public string City { get; set; }
[DataMember(Name = "Country", Order = 2)]
public string Country { get; set; }
[DataMember(Name = "PostalCode", Order = 3)]
public string PostalCode { get; set; }
[DataMember(Name = "State", Order = 4)]
public string State { get; set; }
[DataMember(Name = "Street", Order = 5)]
public string Street { get; set; }
}
DataContract and DataMember attributes provide information to the serialization process of how to structure the data. It is possible to use the more familiar System.Serializable attribute however the output isn’t nearly as friendly. More on that later.
The constructor for DataContractJsonSerializer we’re using requires knowledge of the Type of object being serialized to JSON. For the purposes of this method we’re likely to not know which Type is to be serialized. Quite often it will be a custom type. We need our extension method to be more generic… I love .Net… Here’s the method suitable for such a task:
using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;
...
public static string ToJSON<T>(this T obj)
{
MemoryStream stream = new MemoryStream();
try
{
//serialize data to a stream, then to a JSON string
DataContractJsonSerializer jsSerializer = new DataContractJsonSerializer(typeof(T));
jsSerializer.WriteObject(stream, obj);
return Encoding.UTF8.GetString(stream.ToArray());
}
finally
{
stream.Close();
stream.Dispose();
}
}
Be sure to reference System.Runtime.Serialization and System.ServiceModel.Web.
Let’s dissect a bit. DataContractJsonSerializer needs to know the object type. In our case it’s generic, so we’ll let typeof(T) figure it out for us. The serializer WriteObject method wants a stream to write the serialization output to for which we’re using a MemoryStream; an easy object for retrieving text from. And that’s what we do… convert the stream’s byte array into a string; our JSON string.
Let’s run this pup using the following code:
PersonAddress address = new PersonAddress();
address.City = "Newport Beach";
address.Country = "US";
address.PostalCode = "92626";
address.State = "CA";
address.Street = "123 Candyland Way";
string json = address.ToJSON();
Response.Write(json);
And the results:
{"City":"Newport Beach","Country":"US","PostalCode":"92626","State":"CA","Street":"123 Candyland Way"}
Earlier I mentioned if you used the [Serializable] attribute instead of the [DataContract] attribute your solution would compile and function however your results would come out a bit different. Here’s the output when using Serializable:
<{"<City>k__BackingField":"Newport Beach","<Country>k__BackingField":"US","<PostalCode>k__BackingField":"92626","<State>k__BackingField":"CA","<Street>k__BackingField":"123 Candyland Way"}>
As you can see this is far less friendly to work with from a JSON consumer perspective… Make it easier on yourself and stick with DataContract in this case.
I hope this helps in your JSON endeavors. Let me know if you have any questions of problems!
Download the sample code for this post.
- Posted by Ian Suttle on July 30, 2007
- Filed under .NET 3.5 | C# 3.0 | 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.