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 org.codehaus.jackson.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,
org.codehaus.jackson.JsonGenerator jg)
Implement this method to writes the entity in the provided
JsonGenerator . |
protected void |
writeEntity(Object entity,
org.codehaus.jackson.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,
org.codehaus.jackson.JsonGenerator jg)
Delegates writing of an entity to the
MarshallerRegistry . |
@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, org.codehaus.jackson.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, org.codehaus.jackson.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, org.codehaus.jackson.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 org.codehaus.jackson.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
Copyright © 2017 Nuxeo. All rights reserved.