Class AbstractJsonWriter<EntityType>
- Type Parameters:
EntityType
- The Java type to marshall as Json.
- All Implemented Interfaces:
Marshaller<EntityType>
,Writer<EntityType>
- Direct Known Subclasses:
AbstractJsonEnricher
,AbstractLayoutJsonWriter
,AuthenticationTokenWriter
,DefaultListJsonWriter
,DocumentPropertyJsonWriter
,ExtensibleEntityJsonWriter
,FieldDefinitionJsonWriter
,LayoutDefinitionsJsonWriter
,LayoutJsonWriter
,LayoutTypeConfigurationJsonWriter
,LayoutTypeDefinitionJsonWriter
,LayoutTypeDefinitionsJsonWriter
,RenderingInfoJsonWriter
,ScimResourceWriter
,WidgetReferenceJsonWriter
,WidgetSelectOptionJsonWriter
,WidgetTypeDefinitionJsonWriter
,WidgetTypeDefinitionsJsonWriter
Writer
.
This class provides a easy way to produce json and also provides the current context: ctx
.
It provides you a JsonGenerator
to manage the marshalling.
The use of this class optimize the JsonFactory usage especially when aggregating marshallers.
- Since:
- 7.2
-
Field Summary
Modifier and TypeFieldDescriptionprotected RenderingContext
The currentRenderingContext
.protected MarshallerRegistry
The marshaller registry. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionboolean
Checks if this marshaller can handle the marshalling request.protected com.fasterxml.jackson.core.JsonGenerator
getGenerator
(OutputStream out, boolean getCurrentIfAvailable) Get the current Json generator or create it if none was found.abstract void
write
(EntityType entity, com.fasterxml.jackson.core.JsonGenerator jg) Implement this method to writes the entity in the providedJsonGenerator
.void
write
(EntityType entity, Class<?> clazz, Type genericType, javax.ws.rs.core.MediaType mediatype, OutputStream out) Writes the entity to outOutputStream
using mediatype format.protected void
writeEntity
(Object entity, com.fasterxml.jackson.core.JsonGenerator jg) Delegates writing of an entity to theMarshallerRegistry
.protected <ObjectType>
voidwriteEntity
(ObjectType entity, OutputStream out) Delegates writing of an entity to theMarshallerRegistry
.protected void
writeEntityField
(String fieldName, Object entity, com.fasterxml.jackson.core.JsonGenerator jg) Delegates writing of an entity to theMarshallerRegistry
.protected void
writeSerializable
(Serializable value, com.fasterxml.jackson.core.JsonGenerator jg) Writes aSerializable
.protected void
writeSerializableField
(String fieldName, Serializable value, com.fasterxml.jackson.core.JsonGenerator jg) Writes aSerializable
.protected <T extends Serializable>
voidwriteSerializableListField
(String fieldName, Collection<T> serializables, com.fasterxml.jackson.core.JsonGenerator jg) Writes a list ofSerializable
.protected <T extends Serializable>
voidwriteSerializableMapField
(String fieldName, Map<String, T> map, com.fasterxml.jackson.core.JsonGenerator jg) Writes a map whose values areSerializable
.
-
Field Details
-
ctx
The currentRenderingContext
. -
registry
The marshaller registry.
-
-
Constructor Details
-
AbstractJsonWriter
public AbstractJsonWriter()
-
-
Method Details
-
accept
Description copied from interface:Marshaller
Checks if this marshaller can handle the marshalling request.Please note it's useless to check that clazz is an instance of EntityType or if generic type and entity type are compatible (unlike JAX-RS which just checks the clazz, not the generic type). It's also useless to check
Supports
is compatible with mediatype. This is already done by theMarshallerRegistry
This method implementation can use injected properties. So you can check the current
RenderingContext
to accept or reject a marshalling request.- Specified by:
accept
in interfaceMarshaller<EntityType>
- Parameters:
clazz
- The type to marshall.genericType
- The generic type to marshall.mediatype
- The managed mimetype.- Returns:
- true if this converter handle the request, false otherwise.
-
write
public void write(EntityType entity, Class<?> clazz, Type genericType, javax.ws.rs.core.MediaType mediatype, OutputStream out) throws IOException Description copied from interface:Writer
Writes the entity to outOutputStream
using mediatype format.This method implementation can use injected properties.
- Specified by:
write
in interfaceWriter<EntityType>
- Parameters:
entity
- The entity to marshall.clazz
- The requested marshalled class.genericType
- The requested marshalled generic type.mediatype
- The target mediatype.out
- The output of this marshaller.- Throws:
IOException
- If some error append while writing entity to out.
-
write
public abstract void write(EntityType entity, com.fasterxml.jackson.core.JsonGenerator jg) throws IOException Implement this method to writes the entity in the providedJsonGenerator
.This method implementation can use injected properties.
The
JsonGenerator
's flushing is done by this abstract class, it's also not not necessary to flush it. Do not close the providedJsonGenerator
. It may be used is another marshaller calling this one.- Parameters:
entity
- The entity to marshall as Json.jg
- TheJsonGenerator
used to produce Json output.- Throws:
IOException
- Since:
- 7.2
-
writeEntityField
protected void writeEntityField(String fieldName, Object entity, com.fasterxml.jackson.core.JsonGenerator jg) throws IOException Delegates writing of an entity to theMarshallerRegistry
. This will work if a JsonWriter
is registered in the registry for the given clazz.- Parameters:
fieldName
- The name of the Json field in which the entity will be wrote.entity
- The entity to write.jg
- TheJsonGenerator
used to write the given entity.- Throws:
IOException
- Since:
- 7.2
-
writeEntity
protected void writeEntity(Object entity, com.fasterxml.jackson.core.JsonGenerator jg) throws IOException Delegates writing of an entity to theMarshallerRegistry
. This will work if a JsonWriter
is registered in the registry for the given clazz.- Parameters:
entity
- The entity to write.jg
- TheJsonGenerator
used to write the given entity.- Throws:
IOException
- Since:
- 7.2
-
writeEntity
Delegates writing of an entity to theMarshallerRegistry
. This will work if a JsonWriter
is registered in the registry for the given clazz.- Parameters:
entity
- The entity to write.out
- TheOutputStream
in which the given entity will be wrote.- Throws:
IOException
- If some i/o error append while writing entity.- Since:
- 7.2
-
getGenerator
protected com.fasterxml.jackson.core.JsonGenerator getGenerator(OutputStream out, boolean getCurrentIfAvailable) throws IOException Get the current Json generator or create it if none was found.- Parameters:
out
- TheOutputStream
on which the generator will generate Json.getCurrentIfAvailable
- If true, try to get the current generator in the context.- Returns:
- The created generator.
- Throws:
IOException
- Since:
- 7.2
-
writeSerializableListField
protected <T extends Serializable> void writeSerializableListField(String fieldName, Collection<T> serializables, com.fasterxml.jackson.core.JsonGenerator jg) throws IOException Writes a list ofSerializable
.- Parameters:
fieldName
- The name of the Json field in which the serializables will be wrote.serializables
- The serializables to write.jg
- TheJsonGenerator
used to write the given serializables.- Throws:
IOException
- Since:
- 10.1
-
writeSerializableMapField
protected <T extends Serializable> void writeSerializableMapField(String fieldName, Map<String, T> map, com.fasterxml.jackson.core.JsonGenerator jg) throws IOExceptionWrites a map whose values areSerializable
.- Parameters:
fieldName
- The name of the Json field in which the serializables will be wrote.map
- The map to write.jg
- TheJsonGenerator
used to write the given map.- Throws:
IOException
- Since:
- 10.1
-
writeSerializableField
protected void writeSerializableField(String fieldName, Serializable value, com.fasterxml.jackson.core.JsonGenerator jg) throws IOException Writes aSerializable
.This method will first try to cast value to
Collection
, array,String
,Boolean
andNumber
. If none of previous cast could work, try to write it with marshallers- Parameters:
fieldName
- The name of the Json field in which the serializable will be wrote.value
- The value to write.jg
- TheJsonGenerator
used to write the given serializable.- Throws:
IOException
- Since:
- 10.1
-
writeSerializable
protected void writeSerializable(Serializable value, com.fasterxml.jackson.core.JsonGenerator jg) throws IOException Writes aSerializable
.This method will first try to cast value to
Collection
, array,String
,Boolean
andNumber
. If none of previous cast could work, try to write it with marshallers- Parameters:
value
- The value to write.jg
- TheJsonGenerator
used to write the given serializable.- Throws:
IOException
- Since:
- 10.1
-