Class AbstractJsonWriter<EntityType>

java.lang.Object
org.nuxeo.ecm.core.io.marshallers.json.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

@Supports("application/json") public abstract class AbstractJsonWriter<EntityType> extends Object implements Writer<EntityType>
Base class for Json 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 Details

  • Constructor Details

    • AbstractJsonWriter

      public AbstractJsonWriter()
  • Method Details

    • accept

      public boolean accept(Class<?> clazz, Type genericType, javax.ws.rs.core.MediaType mediatype)
      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 the MarshallerRegistry

      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 interface Marshaller<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 out OutputStream using mediatype format.

      This method implementation can use injected properties.

      Specified by:
      write in interface Writer<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 provided 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.

      Parameters:
      entity - The entity to marshall as Json.
      jg - The JsonGenerator 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 the MarshallerRegistry. This will work if a Json Writer 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 - The JsonGenerator 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 the MarshallerRegistry. This will work if a Json Writer is registered in the registry for the given clazz.
      Parameters:
      entity - The entity to write.
      jg - The JsonGenerator used to write the given entity.
      Throws:
      IOException
      Since:
      7.2
    • writeEntity

      protected <ObjectType> void writeEntity(ObjectType entity, OutputStream out) throws IOException
      Delegates writing of an entity to the MarshallerRegistry. This will work if a Json Writer is registered in the registry for the given clazz.
      Parameters:
      entity - The entity to write.
      out - The OutputStream 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 - The OutputStream 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 of Serializable.
      Parameters:
      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.
      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 IOException
      Writes a map whose values are Serializable.
      Parameters:
      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.
      Throws:
      IOException
      Since:
      10.1
    • writeSerializableField

      protected void writeSerializableField(String fieldName, Serializable value, com.fasterxml.jackson.core.JsonGenerator jg) throws IOException
      Writes a 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 marshallers

      Parameters:
      fieldName - 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.
      Throws:
      IOException
      Since:
      10.1
    • writeSerializable

      protected void writeSerializable(Serializable value, com.fasterxml.jackson.core.JsonGenerator jg) throws IOException
      Writes a 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 marshallers

      Parameters:
      value - The value to write.
      jg - The JsonGenerator used to write the given serializable.
      Throws:
      IOException
      Since:
      10.1