Forcing JSON encoding to include default values (Java?)

1,095 views
Skip to first unread message

Marco

unread,
Nov 1, 2015, 2:43:49 PM11/1/15
to Protocol Buffers
The documentation mentions "An implementation may provide options to emit fields with default values in the JSON-encoded output."

I assumed that meant an implementation of Printer class, ie. an alternative to  JsonFormat.printer().print(myMessage), but it appears that any implementation would depend on GeneratedMessage::getAllFields() which does not include default fields (GeneratedMessage:2123). I also read through descriptor.proto to see if there was an option for this, but that did not appear to be the case.

Is there a way to do this out of the box, or would it require code changes? And would this be different for other languages? (This is using proto3, if that's not implied by the above.)

Feng Xiao

unread,
Nov 1, 2015, 2:46:16 PM11/1/15
to Marco, Protocol Buffers
The option is not added yet. It will be something like:
JsonFormat.printer().printingDefaultValueFields().print(myMessage);

Likely it will be included in the next beta release.

--
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 http://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.

Rich

unread,
Mar 15, 2016, 1:40:25 PM3/15/16
to Protocol Buffers, a.f....@gmail.com
Where is JsonFormat defined?

No reference to how to print JSON from Java in the docs. Nothing in the API index.

Feng Xiao

unread,
Mar 15, 2016, 2:11:50 PM3/15/16
to Rich, Protocol Buffers, Marco Farrugia
On Tue, Mar 15, 2016 at 10:40 AM, Rich <rich....@gmail.com> wrote:
Where is JsonFormat defined?

No reference to how to print JSON from Java in the docs. Nothing in the API index.
Reply all
Reply to author
Forward
0 new messages