Package com.google.protobuf
Class TextFormat
java.lang.Object
com.google.protobuf.TextFormat
Provide text parsing and formatting support for proto2 instances. The implementation largely
follows text_format.cc.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
Thrown byunescapeBytes(java.lang.CharSequence)
andunescapeText(java.lang.String)
when an invalid escape sequence is seen.static class
Thrown when parsing an invalid text format message.static class
Parser for text-format proto2 instances.static final class
Helper class for converting protobufs to text.static class
Deprecated.This exception is unused and will be removed in the next breaking release (v5.x.x). -
Method Summary
Modifier and TypeMethodDescriptionstatic TextFormat.Printer
Printer instance which escapes non-ASCII characters and prints in the debug format.static String
escapeBytes
(byte[] input) LikeescapeBytes(ByteString)
, but used for byte array.static String
escapeBytes
(com.google.protobuf.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 String
Escape double quotes and backslashes in a String for emittingUnicode output of a message.static TextFormat.Parser
Return aTextFormat.Parser
instance which can parse text-format messages.static void
merge
(CharSequence input, ExtensionRegistry extensionRegistry, Message.Builder builder) Parse a text-format message frominput
and merge the contents intobuilder
.static void
merge
(CharSequence input, Message.Builder builder) Parse a text-format message frominput
and merge the contents intobuilder
.static void
merge
(Readable input, ExtensionRegistry extensionRegistry, Message.Builder builder) Parse a text-format message frominput
and merge the contents intobuilder
.static void
merge
(Readable input, Message.Builder builder) Parse a text-format message frominput
and 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 TextFormat.Printer
printer()
Printer instance which escapes non-ASCII characters.static void
printUnknownFieldValue
(int tag, Object value, Appendable output) Outputs a textual representation of the value of an unknown field.static String
shortDebugString
(MessageOrBuilder message) Deprecated.Useprinter().emittingSingleLine(true).printToString(MessageOrBuilder)
static com.google.protobuf.ByteString
unescapeBytes
(CharSequence charString) Un-escape a byte sequence as escaped usingescapeBytes(ByteString)
.static String
unsignedToString
(int value) Convert an unsigned 32-bit integer to a string.static String
unsignedToString
(long value) Convert an unsigned 64-bit integer to a string.
-
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
-
printer
Printer instance which escapes non-ASCII characters. -
debugFormatPrinter
Printer instance which escapes non-ASCII characters and prints in the debug format. -
unsignedToString
Convert an unsigned 32-bit integer to a string. -
unsignedToString
Convert an unsigned 64-bit integer to a string. -
getParser
Return aTextFormat.Parser
instance which can parse text-format messages. The returned instance is thread-safe. -
merge
Parse a text-format message frominput
and merge the contents intobuilder
.- Throws:
IOException
-
merge
public static void merge(CharSequence input, Message.Builder builder) throws TextFormat.ParseException Parse a text-format message frominput
and 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 frominput
and 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 frominput
and 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 com.google.protobuf.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. -
escapeDoubleQuotesAndBackslashes
Escape double quotes and backslashes in a String for emittingUnicode output of a message.
-