JSON编码包的压缩算法? [英] Compression algorithm for JSON encoded packets?

查看:186
本文介绍了JSON编码包的压缩算法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在通过线路发送数据包之前,最好的压缩算法是什么?数据包使用JSON编码。

What would be the best compression algorithm to use to compress packets before sending them over the wire? The packets are encoded using JSON. Would LZW be a good one for this or is there something better?

推荐答案

我认为两个问题会影响你的答案:

I think two questions will affect your answer:

1)在不知道程序的任何特定运行会发生什么的情况下,您可以如何预测数据的组成?例如,如果您的数据包看起来像这样:

1) How well can you predict the composition of the data without knowing what will happen on any particular run of the program? For instance, if your packets look like this:

{
    "vector": {
        "latitude": 16,
        "longitude": 18,
        "altitude": 20
    },
    "vector": {
        "latitude": -8,
        "longitude": 13,
        "altitude": -5
    },
    [... et cetera ...]
}

- 那么你可能会通过创建一个硬编码的文本字符串字典,数据并用适当的字典索引替换每个出现的一个文本字符串。 (实际上,如果您的数据常规,您可能希望通过线发送刚才的值,只需将一个函数写入客户端构建JSON对象)

-- then you would probably get your best compression by creating a hard-coded dictionary of the text strings that keep showing up in your data and replace each occurrence of one of the text strings with the appropriate dictionary index. (Actually, if your data was this regular, you'd probably want to send just the values over the wire and simply write a function into the client to construct a JSON object from the values if a JSON object is needed.)

如果您无法预测使用哪个标头,您可能需要使用LZW,或LZ77,或另一种查看已经经历的数据以找到其可以以特别紧凑的形式表达的数据的方法。但是...

If you cannot predict which headers will be used, you may need to use LZW, or LZ77, or another method which looks at the data which has already gone through to find the data it can express in an especially compact form. However...

2)数据包需要彼此分开压缩吗?如果是这样,那么LZW绝对你想要的方法;它将没有时间将其字典建立到在单个分组结束时给出大量压缩结果的大小。在这种情况下获得真正大量压缩的唯一机会是IMHO,使用硬编码的字典。

2) Do the packets need to be compressed separately from each other? If so then LZW is definitely not the method you want; it will not have time to build its dictionary up to a size that will give substantial compression results by the end of a single packet. The only chance of getting really substantial compression in this scenario, IMHO, is to use a hard-coded dictionary.

(上述所有内容的补充:Michael Kohne点发送JSON意味着你可能发送所有的文本,这意味着你使用的带宽,能够发送比你使用的更广泛的字符的能力。但是,如何包装字符的问题进入范围0-127到容纳值为0-255的容器是相当简单,我认为可以留作一个练习读者,因为他们说)。

(Addendum to all of the above: as Michael Kohne points out, sending JSON means you're probably sending all text, which means that you're underusing bandwidth that has the capability of sending a much wider range of characters than you're using. However, the problem of how to pack characters that fall into the range 0-127 into containers that hold values 0-255 is fairly simple and I think can be left as "an exercise for the reader", as they say.)

这篇关于JSON编码包的压缩算法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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