protobuf json xml比较


1 protobuf/xml/json对比

从数据的存储格式的角度进行对比

假如要存储一个键值对:

{price:150}

1.1 protobuf的表示方式

message  Test {

    optional int32 price = 1;

}

protobuf的物理存储:08 96 01,就3个字节。

采用key-value的方式存放,第一个字节是key,它是field_number << 3 | wire_type构成。

所以field number是1,wire type是0,即varint,有了这个wire type就可以用来解析96 01了。

96 01 = 1001 0110 0000 0001

即001 0110 000 0001

least significant first

1001 0110 = 128 + 16 + 4 + 2 = 150.

只要3 bytes

1.2 xml的存储表示

<some>

  <name>price</name>

  <value>150</value>

</some>

大约要36 bytes

1.3 json的存储表示

{price:150}

大约11bytes

 

比较可见相比于json和xml,protobuf对象序列化时可以节省非常大的空间,从而带来非常快的传输速度。

另外由于protobuf表示简单,解析速度也更快。

 

参考

1 https://developers.google.com/protocol-buffers/docs/encoding

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM