Class 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
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
FieldsModifier and TypeFieldDescriptionprotected RenderingContextThe currentRenderingContext.protected MarshallerRegistryThe marshaller registry. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanChecks if this marshaller can handle the marshalling request.protected com.fasterxml.jackson.core.JsonGeneratorgetGenerator(OutputStream out, boolean getCurrentIfAvailable) Get the current Json generator or create it if none was found.abstract voidwrite(EntityType entity, com.fasterxml.jackson.core.JsonGenerator jg) Implement this method to writes the entity in the providedJsonGenerator.voidwrite(EntityType entity, Class<?> clazz, Type genericType, jakarta.ws.rs.core.MediaType mediatype, OutputStream out) Writes the entity to outOutputStreamusing mediatype format.protected voidwriteEntity(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 <ObjectType>
voidwriteEntityArrayField(String fieldName, Collection<ObjectType> entities, com.fasterxml.jackson.core.JsonGenerator jg) protected <ObjectType>
voidwriteEntityArrayField(String fieldName, ObjectType[] entities, com.fasterxml.jackson.core.JsonGenerator jg) protected voidwriteEntityField(String fieldName, Object entity, com.fasterxml.jackson.core.JsonGenerator jg) Delegates writing of an entity to theMarshallerRegistry.protected voidwriteNumberFieldIfNotNull(String fieldName, Number value, com.fasterxml.jackson.core.JsonGenerator jg) Writes the given number field if the value is not null.protected voidwriteObjectFieldIfNotEmpty(String fieldName, Map<?, ?> value, com.fasterxml.jackson.core.JsonGenerator jg) Writes the given map field if the value is not null and not empty, the map will be written withJsonGenerator.writeObject(Object).protected voidwriteObjectFieldIfNotNull(String fieldName, Object value, com.fasterxml.jackson.core.JsonGenerator jg) Writes the given object field if the value is not null.protected voidwriteSerializable(Serializable value, com.fasterxml.jackson.core.JsonGenerator jg) Deprecated, for removal: This API element is subject to removal in a future version.since 2025.14, use other typed APIs insteadprotected voidwriteSerializableField(String fieldName, Serializable value, com.fasterxml.jackson.core.JsonGenerator jg) Deprecated, for removal: This API element is subject to removal in a future version.since 2025.14, use other typed APIs insteadprotected <T extends Serializable>
voidwriteSerializableListField(String fieldName, Collection<T> serializables, com.fasterxml.jackson.core.JsonGenerator jg) Deprecated, for removal: This API element is subject to removal in a future version.since 2025.14, use other typed APIs instead, likewriteEntityArrayField(String, Collection, JsonGenerator)protected <T extends Serializable>
voidwriteSerializableMapField(String fieldName, Map<String, T> map, com.fasterxml.jackson.core.JsonGenerator jg) Deprecated, for removal: This API element is subject to removal in a future version.since 2025.14, use other typed APIs insteadprotected voidwriteStringFieldIfNotNull(String fieldName, String value, com.fasterxml.jackson.core.JsonGenerator jg) Writes the given string field if the value is not null.protected voidwriteTemporalField(String fieldName, Temporal value, com.fasterxml.jackson.core.JsonGenerator jg) Writes a temporal field in a similar way asJsonGenerator.writeStringField(String, String).
-
Field Details
-
ctx
The currentRenderingContext. -
registry
The marshaller registry.
-
-
Constructor Details
-
AbstractJsonWriter
public AbstractJsonWriter()
-
-
Method Details
-
accept
Description copied from interface:MarshallerChecks 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
Supportsis compatible with mediatype. This is already done by theMarshallerRegistryThis method implementation can use injected properties. So you can check the current
RenderingContextto accept or reject a marshalling request.- Specified by:
acceptin 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, jakarta.ws.rs.core.MediaType mediatype, OutputStream out) throws IOException Description copied from interface:WriterWrites the entity to outOutputStreamusing mediatype format.This method implementation can use injected properties.
- Specified by:
writein 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- TheJsonGeneratorused 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 JsonWriteris 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- TheJsonGeneratorused 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 JsonWriteris registered in the registry for the given clazz.- Parameters:
entity- The entity to write.jg- TheJsonGeneratorused to write the given entity.- Throws:
IOException- Since:
- 7.2
-
writeEntity
Delegates writing of an entity to theMarshallerRegistry. This will work if a JsonWriteris registered in the registry for the given clazz.- Parameters:
entity- The entity to write.out- TheOutputStreamin 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 writevalue- the value to writejg- theJsonGeneratorto 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 writevalue- the value to writejg- theJsonGeneratorto 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 IOExceptionWrites the given map field if the value is not null and not empty, the map will be written withJsonGenerator.writeObject(Object).- Parameters:
fieldName- the field name to writevalue- the value to writejg- theJsonGeneratorto 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 writevalue- the value to writejg- theJsonGeneratorto 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 asJsonGenerator.writeStringField(String, String).- Parameters:
fieldName- the field name to writevalue- the value to writejg- theJsonGeneratorto 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- TheOutputStreamon 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, likewriteEntityArrayField(String, Collection, JsonGenerator)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- TheJsonGeneratorused 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 IOExceptionDeprecated, for removal: This API element is subject to removal in a future version.since 2025.14, use other typed APIs insteadWrites 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- TheJsonGeneratorused 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 insteadWrites aSerializable.This method will first try to cast value to
Collection, array,String,BooleanandNumber. 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- TheJsonGeneratorused 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 insteadWrites aSerializable.This method will first try to cast value to
Collection, array,String,BooleanandNumber. If none of previous cast could work, try to write it with marshallers- Parameters:
value- The value to write.jg- TheJsonGeneratorused 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.
-