Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.fasterxml.jackson.databind.ser.std.BeanSerializerBase Maven / Gradle / Ivy
package com.fasterxml.jackson.databind.ser.std;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.ObjectIdGenerator;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.introspect.Annotated;
import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
import com.fasterxml.jackson.databind.introspect.ObjectIdInfo;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitable;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonObjectFormatVisitor;
import com.fasterxml.jackson.databind.jsonschema.JsonSerializableSchema;
import com.fasterxml.jackson.databind.jsonschema.SchemaAware;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.ser.*;
import com.fasterxml.jackson.databind.ser.impl.ObjectIdWriter;
import com.fasterxml.jackson.databind.ser.impl.PropertyBasedObjectIdGenerator;
import com.fasterxml.jackson.databind.ser.impl.WritableObjectId;
import com.fasterxml.jackson.databind.util.ArrayBuilders;
import com.fasterxml.jackson.databind.util.Converter;
import com.fasterxml.jackson.databind.util.NameTransformer;
public abstract class BeanSerializerBase
extends StdSerializer
implements ContextualSerializer , ResolvableSerializer ,
JsonFormatVisitable , SchemaAware
{
protected final static PropertyName NAME_FOR_OBJECT_REF = new PropertyName("#object-ref" );
final protected static BeanPropertyWriter[] NO_PROPS = new BeanPropertyWriter[0 ];
final protected BeanPropertyWriter[] _props;
final protected BeanPropertyWriter[] _filteredProps;
final protected AnyGetterWriter _anyGetterWriter;
final protected Object _propertyFilterId;
final protected AnnotatedMember _typeId;
final protected ObjectIdWriter _objectIdWriter;
final protected JsonFormat.Shape _serializationShape;
protected BeanSerializerBase (JavaType type, BeanSerializerBuilder builder,
BeanPropertyWriter[] properties, BeanPropertyWriter[] filteredProperties)
{
super (type);
_props = properties;
_filteredProps = filteredProperties;
if (builder == null ) {
_typeId = null ;
_anyGetterWriter = null ;
_propertyFilterId = null ;
_objectIdWriter = null ;
_serializationShape = null ;
} else {
_typeId = builder.getTypeId();
_anyGetterWriter = builder.getAnyGetter();
_propertyFilterId = builder.getFilterId();
_objectIdWriter = builder.getObjectIdWriter();
JsonFormat.Value format = builder.getBeanDescription().findExpectedFormat(null );
_serializationShape = (format == null ) ? null : format.getShape();
}
}
public BeanSerializerBase (BeanSerializerBase src,
BeanPropertyWriter[] properties, BeanPropertyWriter[] filteredProperties)
{
super (src._handledType);
_props = properties;
_filteredProps = filteredProperties;
_typeId = src._typeId;
_anyGetterWriter = src._anyGetterWriter;
_objectIdWriter = src._objectIdWriter;
_propertyFilterId = src._propertyFilterId;
_serializationShape = src._serializationShape;
}
protected BeanSerializerBase (BeanSerializerBase src,
ObjectIdWriter objectIdWriter)
{
this (src, objectIdWriter, src._propertyFilterId);
}
protected BeanSerializerBase (BeanSerializerBase src,
ObjectIdWriter objectIdWriter, Object filterId)
{
super (src._handledType);
_props = src._props;
_filteredProps = src._filteredProps;
_typeId = src._typeId;
_anyGetterWriter = src._anyGetterWriter;
_objectIdWriter = objectIdWriter;
_propertyFilterId = filterId;
_serializationShape = src._serializationShape;
}
protected BeanSerializerBase (BeanSerializerBase src, String[] toIgnore)
{
super (src._handledType);
HashSet ignoredSet = ArrayBuilders.arrayToSet(toIgnore);
final BeanPropertyWriter[] propsIn = src._props;
final BeanPropertyWriter[] fpropsIn = src._filteredProps;
final int len = propsIn.length;
ArrayList propsOut = new ArrayList(len);
ArrayList fpropsOut = (fpropsIn == null ) ? null : new ArrayList(len);
for (int i = 0 ; i < len; ++i) {
BeanPropertyWriter bpw = propsIn[i];
if (ignoredSet.contains(bpw.getName())) {
continue ;
}
propsOut.add(bpw);
if (fpropsIn != null ) {
fpropsOut.add(fpropsIn[i]);
}
}
_props = propsOut.toArray(new BeanPropertyWriter[propsOut.size()]);
_filteredProps = (fpropsOut == null ) ? null : fpropsOut.toArray(new BeanPropertyWriter[fpropsOut.size()]);
_typeId = src._typeId;
_anyGetterWriter = src._anyGetterWriter;
_objectIdWriter = src._objectIdWriter;
_propertyFilterId = src._propertyFilterId;
_serializationShape = src._serializationShape;
}
public abstract BeanSerializerBase withObjectIdWriter (ObjectIdWriter objectIdWriter) ;
protected abstract BeanSerializerBase withIgnorals (String[] toIgnore) ;
protected abstract BeanSerializerBase asArraySerializer () ;
protected abstract BeanSerializerBase withFilterId (Object filterId) ;
protected BeanSerializerBase (BeanSerializerBase src) {
this (src, src._props, src._filteredProps);
}
protected BeanSerializerBase (BeanSerializerBase src, NameTransformer unwrapper) {
this (src, rename(src._props, unwrapper), rename(src._filteredProps, unwrapper));
}
private final static BeanPropertyWriter[] rename(BeanPropertyWriter[] props,
NameTransformer transformer)
{
if (props == null || props.length == 0 || transformer == null || transformer == NameTransformer.NOP) {
return props;
}
final int len = props.length;
BeanPropertyWriter[] result = new BeanPropertyWriter[len];
for (int i = 0 ; i < len; ++i) {
BeanPropertyWriter bpw = props[i];
if (bpw != null ) {
result[i] = bpw.rename(transformer);
}
}
return result;
}
@Override
public void resolve (SerializerProvider provider)
throws JsonMappingException
{
int filteredCount = (_filteredProps == null ) ? 0 : _filteredProps.length;
for (int i = 0 , len = _props.length; i < len; ++i) {
BeanPropertyWriter prop = _props[i];
if (!prop.willSuppressNulls() && !prop.hasNullSerializer()) {
JsonSerializer nullSer = provider.findNullValueSerializer(prop);
if (nullSer != null ) {
prop.assignNullSerializer(nullSer);
if (i < filteredCount) {
BeanPropertyWriter w2 = _filteredProps[i];
if (w2 != null ) {
w2.assignNullSerializer(nullSer);
}
}
}
}
if (prop.hasSerializer()) {
continue ;
}
JsonSerializer ser = findConvertingSerializer(provider, prop);
if (ser == null ) {
JavaType type = prop.getSerializationType();
if (type == null ) {
type = provider.constructType(prop.getGenericPropertyType());
if (!type.isFinal()) {
if (type.isContainerType() || type.containedTypeCount() > 0 ) {
prop.setNonTrivialBaseType(type);
}
continue ;
}
}
ser = provider.findValueSerializer(type, prop);
if (type.isContainerType()) {
TypeSerializer typeSer = type.getContentType().getTypeHandler();
if (typeSer != null ) {
if (ser instanceof ContainerSerializer) {
@SuppressWarnings ("unchecked" )
JsonSerializer ser2 = (JsonSerializer)((ContainerSerializer) ser).withValueTypeSerializer(typeSer);
ser = ser2;
}
}
}
}
prop.assignSerializer(ser);
if (i < filteredCount) {
BeanPropertyWriter w2 = _filteredProps[i];
if (w2 != null ) {
w2.assignSerializer(ser);
}
}
}
if (_anyGetterWriter != null ) {
_anyGetterWriter.resolve(provider);
}
}
protected JsonSerializer findConvertingSerializer (SerializerProvider provider,
BeanPropertyWriter prop)
throws JsonMappingException
{
final AnnotationIntrospector intr = provider.getAnnotationIntrospector();
if (intr != null ) {
Object convDef = intr.findSerializationConverter(prop.getMember());
if (convDef != null ) {
Converter conv = provider.converterInstance(prop.getMember(), convDef);
JavaType delegateType = conv.getOutputType(provider.getTypeFactory());
JsonSerializer ser = provider.findValueSerializer(delegateType, prop);
return new StdDelegatingSerializer(conv, delegateType, ser);
}
}
return null ;
}
@Override
public JsonSerializer createContextual (SerializerProvider provider,
BeanProperty property)
throws JsonMappingException
{
ObjectIdWriter oiw = _objectIdWriter;
String[] ignorals = null ;
Object newFilterId = null ;
final AnnotationIntrospector intr = provider.getAnnotationIntrospector();
final AnnotatedMember accessor = (property == null || intr == null )
? null : property.getMember();
if (accessor != null ) {
ignorals = intr.findPropertiesToIgnore(accessor);
ObjectIdInfo objectIdInfo = intr.findObjectIdInfo(accessor);
if (objectIdInfo == null ) {
if (oiw != null ) {
objectIdInfo = intr.findObjectReferenceInfo(accessor,
new ObjectIdInfo(NAME_FOR_OBJECT_REF, null , null , null ));
oiw = _objectIdWriter.withAlwaysAsId(objectIdInfo.getAlwaysAsId());
}
} else {
objectIdInfo = intr.findObjectReferenceInfo(accessor, objectIdInfo);
ObjectIdGenerator gen;
Class implClass = objectIdInfo.getGeneratorType();
JavaType type = provider.constructType(implClass);
JavaType idType = provider.getTypeFactory().findTypeParameters(type, ObjectIdGenerator.class)[0 ];
if (implClass == ObjectIdGenerators.PropertyGenerator.class) {
String propName = objectIdInfo.getPropertyName().getSimpleName();
BeanPropertyWriter idProp = null ;
for (int i = 0 , len = _props.length ;; ++i) {
if (i == len) {
throw new IllegalArgumentException("Invalid Object Id definition for " +_handledType.getName()
+": can not find property with name '" +propName+"'" );
}
BeanPropertyWriter prop = _props[i];
if (propName.equals(prop.getName())) {
idProp = prop;
if (i > 0 ) {
System.arraycopy(_props, 0 , _props, 1 , i);
_props[0 ] = idProp;
if (_filteredProps != null ) {
BeanPropertyWriter fp = _filteredProps[i];
System.arraycopy(_filteredProps, 0 , _filteredProps, 1 , i);
_filteredProps[0 ] = fp;
}
}
break ;
}
}
idType = idProp.getType();
gen = new PropertyBasedObjectIdGenerator(objectIdInfo, idProp);
oiw = ObjectIdWriter.construct(idType, (PropertyName) null , gen, objectIdInfo.getAlwaysAsId());
} else {
gen = provider.objectIdGeneratorInstance(accessor, objectIdInfo);
oiw = ObjectIdWriter.construct(idType, objectIdInfo.getPropertyName(), gen,
objectIdInfo.getAlwaysAsId());
}
}
Object filterId = intr.findFilterId(accessor);
if (filterId != null ) {
if (_propertyFilterId == null || !filterId.equals(_propertyFilterId)) {
newFilterId = filterId;
}
}
}
BeanSerializerBase contextual = this ;
if (oiw != null ) {
JsonSerializer ser = provider.findValueSerializer(oiw.idType, property);
oiw = oiw.withSerializer(ser);
if (oiw != _objectIdWriter) {
contextual = contextual.withObjectIdWriter(oiw);
}
}
if (ignorals != null && ignorals.length != 0 ) {
contextual = contextual.withIgnorals(ignorals);
}
if (newFilterId != null ) {
contextual = contextual.withFilterId(newFilterId);
}
JsonFormat.Shape shape = null ;
if (accessor != null ) {
JsonFormat.Value format = intr.findFormat((Annotated) accessor);
if (format != null ) {
shape = format.getShape();
}
}
if (shape == null ) {
shape = _serializationShape;
}
if (shape == JsonFormat.Shape.ARRAY) {
contextual = contextual.asArraySerializer();
}
return contextual;
}
@Override
public boolean usesObjectId () {
return (_objectIdWriter != null );
}
@Override
public abstract void serialize (Object bean, JsonGenerator jgen, SerializerProvider provider)
throws IOException, JsonGenerationException ;
@Override
public void serializeWithType (Object bean, JsonGenerator jgen,
SerializerProvider provider, TypeSerializer typeSer)
throws IOException, JsonGenerationException
{
if (_objectIdWriter != null ) {
_serializeWithObjectId(bean, jgen, provider, typeSer);
return ;
}
String typeStr = (_typeId == null ) ? null :_customTypeId(bean);
if (typeStr == null ) {
typeSer.writeTypePrefixForObject(bean, jgen);
} else {
typeSer.writeCustomTypePrefixForObject(bean, jgen, typeStr);
}
if (_propertyFilterId != null ) {
serializeFieldsFiltered(bean, jgen, provider);
} else {
serializeFields(bean, jgen, provider);
}
if (typeStr == null ) {
typeSer.writeTypeSuffixForObject(bean, jgen);
} else {
typeSer.writeCustomTypeSuffixForObject(bean, jgen, typeStr);
}
}
protected final void _serializeWithObjectId (Object bean,
JsonGenerator jgen, SerializerProvider provider,
boolean startEndObject)
throws IOException, JsonGenerationException
{
final ObjectIdWriter w = _objectIdWriter;
WritableObjectId objectId = provider.findObjectId(bean, w.generator);
if (objectId.writeAsId(jgen, provider, w)) {
return ;
}
Object id = objectId.generateId(bean);
if (w.alwaysAsId) {
w.serializer.serialize(id, jgen, provider);
return ;
}
if (startEndObject) {
jgen.writeStartObject();
}
objectId.writeAsField(jgen, provider, w);
if (_propertyFilterId != null ) {
serializeFieldsFiltered(bean, jgen, provider);
} else {
serializeFields(bean, jgen, provider);
}
if (startEndObject) {
jgen.writeEndObject();
}
}
protected final void _serializeWithObjectId (Object bean,
JsonGenerator jgen, SerializerProvider provider,
TypeSerializer typeSer)
throws IOException, JsonGenerationException
{
final ObjectIdWriter w = _objectIdWriter;
WritableObjectId objectId = provider.findObjectId(bean, w.generator);
if (objectId.writeAsId(jgen, provider, w)) {
return ;
}
Object id = objectId.generateId(bean);
if (w.alwaysAsId) {
w.serializer.serialize(id, jgen, provider);
return ;
}
_serializeObjectId(bean, jgen, provider, typeSer, objectId);
}
protected void _serializeObjectId (Object bean,
JsonGenerator jgen,SerializerProvider provider,
TypeSerializer typeSer, WritableObjectId objectId)
throws IOException, JsonProcessingException, JsonGenerationException {
final ObjectIdWriter w = _objectIdWriter;
String typeStr = (_typeId == null ) ? null :_customTypeId(bean);
if (typeStr == null ) {
typeSer.writeTypePrefixForObject(bean, jgen);
} else {
typeSer.writeCustomTypePrefixForObject(bean, jgen, typeStr);
}
objectId.writeAsField(jgen, provider, w);
if (_propertyFilterId != null ) {
serializeFieldsFiltered(bean, jgen, provider);
} else {
serializeFields(bean, jgen, provider);
}
if (typeStr == null ) {
typeSer.writeTypeSuffixForObject(bean, jgen);
} else {
typeSer.writeCustomTypeSuffixForObject(bean, jgen, typeStr);
}
}
private final String _customTypeId (Object bean)
{
final Object typeId = _typeId.getValue(bean);
if (typeId == null ) {
return "" ;
}
return (typeId instanceof String) ? (String) typeId : typeId.toString();
}
protected void serializeFields (Object bean, JsonGenerator jgen, SerializerProvider provider)
throws IOException, JsonGenerationException
{
final BeanPropertyWriter[] props;
if (_filteredProps != null && provider.getActiveView() != null ) {
props = _filteredProps;
} else {
props = _props;
}
int i = 0 ;
try {
for (final int len = props.length; i < len; ++i) {
BeanPropertyWriter prop = props[i];
if (prop != null ) {
prop.serializeAsField(bean, jgen, provider);
}
}
if (_anyGetterWriter != null ) {
_anyGetterWriter.getAndSerialize(bean, jgen, provider);
}
} catch (Exception e) {
String name = (i == props.length) ? "[anySetter]" : props[i].getName();
wrapAndThrow(provider, e, bean, name);
} catch (StackOverflowError e) {
JsonMappingException mapE = new JsonMappingException("Infinite recursion (StackOverflowError)" , e);
String name = (i == props.length) ? "[anySetter]" : props[i].getName();
mapE.prependPath(new JsonMappingException.Reference(bean, name));
throw mapE;
}
}
protected void serializeFieldsFiltered (Object bean, JsonGenerator jgen,
SerializerProvider provider)
throws IOException, JsonGenerationException
{
final BeanPropertyWriter[] props;
if (_filteredProps != null && provider.getActiveView() != null ) {
props = _filteredProps;
} else {
props = _props;
}
final PropertyFilter filter = findPropertyFilter(provider, _propertyFilterId, bean);
if (filter == null ) {
serializeFields(bean, jgen, provider);
return ;
}
int i = 0 ;
try {
for (final int len = props.length; i < len; ++i) {
BeanPropertyWriter prop = props[i];
if (prop != null ) {
filter.serializeAsField(bean, jgen, provider, prop);
}
}
if (_anyGetterWriter != null ) {
_anyGetterWriter.getAndFilter(bean, jgen, provider, filter);
}
} catch (Exception e) {
String name = (i == props.length) ? "[anySetter]" : props[i].getName();
wrapAndThrow(provider, e, bean, name);
} catch (StackOverflowError e) {
JsonMappingException mapE = new JsonMappingException("Infinite recursion (StackOverflowError)" , e);
String name = (i == props.length) ? "[anySetter]" : props[i].getName();
mapE.prependPath(new JsonMappingException.Reference(bean, name));
throw mapE;
}
}
@Deprecated
@Override
public JsonNode getSchema (SerializerProvider provider, Type typeHint)
throws JsonMappingException
{
ObjectNode o = createSchemaNode("object" , true );
JsonSerializableSchema ann = _handledType.getAnnotation(JsonSerializableSchema.class);
if (ann != null ) {
String id = ann.id();
if (id != null && id.length() > 0 ) {
o.put("id" , id);
}
}
ObjectNode propertiesNode = o.objectNode();
final PropertyFilter filter;
if (_propertyFilterId != null ) {
filter = findPropertyFilter(provider, _propertyFilterId, null );
} else {
filter = null ;
}
for (int i = 0 ; i < _props.length; i++) {
BeanPropertyWriter prop = _props[i];
if (filter == null ) {
prop.depositSchemaProperty(propertiesNode, provider);
} else {
filter.depositSchemaProperty(prop, propertiesNode, provider);
}
}
o.put("properties" , propertiesNode);
return o;
}
@Override
public void acceptJsonFormatVisitor (JsonFormatVisitorWrapper visitor, JavaType typeHint)
throws JsonMappingException
{
if (visitor == null ) {
return ;
}
JsonObjectFormatVisitor objectVisitor = visitor.expectObjectFormat(typeHint);
if (objectVisitor == null ) {
return ;
}
if (_propertyFilterId != null ) {
PropertyFilter filter = findPropertyFilter(visitor.getProvider(),
_propertyFilterId, null );
for (int i = 0 ; i < _props.length; i++) {
filter.depositSchemaProperty(_props[i], objectVisitor, visitor.getProvider());
}
} else {
for (int i = 0 ; i < _props.length; i++) {
_props[i].depositSchemaProperty(objectVisitor);
}
}
}
}