Serializing Objects as JSON using Atlas, JSON.NET and AjaxPro [Part 2]

Michael Schwarz on Monday, July 10, 2006

As there was a little discussion [1] about serialization of .NET types and deserialization of these genereted JSON strings I have updated Ajax.NET Professional [2] (version 6.7.9.1) to allow parsing of new Date statements, too. You can use the JSON generated string to do a deserialization right after, now.

I tried to write some other tests to see how these three frameworks compare when running serialize() and deserialze() methods. JSON.NET [3] was not able to serialize a simple DateTime instance. When using a DateTime as property in a class it could be serialized, but deserializing didn't work because of the wrong date format (which is different here in Germany and a  lot of other countries). Atlas [4] is working similar to the AjaxPro serialization, but not using UTC.

I started to add more properties/fields to the Person class example to see which .NET data types are really working?

[AjaxPro.AjaxNoTypeUsage] public class Person { public MyColor color = MyColor.Yellow; public MyColor2 color2 = MyColor2.Red;  // not working with Atlas public DateTime dt = DateTime.Now; public float f = 2.3F; public double d = 1.1; public int i = 999; public int[] I = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; public bool b = false; public char c = '@'; public List<string> s = new List<string>(); public decimal dec = 0.1M; public string S = "\r\n\t\f\b?{\\r\\n\"\'";

public Person() { s.Add("Hello World"); s.Add("öäüÖÄÜ\\'{new Date(12345);}[222]_µ@²³~"); s.Add(" "); } }

public enum MyColor { Black, Red, Yellow, White }

public enum MyColor2 : byte { Black, Red, Yellow, White }

The class above is only working with AjaxPro. JSON.NET and Atlas have both problems with non-US formats like dates or the decimal seperator. Compare following JSON outputs:

Ajax.NET Professional:

{"color":2,"color2":1,"dt":new Date(Date.UTC(2006,6,9,12,18,22,704)),
"f":2.3,"d":1.1,"i":999,"I":[1,2,3,4,5,6,7,8,9],"b":false,"c":"@",
"s":["Hello World","öäüÖÄÜ\\'{new Date(12345);}[222]_µ@²³~","           "],
"dec":0.1,"S":"\r\n\t\f\b?{\\r\\n\"'"}

JSON.NET:

{"color":2,"color2":1,"dt":"<font color="#ff0000">07/09/2006 14:18:22</font>
",
"f":2.3,"d":1.1,"i":999,"I":[1,2,3,4,5,6,7,8,9],"b":false,"c":<font color="#ff0000">64</font>
,
"s":["Hello World","öäüÖÄÜ\\'{new Date(12345);}[222]_µ@²³~","           "],
"dec":"0.1","S":"<font color="#ff0000">\r\n\?</font>
{\\r\\n\"'"}

Atlas:

{"color":2, <font color="#ff0000">MISSING color2!!</font>
"dt":new Date(1152454818040),
"f":2.3,"d":1.1,"i":999,"I":[1,2,3,4,5,6,7,8,9],"b":false,"c":"@",
"s":["Hello World","öäüÖÄÜ\\\'
{new Date(12345);}[222]_µ@²³~","           "],
"dec":0.1,"S":"\r\n\?{\\\r
\\n\"\'"}

If you have a deep look in the JSON output you will see a lot of different serializations. There are some that are ok if the deserialization is only used for .NET, but using the JSON output in JavaScript it will very bad if you do not get a real Date object or if a character is represented as the ASCII code (see JSON.NET that will serialize the @ sign to 64): The color2 property is using a byte enum which is not supported by Atlas, I simple removed this for the test above.

Next, I tried to use the JSON output to create a new instance of the Person class. Hm, again only Ajax.NET Professional is working correct. Atlas cannot convert any value with a decimal seperator if it is using non-US culture info.