Oh my, where do I start, simplicity or contract inclusivity. I generally like simplicity which leans me towards using the JavaScriptSerializer, but I had some doubts when I kept reading that it was deprecated. I finally came across a Scott Gu’s response in this post by Aaron Lerch, that indicates the JavaScriptSerializer is un-deprecated. So which to use? Here are some of the pros and cons:
DataContractJsonSerializer
Pros:
- You can control the property names when serializing. This can be nice if you have JSON coming in from a third party and the fields are like “a” or “exp”.
- WCF uses this for serialization/deserialization
Cons:
- Does not map enumerations. You get an exception when trying to deserialize into a enum.
- From what I’ve seen it’s slower than the JavaScriptSerializer.
- You have to write more code to do a simple serialization.
JavaScriptSerializer
Pros:
- Much easier to deal with.
- Handles enumerations.
Cons:
- Cannot control property names when serializing.
Looking at the cons for DataContractJsonSerializer, it would seem like you wouldn’t want to use it unless you were doing some WCF work. It just happened the other day that I was working on a couple of projects where I decided to use one serializer in one project and the other in the other project. My thinking was that if you’re working with JSON, and the class at hand in somewhat internal and isolated to some component, use the simple JavaScriptSerializer. You loose property name management, but so what, keep it simple. On the other hand, this other project I was working on I thought I might expose it publically for other people to use. In this case I wouldn’t want property names like “a” because it has not meaning when looking at the class definition. So to abide by the 3rd party JSON contract, yet give others a meaningful property name, I decided to use the DataContractJsonSerializer. Below I give a simple example of pretend response JSON from some 3rd party vendor JSON API (let’s prettend a=auth token value, exp=expiration in seconds, and sid=social security ID).
Basic JSON structure from the 3rd party response:
{
"token": {
"a":"…",
"exp":"123456789",
"sid":"…"
}
}
public class Token
{
public string a;
public int epx;
public string sid;
}
public class TokenResponse
{
public token;
}
//
// code to receive and deserialize token class from some web response
//
string json = ...; // our json data from the 3rd party
// create serializer
JavaScriptSerializer serializer = new JavaScriptSerializer();
// return the deserialized object
return serializer.Deserialize<TokenResponse>(json);
[DataContract(Name = "token")]
public class Token
{
[DataMember(Name = "a", IsRequired = true)]
public string Value;
[DataMember(Name = "exp", IsRequired = true)]
public int SecondsBeforeExpiration;
[DataMember(Name = "sid", IsRequired = true)]
public string SocialSecurityNumber;
}
[DataContract]
public class TokenResponse
{
[DataMember(Name = "token", IsRequired = true)]
public Token;
}
//
// code to receive and deserialize token class from some web response
//
string json = ...; // our json data from the 3rd party
// create stream to hold json data
using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json)))
{
// create serializer
DataContractSerializer serializer = new DataContractSerializer(typeof(TokenResponse));
return serializer.ReadObject(ms) as TokenResponse;
}
1 comment:
Interesting read, thanks for sharing
Post a Comment