use of com.fasterxml.jackson.databind.jsontype.TypeDeserializer in project jackson-databind by FasterXML.
the class BeanDeserializerFactory method constructAnySetter.
/**
* Method called to construct fallback {@link SettableAnyProperty}
* for handling unknown bean properties, given a method that
* has been designated as such setter.
*
* @param mutator Either 2-argument method (setter, with key and value), or Field
* that contains Map; either way accessor used for passing "any values"
*/
@SuppressWarnings("unchecked")
protected SettableAnyProperty constructAnySetter(DeserializationContext ctxt, BeanDescription beanDesc, AnnotatedMember mutator) throws JsonMappingException {
//find the java type based on the annotated setter method or setter field
BeanProperty prop;
JavaType keyType;
JavaType valueType;
if (mutator instanceof AnnotatedMethod) {
// we know it's a 2-arg method, second arg is the value
AnnotatedMethod am = (AnnotatedMethod) mutator;
keyType = am.getParameterType(0);
valueType = am.getParameterType(1);
valueType = resolveMemberAndTypeAnnotations(ctxt, mutator, valueType);
prop = new BeanProperty.Std(PropertyName.construct(mutator.getName()), valueType, null, mutator, PropertyMetadata.STD_OPTIONAL);
} else if (mutator instanceof AnnotatedField) {
AnnotatedField af = (AnnotatedField) mutator;
// get the type from the content type of the map object
JavaType mapType = af.getType();
mapType = resolveMemberAndTypeAnnotations(ctxt, mutator, mapType);
keyType = mapType.getKeyType();
valueType = mapType.getContentType();
prop = new BeanProperty.Std(PropertyName.construct(mutator.getName()), mapType, null, mutator, PropertyMetadata.STD_OPTIONAL);
} else {
return ctxt.reportBadDefinition(beanDesc.getType(), String.format("Unrecognized mutator type for any setter: %s", mutator.getClass()));
}
// First: see if there are explicitly specified
// and then possible direct deserializer override on accessor
KeyDeserializer keyDeser = findKeyDeserializerFromAnnotation(ctxt, mutator);
if (keyDeser == null) {
keyDeser = keyType.getValueHandler();
}
if (keyDeser == null) {
keyDeser = ctxt.findKeyDeserializer(keyType, prop);
} else {
if (keyDeser instanceof ContextualKeyDeserializer) {
keyDeser = ((ContextualKeyDeserializer) keyDeser).createContextual(ctxt, prop);
}
}
JsonDeserializer<Object> deser = findContentDeserializerFromAnnotation(ctxt, mutator);
if (deser == null) {
deser = valueType.getValueHandler();
}
if (deser != null) {
// As per [databind#462] need to ensure we contextualize deserializer before passing it on
deser = (JsonDeserializer<Object>) ctxt.handlePrimaryContextualization(deser, prop, valueType);
}
TypeDeserializer typeDeser = valueType.getTypeHandler();
return new SettableAnyProperty(prop, mutator, valueType, keyDeser, deser, typeDeser);
}
use of com.fasterxml.jackson.databind.jsontype.TypeDeserializer in project jackson-databind by FasterXML.
the class BasicDeserializerFactory method createReferenceDeserializer.
@Override
public JsonDeserializer<?> createReferenceDeserializer(DeserializationContext ctxt, ReferenceType type, BeanDescription beanDesc) throws JsonMappingException {
JavaType contentType = type.getContentType();
// Very first thing: is deserializer hard-coded for elements?
JsonDeserializer<Object> contentDeser = contentType.getValueHandler();
final DeserializationConfig config = ctxt.getConfig();
// Then optional type info: if type has been resolved, we may already know type deserializer:
TypeDeserializer contentTypeDeser = contentType.getTypeHandler();
if (contentTypeDeser == null) {
// or if not, may be able to find:
contentTypeDeser = findTypeDeserializer(config, contentType);
}
JsonDeserializer<?> deser = _findCustomReferenceDeserializer(type, config, beanDesc, contentTypeDeser, contentDeser);
if (deser == null) {
// Just one referential type as of JDK 1.7 / Java 7: AtomicReference (Java 8 adds Optional)
if (type.isTypeOrSubTypeOf(AtomicReference.class)) {
Class<?> rawType = type.getRawClass();
ValueInstantiator inst;
if (rawType == AtomicReference.class) {
inst = null;
} else {
/* 23-Oct-2016, tatu: Note that subtypes are probably not supportable
* without either forcing merging (to avoid having to create instance)
* or something else...
*/
inst = findValueInstantiator(ctxt, beanDesc);
}
return new AtomicReferenceDeserializer(type, inst, contentTypeDeser, contentDeser);
}
}
if (deser != null) {
// and then post-process
if (_factoryConfig.hasDeserializerModifiers()) {
for (BeanDeserializerModifier mod : _factoryConfig.deserializerModifiers()) {
deser = mod.modifyReferenceDeserializer(config, type, beanDesc, deser);
}
}
}
return deser;
}
use of com.fasterxml.jackson.databind.jsontype.TypeDeserializer in project jackson-databind by FasterXML.
the class BasicDeserializerFactory method createCollectionLikeDeserializer.
// Copied almost verbatim from "createCollectionDeserializer" -- should try to share more code
@Override
public JsonDeserializer<?> createCollectionLikeDeserializer(DeserializationContext ctxt, CollectionLikeType type, final BeanDescription beanDesc) throws JsonMappingException {
JavaType contentType = type.getContentType();
// Very first thing: is deserializer hard-coded for elements?
JsonDeserializer<Object> contentDeser = contentType.getValueHandler();
final DeserializationConfig config = ctxt.getConfig();
// Then optional type info (1.5): if type has been resolved, we may already know type deserializer:
TypeDeserializer contentTypeDeser = contentType.getTypeHandler();
// but if not, may still be possible to find:
if (contentTypeDeser == null) {
contentTypeDeser = findTypeDeserializer(config, contentType);
}
JsonDeserializer<?> deser = _findCustomCollectionLikeDeserializer(type, config, beanDesc, contentTypeDeser, contentDeser);
if (deser != null) {
// and then new with 2.2: ability to post-process it too (Issue#120)
if (_factoryConfig.hasDeserializerModifiers()) {
for (BeanDeserializerModifier mod : _factoryConfig.deserializerModifiers()) {
deser = mod.modifyCollectionLikeDeserializer(config, type, beanDesc, deser);
}
}
}
return deser;
}
use of com.fasterxml.jackson.databind.jsontype.TypeDeserializer in project jackson-databind by FasterXML.
the class BasicDeserializerFactory method constructCreatorProperty.
/**
* Method that will construct a property object that represents
* a logical property passed via Creator (constructor or static
* factory method)
*/
protected SettableBeanProperty constructCreatorProperty(DeserializationContext ctxt, BeanDescription beanDesc, PropertyName name, int index, AnnotatedParameter param, Object injectableValueId) throws JsonMappingException {
final DeserializationConfig config = ctxt.getConfig();
final AnnotationIntrospector intr = ctxt.getAnnotationIntrospector();
PropertyMetadata metadata;
{
if (intr == null) {
metadata = PropertyMetadata.STD_REQUIRED_OR_OPTIONAL;
} else {
Boolean b = intr.hasRequiredMarker(param);
String desc = intr.findPropertyDescription(param);
Integer idx = intr.findPropertyIndex(param);
String def = intr.findPropertyDefaultValue(param);
metadata = PropertyMetadata.construct(b, desc, idx, def);
}
}
JavaType type = resolveMemberAndTypeAnnotations(ctxt, param, param.getType());
BeanProperty.Std property = new BeanProperty.Std(name, type, intr.findWrapperName(param), param, metadata);
// Type deserializer: either comes from property (and already resolved)
TypeDeserializer typeDeser = (TypeDeserializer) type.getTypeHandler();
// or if not, based on type being referenced:
if (typeDeser == null) {
typeDeser = findTypeDeserializer(config, type);
}
// Note: contextualization of typeDeser _should_ occur in constructor of CreatorProperty
// so it is not called directly here
SettableBeanProperty prop = new CreatorProperty(name, type, property.getWrapperName(), typeDeser, beanDesc.getClassAnnotations(), param, index, injectableValueId, metadata);
JsonDeserializer<?> deser = findDeserializerFromAnnotation(ctxt, param);
if (deser == null) {
deser = type.getValueHandler();
}
if (deser != null) {
// As per [databind#462] need to ensure we contextualize deserializer before passing it on
deser = ctxt.handlePrimaryContextualization(deser, prop, type);
prop = prop.withValueDeserializer(deser);
}
return prop;
}
use of com.fasterxml.jackson.databind.jsontype.TypeDeserializer in project jackson-databind by FasterXML.
the class MapDeserializer method _readAndBindStringKeyMap.
/**
* Optimized method used when keys can be deserialized as plain old
* {@link java.lang.String}s, and there is no custom deserialized
* specified.
*/
protected final void _readAndBindStringKeyMap(JsonParser p, DeserializationContext ctxt, Map<Object, Object> result) throws IOException {
final JsonDeserializer<Object> valueDes = _valueDeserializer;
final TypeDeserializer typeDeser = _valueTypeDeserializer;
MapReferringAccumulator referringAccumulator = null;
boolean useObjectId = (valueDes.getObjectIdReader() != null);
if (useObjectId) {
referringAccumulator = new MapReferringAccumulator(_containerType.getContentType().getRawClass(), result);
}
String key;
if (p.isExpectedStartObjectToken()) {
key = p.nextFieldName();
} else {
JsonToken t = p.getCurrentToken();
if (t == JsonToken.END_OBJECT) {
return;
}
if (t != JsonToken.FIELD_NAME) {
ctxt.reportWrongTokenException(this, JsonToken.FIELD_NAME, null);
}
key = p.getCurrentName();
}
for (; key != null; key = p.nextFieldName()) {
JsonToken t = p.nextToken();
if (_ignorableProperties != null && _ignorableProperties.contains(key)) {
p.skipChildren();
continue;
}
try {
// Note: must handle null explicitly here; value deserializers won't
Object value;
if (t == JsonToken.VALUE_NULL) {
if (_skipNullValues) {
continue;
}
value = _nullProvider.getNullValue(ctxt);
} else if (typeDeser == null) {
value = valueDes.deserialize(p, ctxt);
} else {
value = valueDes.deserializeWithType(p, ctxt, typeDeser);
}
if (useObjectId) {
referringAccumulator.put(key, value);
} else {
result.put(key, value);
}
} catch (UnresolvedForwardReference reference) {
handleUnresolvedReference(ctxt, referringAccumulator, key, reference);
} catch (Exception e) {
wrapAndThrow(e, result, key);
}
}
// 23-Mar-2015, tatu: TODO: verify we got END_OBJECT?
}
Aggregations