protobuf json xml比較

1 protobuf/xml/json對比json

從數據的存儲格式的角度進行對比google

假如要存儲一個鍵值對:xml

{price:150}對象

1.1 protobuf的表示方式io

message  Test {ast

    optional int32 price = 1;coding

}序列化

protobuf的物理存儲:08 96 01,就3個字節。developer

採用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

相關文章
相關標籤/搜索