Package com.google.protobuf.util
Class JsonFormat.PrinterImpl
- java.lang.Object
-
- com.google.protobuf.util.JsonFormat.PrinterImpl
-
- Enclosing class:
- JsonFormat
private static final class JsonFormat.PrinterImpl extends java.lang.ObjectA Printer converts protobuf messages to the ProtoJSON format.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classJsonFormat.PrinterImpl.GsonHolderprivate static interfaceJsonFormat.PrinterImpl.WellKnownTypePrinter
-
Field Summary
Fields Modifier and Type Field Description private java.lang.CharSequenceblankOrSpaceprivate ExtensionRegistryextensionRegistryprivate JsonFormat.TextGeneratorgeneratorprivate com.google.gson.Gsongsonprivate java.util.Set<Descriptors.FieldDescriptor>includingDefaultValueFieldsprivate JsonFormat.TypeRegistryoldRegistryprivate booleanpreservingProtoFieldNamesprivate booleanprintingEnumsAsIntsprivate booleanprintingFullyQualifiedExtensionNamesprivate TypeRegistryregistryprivate static java.lang.String[]replacementCharsprivate JsonFormat.ShouldPrintDefaultsshouldPrintDefaultsprivate booleansortingMapKeysprivate static java.util.Map<java.lang.String,JsonFormat.PrinterImpl.WellKnownTypePrinter>wellKnownTypePrinters
-
Constructor Summary
Constructors Constructor Description PrinterImpl(TypeRegistry registry, JsonFormat.TypeRegistry oldRegistry, ExtensionRegistry extensionRegistry, JsonFormat.ShouldPrintDefaults shouldPrintDefaults, java.util.Set<Descriptors.FieldDescriptor> includingDefaultValueFields, boolean preservingProtoFieldNames, java.lang.Appendable jsonOutput, boolean omittingInsignificantWhitespace, boolean printingEnumsAsInts, boolean sortingMapKeys, boolean printingFullyQualifiedExtensionNames)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static java.util.Map<java.lang.String,JsonFormat.PrinterImpl.WellKnownTypePrinter>buildWellKnownTypePrinters()private static java.lang.StringgetReplacementOrNull(char c)If a character needs to be escaped, returns the replacement string.(package private) voidprint(MessageOrBuilder message)private voidprint(MessageOrBuilder message, java.lang.String typeUrl)Prints a regular message with an optional type URL.private voidprintAny(MessageOrBuilder message)Prints google.protobuf.Anyprivate voidprintDuration(MessageOrBuilder message)Prints google.protobuf.Durationprivate voidprintField(Descriptors.FieldDescriptor field, java.lang.Object value)private voidprintFieldMask(MessageOrBuilder message)Prints google.protobuf.FieldMaskprivate voidprintListValue(MessageOrBuilder message)Prints google.protobuf.ListValueprivate voidprintListValueConcrete(ListValueOrBuilder message)Prints `google.protobuf.ListValue` using the concrete generated type.private voidprintListValueReflectively(MessageOrBuilder message)Prints `google.protobuf.ListValue` using reflection (generally only needed for DynamicMessage)private voidprintMapFieldValue(Descriptors.FieldDescriptor field, java.lang.Object value)private voidprintRepeatedFieldValue(Descriptors.FieldDescriptor field, java.lang.Object value)private voidprintSingleFieldValue(Descriptors.FieldDescriptor field, java.lang.Object value)private voidprintSingleFieldValue(Descriptors.FieldDescriptor field, java.lang.Object value, boolean alwaysWithQuotes)Prints a field's value in the ProtoJSON format.private voidprintStringEscapedAndQuoted(java.lang.CharSequence value)Prints a string value wrapped in double quotes, escaping any illegal or dangerous characters for JSON safety.private voidprintStruct(MessageOrBuilder message)Prints google.protobuf.Structprivate voidprintStructConcrete(StructOrBuilder message)Prints the `google.protobuf.Struct` using the concrete generated class.private voidprintStructReflectively(MessageOrBuilder message)Prints the `google.protobuf.Struct` using reflection only.private voidprintTimestamp(MessageOrBuilder message)Prints google.protobuf.Timestampprivate voidprintValue(MessageOrBuilder message)Prints `google.protobuf.Value`.private voidprintValueConcrete(ValueOrBuilder message)Prints `google.protobuf.Value` using the concrete generated type.private voidprintValueReflectively(MessageOrBuilder message)Prints google.protobuf.Value using reflection (generally only needed for DynamicMessage)private voidprintWrapper(MessageOrBuilder message)Prints wrapper types (e.g., google.protobuf.Int32Value)private booleanshouldSpeciallyPrint(Descriptors.FieldDescriptor field)private ByteStringtoByteString(MessageOrBuilder message)
-
-
-
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
-
printingEnumsAsInts
private final boolean printingEnumsAsInts
-
sortingMapKeys
private final boolean sortingMapKeys
-
printingFullyQualifiedExtensionNames
private final boolean printingFullyQualifiedExtensionNames
-
generator
private final JsonFormat.TextGenerator generator
-
gson
private final com.google.gson.Gson gson
-
blankOrSpace
private final java.lang.CharSequence blankOrSpace
-
wellKnownTypePrinters
private static final java.util.Map<java.lang.String,JsonFormat.PrinterImpl.WellKnownTypePrinter> wellKnownTypePrinters
-
replacementChars
private static final java.lang.String[] replacementChars
-
-
Constructor Detail
-
PrinterImpl
PrinterImpl(TypeRegistry registry, JsonFormat.TypeRegistry oldRegistry, ExtensionRegistry extensionRegistry, JsonFormat.ShouldPrintDefaults shouldPrintDefaults, java.util.Set<Descriptors.FieldDescriptor> includingDefaultValueFields, boolean preservingProtoFieldNames, java.lang.Appendable jsonOutput, boolean omittingInsignificantWhitespace, boolean printingEnumsAsInts, boolean sortingMapKeys, boolean printingFullyQualifiedExtensionNames)
-
-
Method Detail
-
print
void print(MessageOrBuilder message) throws java.io.IOException
- Throws:
java.io.IOException
-
buildWellKnownTypePrinters
private static java.util.Map<java.lang.String,JsonFormat.PrinterImpl.WellKnownTypePrinter> buildWellKnownTypePrinters()
-
printAny
private void printAny(MessageOrBuilder message) throws java.io.IOException
Prints google.protobuf.Any- Throws:
java.io.IOException
-
printWrapper
private void printWrapper(MessageOrBuilder message) throws java.io.IOException
Prints wrapper types (e.g., google.protobuf.Int32Value)- Throws:
java.io.IOException
-
toByteString
private ByteString toByteString(MessageOrBuilder message)
-
printTimestamp
private void printTimestamp(MessageOrBuilder message) throws java.io.IOException
Prints google.protobuf.Timestamp- Throws:
java.io.IOException
-
printDuration
private void printDuration(MessageOrBuilder message) throws java.io.IOException
Prints google.protobuf.Duration- Throws:
java.io.IOException
-
printFieldMask
private void printFieldMask(MessageOrBuilder message) throws java.io.IOException
Prints google.protobuf.FieldMask- Throws:
java.io.IOException
-
printStruct
private void printStruct(MessageOrBuilder message) throws java.io.IOException
Prints google.protobuf.Struct- Throws:
java.io.IOException
-
printStructConcrete
private void printStructConcrete(StructOrBuilder message) throws java.io.IOException
Prints the `google.protobuf.Struct` using the concrete generated class.- Throws:
java.io.IOException
-
printStructReflectively
private void printStructReflectively(MessageOrBuilder message) throws java.io.IOException
Prints the `google.protobuf.Struct` using reflection only. This should only be used for DynamicMessage usecases.- Throws:
java.io.IOException
-
printValue
private void printValue(MessageOrBuilder message) throws java.io.IOException
Prints `google.protobuf.Value`.- Throws:
java.io.IOException
-
printValueConcrete
private void printValueConcrete(ValueOrBuilder message) throws java.io.IOException
Prints `google.protobuf.Value` using the concrete generated type.- Throws:
java.io.IOException
-
printValueReflectively
private void printValueReflectively(MessageOrBuilder message) throws java.io.IOException
Prints google.protobuf.Value using reflection (generally only needed for DynamicMessage)- Throws:
java.io.IOException
-
printListValue
private void printListValue(MessageOrBuilder message) throws java.io.IOException
Prints google.protobuf.ListValue- Throws:
java.io.IOException
-
printListValueConcrete
private void printListValueConcrete(ListValueOrBuilder message) throws java.io.IOException
Prints `google.protobuf.ListValue` using the concrete generated type.- Throws:
java.io.IOException
-
printListValueReflectively
private void printListValueReflectively(MessageOrBuilder message) throws java.io.IOException
Prints `google.protobuf.ListValue` using reflection (generally only needed for DynamicMessage)- Throws:
java.io.IOException
-
shouldSpeciallyPrint
private boolean shouldSpeciallyPrint(Descriptors.FieldDescriptor field)
- Returns:
- Whether a set option means the corresponding field should be printed even if it normally wouldn't be.
-
print
private void print(MessageOrBuilder message, @Nullable java.lang.String typeUrl) throws java.io.IOException
Prints a regular message with an optional type URL.- Throws:
java.io.IOException
-
printField
private void printField(Descriptors.FieldDescriptor field, java.lang.Object value) throws java.io.IOException
- Throws:
java.io.IOException
-
printRepeatedFieldValue
private void printRepeatedFieldValue(Descriptors.FieldDescriptor field, java.lang.Object value) throws java.io.IOException
- Throws:
java.io.IOException
-
printMapFieldValue
private void printMapFieldValue(Descriptors.FieldDescriptor field, java.lang.Object value) throws java.io.IOException
- Throws:
java.io.IOException
-
printSingleFieldValue
private void printSingleFieldValue(Descriptors.FieldDescriptor field, java.lang.Object value) throws java.io.IOException
- Throws:
java.io.IOException
-
printSingleFieldValue
private void printSingleFieldValue(Descriptors.FieldDescriptor field, java.lang.Object value, boolean alwaysWithQuotes) throws java.io.IOException
Prints a field's value in the ProtoJSON format.- Parameters:
alwaysWithQuotes- whether to always add double-quotes to primitive types- Throws:
java.io.IOException
-
getReplacementOrNull
private static java.lang.String getReplacementOrNull(char c)
If a character needs to be escaped, returns the replacement string. Otherwise, returns null.
-
printStringEscapedAndQuoted
private void printStringEscapedAndQuoted(java.lang.CharSequence value) throws java.io.IOExceptionPrints a string value wrapped in double quotes, escaping any illegal or dangerous characters for JSON safety.- Throws:
java.io.IOException
-
-