April 3rd, 2008

Why is JSON Deserialization so Important to Developers?

Recently I have been working on a RESTful interface for a Web 2.0 project that I will be releasing very soon. And one of the self imposed requirements that I set out to achieve was to have the RESTful interface output the same structure JSON as XML. This way that any developer using the interface would feel comfortable using the JSON and XML interchangeably. But, I have been having a lot of trouble recently getting my JSON and XML to output the same structure when using the built in .NET methods. I have tried the following:

  • JavaScriptSerializer/XmlSerializer
  • DataContractJsonSerializer/DataContractSerializer

Neither can be forced to output the same structure. Mostly because they are both concerned about serialization and deserialization. Now I ask the question, Why is JSON deserialization so important to have? JSON stands for JavaScript Object Notation, the keyword in there is JavaScript, why are we trying to force a technology designed for a specific language and purpose in to a different technology. In my opinion it seems akin to trying to communicate between two JavaScript methods with the following C# code:

struct Person {
    public string Name = "Nick Berardi";
    public Uri Blog = new Uri("http://www.coderjournal.com");
    public DateTime BirthDate = new BirthDate(1980, 3, 14, 0, 0, 0);
}

There is no point to doing that because the JavaScript language already has a way to communicate objects, it’s called JSON. So why as C# developers, or whatever language we use, we insist on creating a deserializer when only a serializer is needed to translate our preferred object to JSON so that JavaScript, different language, can understand. By creating a deserializer we are basically encouraging/saying that this is a valid way to communicate between languages.

I personally see JSON as a communication protocol between two non-JavaScript languages as a fad. The communication between two non-JavaScript languages has already been decided to be either Remoting or XML, both were created with this idea of object transportation in mine, and both are very extensible and don’t fall short where JSON does.

Now that all that is said, I personally don’t have a problem with JSON having a deserializer, because there is going to be a time when you need to deserialize JSON, but it should be the exception not the rule, and it shouldn’t be part of the JSON serializer. I say this for the following reason, because when you combine a serializer and deserializer there is an expectation that the type is going to be the same going in as coming out. This was planned for in XML, with XML namespaces to define the type, however this was not planned for in JSON, so special cases have to be developed in order to support deserialization such as the inclusion of “__type” property in the JSON object. As illustrated in this article from Scott Hanselman:

{ "__type":"ConsoleApplication1.Person, ConsoleApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" ... }

You may say, “So what, that is what needs to be done to deserialize the object.” Well that my friends is called a Hack, and as professional developers we shouldn’t be hacking technologies, that were not designed for our purpose, together with our applications.  We should be using the correct technology for the problem that results in the least amount of technical debt.

Tags: , , ,

Social: kick it on DotNetKicks.com | Shout it | Add to DZone

This entry was posted on Thursday, April 3rd, 2008 at 7:48 am and is filed under Programming, Rant. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

3 Responses to “Why is JSON Deserialization so Important to Developers?”

  1. DotNetKicks.com Says:

    Why is JSON Deserialization so Important Developers?…

    You’ve been kicked (a good thing) – Trackback from DotNetKicks.com…

  2. Ray Says:

    “as professional developers we shouldn’t be hacking technologies, that were not designed for our purpose, together with our applications”

    Were that true, innovation and invention would die. This statement suggests that you are a good follower, but not a leader or inventor.

  3. Nick Berardi Says:

    Ray,

    First of all congratulations on taking a specific sentence totally out of context. I have to applaud you, for reading one sentence out of the whole article and jumping to unfounded conclusions about me. I would venture to say that you have a very high opinion of yourself, and have a very tough time working and socializing with people, and probably eat lunch alone everyday. Or would that be me jumping to conclusions off one sentence, and making unfounded statements about somebody else?

    Second re-read the article, I was specifically commenting about the __type attribute applied in JSON just so people could transmit Strongly Typed data using JSON, between two non-JavaScript sources. Not making an overly generalized statement about using technology in a non-standard way.

    JSON == Java Script Object Notation JavaScript definitely doesn’t need the __type property, and neither does .NET to serialize JSON. It is specifically designed for deserialization on a strongly typed system. (i.e. transmittion of data between two non-JavaScript sources) And if you are transmitting data using JSON, you really should be asking yourself why you are doing this and not using a more standardized technology for this such as YAML, XML, or anything else designed to transmit strongly typed data, which have built in data validation.

    With out data validation, what happens if some other data gets inserted in to the JSON object, but doesn’t match the object on the other end, because they are each basing their schema off their own understanding? Or the reverse where a property is not sent because the serializing assembly is old. JSON is a very bad way to transmit strongly typed data, mostly because of the lack of a schema getting transmitted with the data, and that is why I mentioned the __type property as a hack.

    Don’t get me wrong JSON is a great way to transmit data to JavaScript, but when it is used with JavaScript, for example how CouchDB uses JavaScript in order to define views for the JSON data. That in my opinion is a good use of JSON.

    Let me finish off by summarizing the point of the article since you missed it, “Why use an object notation designed for JavaScript, and to be loosely typed, to transmit strongly typed data between two non-JavaScript sources?” There are much better technologies that are strongly typed and can be used to transmit data back and forth between servers in a much more reliable manor.

    Nick