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

CJSON - Compressed JSON

4.58/5 (7 votes)
5 Feb 2014CPOL5 min read 35K  
This is a technique that I have thought of which can help us save bandwidth while communicating.

Introduction

Data means actual understanding being represented in any sort. It may be facts, words, etc. Internet is basically sharing of data. In today’s technologically inclined world sharing of data via internet is increasing day by day and need to share more compressed and reliable form of data is the need so that we can share data at much faster rate and also use low bandwidth. Hence getting a motivation from JSON I have thought of a concept that I like to call CJSON (Compressed JSON). It basically is a more compressed form of JSON. The main idea behind this concept is to share data in a more compressed form and hence reduce bandwidth usage and also enable faster communication between the server and the client with the reliability that JSON provides. Today’s technological progress has ensured better processing power and it is improving with respect to time but the data that is shared between server and client needs to be presented in more compressed form.

CONCEPT

Compressed Javascript Object Notation is a more compressed form of JSON. JSON is a key value pair representation to share data. In real world scenario, when there is exchange of data, majority of times an array of objects are exchanged. Hence when array of object is exchanged, the data that is sent over the line using JSON has keys that are repeated number of times depending on the length of the array.

Example:

JavaScript
Variable = [{"name": "karan", "surname":"savla"}, {"name": "karan1",
    "surname" : "savla1"}]; 

In the above example, we can see that the keys like name, surname are repeated. Hence we are wasting the bandwidth by sending this unwanted data. Hence what we can do instead is...

Example:

JavaScript
Variable = {"name,surname,..": 
["karan,savla,..", " karan1,savla1,…",..]}; 

The above example is a much compressed representation of the JSON array where the keys are not repeated and key and value are separated by the same JSON symbol of ":’ and we are not using curly brackets ({) again and again to separate the elements in the array, hence saving the bandwidth. Also if we observe the CJSON more closely, the number of inverted commas (") used are also reduced dramatically and hence we are saving the bandwidth. CJSON will not only work better for array scenarios but also if we are exchanging simple data like just one object value as below:

JSON:

JavaScript
Variable = {"name":"karan","surname" : "savla"}; 

CJSON:

JavaScript
Variable = {"name,surname" : "karan,savla"}; 

In the above case, we can see that CJSON also works for simple data, i.e., it saves bandwidth. Now for more complex scenarios where we send nested objects like below:

JSON:

JavaScript
Variable = [
{"name" : "a", "place" : "b",
"game" : {"playing" : "yes", "cricket"
:
{"bat":[{"type":"wood","weight":"12"},
    {"type":"aluminium","weight":"10"}],"bowl"
: "red"}}}}
, {"name" : "c", "place" : "d",
"game" : {"playing" : "yes", "cricket"
:
{"bat":[{"type":"aluminium","weight":"14"},
    {"type":"aluminium","weight":"13"},"bowl"
: "green"}}}}]   

CJSON:

JavaScript
Variable = {"name,place,game{playing,cricket{bat[{type,weight}],bowl}}
":["a,b,{yes,{[{'wood','12'},{'aluminium','10'}],red}}","c,d, 
{yes,{[{'aluminium','14'},{'aluminium','13'}],green}}"]}; 

In the above examples, we can see that even the complex objects can be represented using CJSON. The beginning of curly bracket ({) signifies the beginning of a new object. Hence, when we would de-serialize the above CJSON variable, we can just read the keys and find the type of objects in the present inside the main object and then similarly we can read the values and map them to a specific object.

Also to further compress the object that is sent over the wire and to further simply the process of de-serialization, we can construct following CJSON strings:

JavaScript
Variable = {@ObjectName
: "a,b,{{[{'wood','12'},{'aluminium','10'}],red}}"};

Once we receive the JSON string, we need to deserialize it back into object at the receiving end so that we can work on it. For this purpose, currently I am only aware of the two techniques mentioned below:

  1. First approach is that the deserializing code will need to check every object in the solution and match its properties to the object to be deserialized and then deserialize it. (This is what I can think of, there may be some better way but then too scanning of more than one object will be involved).
  2. Second approach is that while deserializing, we explicitly need to specify the object to be deserialized into the deserializing method.

Hence, we can further modify our string and send in the object name itself like:

JavaScript
Variable = {@"Employee" : ["...","..."]};

Also, if the object name at server and client is different, then we can also use the below syntax to communicate:

JavaScript
Variable = {@["SenderName","ReceiverName"] : ["….","…"]}; 

So now mapping becomes much faster and easier, also more bandwidth is saved. Here @ at the beginning will let our code know that following thing before the ":" is the name of the object and not properties. Also all the values will be by default arranged as per the object properties name in alphabetical order while performing serialization.

Also further compression can be achieved when the data is serialized. When we send a data through the wire we send the data as it is, in real world we send arrays of data through the wire. Today’s technological leaps have made sure that processing power has increased. Hence when we send data we can further compress it by.

Example:

JSON:

JavaScript
Variable = [
{"name" : "karan", "place" : "mumbai",
"game" : {"playing" : "yes", "cricket"
:
{"bat":[{"type":"wood","weight":"12"},{"type":"aluminium","weight":"10"}],"bowl"
: "red"}}}}<br />
, {"name" : "karan1", "place" : "@12",
"game" : {"playing" : "yes", "cricket"
: {"bat":[{"type":"@1221","weight":"14"},{"type":"@1221","weight":"14"},"bowl"
: "green"}}}}]

CJSON:

JavaScript
Variable = {"name,place,game{playing,cricket{bat[{type,weight}],bowl}}
":["karan,mumbai,{yes,{[{'wood','12'},{'aluminium','10'}],red}}","karan1,@12,
{yes,{[{'@1221','14'},{'@1221,'14'}],green}}",]};

What the above example states is that whenever we have a field that is repeated, then we can replace that field value with ‘@’ as a prefix and then the first location of the field like location for mumbai is it is in the 1st array and 2nd position in it, hence we get @12. This compression technique will figure out if compressing the field value is required or not and will replace the value with the compressed value only when compression is achieved like in above example "14" is not replace as value is in a compressed form then compression technique will compress it. Then when de-serializing at the client end, we can use the decompression technique to again reform the object as all the values are sent within the CJSON string.

CJSON Grammar

A CJSON text is a sequence of tokens. CJSON text is a serialized object or array.

JavaScript
CJSON-text = object / array

These are the six structural characters:

begin-array = [ left square bracket

JavaScript
begin-object    = { left curly bracket

end-array       =] right square bracket

end-object      = } right curly bracket

name-separator  = : colon

value-separator = , comma

Note: If we have a comma(,) in the value, then we need to convert it to (‘,’) while sending so that we can parse it at the client side

Apart from these, all keys and set of values are grouped using inverted comma and internally separated using comma example:

JavaScript
{"name,surname" : ["karan,savla","abc,xyz"]};

In the above example, name and surname are the keys that are grouped together and separated by comma which is the same case for the values. And the main array values are also separated by using comma.

Points of Interest

This is just a way that I have thought of to compress the JSON data that we are sending to save the bandwidth which is very crucial as the new mobile devices have come up. Please let me know if I have made a mistake somewhere or if this technique may fail in some case or so.

License

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