I have my message definition like

message ID {
    string value = 1;
}

message User {
    ID id = 1;
    google.protobuf.StringValue name = 2;
}

Now if I serialize an instance of User to json, I get something like this

{
    "id": {
        "value" : "myid"
    }
    "name" : "Josh"
}

As you can see for the WKT types the value is unnested. However, for my custom message type User the value is nested. How do I make the output look like

{
    "id": "myid"
    "name" : "Josh"
}

I mean how do I serialize, deserialize to custom type.

One option I could think of is update this function https://github.com/protocolbuffers/protobuf/blob/master/python/google/protobuf/json_format.py#L199 This means, have a copy of json_format.py and extend _IsWrapperMessage to my custom types

0 Answers