javaprotocol-buffersprotocol-buffers-3

protocol buffers, What is in serialized data?


I am new to protocol buffers, and really want to know more about it, so sorry for noob question.

What is in serialized data, only values or both keys and values? I think there is only values, and if someone wants to deserialize it, he/she must has scheme.


Solution

  • it's both key & value:

    As you know, a protocol buffer message is a series of key-value pairs. The binary version of a message just uses the field's number as the key – the name and declared type for each field can only be determined on the decoding end by referencing the message type's definition (i.e. the .proto file). https://developers.google.com/protocol-buffers/docs/encoding

    For eg, say you have a proto file as:

    $  cat my.proto 
    message header {
      required uint32 u1 = 1;
      required uint32 u2 = 2;
      optional uint32 u3 = 3 [default=0];
      optional bool   b1 = 4 [default=true];
      optional string s1 = 5;
      optional uint32 u4 = 6;
      optional uint32 u5 = 7;
      optional string s2 = 9;
      optional string s3   = 10; 
      optional uint32 u6 = 8;
    }
    

    Dump out encoded data from memory:

    (gdb) x/10xb 0x7fd70db7e964
    0x7fd70db7e964: 0x08    0xff    0xff    0x01    0x10    0x08    0x40    0xf7
    0x7fd70db7e96c: 0xd4    0x38
    

    Decode:

    $ echo 08ffff01100840f7d438 | xxd -r -p | protoc --decode_raw
    1: 32767
    2: 8
    8: 928375
    

    1,2,8 are keys

    from proto file above:

    1 => u1, 
    2 => u2,
    8 => u6
    

    So, it becomes:

    u1: 32767
    u2: 8
    u6: 928375
    

    I used data from my question here: