Class JsonFormat.Printer
- java.lang.Object
-
- com.google.protobuf.util.JsonFormat.Printer
-
- Enclosing class:
- JsonFormat
public static class JsonFormat.Printer extends java.lang.ObjectA Printer converts a protobuf message to the ProtoJSON format.
-
-
Field Summary
Fields Modifier and Type Field Description private ExtensionRegistryextensionRegistryprivate java.util.Set<Descriptors.FieldDescriptor>includingDefaultValueFieldsprivate JsonFormat.TypeRegistryoldRegistryprivate booleanomittingInsignificantWhitespaceprivate booleanpreservingProtoFieldNamesprivate booleanprintingEnumsAsIntsprivate booleanprintingFullyQualifiedExtensionNamesprivate TypeRegistryregistryprivate JsonFormat.ShouldPrintDefaultsshouldPrintDefaultsprivate booleansortingMapKeys
-
Constructor Summary
Constructors Modifier Constructor Description privatePrinter(TypeRegistry registry, JsonFormat.TypeRegistry oldRegistry, ExtensionRegistry extensionRegistry, JsonFormat.ShouldPrintDefaults shouldOutputDefaults, java.util.Set<Descriptors.FieldDescriptor> includingDefaultValueFields, boolean preservingProtoFieldNames, boolean omittingInsignificantWhitespace, boolean printingEnumsAsInts, boolean sortingMapKeys, boolean printingFullyQualifiedExtensionNames)
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description JsonFormat.PrinteralwaysPrintFieldsWithNoPresence()Creates a newJsonFormat.Printerthat 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).voidappendTo(MessageOrBuilder message, java.lang.Appendable output)Converts a protobuf message to the ProtoJSON format.private voidcheckUnsetPrintingEnumsAsInts()JsonFormat.PrinterincludingDefaultValueFields()Deprecated.This method is deprecated, and slated for removal in the next Java breaking change (5.x).JsonFormat.PrinterincludingDefaultValueFields(java.util.Set<Descriptors.FieldDescriptor> fieldsToAlwaysOutput)Creates a newJsonFormat.Printerthat will also print default-valued fields if their FieldDescriptors are found in the supplied set.JsonFormat.PrinteromittingInsignificantWhitespace()Create a newJsonFormat.Printerthat omits insignificant whitespace in the JSON output.JsonFormat.PrinterpreservingProtoFieldNames()Creates a newJsonFormat.Printerthat is configured to use the original proto field names as defined in the .proto file rather than converting them to lowerCamelCase.java.lang.Stringprint(MessageOrBuilder message)Converts a protobuf message to the ProtoJSON format.(package private) JsonFormat.PrinterprintingDeprecatedNonConformantShortExtensionNames()Deprecated.This method is for non-conformant code which relies on incorrect behavior.JsonFormat.PrinterprintingEnumsAsInts()Creates a newJsonFormat.Printerthat prints enum field values as integers instead of as string.(package private) JsonFormat.PrinterprintingFullyQualifiedExtensionNames()Internal method only.JsonFormat.PrintersortingMapKeys()Create a newJsonFormat.Printerthat will sort the map keys in the JSON output.(package private) JsonFormat.PrinterusingExtensionRegistry(ExtensionRegistry extensionRegistry)Creates a newJsonFormat.Printerusing the given extension registry.JsonFormat.PrinterusingTypeRegistry(TypeRegistry registry)Creates a newJsonFormat.Printerusing the given registry.JsonFormat.PrinterusingTypeRegistry(JsonFormat.TypeRegistry oldRegistry)Creates a newJsonFormat.Printerusing the given registry.
-
-
-
Field Detail
-
registry
private final TypeRegistry registry
-
oldRegistry
private final JsonFormat.TypeRegistry oldRegistry
-
extensionRegistry
private final ExtensionRegistry extensionRegistry
-
shouldPrintDefaults
private final JsonFormat.ShouldPrintDefaults shouldPrintDefaults
-
includingDefaultValueFields
private final java.util.Set<Descriptors.FieldDescriptor> includingDefaultValueFields
-
preservingProtoFieldNames
private final boolean preservingProtoFieldNames
-
omittingInsignificantWhitespace
private final boolean omittingInsignificantWhitespace
-
printingEnumsAsInts
private final boolean printingEnumsAsInts
-
sortingMapKeys
private final boolean sortingMapKeys
-
printingFullyQualifiedExtensionNames
private final boolean printingFullyQualifiedExtensionNames
-
-
Constructor Detail
-
Printer
private Printer(TypeRegistry registry, JsonFormat.TypeRegistry oldRegistry, ExtensionRegistry extensionRegistry, JsonFormat.ShouldPrintDefaults shouldOutputDefaults, java.util.Set<Descriptors.FieldDescriptor> includingDefaultValueFields, boolean preservingProtoFieldNames, boolean omittingInsignificantWhitespace, boolean printingEnumsAsInts, boolean sortingMapKeys, boolean printingFullyQualifiedExtensionNames)
-
-
Method Detail
-
usingTypeRegistry
public JsonFormat.Printer usingTypeRegistry(JsonFormat.TypeRegistry oldRegistry)
Creates a newJsonFormat.Printerusing the given registry. The new Printer clones all other configurations from the currentJsonFormat.Printer.- Throws:
java.lang.IllegalArgumentException- if a registry is already set
-
usingTypeRegistry
public JsonFormat.Printer usingTypeRegistry(TypeRegistry registry)
Creates a newJsonFormat.Printerusing the given registry. The new Printer clones all other configurations from the currentJsonFormat.Printer.- Throws:
java.lang.IllegalArgumentException- if a registry is already set
-
usingExtensionRegistry
JsonFormat.Printer usingExtensionRegistry(ExtensionRegistry extensionRegistry)
Creates a newJsonFormat.Printerusing the given extension registry. The new Printer clones all other configurations from the currentJsonFormat.Printer.
-
includingDefaultValueFields
@Deprecated public JsonFormat.Printer includingDefaultValueFields()
Deprecated.This method is deprecated, and slated for removal in the next Java breaking change (5.x). PreferalwaysPrintFieldsWithNoPresence()Creates a newJsonFormat.Printerthat 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(java.util.Set<Descriptors.FieldDescriptor> fieldsToAlwaysOutput)
Creates a newJsonFormat.Printerthat 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.Note that non-repeated message fields or fields in a oneof are not honored if provided here.
-
alwaysPrintFieldsWithNoPresence
public JsonFormat.Printer alwaysPrintFieldsWithNoPresence()
Creates a newJsonFormat.Printerthat 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
public JsonFormat.Printer printingEnumsAsInts()
Creates a newJsonFormat.Printerthat prints enum field values as integers instead of as string. The new Printer clones all other configurations from the currentJsonFormat.Printer.
-
checkUnsetPrintingEnumsAsInts
private void checkUnsetPrintingEnumsAsInts()
-
preservingProtoFieldNames
public JsonFormat.Printer preservingProtoFieldNames()
Creates a newJsonFormat.Printerthat 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
public JsonFormat.Printer omittingInsignificantWhitespace()
Create a newJsonFormat.Printerthat 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
public JsonFormat.Printer sortingMapKeys()
Create a newJsonFormat.Printerthat 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.
-
printingFullyQualifiedExtensionNames
JsonFormat.Printer printingFullyQualifiedExtensionNames()
Internal method only. Creates a newJsonFormat.Printerthat prints extensions with their fully qualified name instead of their short name. The new Printer clones all other configurations from the currentJsonFormat.Printer.This method forces extension names to be printed with their fully qualified name. See tests for examples of incorrect behavior.
This method is not supported in open source.
-
printingDeprecatedNonConformantShortExtensionNames
@Deprecated JsonFormat.Printer printingDeprecatedNonConformantShortExtensionNames()
Deprecated.This method is for non-conformant code which relies on incorrect behavior.Internal method only. Creates a newJsonFormat.Printerthat prints extensions with their short name instead of their fully qualified name. The new Printer clones all other configurations from the currentJsonFormat.Printer.This method is only used to restore the previous incorrect behavior if clients depend on it and cannot be migrated at once. See tests for examples of incorrect behavior.
This method is not supported in open source.
-
appendTo
public void appendTo(MessageOrBuilder message, java.lang.Appendable output) throws java.io.IOException
Converts a protobuf message to the ProtoJSON format.- Throws:
InvalidProtocolBufferException- if the message contains Any types that can't be resolvedjava.io.IOException- if writing to the output fails
-
print
public java.lang.String print(MessageOrBuilder message) throws InvalidProtocolBufferException
Converts a protobuf message to the ProtoJSON format. Throws exceptions if there are unknown Any types in the message.- Throws:
InvalidProtocolBufferException
-
-