Class MessageMarshaller
java.lang.Object
org.curioswitch.common.protobuf.json.MessageMarshaller
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);
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classAMessageMarshaller.BuilderofMessageMarshallers, allows registeringMessagetypes to marshall and set options. -
Method Summary
Modifier and TypeMethodDescriptionstatic MessageMarshaller.Builderbuilder()Returns a newMessageMarshaller.Builderfor registeringMessagetypes for use in aMessageMarshalleras well as setting various serialization and parsing options.voidmergeValue(byte[] json, com.google.protobuf.Message.Builder builder) Merges the JSON UTF-8 bytes into the providedMessage.Builder.voidmergeValue(com.fasterxml.jackson.core.JsonParser jsonParser, com.google.protobuf.Message.Builder builder) Merges the content inside theJsonParserinto the providedMessage.Builder.voidmergeValue(InputStream json, com.google.protobuf.Message.Builder builder) Merges the JSON bytes inside the providedInputStreaminto the providedMessage.Builder.voidmergeValue(String json, com.google.protobuf.Message.Builder builder) Merges the JSONStringinto the providedMessage.Builder.<T extends com.google.protobuf.Message>
voidwriteValue(T message, com.fasterxml.jackson.core.JsonGenerator gen) Converts aMessageinto a JSON, writing to the providedJsonGenerator.<T extends com.google.protobuf.Message>
voidwriteValue(T message, OutputStream out) Converts aMessageinto JSON, writing to the providedOutputStream.<T extends com.google.protobuf.Message>
byte[]writeValueAsBytes(T message) Converts aMessageinto JSON as UTF-8 encoded bytes.<T extends com.google.protobuf.Message>
StringwriteValueAsString(T message) Converts aMessageinto a JSONString.
-
Method Details
-
builder
Returns a newMessageMarshaller.Builderfor registeringMessagetypes for use in aMessageMarshalleras well as setting various serialization and parsing options. -
mergeValue
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.IOException
-
mergeValue
Merges the JSONStringinto the providedMessage.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 providedInputStreaminto 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.IOException
-
mergeValue
public void mergeValue(com.fasterxml.jackson.core.JsonParser jsonParser, com.google.protobuf.Message.Builder builder) throws IOException Merges the content inside theJsonParserinto the providedMessage.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 aMessageinto 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 aMessageinto a JSONString.- 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 aMessageinto JSON, writing to the providedOutputStream. Does not close theOutputStream.- Throws:
IOException
-
writeValue
public <T extends com.google.protobuf.Message> void writeValue(T message, com.fasterxml.jackson.core.JsonGenerator gen) throws IOException Converts aMessageinto a JSON, writing to the providedJsonGenerator.- Throws:
com.google.protobuf.InvalidProtocolBufferException- if there are unknown Any types in the message.IOException
-