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, AuthenticationTokenWriter, DefaultListJsonWriter, DocumentPropertyJsonWriter, ExtensibleEntityJsonWriter, ScaleActivityJsonWriter, ScimResourceWriter, StreamIntrospectionJsonWriter, StreamIntrospectionProcessorTopologyJsonWriter, StreamIntrospectionProcessorTopologyJsonWriter.ListJsonWriter, StreamIntrospectionStreamJsonWriter, StreamIntrospectionStreamJsonWriter.ListJsonWriter, StreamLagChangeJsonWriter, StreamLagJsonWriter

@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, jakarta.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 Jakarta 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, jakarta.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
    • writeEntityArrayField

      protected <ObjectType> void writeEntityArrayField(String fieldName, ObjectType[] entities, com.fasterxml.jackson.core.JsonGenerator jg) throws IOException
      Throws:
      IOException
    • writeEntityArrayField

      protected <ObjectType> void writeEntityArrayField(String fieldName, Collection<ObjectType> entities, com.fasterxml.jackson.core.JsonGenerator jg) throws IOException
      Throws:
      IOException
    • writeNumberFieldIfNotNull

      protected void writeNumberFieldIfNotNull(String fieldName, @Nullable Number value, com.fasterxml.jackson.core.JsonGenerator jg) throws IOException
      Writes the given number field if the value is not null.
      Parameters:
      fieldName - the field name to write
      value - the value to write
      jg - the JsonGenerator to write the given value
      Throws:
      IOException
      Since:
      2025.14
    • writeObjectFieldIfNotNull

      protected void writeObjectFieldIfNotNull(String fieldName, @Nullable Object value, com.fasterxml.jackson.core.JsonGenerator jg) throws IOException
      Writes the given object field if the value is not null.
      Parameters:
      fieldName - the field name to write
      value - the value to write
      jg - the JsonGenerator to write the given value
      Throws:
      IOException
      Since:
      2025.14
    • writeObjectFieldIfNotEmpty

      protected void writeObjectFieldIfNotEmpty(String fieldName, @Nullable Map<?,?> value, com.fasterxml.jackson.core.JsonGenerator jg) throws IOException
      Writes the given map field if the value is not null and not empty, the map will be written with JsonGenerator.writeObject(Object).
      Parameters:
      fieldName - the field name to write
      value - the value to write
      jg - the JsonGenerator to write the given value
      Throws:
      IOException
      Since:
      2025.14
    • writeStringFieldIfNotNull

      protected void writeStringFieldIfNotNull(String fieldName, @Nullable String value, com.fasterxml.jackson.core.JsonGenerator jg) throws IOException
      Writes the given string field if the value is not null.
      Parameters:
      fieldName - the field name to write
      value - the value to write
      jg - the JsonGenerator to write the given value
      Throws:
      IOException
      Since:
      2025.14
    • writeTemporalField

      protected void writeTemporalField(String fieldName, @Nullable Temporal value, com.fasterxml.jackson.core.JsonGenerator jg) throws IOException
      Writes a temporal field in a similar way as JsonGenerator.writeStringField(String, String).
      Parameters:
      fieldName - the field name to write
      value - the value to write
      jg - the JsonGenerator to write the given value
      Throws:
      IOException
      Since:
      2025.14
    • 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

      @Deprecated(since="2025.14", forRemoval=true) protected <T extends Serializable> void writeSerializableListField(String fieldName, Collection<T> serializables, com.fasterxml.jackson.core.JsonGenerator jg) throws IOException
      Deprecated, for removal: This API element is subject to removal in a future version.
      since 2025.14, use other typed APIs instead, like writeEntityArrayField(String, Collection, JsonGenerator)
      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
      Implementation Note:
      It is deprecated because instanceof usage has poor performance, while we want to have an efficient serializer.
    • writeSerializableMapField

      @Deprecated(since="2025.14", forRemoval=true) protected <T extends Serializable> void writeSerializableMapField(String fieldName, Map<String,T> map, com.fasterxml.jackson.core.JsonGenerator jg) throws IOException
      Deprecated, for removal: This API element is subject to removal in a future version.
      since 2025.14, use other typed APIs instead
      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
      Implementation Note:
      It is deprecated because instanceof usage has poor performance, while we want to have an efficient serializer.
    • writeSerializableField

      @Deprecated(since="2025.14", forRemoval=true) protected void writeSerializableField(String fieldName, Serializable value, com.fasterxml.jackson.core.JsonGenerator jg) throws IOException
      Deprecated, for removal: This API element is subject to removal in a future version.
      since 2025.14, use other typed APIs instead
      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
      Implementation Note:
      It is deprecated because instanceof usage has poor performance, while we want to have an efficient serializer.
    • writeSerializable

      @Deprecated(since="2025.14", forRemoval=true) protected void writeSerializable(Serializable value, com.fasterxml.jackson.core.JsonGenerator jg) throws IOException
      Deprecated, for removal: This API element is subject to removal in a future version.
      since 2025.14, use other typed APIs instead
      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
      Implementation Note:
      It is deprecated because instanceof usage has poor performance, while we want to have an efficient serializer.