Pages

Tuesday, December 11, 2012

JsonR(aw): Lightweight JSON protocol proposal

Lightweight JSON protocol proposal: JsonR(aw)


Intro

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 it's 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 seperated and later recombined, or where keys can be completely ommited and later added via implicit casting or via a hint to the objetcs real type.
Gains are in the order of +102% to -1% per/object, and become more obvious in collections

JSON (classic) 156 chars

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

JsonR (Implicit) 77 chars

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

JsonR (With Hint) 89 chars

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

JsonR (Without Hint) 153 chars

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

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.