Class MessageMarshaller


  • public class MessageMarshaller
    extends java.lang.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 Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      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.
      void mergeValue​(byte[] json, com.google.protobuf.Message.Builder builder)
      Merges the JSON UTF-8 bytes into the provided Message.Builder.
      void mergeValue​(com.fasterxml.jackson.core.JsonParser jsonParser, com.google.protobuf.Message.Builder builder)
      Merges the content inside the JsonParser into the provided Message.Builder.
      void mergeValue​(java.io.InputStream json, com.google.protobuf.Message.Builder builder)
      Merges the JSON bytes inside the provided InputStream into the provided Message.Builder.
      void mergeValue​(java.lang.String json, com.google.protobuf.Message.Builder builder)
      Merges the JSON String into the provided Message.Builder.
      <T extends com.google.protobuf.Message>
      void
      writeValue​(T message, com.fasterxml.jackson.core.JsonGenerator gen)
      Converts a Message into a JSON, writing to the provided JsonGenerator.
      <T extends com.google.protobuf.Message>
      void
      writeValue​(T message, java.io.OutputStream out)
      Converts a Message into JSON, writing to the provided OutputStream.
      <T extends com.google.protobuf.Message>
      byte[]
      writeValueAsBytes​(T message)
      Converts a Message into JSON as UTF-8 encoded bytes.
      <T extends com.google.protobuf.Message>
      java.lang.String
      writeValueAsString​(T message)
      Converts a Message into a JSON String.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • mergeValue

        public void mergeValue​(byte[] json,
                               com.google.protobuf.Message.Builder builder)
                        throws java.io.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.
        java.io.IOException
      • mergeValue

        public void mergeValue​(java.lang.String json,
                               com.google.protobuf.Message.Builder builder)
                        throws java.io.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.
        java.io.IOException
      • mergeValue

        public void mergeValue​(java.io.InputStream json,
                               com.google.protobuf.Message.Builder builder)
                        throws java.io.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.
        java.io.IOException
      • mergeValue

        public void mergeValue​(com.fasterxml.jackson.core.JsonParser jsonParser,
                               com.google.protobuf.Message.Builder builder)
                        throws java.io.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.
        java.io.IOException
      • writeValueAsBytes

        public <T extends com.google.protobuf.Message> byte[] writeValueAsBytes​(T message)
                                                                         throws java.io.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.
        java.io.IOException
      • writeValueAsString

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

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

        public <T extends com.google.protobuf.Message> void writeValue​(T message,
                                                                       com.fasterxml.jackson.core.JsonGenerator gen)
                                                                throws java.io.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.
        java.io.IOException