开发者

JSON format with gzip compression

开发者 https://www.devze.com 2023-03-14 23:24 出处:网络
My current project sends a lot of data to the browser in JSON via ajax requests. I\'ve been trying to decide which format I should use. The two I have in mind are

My current project sends a lot of data to the browser in JSON via ajax requests.

I've been trying to decide which format I should use. The two I have in mind are

[
    "colname1" : "content",
    "colname2" : "content",
],
[
    "colname1" : "content",
    "colname2" : "content",
],  
...
开发者_运维知识库

and

{
"columns": [
    "column name 1", 
    "column name 2", 
], 
"rows": [
    [
        "content", 
        "content"
    ], 
    [
        "content", 
        "content"
    ]
    ...
]

}

The first method is better because it is easier to work with. I just have to convert to an object once received. The second will need some post processing to convert it into a format more like the first so it is easier to work with in JavaScript.

The second is better because it is less verbose and therefore takes up less bandwidth and downloads more quickly. Before compression it is usually between 0.75% and 0.85% of the size of the first format.

GZip compression complicates things further. Making the difference in file size nearer 0.85% to 0.95%

Which format should I go with and why?


I'd suggest using RJSON:

RJSON (Recursive JSON) converts any JSON data collection into more compact recursive form. Compressed data is still JSON and can be parsed with JSON.parse. RJSON can compress not only homogeneous collections, but any data sets with free structure.

Example:

JSON:

{
"id": 7,
"tags": ["programming", "javascript"],
"users": [
    {"first": "Homer", "last": "Simpson"},
    {"first": "Hank", "last": "Hill"},
    {"first": "Peter", "last": "Griffin"}
],
"books": [
    {"title": "JavaScript", "author": "Flanagan", "year": 2006},
    {"title": "Cascading Style Sheets", "author": "Meyer", "year": 2004}
]
}

RJSON:

{
"id": 7,
"tags": ["programming", "javascript"],
"users": [
    {"first": "Homer", "last": "Simpson"},
    [2, "Hank", "Hill", "Peter", "Griffin"]
],
"books": [
    {"title": "JavaScript", "author": "Flanagan", "year": 2006},
    [3, "Cascading Style Sheets", "Meyer", 2004]
]
}


Shouldn't the second bit of example 1 be "rowname1"..etc.? I don't really get example 2 so I guess I would aim you towards 1. There is much to be said for having data immediately workable without pre-processing it first. Justification: I once spend too long optimizing array system that turned out to work perfectly but its hell to update it now.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号