Overriding default field names in JSON serialization

921 views
Skip to first unread message

David Koch

unread,
Apr 25, 2016, 5:59:52 PM4/25/16
to Protocol Buffers
Hello,

Using the JSON printing/parsing utilities in proto3 - is it possible to override the default JSON field names generated? If so, how can this be achieved?

I use the standard way to instantiate the printer and parser for now:
private static JsonFormat.TypeRegistry registry = JsonFormat.TypeRegistry.newBuilder()
      .add(MyClass.getDescriptor()).build();
private static JsonFormat.Printer printer = JsonFormat.printer().usingTypeRegistry(registry);
private static JsonFormat.Parser parser = JsonFormat.parser().usingTypeRegistry(registry);

Thanks,

Regards,

David


Feng Xiao

unread,
Apr 25, 2016, 6:04:07 PM4/25/16
to David Koch, Protocol Buffers
You can use the json_name field option to specify the name you want to use in JSON. For example:
message Foo {
  string id = 1 [json_name = "@id"];
}

The JSON format will then be:
{
  "@id": "..some value..."
}

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.
To post to this group, send email to prot...@googlegroups.com.
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.

David Koch

unread,
Apr 29, 2016, 6:14:27 AM4/29/16
to Protocol Buffers, ogd...@googlemail.com
Hi Feng,

Thank you!
Reply all
Reply to author
Forward
0 new messages