Discussion:
[protobuf] Forcing JSON encoding to include default values (Java?)
Marco
2015-11-01 07:32:24 UTC
Permalink
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.)
--
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+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at http://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.
'Feng Xiao' via Protocol Buffers
2015-11-01 19:46:03 UTC
Permalink
Post by Marco
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.)
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.
Post by Marco
--
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
Visit this group at http://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.
--
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+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at http://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.
Rich
2016-03-15 17:40:25 UTC
Permalink
Where is JsonFormat defined?

No reference to how to print JSON from Java in the docs. Nothing in the API
index.
Post by 'Feng Xiao' via Protocol Buffers
Post by Marco
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.)
JsonFormat.printer().printingDefaultValueFields().print(myMessage);
Likely it will be included in the next beta release.
Post by Marco
--
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
<javascript:>.
Visit this group at http://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.
--
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+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.
'Feng Xiao' via Protocol Buffers
2016-03-15 18:11:44 UTC
Permalink
Post by Rich
Where is JsonFormat defined?
No reference to how to print JSON from Java in the docs. Nothing in the
API index.
See:
https://github.com/google/protobuf/blob/master/java/util/src/main/java/com/google/protobuf/util/JsonFormat.java

It's deployed to maven as a separate artifact: protobuf-java-util.
<http://mvnrepository.com/artifact/com.google.protobuf/protobuf-java-util/3.0.0-beta-2>
Post by Rich
Post by 'Feng Xiao' via Protocol Buffers
Post by Marco
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.)
JsonFormat.printer().printingDefaultValueFields().print(myMessage);
Likely it will be included in the next beta release.
Post by Marco
--
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
Visit this group at http://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.
--
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
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.
--
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+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.
Loading...