Package com.google.protobuf
Class TextFormat.Printer
java.lang.Object
com.google.protobuf.TextFormat.Printer
- Enclosing class:
TextFormat
Helper class for converting protobufs to text.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static enum
A list of the public APIs that output human-readable text from a message.(package private) static class
An adapter class that can take aMapEntry
and returns its key and entry. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final TextFormat.Printer
private static final TextFormat.Printer
private final boolean
Whether to enable redaction of sensitive fields and introduce randomization.private final boolean
Whether to escape non ASCII characters with backslash and octal.private final ExtensionRegistryLite
private static final ThreadLocal
<TextFormat.Printer.FieldReporterLevel> private final boolean
private final TypeRegistry
private final boolean
Whether to print repeated primitive fields using short square bracket notation. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivate
Printer
(boolean escapeNonAscii, boolean useShortRepeatedPrimitives, TypeRegistry typeRegistry, ExtensionRegistryLite extensionRegistry, boolean enablingSafeDebugFormat, boolean singleLine) -
Method Summary
Modifier and TypeMethodDescriptionprivate void
applyUnstablePrefix
(Appendable output) emittingSingleLine
(boolean singleLine) Return a new Printer instance with the specified line formatting status.(package private) TextFormat.Printer
enablingSafeDebugFormat
(boolean enablingSafeDebugFormat) Return a new Printer instance that outputs a redacted and unstable format suitable for debugging.escapingNonAscii
(boolean escapeNonAscii) Return a new Printer instance with the specified escape mode.private void
print
(MessageOrBuilder message, TextFormat.TextGenerator generator) void
print
(MessageOrBuilder message, Appendable output) Outputs a textual representation of the Protocol Message supplied into the parameter output.(package private) void
print
(MessageOrBuilder message, Appendable output, TextFormat.Printer.FieldReporterLevel level) void
print
(UnknownFieldSet fields, Appendable output) Outputs a textual representation offields
tooutput
.private boolean
printAny
(MessageOrBuilder message, TextFormat.TextGenerator generator) Attempt to print the 'google.protobuf.Any' message in a human-friendly format.private void
printField
(Descriptors.FieldDescriptor field, Object value, TextFormat.TextGenerator generator) void
printField
(Descriptors.FieldDescriptor field, Object value, Appendable output) printFieldToString
(Descriptors.FieldDescriptor field, Object value) private void
printFieldValue
(Descriptors.FieldDescriptor field, Object value, TextFormat.TextGenerator generator) void
printFieldValue
(Descriptors.FieldDescriptor field, Object value, Appendable output) Outputs a textual representation of the value of given field value.private void
printMessage
(MessageOrBuilder message, TextFormat.TextGenerator generator) private void
printShortRepeatedField
(Descriptors.FieldDescriptor field, Object value, TextFormat.TextGenerator generator) private void
printSingleField
(Descriptors.FieldDescriptor field, Object value, TextFormat.TextGenerator generator) printToString
(MessageOrBuilder message) Likeprint()
, but writes directly to aString
and returns it.(package private) String
printToString
(MessageOrBuilder message, TextFormat.Printer.FieldReporterLevel level) printToString
(UnknownFieldSet fields) Likeprint()
, but writes directly to aString
and returns it.private static void
printUnknownField
(int number, int wireType, List<?> values, TextFormat.TextGenerator generator, boolean redact) private static void
printUnknownFields
(UnknownFieldSet unknownFields, TextFormat.TextGenerator generator, boolean redact) private static void
printUnknownFieldValue
(int tag, Object value, TextFormat.TextGenerator generator, boolean redact) (package private) void
shortDebugString
(Descriptors.FieldDescriptor field, Object value) Deprecated.shortDebugString
(MessageOrBuilder message) Deprecated.Usethis.printer().emittingSingleLine(true).printToString(MessageOrBuilder)
shortDebugString
(UnknownFieldSet fields) Deprecated.Usethis.emittingSingleLine(true).printToString(UnknownFieldSet)
private boolean
shouldRedact
(Descriptors.FieldDescriptor field, TextFormat.TextGenerator generator) usingExtensionRegistry
(ExtensionRegistryLite extensionRegistry) Creates a newTextFormat.Printer
using the given extensionRegistry.usingShortRepeatedPrimitives
(boolean useShortRepeatedPrimitives) Return a new Printer instance that outputs primitive repeated fields in short notationusingTypeRegistry
(TypeRegistry typeRegistry) Creates a newTextFormat.Printer
using the given typeRegistry.
-
Field Details
-
DEFAULT_TEXT_FORMAT
-
DEFAULT_DEBUG_FORMAT
-
escapeNonAscii
private final boolean escapeNonAsciiWhether to escape non ASCII characters with backslash and octal. -
useShortRepeatedPrimitives
private final boolean useShortRepeatedPrimitivesWhether to print repeated primitive fields using short square bracket notation. -
typeRegistry
-
extensionRegistry
-
enablingSafeDebugFormat
private final boolean enablingSafeDebugFormatWhether to enable redaction of sensitive fields and introduce randomization. Note that when this is enabled, the output will no longer be deserializable. -
singleLine
private final boolean singleLine -
sensitiveFieldReportingLevel
private static final ThreadLocal<TextFormat.Printer.FieldReporterLevel> sensitiveFieldReportingLevel
-
-
Constructor Details
-
Printer
private Printer(boolean escapeNonAscii, boolean useShortRepeatedPrimitives, TypeRegistry typeRegistry, ExtensionRegistryLite extensionRegistry, boolean enablingSafeDebugFormat, boolean singleLine)
-
-
Method Details
-
escapingNonAscii
Return a new Printer instance with the specified escape mode.- Parameters:
escapeNonAscii
- If true, the new Printer will escape non-ASCII characters (this is the default behavior. If false, the new Printer will print non-ASCII characters as is. In either case, the new Printer still escapes newlines and quotes in strings.- Returns:
- a new Printer that clones all other configurations from the current
TextFormat.Printer
, with the escape mode set to the given parameter.
-
usingTypeRegistry
Creates a newTextFormat.Printer
using the given typeRegistry. The new Printer clones all other configurations from the currentTextFormat.Printer
.- Throws:
IllegalArgumentException
- if a registry is already set.
-
usingExtensionRegistry
Creates a newTextFormat.Printer
using the given extensionRegistry. The new Printer clones all other configurations from the currentTextFormat.Printer
.- Throws:
IllegalArgumentException
- if a registry is already set.
-
enablingSafeDebugFormat
Return a new Printer instance that outputs a redacted and unstable format suitable for debugging.- Parameters:
enablingSafeDebugFormat
- If true, the new Printer will redact all proto fields that are marked by a debug_redact=true option, and apply an unstable prefix to the output.- Returns:
- a new Printer that clones all other configurations from the current
TextFormat.Printer
, with the enablingSafeDebugFormat mode set to the given parameter.
-
usingShortRepeatedPrimitives
Return a new Printer instance that outputs primitive repeated fields in short notation- Parameters:
useShortRepeatedPrimitives
- If true, repeated fields with a primitive type are printed using the short hand notation with comma-delimited field values in square brackets.- Returns:
- a new Printer that clones all other configurations from the current
TextFormat.Printer
, with the useShortRepeatedPrimitives mode set to the given parameter.
-
emittingSingleLine
Return a new Printer instance with the specified line formatting status.- Parameters:
singleLine
- If true, the new Printer will output no newline characters.- Returns:
- a new Printer that clones all other configurations from the current
TextFormat.Printer
, with the singleLine mode set to the given parameter.
-
setSensitiveFieldReportingLevel
-
print
Outputs a textual representation of the Protocol Message supplied into the parameter output. (This representation is the new version of the classic "ProtocolPrinter" output from the original Protocol Buffer system)- Throws:
IOException
-
print
void print(MessageOrBuilder message, Appendable output, TextFormat.Printer.FieldReporterLevel level) throws IOException - Throws:
IOException
-
print
Outputs a textual representation offields
tooutput
.- Throws:
IOException
-
print
- Throws:
IOException
-
applyUnstablePrefix
-
printAny
private boolean printAny(MessageOrBuilder message, TextFormat.TextGenerator generator) throws IOException Attempt to print the 'google.protobuf.Any' message in a human-friendly format. Returns false if the message isn't a valid 'google.protobuf.Any' message (in which case the message should be rendered just like a regular message to help debugging).- Throws:
IOException
-
printFieldToString
-
printField
public void printField(Descriptors.FieldDescriptor field, Object value, Appendable output) throws IOException - Throws:
IOException
-
printField
private void printField(Descriptors.FieldDescriptor field, Object value, TextFormat.TextGenerator generator) throws IOException - Throws:
IOException
-
printFieldValue
public void printFieldValue(Descriptors.FieldDescriptor field, Object value, Appendable output) throws IOException Outputs a textual representation of the value of given field value.- Parameters:
field
- the descriptor of the fieldvalue
- the value of the fieldoutput
- the output to which to append the formatted value- Throws:
ClassCastException
- if the value is not appropriate for the given field descriptorIOException
- if there is an exception writing to the output
-
printFieldValue
private void printFieldValue(Descriptors.FieldDescriptor field, Object value, TextFormat.TextGenerator generator) throws IOException - Throws:
IOException
-
shouldRedact
-
printToString
Likeprint()
, but writes directly to aString
and returns it. -
printToString
-
printToString
Likeprint()
, but writes directly to aString
and returns it. -
shortDebugString
Deprecated.Usethis.printer().emittingSingleLine(true).printToString(MessageOrBuilder)
Generates a human readable form of this message, useful for debugging and other purposes, with no newline characters. -
shortDebugString
@Deprecated @InlineMe(replacement="this.emittingSingleLine(true).printFieldToString(field, value)") public String shortDebugString(Descriptors.FieldDescriptor field, Object value) Deprecated.Usethis.emittingSingleLine(true).printFieldToString(FieldDescriptor, Object)
Generates a human readable form of the field, useful for debugging and other purposes, with no newline characters. -
shortDebugString
@Deprecated @InlineMe(replacement="this.emittingSingleLine(true).printToString(fields)") public String shortDebugString(UnknownFieldSet fields) Deprecated.Usethis.emittingSingleLine(true).printToString(UnknownFieldSet)
Generates a human readable form of the unknown fields, useful for debugging and other purposes, with no newline characters. -
printUnknownFieldValue
private static void printUnknownFieldValue(int tag, Object value, TextFormat.TextGenerator generator, boolean redact) throws IOException - Throws:
IOException
-
printMessage
private void printMessage(MessageOrBuilder message, TextFormat.TextGenerator generator) throws IOException - Throws:
IOException
-
printShortRepeatedField
private void printShortRepeatedField(Descriptors.FieldDescriptor field, Object value, TextFormat.TextGenerator generator) throws IOException - Throws:
IOException
-
printSingleField
private void printSingleField(Descriptors.FieldDescriptor field, Object value, TextFormat.TextGenerator generator) throws IOException - Throws:
IOException
-
printUnknownFields
private static void printUnknownFields(UnknownFieldSet unknownFields, TextFormat.TextGenerator generator, boolean redact) throws IOException - Throws:
IOException
-
printUnknownField
private static void printUnknownField(int number, int wireType, List<?> values, TextFormat.TextGenerator generator, boolean redact) throws IOException - Throws:
IOException
-
this.emittingSingleLine(true).printFieldToString(FieldDescriptor, Object)