Package com.google.protobuf
Class TextFormat.Printer
- java.lang.Object
-
- com.google.protobuf.TextFormat.Printer
-
- Enclosing class:
- TextFormat
public static final class TextFormat.Printer extends java.lang.ObjectHelper class for converting protobufs to text.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static classTextFormat.Printer.FieldReporterLevelA list of the public APIs that output human-readable text from a message.private static classTextFormat.Printer.MapEntryAdapterAn adapter class that can take aMapEntryand returns its key and entry.
-
Field Summary
Fields Modifier and Type Field Description private static TextFormat.PrinterDEFAULT_DEBUG_FORMATprivate static TextFormat.PrinterDEFAULT_TEXT_FORMATprivate booleanenablingSafeDebugFormatWhether to enable redaction of sensitive fields and introduce randomization.private booleanescapeNonAsciiWhether to escape non ASCII characters with backslash and octal.private ExtensionRegistryLiteextensionRegistryprivate static java.lang.ThreadLocal<TextFormat.Printer.FieldReporterLevel>sensitiveFieldReportingLevelprivate booleansingleLineprivate TypeRegistrytypeRegistry
-
Constructor Summary
Constructors Modifier Constructor Description privatePrinter(boolean escapeNonAscii, TypeRegistry typeRegistry, ExtensionRegistryLite extensionRegistry, boolean enablingSafeDebugFormat, boolean singleLine)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description private voidapplyUnstablePrefix(java.lang.Appendable output)TextFormat.PrinteremittingSingleLine(boolean singleLine)Return a new Printer instance with the specified line formatting status.(package private) TextFormat.PrinterenablingSafeDebugFormat(boolean enablingSafeDebugFormat)Return a new Printer instance that outputs a redacted and unstable format suitable for debugging.TextFormat.PrinterescapingNonAscii(boolean escapeNonAscii)Return a new Printer instance with the specified escape mode.private voidprint(MessageOrBuilder message, TextFormat.TextGenerator generator)voidprint(MessageOrBuilder message, java.lang.Appendable output)Outputs a textual representation of the Protocol Message supplied into the parameter output.(package private) voidprint(MessageOrBuilder message, java.lang.Appendable output, TextFormat.Printer.FieldReporterLevel level)voidprint(UnknownFieldSet fields, java.lang.Appendable output)Outputs a textual representation offieldstooutput.private booleanprintAny(MessageOrBuilder message, TextFormat.TextGenerator generator)Attempt to print the 'google.protobuf.Any' message in a human-friendly format.private voidprintField(Descriptors.FieldDescriptor field, java.lang.Object value, TextFormat.TextGenerator generator)voidprintField(Descriptors.FieldDescriptor field, java.lang.Object value, java.lang.Appendable output)java.lang.StringprintFieldToString(Descriptors.FieldDescriptor field, java.lang.Object value)private voidprintFieldValue(Descriptors.FieldDescriptor field, java.lang.Object value, TextFormat.TextGenerator generator)voidprintFieldValue(Descriptors.FieldDescriptor field, java.lang.Object value, java.lang.Appendable output)Outputs a textual representation of the value of given field value.private voidprintMessage(MessageOrBuilder message, TextFormat.TextGenerator generator)private voidprintSingleField(Descriptors.FieldDescriptor field, java.lang.Object value, TextFormat.TextGenerator generator)java.lang.StringprintToString(MessageOrBuilder message)Likeprint(), but writes directly to aStringand returns it.(package private) java.lang.StringprintToString(MessageOrBuilder message, TextFormat.Printer.FieldReporterLevel level)java.lang.StringprintToString(UnknownFieldSet fields)Likeprint(), but writes directly to aStringand returns it.private static voidprintUnknownField(int number, int wireType, java.util.List<?> values, TextFormat.TextGenerator generator, boolean redact)private static voidprintUnknownFields(UnknownFieldSet unknownFields, TextFormat.TextGenerator generator, boolean redact)private static voidprintUnknownFieldValue(int tag, java.lang.Object value, TextFormat.TextGenerator generator, boolean redact)(package private) voidsetSensitiveFieldReportingLevel(TextFormat.Printer.FieldReporterLevel level)java.lang.StringshortDebugString(Descriptors.FieldDescriptor field, java.lang.Object value)Deprecated.Usethis.emittingSingleLine(true).printFieldToString(FieldDescriptor, Object)java.lang.StringshortDebugString(MessageOrBuilder message)Deprecated.Usethis.printer().emittingSingleLine(true).printToString(MessageOrBuilder)java.lang.StringshortDebugString(UnknownFieldSet fields)Deprecated.Usethis.emittingSingleLine(true).printToString(UnknownFieldSet)private booleanshouldRedact(Descriptors.FieldDescriptor field, TextFormat.TextGenerator generator)private booleanshouldRedactOptionValue(Descriptors.EnumValueDescriptor optionValue)private booleanshouldReport(TextFormat.Printer.FieldReporterLevel level)TextFormat.PrinterusingExtensionRegistry(ExtensionRegistryLite extensionRegistry)Creates a newTextFormat.Printerusing the given extensionRegistry.TextFormat.PrinterusingTypeRegistry(TypeRegistry typeRegistry)Creates a newTextFormat.Printerusing the given typeRegistry.
-
-
-
Field Detail
-
DEFAULT_TEXT_FORMAT
private static final TextFormat.Printer DEFAULT_TEXT_FORMAT
-
DEFAULT_DEBUG_FORMAT
private static final TextFormat.Printer DEFAULT_DEBUG_FORMAT
-
escapeNonAscii
private final boolean escapeNonAscii
Whether to escape non ASCII characters with backslash and octal.
-
typeRegistry
private final TypeRegistry typeRegistry
-
extensionRegistry
private final ExtensionRegistryLite extensionRegistry
-
enablingSafeDebugFormat
private final boolean enablingSafeDebugFormat
Whether 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 java.lang.ThreadLocal<TextFormat.Printer.FieldReporterLevel> sensitiveFieldReportingLevel
-
-
Constructor Detail
-
Printer
private Printer(boolean escapeNonAscii, TypeRegistry typeRegistry, ExtensionRegistryLite extensionRegistry, boolean enablingSafeDebugFormat, boolean singleLine)
-
-
Method Detail
-
escapingNonAscii
public TextFormat.Printer escapingNonAscii(boolean escapeNonAscii)
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
public TextFormat.Printer usingTypeRegistry(TypeRegistry typeRegistry)
Creates a newTextFormat.Printerusing the given typeRegistry. The new Printer clones all other configurations from the currentTextFormat.Printer.- Throws:
java.lang.IllegalArgumentException- if a registry is already set.
-
usingExtensionRegistry
public TextFormat.Printer usingExtensionRegistry(ExtensionRegistryLite extensionRegistry)
Creates a newTextFormat.Printerusing the given extensionRegistry. The new Printer clones all other configurations from the currentTextFormat.Printer.- Throws:
java.lang.IllegalArgumentException- if a registry is already set.
-
enablingSafeDebugFormat
TextFormat.Printer enablingSafeDebugFormat(boolean 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.
-
emittingSingleLine
public TextFormat.Printer emittingSingleLine(boolean singleLine)
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
void setSensitiveFieldReportingLevel(TextFormat.Printer.FieldReporterLevel level)
-
print
public void print(MessageOrBuilder message, java.lang.Appendable output) throws java.io.IOException
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:
java.io.IOException
-
print
void print(MessageOrBuilder message, java.lang.Appendable output, TextFormat.Printer.FieldReporterLevel level) throws java.io.IOException
- Throws:
java.io.IOException
-
print
public void print(UnknownFieldSet fields, java.lang.Appendable output) throws java.io.IOException
Outputs a textual representation offieldstooutput.- Throws:
java.io.IOException
-
print
private void print(MessageOrBuilder message, TextFormat.TextGenerator generator) throws java.io.IOException
- Throws:
java.io.IOException
-
applyUnstablePrefix
private void applyUnstablePrefix(java.lang.Appendable output)
-
printAny
private boolean printAny(MessageOrBuilder message, TextFormat.TextGenerator generator) throws java.io.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:
java.io.IOException
-
printFieldToString
public java.lang.String printFieldToString(Descriptors.FieldDescriptor field, java.lang.Object value)
-
printField
public void printField(Descriptors.FieldDescriptor field, java.lang.Object value, java.lang.Appendable output) throws java.io.IOException
- Throws:
java.io.IOException
-
printField
private void printField(Descriptors.FieldDescriptor field, java.lang.Object value, TextFormat.TextGenerator generator) throws java.io.IOException
- Throws:
java.io.IOException
-
printFieldValue
public void printFieldValue(Descriptors.FieldDescriptor field, java.lang.Object value, java.lang.Appendable output) throws java.io.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:
java.lang.ClassCastException- if the value is not appropriate for the given field descriptorjava.io.IOException- if there is an exception writing to the output
-
printFieldValue
private void printFieldValue(Descriptors.FieldDescriptor field, java.lang.Object value, TextFormat.TextGenerator generator) throws java.io.IOException
- Throws:
java.io.IOException
-
shouldRedactOptionValue
private boolean shouldRedactOptionValue(Descriptors.EnumValueDescriptor optionValue)
-
shouldRedact
private boolean shouldRedact(Descriptors.FieldDescriptor field, TextFormat.TextGenerator generator)
-
shouldReport
private boolean shouldReport(TextFormat.Printer.FieldReporterLevel level)
-
printToString
public java.lang.String printToString(MessageOrBuilder message)
Likeprint(), but writes directly to aStringand returns it.
-
printToString
java.lang.String printToString(MessageOrBuilder message, TextFormat.Printer.FieldReporterLevel level)
-
printToString
public java.lang.String printToString(UnknownFieldSet fields)
Likeprint(), but writes directly to aStringand returns it.
-
shortDebugString
@Deprecated public java.lang.String shortDebugString(MessageOrBuilder message)
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 java.lang.String shortDebugString(Descriptors.FieldDescriptor field, java.lang.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 java.lang.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, java.lang.Object value, TextFormat.TextGenerator generator, boolean redact) throws java.io.IOException- Throws:
java.io.IOException
-
printMessage
private void printMessage(MessageOrBuilder message, TextFormat.TextGenerator generator) throws java.io.IOException
- Throws:
java.io.IOException
-
printSingleField
private void printSingleField(Descriptors.FieldDescriptor field, java.lang.Object value, TextFormat.TextGenerator generator) throws java.io.IOException
- Throws:
java.io.IOException
-
printUnknownFields
private static void printUnknownFields(UnknownFieldSet unknownFields, TextFormat.TextGenerator generator, boolean redact) throws java.io.IOException
- Throws:
java.io.IOException
-
printUnknownField
private static void printUnknownField(int number, int wireType, java.util.List<?> values, TextFormat.TextGenerator generator, boolean redact) throws java.io.IOException- Throws:
java.io.IOException
-
-