Class JsonFormat.Printer

  • Enclosing class:
    JsonFormat

    public static class JsonFormat.Printer
    extends java.lang.Object
    A Printer converts a protobuf message to the proto3 JSON format.
    • Method Detail

      • includingDefaultValueFields

        public JsonFormat.Printer includingDefaultValueFields()
        Creates a new JsonFormat.Printer that will also print fields set to their defaults. Empty repeated fields and map fields will be printed as well. The new Printer clones all other configurations from the current JsonFormat.Printer.
      • includingDefaultValueFields

        public JsonFormat.Printer includingDefaultValueFields​(java.util.Set<Descriptors.FieldDescriptor> fieldsToAlwaysOutput)
        Creates a new JsonFormat.Printer that will also print default-valued fields if their FieldDescriptors are found in the supplied set. Empty repeated fields and map fields will be printed as well, if they match. The new Printer clones all other configurations from the current JsonFormat.Printer. Call includingDefaultValueFields() with no args to unconditionally output all fields.
      • preservingProtoFieldNames

        public JsonFormat.Printer preservingProtoFieldNames()
        Creates a new JsonFormat.Printer that is configured to use the original proto field names as defined in the .proto file rather than converting them to lowerCamelCase. The new Printer clones all other configurations from the current JsonFormat.Printer.
      • omittingInsignificantWhitespace

        public JsonFormat.Printer omittingInsignificantWhitespace()
        Create a new JsonFormat.Printer that omits insignificant whitespace in the JSON output. This new Printer clones all other configurations from the current Printer. Insignificant whitespace is defined by the JSON spec as whitespace that appears between JSON structural elements:
         ws = *(
         %x20 /              ; Space
         %x09 /              ; Horizontal tab
         %x0A /              ; Line feed or New line
         %x0D )              ; Carriage return
         
        See https://tools.ietf.org/html/rfc7159.
      • sortingMapKeys

        public JsonFormat.Printer sortingMapKeys()
        Create a new JsonFormat.Printer that will sort the map keys in the JSON output.

        Use of this modifier is discouraged. The generated JSON messages are equivalent with and without this option set, but there are some corner use cases that demand a stable output, while order of map keys is otherwise arbitrary.

        The generated order is not well-defined and should not be depended on, but it's stable.

        This new Printer clones all other configurations from the current JsonFormat.Printer.

      • appendTo

        public void appendTo​(MessageOrBuilder message,
                             java.lang.Appendable output)
                      throws java.io.IOException
        Converts a protobuf message to the proto3 JSON format.
        Throws:
        InvalidProtocolBufferException - if the message contains Any types that can't be resolved
        java.io.IOException - if writing to the output fails