Class MessageMarshaller

java.lang.Object
org.curioswitch.common.protobuf.json.MessageMarshaller

public class MessageMarshaller extends Object
A marshaller of pre-registered Message types. Specific bytecode for marshalling the Message will be generated as a subclass of TypeSpecificMarshaller and used for optimal serializing and parsing of JSON for protobufs. Use builder() for setting up the MessageMarshaller and registering types.

For example:


 MessageMarshaller marshaller = MessageMarshaller.builder()
     .omittingInsignificantWhitespace(true)
     .register(MyRequest.getDefaultInstance())
     .register(MyResponse.getDefaultInstance())
     .build();

 MyRequest.Builder requestBuilder = MyRequest.newBuilder();
 marshaller.mergeValue(json, requestBuilder);

 MyResponse response = handle(requestBuilder.build());
 return marshaller.writeValueAsBytes(response);

 
  • Method Details

    • builder

      public static MessageMarshaller.Builder builder()
      Returns a new MessageMarshaller.Builder for registering Message types for use in a MessageMarshaller as well as setting various serialization and parsing options.
    • mergeValue

      public void mergeValue(byte[] json, com.google.protobuf.Message.Builder builder) throws IOException
      Merges the JSON UTF-8 bytes into the provided Message.Builder.
      Throws:
      com.google.protobuf.InvalidProtocolBufferException - if the input is not valid JSON format or there are unknown fields in the input.
      IOException
    • mergeValue

      public void mergeValue(String json, com.google.protobuf.Message.Builder builder) throws IOException
      Merges the JSON String into the provided Message.Builder.
      Throws:
      com.google.protobuf.InvalidProtocolBufferException - if the input is not valid JSON format or there are unknown fields in the input.
      IOException
    • mergeValue

      public void mergeValue(InputStream json, com.google.protobuf.Message.Builder builder) throws IOException
      Merges the JSON bytes inside the provided InputStream into the provided Message.Builder. Will not close the InputStream.
      Throws:
      com.google.protobuf.InvalidProtocolBufferException - if the input is not valid JSON format or there are unknown fields in the input.
      IOException
    • mergeValue

      public void mergeValue(com.fasterxml.jackson.core.JsonParser jsonParser, com.google.protobuf.Message.Builder builder) throws IOException
      Merges the content inside the JsonParser into the provided Message.Builder.
      Throws:
      com.google.protobuf.InvalidProtocolBufferException - if the input is not valid JSON format or there are unknown fields in the input.
      IOException
    • writeValueAsBytes

      public <T extends com.google.protobuf.Message> byte[] writeValueAsBytes(T message) throws IOException
      Converts a Message into JSON as UTF-8 encoded bytes.
      Throws:
      com.google.protobuf.InvalidProtocolBufferException - if there are unknown Any types in the message.
      IOException
    • writeValueAsString

      public <T extends com.google.protobuf.Message> String writeValueAsString(T message) throws IOException
      Converts a Message into a JSON String.
      Throws:
      com.google.protobuf.InvalidProtocolBufferException - if there are unknown Any types in the message.
      IOException
    • writeValue

      public <T extends com.google.protobuf.Message> void writeValue(T message, OutputStream out) throws IOException
      Converts a Message into JSON, writing to the provided OutputStream. Does not close the OutputStream.
      Throws:
      IOException
    • writeValue

      public <T extends com.google.protobuf.Message> void writeValue(T message, com.fasterxml.jackson.core.JsonGenerator gen) throws IOException
      Converts a Message into a JSON, writing to the provided JsonGenerator.
      Throws:
      com.google.protobuf.InvalidProtocolBufferException - if there are unknown Any types in the message.
      IOException
    • toBuilder

      public MessageMarshaller.Builder toBuilder()
      Returns a new MessageMarshaller.Builder prepopulated with the messages that have been registered in this MessageMarshaller. This can be useful to incrementally add more messages to an already built marshaller. Be careful when doing this on a hot path as compilation can take a relatively large amount of time.