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 Instance Methods Concrete Methods Modifier and Type Method Description boolean
binaryProtocol()
<T extends PMessage<T,TF>,TF extends PField>
Tdeserialize(java.io.InputStream input, PMessageDescriptor<T,TF> type)
Deserialize a message.<T extends PMessage<T,F>,F extends PField>
PServiceCall<T,F>deserialize(java.io.InputStream input, PService service)
Deserialize a service call.<T extends PMessage<T,TF>,TF extends PField>
Tdeserialize(java.io.Reader input, PMessageDescriptor<T,TF> type)
java.lang.String
mediaType()
JsonSerializer
named()
JsonSerializer
pretty()
<T extends PMessage<T,F>,F extends PField>
intserialize(java.io.OutputStream output, PServiceCall<T,F> call)
Serialize a service call.<T extends PMessage<T,F>,F extends PField>
intserialize(java.io.OutputStream output, T message)
Serialize a message.<T extends PMessage<T,F>,F extends PField>
voidserialize(java.io.PrintWriter output, T message)
JsonSerializer
withNamedEnums()
JsonSerializer
withNamedFields()
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from class net.morimekta.providence.serializer.Serializer
isRequestCallType
-
-
-
-
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
-
pretty
public JsonSerializer pretty()
-
named
public JsonSerializer named()
-
withNamedFields
public JsonSerializer withNamedFields()
-
withNamedEnums
public JsonSerializer withNamedEnums()
-
serialize
public <T extends PMessage<T,F>,F extends PField> void serialize(@Nonnull java.io.PrintWriter output, @Nonnull T message) throws java.io.IOException
- Throws:
java.io.IOException
-
serialize
public <T extends PMessage<T,F>,F extends PField> int serialize(@Nonnull java.io.OutputStream output, @Nonnull 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.F
- The message field 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,F>,F extends PField> int serialize(@Nonnull java.io.OutputStream output, @Nonnull PServiceCall<T,F> 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.F
- The message field type.- 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,TF>,TF extends PField> T deserialize(@Nonnull java.io.InputStream input, @Nonnull PMessageDescriptor<T,TF> type) throws java.io.IOException
Description copied from class:Serializer
Deserialize a message.- Specified by:
deserialize
in classSerializer
- Type Parameters:
T
- The message type.TF
- The message field 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,TF>,TF extends PField> T deserialize(@Nonnull java.io.Reader input, @Nonnull PMessageDescriptor<T,TF> type) throws java.io.IOException
- Throws:
java.io.IOException
-
deserialize
@Nonnull public <T extends PMessage<T,F>,F extends PField> PServiceCall<T,F> 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.F
- The message field type.- 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.
-
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.
-
-