Package com.google.protobuf.util
Class JsonFormat.Printer
java.lang.Object
com.google.protobuf.util.JsonFormat.Printer
- Enclosing class:
JsonFormat
A Printer converts protobuf message to JSON format.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate boolean
private Set
<Descriptors.FieldDescriptor> private final JsonFormat.TypeRegistry
private final boolean
private final boolean
private final boolean
private final TypeRegistry
private final boolean
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprivate
Printer
(TypeRegistry registry, JsonFormat.TypeRegistry oldRegistry, boolean alwaysOutputDefaultValueFields, Set<Descriptors.FieldDescriptor> includingDefaultValueFields, boolean preservingProtoFieldNames, boolean omittingInsignificantWhitespace, boolean printingEnumsAsInts, boolean sortingMapKeys) -
Method Summary
Modifier and TypeMethodDescriptionvoid
appendTo
(MessageOrBuilder message, Appendable output) Converts a protobuf message to JSON format.private void
private void
Creates a newJsonFormat.Printer
that will also print fields set to their defaults.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 will omit all 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 JSON format.Creates a newJsonFormat.Printer
that will print 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.
-
Field Details
-
registry
-
oldRegistry
-
alwaysOutputDefaultValueFields
private boolean alwaysOutputDefaultValueFields -
includingDefaultValueFields
-
preservingProtoFieldNames
private final boolean preservingProtoFieldNames -
omittingInsignificantWhitespace
private final boolean omittingInsignificantWhitespace -
printingEnumsAsInts
private final boolean printingEnumsAsInts -
sortingMapKeys
private final boolean sortingMapKeys
-
-
Constructor Details
-
Printer
private Printer(TypeRegistry registry, JsonFormat.TypeRegistry oldRegistry, boolean alwaysOutputDefaultValueFields, Set<Descriptors.FieldDescriptor> includingDefaultValueFields, boolean preservingProtoFieldNames, boolean omittingInsignificantWhitespace, boolean printingEnumsAsInts, boolean sortingMapKeys)
-
-
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
Creates a newJsonFormat.Printer
that will also print fields set to their defaults. Empty repeated fields and map fields will be printed as well. The new Printer clones all other configurations from the currentJsonFormat.Printer
. -
printingEnumsAsInts
Creates a newJsonFormat.Printer
that will print enum field values as integers instead of as string. The new Printer clones all other configurations from the currentJsonFormat.Printer
. -
checkUnsetPrintingEnumsAsInts
private void checkUnsetPrintingEnumsAsInts() -
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. -
checkUnsetIncludingDefaultValueFields
private void checkUnsetIncludingDefaultValueFields() -
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 will omit all 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 appear 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 currentJsonFormat.Printer
. -
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 JSON format.- Throws:
InvalidProtocolBufferException
- if the message contains Any types that can't be resolved.IOException
- if writing to the output fails.
-
print
Converts a protobuf message to JSON format. Throws exceptions if there are unknown Any types in the message.- Throws:
InvalidProtocolBufferException
-