Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

JsonR(aw): Lightweight JSON Protocol

5.00/5 (1 vote)
21 Dec 2012CPOL2 min read 19.2K   43  
Lightweight JSON protocol proposal

Lightweight JSON Protocol Proposal: JsonR(aw)

Update

  • GitHub
    • Now has a functional Client-Side JS deserializer
    • Added testbed/playground to see savings
    • Added Sample Project (2012-12-22) ..Merry X-MAS !
      • Includes: C# serializer, JS deserializer, Sample ASP.NET Website

Introduction

At the beginning, there was XML, and then came along JSON. Not only was it much lighter than XML by suppressing all those opening & closing tags, but it was also directly compatible with browsers being JavaScript at its base. In the meantime, we have gained lots of tooling & libraries to work with this great format, and browser vendors progressively started supporting it natively.

I however work on a site that consumes enormous amounts of Ajax data, and over 80% of those requests are returning collections of this & that. Immediately, I thought of what a waste of bandwidth it was to be sending all those Key/Value pairs over the wire each time, when in reality the Values themselves could do just fine!

The proposal below is an advanced version of what I have been using for years now, and think it would be cool if everyone could benefit from it. When we implemented a simplified version of this, it cut our bandwidth by over 40%.

The Proposal

Simplified and lightweight protocol where key/value pairs are either separated and later recombined, or where keys can be completely omitted and later added via implicit casting or via a hint to the objects real type.

Gains are in the order of +102% to -1% per/object, and become more obvious in collections.

JSON (classic) 156 Chars

JavaScript
var object = {
    "Pseudo" : "Jason",
    "Age"    : 31,
    "Photos" : ["123.jpg", "222.jpg"]
    "Friends": [ 
           {
               "FirstName": "Bob",
               "LastName" : "Hope"
            },
            {
                "FirstName": "Foo",
                "LastName" : "Bar"
            }
    ] 
};

JsonR (Implicit) 77 Chars

JavaScript
var object = [
    "Jason",
    31,
    ["123.jpg", "222.jpg"],
    [["Bob", "Hope"], ["Foo", "Bar"]]
];

JsonR (With Hint) 89 Chars

JavaScript
var object = {
    Type  : "User",
    Values:[
        "Jason",
        31,
        ["123.jpg", "222.jpg"],
        [["Bob", "Hope"], ["Foo", "Bar"]]
    ]
};

JsonR (Without Hint) 153 Chars

JavaScript
var object = {
    Keys:[
       "Pseudo",
       "Age",
       "Photos",
       {"Friends": ["FirstName", "LastName"]}
    ],
    Values:[
        "Jason",
        31,
        ["123.jpg", "222.jpg"],
        [["Bob", "Hope"], ["Foo", "Bar"]]
    ]
};

JsonR (Full Signature) 164 Chars

JavaScript
var object = {
    Type: "User",
    Keys:[
      "Pseudo",
      "Age",
      "Photos",
      {"Friends": ["FirstName", "LastName"]}
     ],
     Values:[
        "Jason",
        31,
        ["123.jpg", "222.jpg"],
        [["Bob", "Hope"], ["Foo", "Bar"]]
     ]
};

P.S. If you are good with recursion in your language of choice, or would like to participate in any possible way, then you are more than welcome to come and join the project being set up on GitHub right now.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)