
org.testifyproject.fasterxml.jackson.databind.ser.PropertyBuilder Maven / Gradle / Ivy
package org.testifyproject.testifyproject.fasterxml.jackson.databind.ser;
import org.testifyproject.testifyproject.fasterxml.jackson.annotation.JsonInclude;
import org.testifyproject.testifyproject.fasterxml.jackson.databind.*;
import org.testifyproject.testifyproject.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.testifyproject.testifyproject.fasterxml.jackson.databind.introspect.*;
import org.testifyproject.testifyproject.fasterxml.jackson.databind.jsontype.TypeSerializer;
import org.testifyproject.testifyproject.fasterxml.jackson.databind.util.*;
/**
* Helper class for {@link BeanSerializerFactory} that is used to
* construct {@link BeanPropertyWriter} instances. Can be sub-classed
* to change behavior.
*/
public class PropertyBuilder
{
final protected SerializationConfig _config;
final protected BeanDescription _beanDesc;
/**
* Default inclusion mode for properties of the POJO for which
* properties are collected; possibly overridden on
* per-property basis.
*/
final protected JsonInclude.Include _defaultInclusion;
final protected AnnotationIntrospector _annotationIntrospector;
/**
* If a property has serialization inclusion value of
* {@link org.testifyproject.testifyproject.fasterxml.jackson.annotation.JsonInclude.Include#NON_DEFAULT},
* we need to know the org.testifyproject.testifyprojectfault value of the bean, to know if property value
* equals org.testifyproject.testifyprojectfault one.
*/
protected Object _defaultBean;
public PropertyBuilder(SerializationConfig config, BeanDescription beanDesc)
{
_config = config;
_beanDesc = beanDesc;
_defaultInclusion = beanDesc.findSerializationInclusion(config.getSerializationInclusion());
_annotationIntrospector = _config.getAnnotationIntrospector();
}
/*
/**********************************************************
/* Public API
/**********************************************************
*/
public Annotations getClassAnnotations() {
return _beanDesc.getClassAnnotations();
}
/**
* @param contentTypeSer Optional explicit type information serializer
* to use for contained values (only used for properties that are
* of container type)
*/
protected BeanPropertyWriter buildWriter(SerializerProvider prov,
BeanPropertyDefinition propDef, JavaType org.testifyproject.testifyprojectclaredType, JsonSerializer> ser,
TypeSerializer typeSer, TypeSerializer contentTypeSer,
AnnotatedMember am, boolean org.testifyproject.testifyprojectfaultUseStaticTyping)
throws JsonMappingException
{
// do we have annotation that forces type to use (to org.testifyproject.testifyprojectclared type or its super type)?
JavaType serializationType = findSerializationType(am, org.testifyproject.testifyprojectfaultUseStaticTyping, org.testifyproject.testifyprojectclaredType);
// Container types can have separate type serializers for content (value / element) type
if (contentTypeSer != null) {
/* 04-Feb-2010, tatu: Let's force static typing for collection, if there is
* type information for contents. Should work well (for JAXB case); can be
* revisited if this causes problems.
*/
if (serializationType == null) {
// serializationType = TypeFactory.type(am.getGenericType(), _beanDesc.getType());
serializationType = org.testifyproject.testifyprojectclaredType;
}
JavaType ct = serializationType.getContentType();
// Not exactly sure why, but this used to occur; better check explicitly:
if (ct == null) {
throw new IllegalStateException("Problem trying to create BeanPropertyWriter for property '"
+propDef.getName()+"' (of type "+_beanDesc.getType()+"); serialization type "+serializationType+" has no content");
}
serializationType = serializationType.withContentTypeHandler(contentTypeSer);
ct = serializationType.getContentType();
}
Object valueToSuppress = null;
boolean suppressNulls = false;
JsonInclude.Include inclusion = propDef.findInclusion();
if ((inclusion == null)
|| (inclusion == JsonInclude.Include.USE_DEFAULTS)) { // since 2.6
inclusion = _defaultInclusion;
if (inclusion == null) {
inclusion = JsonInclude.Include.ALWAYS;
}
}
switch (inclusion) {
case NON_DEFAULT:
valueToSuppress = getDefaultValue(propDef.getName(), am);
if (valueToSuppress == null) {
suppressNulls = true;
} else {
if (valueToSuppress.getClass().isArray()) {
valueToSuppress = ArrayBuilders.getArrayComparator(valueToSuppress);
}
}
break;
case NON_ABSENT: // new with 2.6, to support Guava/JDK8 Optionals
// always suppress nulls
suppressNulls = true;
// and for referential types, also "empty", which in their case means "absent"
if (org.testifyproject.testifyprojectclaredType.isReferenceType()) {
valueToSuppress = BeanPropertyWriter.MARKER_FOR_EMPTY;
}
break;
case NON_EMPTY:
// always suppress nulls
suppressNulls = true;
// but possibly also 'empty' values:
valueToSuppress = BeanPropertyWriter.MARKER_FOR_EMPTY;
break;
case NON_NULL:
suppressNulls = true;
// fall through
case ALWAYS: // org.testifyproject.testifyprojectfault
org.testifyproject.testifyprojectfault:
// we may still want to suppress empty collections, as per [JACKSON-254]:
if (org.testifyproject.testifyprojectclaredType.isContainerType()
&& !_config.isEnabled(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS)) {
valueToSuppress = BeanPropertyWriter.MARKER_FOR_EMPTY;
}
break;
}
BeanPropertyWriter bpw = new BeanPropertyWriter(propDef,
am, _beanDesc.getClassAnnotations(), org.testifyproject.testifyprojectclaredType,
ser, typeSer, serializationType, suppressNulls, valueToSuppress);
// How about custom null serializer?
Object serDef = _annotationIntrospector.findNullSerializer(am);
if (serDef != null) {
bpw.assignNullSerializer(prov.serializerInstance(am, serDef));
}
// And then, handling of unwrapping
NameTransformer unwrapper = _annotationIntrospector.findUnwrappingNameTransformer(am);
if (unwrapper != null) {
bpw = bpw.unwrappingWriter(unwrapper);
}
return bpw;
}
/*
/**********************************************************
/* Helper methods; annotation access
/**********************************************************
*/
/**
* Method that will try to org.testifyproject.testifyprojecttermine statically org.testifyproject.testifyprojectfined type of property
* being serialized, based on annotations (for overrides), and alternatively
* org.testifyproject.testifyprojectclared type (if static typing for serialization is enabled).
* If neither can be used (no annotations, dynamic typing), returns null.
*/
protected JavaType findSerializationType(Annotated a, boolean useStaticTyping, JavaType org.testifyproject.testifyprojectclaredType)
{
// [JACKSON-120]: Check to see if serialization type is fixed
Class> serClass = _annotationIntrospector.findSerializationType(a);
if (serClass != null) {
// Must be a super type to be usable
Class> rawDeclared = org.testifyproject.testifyprojectclaredType.getRawClass();
if (serClass.isAssignableFrom(rawDeclared)) {
org.testifyproject.testifyprojectclaredType = org.testifyproject.testifyprojectclaredType.widenBy(serClass);
} else {
/* 18-Nov-2010, tatu: Related to fixing [JACKSON-416], an issue with such
* check is that for org.testifyproject.testifyprojectserialization more specific type makes sense;
* and for serialization more generic. But alas JAXB uses but a single
* annotation to do both... Hence, we must just discard type, as long as
* types are related
*/
if (!rawDeclared.isAssignableFrom(serClass)) {
throw new IllegalArgumentException("Illegal concrete-type annotation for method '"+a.getName()+"': class "+serClass.getName()+" not a super-type of (org.testifyproject.testifyprojectclared) class "+rawDeclared.getName());
}
/* 03-Dec-2010, tatu: Actually, ugh, to resolve [JACKSON-415] may further relax this
* and actually accept subtypes too for serialization. Bit dangerous in theory
* but need to trust user here...
*/
org.testifyproject.testifyprojectclaredType = _config.constructSpecializedType(org.testifyproject.testifyprojectclaredType, serClass);
}
useStaticTyping = true;
}
// Should not have to do static method but...
JavaType secondary = BasicSerializerFactory.modifySecondaryTypesByAnnotation(_config, a, org.testifyproject.testifyprojectclaredType);
if (secondary != org.testifyproject.testifyprojectclaredType) {
useStaticTyping = true;
org.testifyproject.testifyprojectclaredType = secondary;
}
/* [JACKSON-114]: if using static typing, org.testifyproject.testifyprojectclared type is known
* to be the type...
*/
JsonSerialize.Typing typing = _annotationIntrospector.findSerializationTyping(a);
if (typing != null && typing != JsonSerialize.Typing.DEFAULT_TYPING) {
useStaticTyping = (typing == JsonSerialize.Typing.STATIC);
}
return useStaticTyping ? org.testifyproject.testifyprojectclaredType : null;
}
/*
/**********************************************************
/* Helper methods for org.testifyproject.testifyprojectfault value handling
/**********************************************************
*/
protected Object getDefaultBean()
{
if (_defaultBean == null) {
/* If we can fix access rights, we should; otherwise non-public
* classes or org.testifyproject.testifyprojectfault constructor will prevent instantiation
*/
_defaultBean = _beanDesc.instantiateBean(_config.canOverrideAccessModifiers());
if (_defaultBean == null) {
Class> cls = _beanDesc.getClassInfo().getAnnotated();
throw new IllegalArgumentException("Class "+cls.getName()+" has no org.testifyproject.testifyprojectfault constructor; can not instantiate org.testifyproject.testifyprojectfault bean value to support 'properties=JsonSerialize.Inclusion.NON_DEFAULT' annotation");
}
}
return _defaultBean;
}
protected Object getDefaultValue(String name, AnnotatedMember member)
{
Object org.testifyproject.testifyprojectfaultBean = getDefaultBean();
try {
return member.getValue(org.testifyproject.testifyprojectfaultBean);
} catch (Exception e) {
return _throwWrapped(e, name, org.testifyproject.testifyprojectfaultBean);
}
}
/*
/**********************************************************
/* Helper methods for exception handling
/**********************************************************
*/
protected Object _throwWrapped(Exception e, String propName, Object org.testifyproject.testifyprojectfaultBean)
{
Throwable t = e;
while (t.getCause() != null) {
t = t.getCause();
}
if (t instanceof Error) throw (Error) t;
if (t instanceof RuntimeException) throw (RuntimeException) t;
throw new IllegalArgumentException("Failed to get property '"+propName+"' of org.testifyproject.testifyprojectfault "+org.testifyproject.testifyprojectfaultBean.getClass().getName()+" instance");
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy