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.StringJSON_MEDIA_TYPEstatic java.lang.StringMEDIA_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 booleanbinaryProtocol()<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.StringmediaType()JsonSerializernamed()JsonSerializerpretty()<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)JsonSerializerwithNamedEnums()JsonSerializerwithNamedFields()-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
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
-
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:SerializerSerialize a message.- Specified by:
serializein 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:SerializerSerialize a service call.- Specified by:
serializein 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:SerializerDeserialize a message.- Specified by:
deserializein 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:SerializerDeserialize a service call.- Specified by:
deserializein 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:
binaryProtocolin 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:
mediaTypein classSerializer- Returns:
- Media type of the content generated by the serializer. Can be used for Content-Type and Accept HTTP headers, MIME type etc.
-
-