Class Protobuf


public class Protobuf extends StreamMarshaller
A pairing of a ProtobufSerializer and ProtobufParser into a single class with convenience read/write methods.

Output is binary (byte[]) in the Protocol Buffers binary wire format.

Distinct from the text-format Prototext marshaller (text/protobuf): this marshaller emits the compact, non-self-describing protobuf binary wire format and requires the target type on read.

Examples:

// Using static convenience methods byte[] protobuf = Protobuf.of(myBean); MyBean parsed = Protobuf.to(protobuf, MyBean.class);

// Using the DEFAULT instance byte[] protobuf = Protobuf.DEFAULT.write(myBean); MyBean parsed = Protobuf.DEFAULT.read(protobuf, MyBean.class);

// Using static shortcuts. MyPojo myPojo = Protobuf.to(bytes, MyPojo.class); byte[] bytes = Protobuf.of(myPojo);

See Also:
  • Field Details

    • DEFAULT

      public static final Protobuf DEFAULT
      Default reusable instance.
  • Constructor Details

    • Protobuf

      public Protobuf()
      Constructor.

      Uses ProtobufSerializer.DEFAULT and ProtobufParser.DEFAULT.

    • Protobuf

      public Protobuf(org.apache.juneau.marshall.protobuf.ProtobufSerializer s, org.apache.juneau.marshall.protobuf.ProtobufParser p)
      Constructor.
      Parameters:
      s - The serializer to use for serializing output.
      p - The parser to use for parsing input.
  • Method Details

    • of

      public static byte[] of(Object object) throws SerializeException
      Serializes a POJO to a byte[] using the DEFAULT marshaller.

      A shortcut for calling DEFAULT.write(object).

      Parameters:
      object - The object to serialize.
      Returns:
      The serialized object.
      Throws:
      SerializeException - If a problem occurred trying to convert the output.
    • to

      public static <T> T to(byte[] input, Class<T> type) throws ParseException
      Parses an input into the specified object type using the DEFAULT marshaller.

      A shortcut for calling DEFAULT.read(input, type).

      Type Parameters:
      T - The class type of the object being created.
      Parameters:
      input - The input.
      type - The object type to create.
      Returns:
      The parsed object.
      Throws:
      ParseException - Malformed input encountered.
    • to

      public static <T> T to(byte[] input, Type type, Type... args) throws ParseException
      Parses an input into the specified parameterized object type using the DEFAULT marshaller.

      A shortcut for calling DEFAULT.read(input, type, args).

      Type Parameters:
      T - The class type of the object to create.
      Parameters:
      input - The input.
      type - The object type to create.
      args - The type arguments of the class if it's a collection or map.
      Returns:
      The parsed object.
      Throws:
      ParseException - Malformed input encountered.
    • to

      public static <T> T to(InputStream input, Class<T> type) throws ParseException
      Parses the contents of an InputStream into the specified object type using the DEFAULT marshaller.

      A shortcut for calling DEFAULT.read(input, type) that catches any IOException from the underlying stream and rethrows it as an unchecked ParseException, so the caller is not burdened with a checked exception.

      Type Parameters:
      T - The class type of the object being created.
      Parameters:
      input - The input stream.
      type - The object type to create.
      Returns:
      The parsed object.
      Throws:
      ParseException - Malformed input encountered or an I/O error occurred on the underlying stream.
    • to

      public static <T> T to(InputStream input, Type type, Type... args) throws ParseException
      Parses the contents of an InputStream into the specified parameterized object type using the DEFAULT marshaller.

      A shortcut for calling DEFAULT.read(input, type, args) that catches any IOException from the underlying stream and rethrows it as an unchecked ParseException, so the caller is not burdened with a checked exception.

      Type Parameters:
      T - The class type of the object to create.
      Parameters:
      input - The input stream.
      type - The object type to create.
      args - The type arguments of the class if it's a collection or map.
      Returns:
      The parsed object.
      Throws:
      ParseException - Malformed input encountered or an I/O error occurred on the underlying stream.
    • of

      public static void of(Object object, OutputStream output) throws SerializeException
      Serializes a POJO to the specified OutputStream using the DEFAULT marshaller.

      A shortcut for calling DEFAULT.write(object, output) that catches any IOException from the underlying stream and rethrows it as an unchecked SerializeException, so the caller is not burdened with a checked exception.

      Parameters:
      object - The object to serialize.
      output - The output stream to serialize to.
      Throws:
      SerializeException - If a problem occurred trying to convert the output or an I/O error occurred on the underlying stream.