0

Action results encoded using JsonResult seem to be excessively verbose in their serialisation. For example:

{"foo":"bar","list":["a","b"],"map":{"a":1,"b":2,"c":3}}

JavaScript seems perfectly happy with this shorter version as an equivalent:

{foo:"bar",list:["a","b"],map:{a:1,b:2,c:3}}

So this question is two-fold:

  1. Is there a reason that the former is better, assuming all my labels are valid JavaScript identifiers?
  2. Can I force the ASP.NET MVC framework to encode my messages using this shorter syntax?

I'm writing quite an AJAX-heavy app and have calculated that I'd save between 15% and 20% on message size. In many cases it will be the difference between fitting the response in a single TCP packet or not. Even disregarding performance, a reduction of that much in terms of my monthly bandwidth bill would be a boon.

EDIT

As Justin points out, the quotes are needed according to the JSON standard, but I don't need them in a browser for pure JavaScript use. Can I force the .NET web framework to drop the quotes in some fashion that's simpler than writing my own JSON serialiser?

Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
  • The quotes is necessary when posting data to ASMX and WCF service, and you cannot even replace it with single quotes, otherwise you will see exception's thrown by service. I can't find any JSON standard says quotes is necessary, though, at least not for Javascript. – tshao Nov 09 '10 at 01:49
  • Do your calculations account for compression of the http stream? I'm not sure you'll actually save 15-20% but that's just a hunch. – Ryan Nov 09 '10 at 02:12
  • @Ryan. Good point, but if my messages are small (1.5kB) then compression may actually increase their size. I'll be sure to consider this as an option though, cheers. – Drew Noakes Nov 09 '10 at 02:25

2 Answers2

1

There was another thread talking about the quotes:

JSON syntax for property names

The basic gist is that not using the quotes qualifies as valid javascript, but NOT as valid JSON.


As far as modifying your JSON to not include the quotes for properties - I'm not so sure it's a great idea. If you choose to open up your JSON calls for an external API, who knows what could go wrong with the invalid format.

But if you want to go ahead here, I think your best bet is to download and modify the JSON.NET open source library:

http://json.codeplex.com/

In their documentation, there is a specific article on reducing JSON footprint size, though they do not mention the removal of quotes:

http://james.newtonking.com/projects/json/help/ReducingSerializedJSONSize.html

Once you download this code and modify the JSONTextWriter class, you could create a custom ActionResult in MVC that uses this serializer instead of the System.Web.Script.Serialization.JavaScriptSerializer class that is used by default in the JsonResult class. There are some pretty great examples of doing this with other datatypes in the MVC Contrib project:

http://mvccontrib.codeplex.com/

Hope this helps.

Community
  • 1
  • 1
Justin Beckwith
  • 7,686
  • 1
  • 33
  • 55
  • Thanks for the links. Now, as for the 2nd part (and real meat) of the question, can I force the .NET web framework to drop the quotes in some fashion that's simpler than writing my own JSON serialiser? – Drew Noakes Nov 09 '10 at 12:09
1

At -30:20 in the video "JavaScript - The Good Parts", Douglas Crockford notes that the first ever JSON message to be sent between client and server failed because one of the field names they used was 'do', which is a reserved word in JavaScript. He explains that it doesn't have to be this way (the interpreter could differentiate based on context) but it's one of the many quirks that slipped through the hurriedly developed language into the standard.

Hence, quotes are required around field names. Of course, it should be possible to keep a list of reserved terms and enclose only those in quotes.

Drew Noakes
  • 300,895
  • 165
  • 679
  • 742