Class MessageMarshaller
- java.lang.Object
-
- org.curioswitch.common.protobuf.json.MessageMarshaller
-
public class MessageMarshaller extends java.lang.Object
A marshaller of pre-registeredMessage
types. Specific bytecode for marshalling theMessage
will be generated as a subclass ofTypeSpecificMarshaller
and used for optimal serializing and parsing of JSON for protobufs. Usebuilder()
for setting up theMessageMarshaller
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);
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
MessageMarshaller.Builder
AMessageMarshaller.Builder
ofMessageMarshaller
s, allows registeringMessage
types to marshall and set options.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static MessageMarshaller.Builder
builder()
Returns a newMessageMarshaller.Builder
for registeringMessage
types for use in aMessageMarshaller
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 providedMessage.Builder
.void
mergeValue(com.fasterxml.jackson.core.JsonParser jsonParser, com.google.protobuf.Message.Builder builder)
Merges the content inside theJsonParser
into the providedMessage.Builder
.void
mergeValue(java.io.InputStream json, com.google.protobuf.Message.Builder builder)
Merges the JSON bytes inside the providedInputStream
into the providedMessage.Builder
.void
mergeValue(java.lang.String json, com.google.protobuf.Message.Builder builder)
Merges the JSONString
into the providedMessage.Builder
.<T extends com.google.protobuf.Message>
voidwriteValue(T message, com.fasterxml.jackson.core.JsonGenerator gen)
Converts aMessage
into a JSON, writing to the providedJsonGenerator
.<T extends com.google.protobuf.Message>
voidwriteValue(T message, java.io.OutputStream out)
Converts aMessage
into JSON, writing to the providedOutputStream
.<T extends com.google.protobuf.Message>
byte[]writeValueAsBytes(T message)
Converts aMessage
into JSON as UTF-8 encoded bytes.<T extends com.google.protobuf.Message>
java.lang.StringwriteValueAsString(T message)
Converts aMessage
into a JSONString
.
-
-
-
Method Detail
-
builder
public static MessageMarshaller.Builder builder()
Returns a newMessageMarshaller.Builder
for registeringMessage
types for use in aMessageMarshaller
as well as setting various serialization and parsing options.
-
mergeValue
public void mergeValue(byte[] json, com.google.protobuf.Message.Builder builder) throws java.io.IOException
Merges the JSON UTF-8 bytes into the providedMessage.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 JSONString
into the providedMessage.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 providedInputStream
into the providedMessage.Builder
. Will not close theInputStream
.- 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 theJsonParser
into the providedMessage.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 aMessage
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 aMessage
into a JSONString
.- 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 aMessage
into JSON, writing to the providedOutputStream
. Does not close theOutputStream
.- 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 aMessage
into a JSON, writing to the providedJsonGenerator
.- Throws:
com.google.protobuf.InvalidProtocolBufferException
- if there are unknown Any types in the message.java.io.IOException
-
-