Package com.google.protobuf.util
Class JsonFormat.Printer
java.lang.Object
com.google.protobuf.util.JsonFormat.Printer
- Enclosing class:
JsonFormat
A Printer converts a protobuf message to the proto3 JSON format.
-
Method Summary
Modifier and TypeMethodDescriptionCreates a newJsonFormat.Printer
that will print any field that does not support presence even if it would not otherwise be printed (empty repeated fields, empty map fields, and implicit presence scalars set to their default value).void
appendTo
(MessageOrBuilder message, Appendable output) Converts a protobuf message to the proto3 JSON format.Deprecated.This method is deprecated, and slated for removal in the next Java breaking change (5.x).includingDefaultValueFields
(Set<Descriptors.FieldDescriptor> fieldsToAlwaysOutput) Creates a newJsonFormat.Printer
that will also print default-valued fields if their FieldDescriptors are found in the supplied set.Create a newJsonFormat.Printer
that omits insignificant whitespace in the JSON output.Creates a newJsonFormat.Printer
that is configured to use the original proto field names as defined in the .proto file rather than converting them to lowerCamelCase.print
(MessageOrBuilder message) Converts a protobuf message to the proto3 JSON format.Creates a newJsonFormat.Printer
that prints enum field values as integers instead of as string.Create a newJsonFormat.Printer
that will sort the map keys in the JSON output.usingTypeRegistry
(TypeRegistry registry) Creates a newJsonFormat.Printer
using the given registry.usingTypeRegistry
(JsonFormat.TypeRegistry oldRegistry) Creates a newJsonFormat.Printer
using the given registry.
-
Method Details
-
usingTypeRegistry
Creates a newJsonFormat.Printer
using the given registry. The new Printer clones all other configurations from the currentJsonFormat.Printer
.- Throws:
IllegalArgumentException
- if a registry is already set
-
usingTypeRegistry
Creates a newJsonFormat.Printer
using the given registry. The new Printer clones all other configurations from the currentJsonFormat.Printer
.- Throws:
IllegalArgumentException
- if a registry is already set
-
includingDefaultValueFields
Deprecated.This method is deprecated, and slated for removal in the next Java breaking change (5.x). PreferalwaysPrintFieldsWithNoPresence()
Creates a newJsonFormat.Printer
that will always print fields unless they are a message type or in a oneof.Note that this does print Proto2 Optional but does not print Proto3 Optional fields, as the latter is represented using a synthetic oneof.
The new Printer clones all other configurations from the current
JsonFormat.Printer
. -
includingDefaultValueFields
public JsonFormat.Printer includingDefaultValueFields(Set<Descriptors.FieldDescriptor> fieldsToAlwaysOutput) Creates a newJsonFormat.Printer
that will also print default-valued fields if their FieldDescriptors are found in the supplied set. Empty repeated fields and map fields will be printed as well, if they match. The new Printer clones all other configurations from the currentJsonFormat.Printer
. Call includingDefaultValueFields() with no args to unconditionally output all fields.Note that non-repeated message fields or fields in a oneof are not honored if provided here.
-
alwaysPrintFieldsWithNoPresence
Creates a newJsonFormat.Printer
that will print any field that does not support presence even if it would not otherwise be printed (empty repeated fields, empty map fields, and implicit presence scalars set to their default value). The new Printer clones all other configurations from the currentJsonFormat.Printer
. -
printingEnumsAsInts
Creates a newJsonFormat.Printer
that prints enum field values as integers instead of as string. The new Printer clones all other configurations from the currentJsonFormat.Printer
. -
preservingProtoFieldNames
Creates a newJsonFormat.Printer
that is configured to use the original proto field names as defined in the .proto file rather than converting them to lowerCamelCase. The new Printer clones all other configurations from the currentJsonFormat.Printer
. -
omittingInsignificantWhitespace
Create a newJsonFormat.Printer
that omits insignificant whitespace in the JSON output. This new Printer clones all other configurations from the current Printer. Insignificant whitespace is defined by the JSON spec as whitespace that appears between JSON structural elements:ws = *( %x20 / ; Space %x09 / ; Horizontal tab %x0A / ; Line feed or New line %x0D ) ; Carriage return
See https://tools.ietf.org/html/rfc7159. -
sortingMapKeys
Create a newJsonFormat.Printer
that will sort the map keys in the JSON output.Use of this modifier is discouraged. The generated JSON messages are equivalent with and without this option set, but there are some corner use cases that demand a stable output, while order of map keys is otherwise arbitrary.
The generated order is not well-defined and should not be depended on, but it's stable.
This new Printer clones all other configurations from the current
JsonFormat.Printer
. -
appendTo
Converts a protobuf message to the proto3 JSON format.- Throws:
com.google.protobuf.InvalidProtocolBufferException
- if the message contains Any types that can't be resolvedIOException
- if writing to the output fails
-
print
public String print(MessageOrBuilder message) throws com.google.protobuf.InvalidProtocolBufferException Converts a protobuf message to the proto3 JSON format. Throws exceptions if there are unknown Any types in the message.- Throws:
com.google.protobuf.InvalidProtocolBufferException
-