dongzhi9457 2019-07-12 14:07
浏览 127
已采纳

压缩的JSON大于未压缩的版本

I'll try to clear up my question.

myJSON is a simple JSON string. len(myJSON) = 78

e is json.Marshal(myJSON)

From what I understand, e is now a []byte

Then I gzip e like this:

var buf bytes.Buffer
gz := gzip.NewWriter(&buf)
gz.Write(e)
gz.Close()

And buf.Len() = 96

So... why is my compressed buffer bigger than the original non-compressed string?

Edit: It's hilarious the trolls that down vote a question when someone is trying to understand WHY something is happening. Guess I should just blindly accept it and not ask.

  • 写回答

2条回答 默认 最新

  • doushang1778 2019-07-12 14:27
    关注

    gzip will add a header and make some changes to the original data. For the case, the original data is really small it will not guarantee compressed data will smaller than original data.

    So if your program will constantly deal with the small data like this. Compress data use compress library may not a good idea. Some time we serialize the data into binary stream for the case that data is constantly small.

    Go gzip package ref:

    Package gzip implements reading and writing of gzip format compressed files, as specified in RFC 1952.

    RFC1952

    gzip format and header:

    http://www.onicos.com/staff/iz/formats/gzip.html

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献