Package com.google.protobuf
Class TextFormat
- java.lang.Object
-
- com.google.protobuf.TextFormat
-
public final class TextFormat extends java.lang.ObjectThis 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 Classes Modifier and Type Class Description static classTextFormat.InvalidEscapeSequenceExceptionThrown byunescapeBytes(java.lang.CharSequence)andunescapeText(java.lang.String)when an invalid escape sequence is seen.static classTextFormat.ParseExceptionThrown when parsing an invalid text format message.static classTextFormat.ParserParser for text-format instances.static classTextFormat.PrinterHelper class for converting protobufs to text.static classTextFormat.UnknownFieldParseExceptionDeprecated.This exception is unused and will be removed in the next breaking release (v5.x.x).
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static TextFormat.PrinterdebugFormatPrinter()Printer instance which escapes non-ASCII characters and prints in the debug format.static TextFormat.PrinterdefaultFormatPrinter()Printer instance which escapes non-ASCII characters and prints in the debug format.static java.lang.StringescapeBytes(byte[] input)LikeescapeBytes(ByteString), but used for byte array.static java.lang.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 java.lang.StringescapeDoubleQuotesAndBackslashes(java.lang.String input)Escape double quotes and backslashes in a String for emittingUnicode output of a message.static TextFormat.ParsergetParser()Return aTextFormat.Parserinstance which can parse text-format messages.static voidmerge(java.lang.CharSequence input, ExtensionRegistry extensionRegistry, Message.Builder builder)Parse a text-format message frominputand merge the contents intobuilder.static voidmerge(java.lang.CharSequence input, Message.Builder builder)Parse a text-format message frominputand merge the contents intobuilder.static voidmerge(java.lang.Readable input, ExtensionRegistry extensionRegistry, Message.Builder builder)Parse a text-format message frominputand merge the contents intobuilder.static voidmerge(java.lang.Readable input, Message.Builder builder)Parse a text-format message frominputand merge the contents intobuilder.static <T extends Message>
Tparse(java.lang.CharSequence input, ExtensionRegistry extensionRegistry, java.lang.Class<T> protoClass)Parse a text-format message frominput.static <T extends Message>
Tparse(java.lang.CharSequence input, java.lang.Class<T> protoClass)Parse a text-format message frominput.static voidprint(MessageOrBuilder message, java.lang.Appendable output)Deprecated.Useprinter().print(MessageOrBuilder, Appendable)static TextFormat.Printerprinter()Printer instance which escapes non-ASCII characters.static voidprintFieldValue(Descriptors.FieldDescriptor field, java.lang.Object value, java.lang.Appendable output)Deprecated.Useprinter().printFieldValue(FieldDescriptor, Object, Appendable)static java.lang.StringprintToString(MessageOrBuilder message)Deprecated.Usemessage.toString()static java.lang.StringprintToUnicodeString(MessageOrBuilder message)Deprecated.Useprinter().escapingNonAscii(false).printToString(MessageOrBuilder)static voidprintUnicode(MessageOrBuilder message, java.lang.Appendable output)Deprecated.Useprinter().escapingNonAscii(false).print(MessageOrBuilder, Appendable)static voidprintUnknownFieldValue(int tag, java.lang.Object value, java.lang.Appendable output)Outputs a textual representation of the value of an unknown field.static java.lang.StringshortDebugString(MessageOrBuilder message)Deprecated.Useprinter().emittingSingleLine(true).printToString(MessageOrBuilder)static ByteStringunescapeBytes(java.lang.CharSequence charString)Un-escape a byte sequence as escaped usingescapeBytes(ByteString).static java.lang.StringunsignedToString(int value)Convert an unsigned 32-bit integer to a string.static java.lang.StringunsignedToString(long value)Convert an unsigned 64-bit integer to a string.
-
-
-
Method Detail
-
shortDebugString
@Deprecated public static java.lang.String shortDebugString(MessageOrBuilder message)
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, java.lang.Object value, java.lang.Appendable output) throws java.io.IOExceptionOutputs 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:
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
-
printer
public static TextFormat.Printer printer()
Printer instance which escapes non-ASCII characters.
-
debugFormatPrinter
public static TextFormat.Printer debugFormatPrinter()
Printer instance which escapes non-ASCII characters and prints in the debug format.
-
defaultFormatPrinter
public static TextFormat.Printer 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, java.lang.Appendable output) throws java.io.IOExceptionDeprecated.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:
java.io.IOException
-
printUnicode
@Deprecated public static void printUnicode(MessageOrBuilder message, java.lang.Appendable output) throws java.io.IOException
Deprecated.Useprinter().escapingNonAscii(false).print(MessageOrBuilder, Appendable)Same asprint(), except that non-ASCII characters are not escaped.- Throws:
java.io.IOException
-
printToString
@Deprecated public static java.lang.String printToString(MessageOrBuilder message)
Deprecated.Usemessage.toString()Likeprint(), but writes directly to aStringand returns it.
-
printToUnicodeString
@Deprecated public static java.lang.String printToUnicodeString(MessageOrBuilder message)
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, java.lang.Object value, java.lang.Appendable output) throws java.io.IOExceptionDeprecated.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:
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
-
unsignedToString
public static java.lang.String unsignedToString(int value)
Convert an unsigned 32-bit integer to a string.
-
unsignedToString
public static java.lang.String unsignedToString(long value)
Convert an unsigned 64-bit integer to a string.
-
getParser
public static TextFormat.Parser getParser()
Return aTextFormat.Parserinstance which can parse text-format messages. The returned instance is thread-safe.
-
merge
public static void merge(java.lang.Readable input, Message.Builder builder) throws java.io.IOExceptionParse a text-format message frominputand merge the contents intobuilder.- Throws:
java.io.IOException
-
merge
public static void merge(java.lang.CharSequence input, Message.Builder builder) throws TextFormat.ParseExceptionParse a text-format message frominputand merge the contents intobuilder.- Throws:
TextFormat.ParseException
-
parse
public static <T extends Message> T parse(java.lang.CharSequence input, java.lang.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(java.lang.Readable input, ExtensionRegistry extensionRegistry, Message.Builder builder) throws java.io.IOExceptionParse a text-format message frominputand merge the contents intobuilder. Extensions will be recognized if they are registered inextensionRegistry.- Throws:
java.io.IOException
-
merge
public static void merge(java.lang.CharSequence input, ExtensionRegistry extensionRegistry, Message.Builder builder) throws TextFormat.ParseExceptionParse 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(java.lang.CharSequence input, ExtensionRegistry extensionRegistry, java.lang.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
public static java.lang.String escapeBytes(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. 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
public static java.lang.String escapeBytes(byte[] input)
LikeescapeBytes(ByteString), but used for byte array.
-
unescapeBytes
public static ByteString unescapeBytes(java.lang.CharSequence charString) throws TextFormat.InvalidEscapeSequenceException
Un-escape a byte sequence as escaped usingescapeBytes(ByteString). Two-digit hex escapes (starting with "\x") are also recognized.
-
escapeDoubleQuotesAndBackslashes
public static java.lang.String escapeDoubleQuotesAndBackslashes(java.lang.String input)
Escape double quotes and backslashes in a String for emittingUnicode output of a message.
-
-