EntityType
- The Java type to marshall as Json.@Supports(value="application/json") public abstract class AbstractJsonWriter<EntityType> extends Object implements Writer<EntityType>
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.
Modifier and Type | Field and Description |
---|---|
protected RenderingContext |
ctx
The current
RenderingContext . |
protected MarshallerRegistry |
registry
The marshaller registry.
|
Constructor and Description |
---|
AbstractJsonWriter() |
Modifier and Type | Method and Description |
---|---|
boolean |
accept(Class<?> clazz,
Type genericType,
javax.ws.rs.core.MediaType mediatype)
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.
|
void |
write(EntityType entity,
Class<?> clazz,
Type genericType,
javax.ws.rs.core.MediaType mediatype,
OutputStream out)
Writes the entity to out
OutputStream using mediatype format. |
abstract void |
write(EntityType entity,
com.fasterxml.jackson.core.JsonGenerator jg)
Implement this method to writes the entity in the provided
JsonGenerator . |
protected void |
writeEntity(Object entity,
com.fasterxml.jackson.core.JsonGenerator jg)
Delegates writing of an entity to the
MarshallerRegistry . |
protected <ObjectType> |
writeEntity(ObjectType entity,
OutputStream out)
Delegates writing of an entity to the
MarshallerRegistry . |
protected void |
writeEntityField(String fieldName,
Object entity,
com.fasterxml.jackson.core.JsonGenerator jg)
Delegates writing of an entity to the
MarshallerRegistry . |
protected void |
writeSerializable(Serializable value,
com.fasterxml.jackson.core.JsonGenerator jg)
Writes a
Serializable . |
protected void |
writeSerializableField(String fieldName,
Serializable value,
com.fasterxml.jackson.core.JsonGenerator jg)
Writes a
Serializable . |
protected <T extends Serializable> |
writeSerializableListField(String fieldName,
Collection<T> serializables,
com.fasterxml.jackson.core.JsonGenerator jg)
Writes a list of
Serializable . |
protected <T extends Serializable> |
writeSerializableMapField(String fieldName,
Map<String,T> map,
com.fasterxml.jackson.core.JsonGenerator jg)
Writes a map whose values are
Serializable . |
@Inject protected RenderingContext ctx
RenderingContext
.@Inject protected MarshallerRegistry registry
public AbstractJsonWriter()
public boolean accept(Class<?> clazz, Type genericType, javax.ws.rs.core.MediaType mediatype)
Marshaller
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 the MarshallerRegistry
This method implementation can use injected properties. So you can check the current RenderingContext
to
accept or reject a marshalling request.
accept
in interface Marshaller<EntityType>
clazz
- The type to marshall.genericType
- The generic type to marshall.mediatype
- The managed mimetype.public void write(EntityType entity, Class<?> clazz, Type genericType, javax.ws.rs.core.MediaType mediatype, OutputStream out) throws IOException
Writer
OutputStream
using mediatype format.
This method implementation can use injected properties.
write
in interface Writer<EntityType>
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.IOException
- If some error append while writing entity to out.public abstract void write(EntityType entity, com.fasterxml.jackson.core.JsonGenerator jg) throws IOException
JsonGenerator
.
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 provided JsonGenerator
. It may be used is another marshaller calling this one.
entity
- The entity to marshall as Json.jg
- The JsonGenerator
used to produce Json output.IOException
protected void writeEntityField(String fieldName, Object entity, com.fasterxml.jackson.core.JsonGenerator jg) throws IOException
MarshallerRegistry
. This will work if a Json Writer
is
registered in the registry for the given clazz.fieldName
- The name of the Json field in which the entity will be wrote.entity
- The entity to write.jg
- The JsonGenerator
used to write the given entity.IOException
protected void writeEntity(Object entity, com.fasterxml.jackson.core.JsonGenerator jg) throws IOException
MarshallerRegistry
. This will work if a Json Writer
is
registered in the registry for the given clazz.entity
- The entity to write.jg
- The JsonGenerator
used to write the given entity.IOException
protected <ObjectType> void writeEntity(ObjectType entity, OutputStream out) throws IOException
MarshallerRegistry
. This will work if a Json Writer
is
registered in the registry for the given clazz.entity
- The entity to write.out
- The OutputStream
in which the given entity will be wrote.IOException
- If some i/o error append while writing entity.protected com.fasterxml.jackson.core.JsonGenerator getGenerator(OutputStream out, boolean getCurrentIfAvailable) throws IOException
out
- The OutputStream
on which the generator will generate Json.getCurrentIfAvailable
- If true, try to get the current generator in the context.IOException
protected <T extends Serializable> void writeSerializableListField(String fieldName, Collection<T> serializables, com.fasterxml.jackson.core.JsonGenerator jg) throws IOException
Serializable
.fieldName
- The name of the Json field in which the serializables will be wrote.serializables
- The serializables to write.jg
- The JsonGenerator
used to write the given serializables.IOException
protected <T extends Serializable> void writeSerializableMapField(String fieldName, Map<String,T> map, com.fasterxml.jackson.core.JsonGenerator jg) throws IOException
Serializable
.fieldName
- The name of the Json field in which the serializables will be wrote.map
- The map to write.jg
- The JsonGenerator
used to write the given map.IOException
protected void writeSerializableField(String fieldName, Serializable value, com.fasterxml.jackson.core.JsonGenerator jg) throws IOException
Serializable
.
This method will first try to cast value to Collection
, array, String
, Boolean
and
Number
. If none of previous cast could work, try to write it with marshallersfieldName
- The name of the Json field in which the serializable will be wrote.value
- The value to write.jg
- The JsonGenerator
used to write the given serializable.IOException
protected void writeSerializable(Serializable value, com.fasterxml.jackson.core.JsonGenerator jg) throws IOException
Serializable
.
This method will first try to cast value to Collection
, array, String
, Boolean
and
Number
. If none of previous cast could work, try to write it with marshallersvalue
- The value to write.jg
- The JsonGenerator
used to write the given serializable.IOException
Copyright © 2019 Nuxeo. All rights reserved.