Skip to content

Handling of Unknown Fields

sijuv edited this page Jan 3, 2011 · 2 revisions

The goal is to ensure that unknown fields are retained in the serialized output and it is possible for it to be merged into a Message.

Handling of unknown fields is controlled by 2 Features :

  • SUPPORT_UNKNOWN_FIELDS : This feature dictates whether to handle unknown fields at all. This feature is set to TRUE by default for all codecs.
  • UNKNOWN_FIELD_ELEM_NAME : The name of the element in the json/xml which contains the unknown fields. By default the value is set to Codec.DEFAULT_UNKNOWN_FIELD_ELEM_NAME=unknownfields

The value of the unknown field during serialization to xml/json is the Hex encoded byte[] corresponding to the UnknownFieldSet in the Message

Consider the below 2 proto defns:

  • A: enum Lang{ JAVA=1; HASKELL=2; }

      message Version{
      	optional string name=1;
      	optional int32 vernum=2;
      }
      message Unknown{
      	optional string name=1;
      	optional int32 id=2;
      	repeated string alias =3;
      	repeated Version verions=4;
      	optional Version liveversion=5;
      	extensions 100 to 200;
      }
    
      extend Unknown{
      	optional string othername =100;
      	optional int32 otherid=101;
      	repeated Lang lang=102;
      	optional Version extversion=103;
      	repeated Version extversions=104;
      }
    
  • B: message Unknown{ optional string name=1; repeated Version verions=4; extensions 100 to 200; }

      message Version{
      	optional string name=1;
      }
    
      enum Lang{
      	JAVA=1;
      	HASKELL=2;
      }
    
      extend Unknown{
      	optional int32 otherid=101;
      	repeated Lang lang=102;
      }
    

If a message created with defn A is merged into a message from defn B and if this message is serialized to json below will be the response : { "name" : "HelloWorld", "unknownfields" : "EAEaCHN1cGVybWFuGglzcGlkZXJtYW4qDwoLbGl2ZXZlcnNpb24QZKIGCm90aGVyIGV4dG4=", "verions" : [ { "name" : "ver1", "unknownfields" : "EAE=" }, { "name" : "ver2", "unknownfields" : "EAI=" } ] }

Here the unknownfields field corresponds to the base64 encoded byte[] of the UnknownFieldSet in that Message. Similar will be in the case of xml.