Click here to Skip to main content
15,867,568 members
Articles / Web Development / ASP.NET

JsonR(aw): Lightweight JSON Protocol

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
21 Dec 2012CPOL2 min read 18.3K   43   10   9
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)


Written By
Software Developer (Senior) Index Multimedia
France France
Internet & Technology related professional since 1994. Passionate about WEB 2.0 and Community/Social networking related type Websites, or anything that is tech related to bridging the gap between the User and his everyday Multimedia Experience.

Comments and Discussions

 
Questionbad link? Pin
stefanveliki13-Dec-12 4:52
stefanveliki13-Dec-12 4:52 
http://itechnology.github.com/jsonraw/
it brings up page does not exist message
AnswerRe: bad link? Pin
Robert Hoffmann13-Dec-12 4:57
Robert Hoffmann13-Dec-12 4:57 
QuestionExamples of usage Pin
stefanveliki11-Dec-12 4:06
stefanveliki11-Dec-12 4:06 
AnswerRe: Examples of usage Pin
Robert Hoffmann11-Dec-12 4:32
Robert Hoffmann11-Dec-12 4:32 
GeneralRe: Examples of usage Pin
stefanveliki11-Dec-12 4:57
stefanveliki11-Dec-12 4:57 
GeneralRe: Examples of usage Pin
Robert Hoffmann11-Dec-12 5:05
Robert Hoffmann11-Dec-12 5:05 
GeneralRe: Examples of usage Pin
Jibesh21-Dec-12 14:18
professionalJibesh21-Dec-12 14:18 
GeneralRe: Examples of usage Pin
Robert Hoffmann21-Dec-12 14:24
Robert Hoffmann21-Dec-12 14:24 
GeneralRe: Examples of usage Pin
Jibesh21-Dec-12 14:25
professionalJibesh21-Dec-12 14:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.