Package com.google.protobuf
Class TextFormat
- java.lang.Object
-
- com.google.protobuf.TextFormat
-
public final class TextFormat extends Object
Provide text parsing and formatting support for proto2 instances. The implementation largely follows text_format.cc.- Author:
- wenboz@google.com Wenbo Zhu, kenton@google.com Kenton Varda
-
-
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 proto2 instances.static classTextFormat.PrinterHelper class for converting protobufs to text.static classTextFormat.UnknownFieldParseExceptionThrown when encountering an unknown field while parsing a text format message.
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description 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 StringescapeDoubleQuotesAndBackslashes(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(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.static voidprint(MessageOrBuilder message, Appendable output)Deprecated.Useprinter().print(MessageOrBuilder, Appendable)static voidprint(UnknownFieldSet fields, Appendable output)Deprecated.Useprinter().print(UnknownFieldSet, Appendable)static TextFormat.Printerprinter()Printer instance which escapes non-ASCII characters.static voidprintField(Descriptors.FieldDescriptor field, Object value, Appendable output)Deprecated.Useprinter().printField(FieldDescriptor, Object, Appendable)static StringprintFieldToString(Descriptors.FieldDescriptor field, Object value)Deprecated.Useprinter().printFieldToString(FieldDescriptor, Object)static voidprintFieldValue(Descriptors.FieldDescriptor field, Object value, Appendable output)Deprecated.Useprinter().printFieldValue(FieldDescriptor, Object, Appendable)static StringprintToString(MessageOrBuilder message)Deprecated.Usemessage.toString()static StringprintToString(UnknownFieldSet fields)Deprecated.static StringprintToUnicodeString(MessageOrBuilder message)Deprecated.Useprinter().escapingNonAscii(false).printToString(MessageOrBuilder)static StringprintToUnicodeString(UnknownFieldSet fields)Deprecated.Useprinter().escapingNonAscii(false).printToString(UnknownFieldSet)static voidprintUnicode(MessageOrBuilder message, Appendable output)Deprecated.Useprinter().escapingNonAscii(false).print(MessageOrBuilder, Appendable)static voidprintUnicode(UnknownFieldSet fields, Appendable output)Deprecated.Useprinter().escapingNonAscii(false).print(UnknownFieldSet, Appendable)static voidprintUnicodeFieldValue(Descriptors.FieldDescriptor field, Object value, Appendable output)Deprecated.Useprinter().escapingNonAscii(false).printFieldValue(FieldDescriptor, Object, Appendable)static voidprintUnknownFieldValue(int tag, Object value, Appendable output)Outputs a textual representation of the value of an unknown field.static StringshortDebugString(Descriptors.FieldDescriptor field, Object value)Deprecated.Useprinter().shortDebugString(FieldDescriptor, Object)static StringshortDebugString(MessageOrBuilder message)Generates a human readable form of this message, useful for debugging and other purposes, with no newline characters.static StringshortDebugString(UnknownFieldSet fields)Deprecated.Useprinter().shortDebugString(UnknownFieldSet)static ByteStringunescapeBytes(CharSequence charString)Un-escape a byte sequence as escaped usingescapeBytes(ByteString).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.
-
-
-
Method Detail
-
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
-
print
@Deprecated public static void print(UnknownFieldSet fields, Appendable output) throws IOException
Deprecated.Useprinter().print(UnknownFieldSet, Appendable)Outputs a textual representation offieldstooutput.- Throws:
IOException
-
printUnicode
@Deprecated @InlineMe(replacement="TextFormat.printer().escapingNonAscii(false).print(message, output)", imports="com.google.protobuf.TextFormat") 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
-
printUnicode
@Deprecated public static void printUnicode(UnknownFieldSet fields, Appendable output) throws IOException
Deprecated.Useprinter().escapingNonAscii(false).print(UnknownFieldSet, Appendable)Same asprint(), except that non-ASCII characters are not escaped.- Throws:
IOException
-
shortDebugString
public static String shortDebugString(MessageOrBuilder message)
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).
-
shortDebugString
@Deprecated public static String shortDebugString(Descriptors.FieldDescriptor field, Object value)
Deprecated.Useprinter().shortDebugString(FieldDescriptor, Object)Generates a human readable form of the field, useful for debugging and other purposes, with no newline characters.
-
shortDebugString
@Deprecated public static String shortDebugString(UnknownFieldSet fields)
Deprecated.Useprinter().shortDebugString(UnknownFieldSet)Generates a human readable form of the unknown fields, useful for debugging and other purposes, with no newline characters.
-
printToString
@Deprecated @InlineMe(replacement="TextFormat.printer().printToString(message)", imports="com.google.protobuf.TextFormat") public static String printToString(MessageOrBuilder message)
Deprecated.Usemessage.toString()Likeprint(), but writes directly to aStringand returns it.
-
printToString
@Deprecated public static String printToString(UnknownFieldSet fields)
Deprecated.Likeprint(), but writes directly to aStringand returns it.
-
printToUnicodeString
@Deprecated @InlineMe(replacement="TextFormat.printer().escapingNonAscii(false).printToString(message)", imports="com.google.protobuf.TextFormat") public static 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.
-
printToUnicodeString
@Deprecated public static String printToUnicodeString(UnknownFieldSet fields)
Deprecated.Useprinter().escapingNonAscii(false).printToString(UnknownFieldSet)Same asprintToString(), except that non-ASCII characters in string type fields are not escaped in backslash+octals.
-
printField
@Deprecated public static void printField(Descriptors.FieldDescriptor field, Object value, Appendable output) throws IOException
Deprecated.Useprinter().printField(FieldDescriptor, Object, Appendable)- Throws:
IOException
-
printFieldToString
@Deprecated public static String printFieldToString(Descriptors.FieldDescriptor field, Object value)
Deprecated.Useprinter().printFieldToString(FieldDescriptor, Object)
-
printUnicodeFieldValue
@Deprecated public static void printUnicodeFieldValue(Descriptors.FieldDescriptor field, Object value, Appendable output) throws IOException
Deprecated.Useprinter().escapingNonAscii(false).printFieldValue(FieldDescriptor, Object, Appendable)Outputs a unicode textual representation of the value of given field value.Same as
printFieldValue(), except that non-ASCII characters in string type fields are not escaped in backslash+octals.- 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
@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
-
printUnknownFieldValue
public static void printUnknownFieldValue(int tag, Object value, Appendable output) throws 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:
ClassCastException- if the value is not appropriate for the given field descriptorIOException- if there is an exception writing to the output
-
printer
public static TextFormat.Printer printer()
Printer instance which escapes non-ASCII characters.
-
unsignedToString
public static String unsignedToString(int value)
Convert an unsigned 32-bit integer to a string.
-
unsignedToString
public static 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(Readable input, Message.Builder builder) throws IOException
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
public static 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 String escapeBytes(byte[] input)
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.
-
-