Class JsonSerializer
- java.lang.Object
-
- net.morimekta.providence.serializer.Serializer
-
- net.morimekta.providence.serializer.JsonSerializer
-
public class JsonSerializer extends Serializer
Compact JSON serializer. This uses the most compact type-safe JSON format allowable. There are two optional variants switching the struct field ID between numeric ID and field name.Format is like this:
{ "id":value, "structId":{ ... }, "listId":[value1,value2], "mapId":{"id1":value1,"id2":value2} }
But without formatting spaces. The formatted JSON can be read normally. Binary fields are base64 encoded.This format supports 'compact' struct formatting. A compact struct is formatted as a list with fields in order from 1 to N. E.g.:
["tag",5,6.45]
is equivalent to:{"1":"tag","2":5,"3":6.45}
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
JSON_MEDIA_TYPE
static java.lang.String
MEDIA_TYPE
-
Fields inherited from class net.morimekta.providence.serializer.Serializer
DEFAULT_STRICT
-
-
Constructor Summary
Constructors Constructor Description JsonSerializer()
JsonSerializer(boolean strict)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
binaryProtocol()
<T extends PMessage<T>>
Tdeserialize(java.io.InputStream input, PMessageDescriptor<T> type)
Deserialize a message.<T extends PMessage<T>>
PServiceCall<T>deserialize(java.io.InputStream input, PService service)
Deserialize a service call.<T extends PMessage<T>>
Tdeserialize(java.io.Reader input, PMessageDescriptor<T> type)
JsonSerializer
grpc()
java.lang.String
mediaType()
JsonSerializer
named()
static <Message extends PMessage<Message>>
MessageparseJsonString(java.lang.String string, PMessageDescriptor<Message> descriptor)
Parses a JSON string, and makes exceptions unchecked.JsonSerializer
pretty()
<T extends PMessage<T>>
intserialize(java.io.OutputStream output, PMessageOrBuilder<T> message)
Serialize a message.<T extends PMessage<T>>
intserialize(java.io.OutputStream output, PServiceCall<T> call)
Serialize a service call.<T extends PMessage<T>>
voidserialize(java.io.PrintWriter output, PMessageOrBuilder<T> message)
static <Message extends PMessage<Message>>
java.lang.StringtoJsonString(PMessageOrBuilder<Message> message)
Prints a JSON string of the provided message.static <Message extends PMessage<Message>>
java.lang.StringtoPrettyJsonString(PMessageOrBuilder<Message> message)
Prints a pretty formatted JSON string of the provided message.void
verifyEndOfContent(java.io.InputStream input)
Verify that the input stream does not contain any more readable content and close the stream.void
verifyEndOfContent(java.io.Reader input)
JsonSerializer
withFlattenUnionOf()
JsonSerializer
withIdEnums()
JsonSerializer
withIdFields()
JsonSerializer
withNamedEnums()
JsonSerializer
withNamedFields()
JsonSerializer
withPojoNamedFields()
-
Methods inherited from class net.morimekta.providence.serializer.Serializer
isRequestCallType, toString
-
-
-
-
Field Detail
-
MEDIA_TYPE
public static final java.lang.String MEDIA_TYPE
- See Also:
- Constant Field Values
-
JSON_MEDIA_TYPE
public static final java.lang.String JSON_MEDIA_TYPE
- See Also:
- Constant Field Values
-
-
Method Detail
-
toJsonString
@Nonnull public static <Message extends PMessage<Message>> java.lang.String toJsonString(PMessageOrBuilder<Message> message)
Prints a JSON string of the provided message.- Type Parameters:
Message
- The message type.- Parameters:
message
- The message to stringify.- Returns:
- The resulting string.
-
toPrettyJsonString
@Nonnull public static <Message extends PMessage<Message>> java.lang.String toPrettyJsonString(PMessageOrBuilder<Message> message)
Prints a pretty formatted JSON string of the provided message.- Type Parameters:
Message
- The message type.- Parameters:
message
- The message to stringify.- Returns:
- The resulting string.
-
parseJsonString
@Nonnull public static <Message extends PMessage<Message>> Message parseJsonString(java.lang.String string, PMessageDescriptor<Message> descriptor)
Parses a JSON string, and makes exceptions unchecked.- Type Parameters:
Message
- The message type.- Parameters:
string
- The message string to parse.descriptor
- The message descriptor.- Returns:
- The parsed message.
-
pretty
public JsonSerializer pretty()
-
named
public JsonSerializer named()
-
grpc
public JsonSerializer grpc()
-
withPojoNamedFields
public JsonSerializer withPojoNamedFields()
-
withNamedFields
public JsonSerializer withNamedFields()
-
withNamedEnums
public JsonSerializer withNamedEnums()
-
withIdFields
public JsonSerializer withIdFields()
-
withIdEnums
public JsonSerializer withIdEnums()
-
withFlattenUnionOf
public JsonSerializer withFlattenUnionOf()
-
serialize
public <T extends PMessage<T>> void serialize(@Nonnull java.io.PrintWriter output, @Nonnull PMessageOrBuilder<T> message) throws java.io.IOException
- Throws:
java.io.IOException
-
serialize
public <T extends PMessage<T>> int serialize(@Nonnull java.io.OutputStream output, @Nonnull PMessageOrBuilder<T> message) throws java.io.IOException
Description copied from class:Serializer
Serialize a message.- Specified by:
serialize
in classSerializer
- Type Parameters:
T
- The message type.- Parameters:
output
- The output stream to write to.message
- The message to write.- Returns:
- The number of bytes written to the output stream.
- Throws:
java.io.IOException
- If writing or serialization failed.
-
serialize
public <T extends PMessage<T>> int serialize(@Nonnull java.io.OutputStream output, @Nonnull PServiceCall<T> call) throws java.io.IOException
Description copied from class:Serializer
Serialize a service call.- Specified by:
serialize
in classSerializer
- Type Parameters:
T
- The message type contained in the service call.- Parameters:
output
- The output stream to write to.call
- The service call to write.- Returns:
- The number of bytes written to the output stream.
- Throws:
java.io.IOException
- If writing or serialization failed.
-
deserialize
@Nonnull public <T extends PMessage<T>> T deserialize(@Nonnull java.io.InputStream input, @Nonnull PMessageDescriptor<T> type) throws java.io.IOException
Description copied from class:Serializer
Deserialize a message.- Specified by:
deserialize
in classSerializer
- Type Parameters:
T
- The message type.- Parameters:
input
- The input stream to read from.type
- The descriptor of the message to read.- Returns:
- The message read.
- Throws:
java.io.IOException
- If reading or deserialization failed.
-
deserialize
public <T extends PMessage<T>> T deserialize(@Nonnull java.io.Reader input, @Nonnull PMessageDescriptor<T> type) throws java.io.IOException
- Throws:
java.io.IOException
-
deserialize
@Nonnull public <T extends PMessage<T>> PServiceCall<T> deserialize(@Nonnull java.io.InputStream input, @Nonnull PService service) throws java.io.IOException
Description copied from class:Serializer
Deserialize a service call.- Specified by:
deserialize
in classSerializer
- Type Parameters:
T
- The message type contained in the returned service call.- Parameters:
input
- The input stream to read from.service
- The service definition to read for.- Returns:
- The service call read.
- Throws:
java.io.IOException
- If reading or deserialization failed.
-
binaryProtocol
public boolean binaryProtocol()
- Specified by:
binaryProtocol
in classSerializer
- Returns:
- True if the protocol of the serializer is "binary" a.k.a. unreadable to most humans.
-
verifyEndOfContent
public void verifyEndOfContent(@Nonnull java.io.InputStream input) throws java.io.IOException
Description copied from class:Serializer
Verify that the input stream does not contain any more readable content and close the stream. Throws exception if unable to verify, or the streams contains any meaningful content.- Specified by:
verifyEndOfContent
in classSerializer
- Parameters:
input
- The input stream to check.- Throws:
java.io.IOException
- If unable to read from stream or stream contains meaningful content.
-
verifyEndOfContent
public void verifyEndOfContent(@Nonnull java.io.Reader input) throws java.io.IOException
- Throws:
java.io.IOException
-
mediaType
@Nonnull public java.lang.String mediaType()
- Specified by:
mediaType
in classSerializer
- Returns:
- Media type of the content generated by the serializer. Can be used for Content-Type and Accept HTTP headers, MIME type etc.
-
-