尝试减少JSON大小是否值得努力? [英] Is it worth the effort to try to reduce JSON size?

查看:103
本文介绍了尝试减少JSON大小是否值得努力?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从移动应用程序(最多1000个JSON对象)提交相对大量的数据,我通常会这样编码:

I am submitting relatively lots of data from a mobile application (up to 1000 JSON objects), that I would normally encode like this:

[{
    id: 12,
    score: 34,
    interval: 5678,
    sub: 9012
}, {
    id: ...
}, ...]

我可以通过提交减少负载而是一个数组数组:

I could make the payload smaller by submitting an array of arrays instead:

[[12, 34, 5678, 9012], [...], ...]

在属性名称上保存一些空间,并在服务器上重新创建对象(作为模式)是固定的,或者至少它是服务器和客户端之间的合同。)

to save some space on the property names, and recreate the objects on the server (as the schema is fixed, or at least it is a contract between the server and the client).

然后以 POST 请求,最有可能是通过3G连接(或者可能是wifi)。

The payload in then submitted in a POST request, most likely over a 3G connection (or could be wifi).

看起来我通过使用嵌套数组节省了一些带宽,但我'我不确定应用 gzip 时是否会引起注意,我不确定如何准确客观地对待我差不多。

It looks like I am saving some bandwidth by using nested arrays, but I'm not sure it is noticeable when gzip is applied, and I'm not sure how to precisely and objectively measure the difference.

另一方面,嵌套数组并不像其他人那样感觉:它们的可读性更低,因此更难在调试时发现错误。此外,由于我们正在冲洗马桶的可读性,我们可以简化阵列,因为每个子阵列都有固定数量的元素,服务器可以将其切片并再次重建对象。

On the other hand, the nested arrays don't feel like a good idea: they are less readable and thus harder to spot errors while debugging. Also, since we're flushing readability down the toilet, we could just flatten the array, since each child array has a fixed number of elements, the server could just slice it up and reconstruct the objects again.

非常感谢有关此主题的任何进一步阅读材料。

Any further reading material on this topic is much appreciated.

推荐答案

JSONH,又名hpack,< a href =https://github.com/WebReflection/JSONH> https://github.com/WebReflection/JSONH 与您的示例非常相似:

JSONH, aka hpack, https://github.com/WebReflection/JSONH does something very similar to your example:

[{
    id: 12,
    score: 34,
    interval: 5678,
    sub: 9012
}, {
    id: 98,
    score: 76,
    interval: 5432,
    sub: 1098
}, ...]

会变成:

[["id","score","interval","sub"],12,34,5678,9012,98,76,5432,1098,...]

这篇关于尝试减少JSON大小是否值得努力?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆