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 google/protobuf/text_format.cc.
Author:
wenboz@google.com Wenbo Zhu, kenton@google.com Kenton Varda
  • Method Details

    • print

      public static void print(MessageOrBuilder message, Appendable output) throws 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:
      IOException
    • print

      public static void print(UnknownFieldSet fields, Appendable output) throws IOException
      Outputs a textual representation of fields to output.
      Throws:
      IOException
    • printUnicode

      public static void printUnicode(MessageOrBuilder message, Appendable output) throws IOException
      Same as print(), except that non-ASCII characters are not escaped.
      Throws:
      IOException
    • printUnicode

      public static void printUnicode(UnknownFieldSet fields, Appendable output) throws IOException
      Same as print(), 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.
    • shortDebugString

      public static String shortDebugString(UnknownFieldSet fields)
      Generates a human readable form of the unknown fields, useful for debugging and other purposes, with no newline characters.
    • printToString

      public static String printToString(MessageOrBuilder message)
      Like print(), but writes directly to a String and returns it.
    • printToString

      public static String printToString(UnknownFieldSet fields)
      Like print(), but writes directly to a String and returns it.
    • printToUnicodeString

      public static String printToUnicodeString(MessageOrBuilder message)
      Same as printToString(), except that non-ASCII characters in string type fields are not escaped in backslash+octals.
    • printToUnicodeString

      public static String printToUnicodeString(UnknownFieldSet fields)
      Same as printToString(), except that non-ASCII characters in string type fields are not escaped in backslash+octals.
    • printField

      public static void printField(Descriptors.FieldDescriptor field, Object value, Appendable output) throws IOException
      Throws:
      IOException
    • printFieldToString

      public static String printFieldToString(Descriptors.FieldDescriptor field, Object value)
    • printFieldValue

      public static void printFieldValue(Descriptors.FieldDescriptor field, Object value, Appendable output) throws IOException
      Outputs a textual representation of the value of given field value.
      Parameters:
      field - the descriptor of the field
      value - the value of the field
      output - the output to which to append the formatted value
      Throws:
      ClassCastException - if the value is not appropriate for the given field descriptor
      IOException - if there is an exception writing to the output
    • 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 number
      value - the value of the field
      output - the output to which to append the formatted value
      Throws:
      ClassCastException - if the value is not appropriate for the given field descriptor
      IOException - if there is an exception writing to the output
    • 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 a TextFormat.Parser instance 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 from input and merge the contents into builder.
      Throws:
      IOException
    • merge

      public static void merge(CharSequence input, Message.Builder builder) throws TextFormat.ParseException
      Parse a text-format message from input and merge the contents into builder.
      Throws:
      TextFormat.ParseException
    • merge

      public static void merge(Readable input, ExtensionRegistry extensionRegistry, Message.Builder builder) throws IOException
      Parse a text-format message from input and merge the contents into builder. Extensions will be recognized if they are registered in extensionRegistry.
      Throws:
      IOException
    • merge

      public static void merge(CharSequence input, ExtensionRegistry extensionRegistry, Message.Builder builder) throws TextFormat.ParseException
      Parse a text-format message from input and merge the contents into builder. Extensions will be recognized if they are registered in extensionRegistry.
      Throws:
      TextFormat.ParseException
    • escapeDoubleQuotesAndBackslashes

      public static String escapeDoubleQuotesAndBackslashes(String input)
      Escape double quotes and backslashes in a String for unicode output of a message.