Class TextFormat
java.lang.Object
com.google.protobuf.TextFormat
This class implements the Protobuf Text Format.
Printing and parsing protocol messages in text format is useful for debugging and human editing of messages.
Unlike the Binary and ProtoJSON formats, Text Format is not designed to be used as a wire format; instead it is intended for human-in-the-loop configuration use-cases.
Systems processing untrusted inputs should strongly prefer to use Binary format instead. If a textual format of untrusted inputs is required, consider using ProtoJSON format instead.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classThrown byunescapeBytes(CharSequence)andunescapeText(String)when an invalid escape sequence is seen.static classThrown when parsing an invalid text format message.static classParser for text-format instances.static final classHelper class for converting protobufs to text.private static final classAn inner class for writing text to the output stream.private static final classRepresents a stream of tokens parsed from aString.static classDeprecated.This exception is unused and will be removed in the next breaking release (v5.x.x). -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final Stringprivate static final booleanprivate static final Stringprivate static final Loggerprivate static final TextFormat.Parserprivate static final String -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic TextFormat.PrinterPrinter instance which escapes non-ASCII characters and prints in the debug format.static TextFormat.PrinterPrinter instance which escapes non-ASCII characters and prints in the debug format.private static intdigitValue(byte c) Interpret a character as a digit (in any base up to 36) and return the numeric value.static StringescapeBytes(byte[] input) LikeescapeBytes(ByteString), but used for byte array.static StringescapeBytes(ByteString input) Escapes bytes in the format used in protocol buffer text format, which is the same as the format used for C string literals.static StringEscape double quotes and backslashes in a String for emittingUnicode output of a message.(package private) static StringescapeText(String input) LikeescapeBytes(ByteString), but escapes a text string.static TextFormat.ParserReturn aTextFormat.Parserinstance which can parse text-format messages.private static booleanisHex(byte c) Is this a hex digit?private static booleanisOctal(byte c) Is this an octal digit?static voidmerge(CharSequence input, ExtensionRegistry extensionRegistry, Message.Builder builder) Parse a text-format message frominputand merge the contents intobuilder.static voidmerge(CharSequence input, Message.Builder builder) Parse a text-format message frominputand merge the contents intobuilder.static voidmerge(Readable input, ExtensionRegistry extensionRegistry, Message.Builder builder) Parse a text-format message frominputand merge the contents intobuilder.static voidmerge(Readable input, Message.Builder builder) Parse a text-format message frominputand merge the contents intobuilder.static <T extends Message>
Tparse(CharSequence input, ExtensionRegistry extensionRegistry, Class<T> protoClass) Parse a text-format message frominput.static <T extends Message>
Tparse(CharSequence input, Class<T> protoClass) Parse a text-format message frominput.(package private) static intparseInt32(String text) Parse a 32-bit signed integer from the text.(package private) static longparseInt64(String text) Parse a 64-bit signed integer from the text.private static longparseInteger(String text, boolean isSigned, boolean isLong) (package private) static intparseUInt32(String text) Parse a 32-bit unsigned integer from the text.(package private) static longparseUInt64(String text) Parse a 64-bit unsigned integer from the text.static voidprint(MessageOrBuilder message, Appendable output) Deprecated.Useprinter().print(MessageOrBuilder, Appendable)static TextFormat.Printerprinter()Printer instance which escapes non-ASCII characters.static voidprintFieldValue(Descriptors.FieldDescriptor field, Object value, Appendable output) Deprecated.Useprinter().printFieldValue(FieldDescriptor, Object, Appendable)static StringprintToString(MessageOrBuilder message) Deprecated.Usemessage.toString()static StringprintToUnicodeString(MessageOrBuilder message) Deprecated.Useprinter().escapingNonAscii(false).printToString(MessageOrBuilder)static voidprintUnicode(MessageOrBuilder message, Appendable output) Deprecated.Useprinter().escapingNonAscii(false).print(MessageOrBuilder, Appendable)private static voidprintUnknownFieldValue(int tag, Object value, TextFormat.TextGenerator generator, boolean redact) static voidprintUnknownFieldValue(int tag, Object value, Appendable output) Outputs a textual representation of the value of an unknown field.private static TextFormat.TextGeneratorsetSingleLineOutput(Appendable output, boolean singleLine) private static TextFormat.TextGeneratorsetSingleLineOutput(Appendable output, boolean singleLine, Descriptors.Descriptor rootMessageType, TextFormat.Printer.FieldReporterLevel fieldReporterLevel, boolean shouldEmitSilentMarker) static StringshortDebugString(MessageOrBuilder message) Deprecated.Useprinter().emittingSingleLine(true).printToString(MessageOrBuilder)static ByteStringunescapeBytes(CharSequence charString) Un-escape a byte sequence as escaped usingescapeBytes(ByteString).(package private) static StringunescapeText(String input) Un-escape a text string as escaped usingescapeText(String).static StringunsignedToString(int value) Convert an unsigned 32-bit integer to a string.static StringunsignedToString(long value) Convert an unsigned 64-bit integer to a string.
-
Field Details
-
logger
-
DEBUG_STRING_SILENT_MARKER
- See Also:
-
ENABLE_INSERT_SILENT_MARKER_ENV_NAME
- See Also:
-
ENABLE_INSERT_SILENT_MARKER
private static final boolean ENABLE_INSERT_SILENT_MARKER -
REDACTED_MARKER
- See Also:
-
PARSER
-
-
Constructor Details
-
TextFormat
private TextFormat()
-
-
Method Details
-
shortDebugString
Deprecated.Useprinter().emittingSingleLine(true).printToString(MessageOrBuilder)Generates a human readable form of this message, useful for debugging and other purposes, with no newline characters. This is just a trivial wrapper aroundTextFormat.Printer.shortDebugString(MessageOrBuilder). -
printUnknownFieldValue
public static void printUnknownFieldValue(int tag, Object value, Appendable output) throws IOException Outputs a textual representation of the value of an unknown field.- Parameters:
tag- the field's tag numbervalue- 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
-
printUnknownFieldValue
private static void printUnknownFieldValue(int tag, Object value, TextFormat.TextGenerator generator, boolean redact) throws IOException - Throws:
IOException
-
printer
Printer instance which escapes non-ASCII characters. -
debugFormatPrinter
Printer instance which escapes non-ASCII characters and prints in the debug format. -
defaultFormatPrinter
Printer instance which escapes non-ASCII characters and prints in the debug format. -
print
@Deprecated @InlineMe(replacement="TextFormat.printer().print(message, output)", imports="com.google.protobuf.TextFormat") public static void print(MessageOrBuilder message, Appendable output) throws IOException Deprecated.Useprinter().print(MessageOrBuilder, Appendable)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
-
printUnicode
@Deprecated public static void printUnicode(MessageOrBuilder message, Appendable output) throws IOException Deprecated.Useprinter().escapingNonAscii(false).print(MessageOrBuilder, Appendable)Same asprint(), except that non-ASCII characters are not escaped.- Throws:
IOException
-
printToString
Deprecated.Usemessage.toString()Likeprint(), but writes directly to aStringand returns it. -
printToUnicodeString
Deprecated.Useprinter().escapingNonAscii(false).printToString(MessageOrBuilder)Same asprintToString(), except that non-ASCII characters in string type fields are not escaped in backslash+octals. -
printFieldValue
@Deprecated @InlineMe(replacement="TextFormat.printer().printFieldValue(field, value, output)", imports="com.google.protobuf.TextFormat") public static void printFieldValue(Descriptors.FieldDescriptor field, Object value, Appendable output) throws IOException Deprecated.Useprinter().printFieldValue(FieldDescriptor, Object, Appendable)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
-
unsignedToString
Convert an unsigned 32-bit integer to a string. -
unsignedToString
Convert an unsigned 64-bit integer to a string. -
setSingleLineOutput
-
setSingleLineOutput
private static TextFormat.TextGenerator setSingleLineOutput(Appendable output, boolean singleLine, Descriptors.Descriptor rootMessageType, TextFormat.Printer.FieldReporterLevel fieldReporterLevel, boolean shouldEmitSilentMarker) -
getParser
Return aTextFormat.Parserinstance which can parse text-format messages. The returned instance is thread-safe. -
merge
Parse a text-format message frominputand merge the contents intobuilder.- Throws:
IOException
-
merge
public static void merge(CharSequence input, Message.Builder builder) throws TextFormat.ParseException Parse a text-format message frominputand merge the contents intobuilder.- Throws:
TextFormat.ParseException
-
parse
public static <T extends Message> T parse(CharSequence input, Class<T> protoClass) throws TextFormat.ParseException Parse a text-format message frominput.- Returns:
- the parsed message, guaranteed initialized
- Throws:
TextFormat.ParseException
-
merge
public static void merge(Readable input, ExtensionRegistry extensionRegistry, Message.Builder builder) throws IOException Parse a text-format message frominputand merge the contents intobuilder. Extensions will be recognized if they are registered inextensionRegistry.- Throws:
IOException
-
merge
public static void merge(CharSequence input, ExtensionRegistry extensionRegistry, Message.Builder builder) throws TextFormat.ParseException Parse a text-format message frominputand merge the contents intobuilder. Extensions will be recognized if they are registered inextensionRegistry.- Throws:
TextFormat.ParseException
-
parse
public static <T extends Message> T parse(CharSequence input, ExtensionRegistry extensionRegistry, Class<T> protoClass) throws TextFormat.ParseException Parse a text-format message frominput. Extensions will be recognized if they are registered inextensionRegistry.- Returns:
- the parsed message, guaranteed initialized
- Throws:
TextFormat.ParseException
-
escapeBytes
Escapes bytes in the format used in protocol buffer text format, which is the same as the format used for C string literals. All bytes that are not printable 7-bit ASCII characters are escaped, as well as backslash, single-quote, and double-quote characters. Characters for which no defined short-hand escape sequence is defined will be escaped using 3-digit octal sequences. -
escapeBytes
LikeescapeBytes(ByteString), but used for byte array. -
unescapeBytes
public static ByteString unescapeBytes(CharSequence charString) throws TextFormat.InvalidEscapeSequenceException Un-escape a byte sequence as escaped usingescapeBytes(ByteString). Two-digit hex escapes (starting with "\x") are also recognized. -
escapeText
LikeescapeBytes(ByteString), but escapes a text string. Non-ASCII characters are first encoded as UTF-8, then each byte is escaped individually as a 3-digit octal escape. Yes, it's weird. -
escapeDoubleQuotesAndBackslashes
-
unescapeText
Un-escape a text string as escaped usingescapeText(String). Two-digit hex escapes (starting with "\x") are also recognized. -
isOctal
private static boolean isOctal(byte c) Is this an octal digit? -
isHex
private static boolean isHex(byte c) Is this a hex digit? -
digitValue
private static int digitValue(byte c) Interpret a character as a digit (in any base up to 36) and return the numeric value. This is likeCharacter.digit()but we don't accept non-ASCII digits. -
parseInt32
Parse a 32-bit signed integer from the text. Unlike the Java standardInteger.parseInt(), this function recognizes the prefixes "0x" and "0" to signify hexadecimal and octal numbers, respectively.- Throws:
NumberFormatException
-
parseUInt32
Parse a 32-bit unsigned integer from the text. Unlike the Java standardInteger.parseInt(), this function recognizes the prefixes "0x" and "0" to signify hexadecimal and octal numbers, respectively. The result is coerced to a (signed)intwhen returned since Java has no unsigned integer type.- Throws:
NumberFormatException
-
parseInt64
Parse a 64-bit signed integer from the text. Unlike the Java standardInteger.parseInt(), this function recognizes the prefixes "0x" and "0" to signify hexadecimal and octal numbers, respectively.- Throws:
NumberFormatException
-
parseUInt64
Parse a 64-bit unsigned integer from the text. Unlike the Java standardInteger.parseInt(), this function recognizes the prefixes "0x" and "0" to signify hexadecimal and octal numbers, respectively. The result is coerced to a (signed)longwhen returned since Java has no unsigned long type.- Throws:
NumberFormatException
-
parseInteger
private static long parseInteger(String text, boolean isSigned, boolean isLong) throws NumberFormatException - Throws:
NumberFormatException
-