/** * Helper method that is needed to properly handle polymorphic referenced * types, such as types referenced by {@link java.util.concurrent.atomic.AtomicReference}, * or various "optional" types. * * @since 2.4 */ public TypeDeserializer findTypeDeserializer(JavaType baseType) throws JsonMappingException { BeanDescription bean = introspectClassAnnotations(baseType.getRawClass()); AnnotatedClass ac = bean.getClassInfo(); TypeResolverBuilder<?> b = getAnnotationIntrospector().findTypeResolver(this, ac, baseType); /* Ok: if there is no explicit type info handler, we may want to * use a default. If so, config object knows what to use. */ Collection<NamedType> subtypes = null; if (b == null) { b = getDefaultTyper(baseType); if (b == null) { return null; } } else { subtypes = getSubtypeResolver().collectAndResolveSubtypesByTypeId(this, ac); } return b.buildTypeDeserializer(this, baseType, subtypes); } }
/** * Method for constructing a new reader instance that is configured * to data bind into specified type. *<p> * Note that the method does NOT change state of this reader, but * rather construct and returns a newly configured instance. * * @since 2.5 */ public ObjectReader forType(Class<?> valueType) { return forType(_config.constructType(valueType)); }
public static KeyDeserializer findStringBasedKeyDeserializer(DeserializationConfig config, JavaType type) { /* We don't need full deserialization information, just need to * know creators. */ BeanDescription beanDesc = config.introspect(type); // Ok, so: can we find T(String) constructor? Constructor<?> ctor = beanDesc.findSingleArgConstructor(String.class); if (ctor != null) { if (config.canOverrideAccessModifiers()) { ClassUtil.checkAndFixAccess(ctor, config.isEnabled(MapperFeature.OVERRIDE_PUBLIC_ACCESS_MODIFIERS)); } return new StdKeyDeserializer.StringCtorKeyDeserializer(ctor); } /* or if not, "static T valueOf(String)" (or equivalent marked * with @JsonCreator annotation?) */ Method m = beanDesc.findFactoryMethod(String.class); if (m != null){ if (config.canOverrideAccessModifiers()) { ClassUtil.checkAndFixAccess(m, config.isEnabled(MapperFeature.OVERRIDE_PUBLIC_ACCESS_MODIFIERS)); } return new StdKeyDeserializer.StringFactoryKeyDeserializer(m); } // nope, no such luck... return null; }
/** * Method for changing state of an on/off mapper feature for * this mapper instance. */ public ObjectMapper configure(MapperFeature f, boolean state) { _serializationConfig = state ? _serializationConfig.with(f) : _serializationConfig.without(f); _deserializationConfig = state ? _deserializationConfig.with(f) : _deserializationConfig.without(f); return this; }
protected EnumResolver constructEnumResolver(Class<?> enumClass, DeserializationConfig config, AnnotatedMember jsonValueAccessor) { if (jsonValueAccessor != null) { if (config.canOverrideAccessModifiers()) { ClassUtil.checkAndFixAccess(jsonValueAccessor.getMember(), config.isEnabled(MapperFeature.OVERRIDE_PUBLIC_ACCESS_MODIFIERS)); } return EnumResolver.constructUnsafeUsingMethod(enumClass, jsonValueAccessor, config.getAnnotationIntrospector()); } // 14-Mar-2016, tatu: We used to check `DeserializationFeature.READ_ENUMS_USING_TO_STRING` // here, but that won't do: it must be dynamically changeable... return EnumResolver.constructUnsafe(enumClass, config.getAnnotationIntrospector()); }
null; final BeanDescription beanDesc = _mapper.getSerializationConfig().introspect(type); JsonIdentityInfo jsonIdentityInfo = AnnotationsUtils.getAnnotation(JsonIdentityInfo.class, annotatedType.getCtxAnnotations()); if (jsonIdentityInfo == null) { jsonIdentityInfo = type.getRawClass().getAnnotation(JsonIdentityInfo.class); XML xml = resolveXml(beanDesc.getClassInfo(), annotatedType.getCtxAnnotations(), resolvedSchemaAnnotation); if (xml != null) { model.xml(xml); final AnnotatedMethod jsonValueMethod = beanDesc.findJsonValueMethod(); if(jsonValueMethod != null) { AnnotatedType aType = new AnnotatedType() XML xml = resolveXml(beanDesc.getClassInfo(), annotatedType.getCtxAnnotations(), resolvedSchemaAnnotation); if (xml != null) { model.xml(xml); final BeanDescription deserBeanDesc = _mapper.getDeserializationConfig().introspect(type); List<BeanPropertyDefinition> deserProperties = deserBeanDesc.findProperties(); for (BeanPropertyDefinition prop : deserProperties) { final BeanDescription propBeanDesc = _mapper.getSerializationConfig().introspect(propType); if (property != null && !propType.isContainerType()) { if ("object".equals(property.getType())) {
throws JsonMappingException final DeserializationConfig config = ctxt.getConfig(); BeanDescription beanDesc = config.introspect(type); JsonDeserializer<?> des = findDeserializerFromAnnotation(ctxt, beanDesc.getClassInfo()); if (des != null) { return StdKeyDeserializers.constructDelegatingKeyDeserializer(config, type, des); Class<?> enumClass = type.getRawClass(); EnumResolver<?> enumRes = constructEnumResolver(enumClass, config, beanDesc.findJsonValueMethod()); for (AnnotatedMethod factory : beanDesc.getFactoryMethods()) { if (config.getAnnotationIntrospector().hasCreatorAnnotation(factory)) { int argCount = factory.getParameterCount(); if (argCount == 1) { throw new IllegalArgumentException("Parameter #0 type for factory method ("+factory+") not suitable, must be java.lang.String"); if (config.canOverrideAccessModifiers()) { ClassUtil.checkAndFixAccess(factory.getMember());
throws JsonMappingException final DeserializationConfig config = ctxt.getConfig(); JavaType keyType = type.getKeyType(); JavaType contentType = type.getContentType(); JsonDeserializer<Object> contentDeser = (JsonDeserializer<Object>) contentType.getValueHandler(); KeyDeserializer keyDes = (KeyDeserializer) keyType.getValueHandler(); TypeDeserializer contentTypeDeser = contentType.getTypeHandler(); if (fallback != null) { mapClass = fallback; type = (MapType) config.constructSpecializedType(type, mapClass); beanDesc = config.introspectForCreation(type); } else { ValueInstantiator inst = findValueInstantiator(ctxt, beanDesc); MapDeserializer md = new MapDeserializer(type, inst, keyDes, contentDeser, contentTypeDeser); md.setIgnorableProperties(config.getAnnotationIntrospector().findPropertiesToIgnore(beanDesc.getClassInfo())); deser = md; if (_factoryConfig.hasDeserializerModifiers()) { for (BeanDeserializerModifier mod : _factoryConfig.deserializerModifiers()) { deser = mod.modifyMapDeserializer(config, type, beanDesc, deser);
throws JsonMappingException final DeserializationConfig config = ctxt.getConfig(); if (type.isAbstract() || type.isMapLikeType() || type.isCollectionLikeType()) { type = factory.mapAbstractType(config, type); BeanDescription beanDesc = config.introspect(type); beanDesc.getClassInfo()); if (deser != null) { return deser; JavaType newType = modifyTypeByAnnotation(ctxt, beanDesc.getClassInfo(), type); if (newType != type) { type = newType; beanDesc = config.introspect(newType); Class<?> builder = beanDesc.findPOJOBuilder(); if (builder != null) { return (JsonDeserializer<Object>) factory.createBuilderBasedDeserializer( JavaType delegateType = conv.getInputType(ctxt.getTypeFactory()); if (!delegateType.hasRawClass(type.getRawClass())) { beanDesc = config.introspect(delegateType);
BeanDescription beanDesc, ValueInstantiator defaultInstantiator) JavaType type = beanDesc.getType(); Class<?> raw = type.getRawClass(); ac = beanDesc.getClassInfo(); } else { config.constructType(ZoneId.class), config);
throws JsonMappingException final DeserializationConfig config = ctxt.getConfig(); final Class<?> enumClass = type.getRawClass(); ValueInstantiator valueInstantiator = _constructDefaultValueInstantiator(ctxt, beanDesc); SettableBeanProperty[] creatorProps = (valueInstantiator == null) ? null : valueInstantiator.getFromObjectArguments(ctxt.getConfig()); for (AnnotatedMethod factory : beanDesc.getFactoryMethods()) { if (_hasCreatorAnnotation(ctxt, factory)) { if (factory.getParameterCount() == 0) { // [databind#960] config, beanDesc.findJsonValueAccessor()), config.isEnabled(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS)); if (_factoryConfig.hasDeserializerModifiers()) { for (BeanDeserializerModifier mod : _factoryConfig.deserializerModifiers()) { deser = mod.modifyEnumDeserializer(config, type, beanDesc, deser);
throws JsonMappingException final DeserializationConfig config = ctxt.getConfig(); Class<?> enumClass = type.getRawClass(); BeanDescription beanDesc = config.introspect(type); KeyDeserializer des = findKeyDeserializerFromAnnotation(ctxt, beanDesc.getClassInfo()); if (des != null) { return des; return StdKeyDeserializers.constructDelegatingKeyDeserializer(config, type, custom); JsonDeserializer<?> valueDesForKey = findDeserializerFromAnnotation(ctxt, beanDesc.getClassInfo()); if (valueDesForKey != null) { return StdKeyDeserializers.constructDelegatingKeyDeserializer(config, type, valueDesForKey); EnumResolver enumRes = constructEnumResolver(enumClass, config, beanDesc.findJsonValueAccessor()); for (AnnotatedMethod factory : beanDesc.getFactoryMethods()) { throw new IllegalArgumentException("Parameter #0 type for factory method ("+factory+") not suitable, must be java.lang.String"); if (config.canOverrideAccessModifiers()) { ClassUtil.checkAndFixAccess(factory.getMember(), ctxt.isEnabled(MapperFeature.OVERRIDE_PUBLIC_ACCESS_MODIFIERS));
_context.reportBadDefinition(_beanDesc.getType(), String.format("Builder class %s does not have build method (name: '%s')", _beanDesc.getBeanClass().getName(), expBuildMethodName)); Class<?> rawValueType = valueType.getRawClass(); if ((rawBuildType != rawValueType) && !rawBuildType.isAssignableFrom(rawValueType) && !rawValueType.isAssignableFrom(rawBuildType)) { _context.reportBadDefinition(_beanDesc.getType(), String.format("Build method '%s' has wrong return type (%s), not compatible with POJO type (%s)", _buildMethod.getFullName(), rawBuildType.getName(), valueType.getRawClass().getName())); _fixAccess(props); BeanPropertyMap propertyMap = BeanPropertyMap.construct(props, _config.isEnabled(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES), _collectAliases(props)); propertyMap.assignIndexes(); boolean anyViews = !_config.isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION);
throws JsonMappingException final DeserializationConfig config = ctxt.getConfig(); if (type.isThrowable()) { return buildThrowableDeserializer(ctxt, type, beanDesc); if (type.isAbstract() && !type.isPrimitive() && !type.isEnumType()) { beanDesc = config.introspect(concreteType); return buildBeanDeserializer(ctxt, concreteType, beanDesc);
@Override public JavaType findTypeMapping(DeserializationConfig config, JavaType type) { // this is the main mapping base, so let's Class<?> src = type.getRawClass(); Class<?> dst = _mappings.get(new ClassKey(src)); if (dst == null) { return null; } // 09-Aug-2015, tatu: Instead of direct call via JavaType, better use TypeFactory return config.getTypeFactory().constructSpecializedType(type, dst); }
/** * @deprecated since 2.5 Use {@link #forType(Class)} instead */ @Deprecated public ObjectReader withType(java.lang.reflect.Type valueType) { return forType(_config.getTypeFactory().constructType(valueType)); }
private AnnotatedType resolveDeserializableType(Annotated accessor, AnnotatedType realType, JavaType baseType, ObjectMapper objectMapper) { AnnotationIntrospector introspector = objectMapper.getDeserializationConfig().getAnnotationIntrospector(); try { objectMapper.getDeserializationContext().getFactory().mapAbstractType(objectMapper.getDeserializationConfig(), objectMapper.constructType(Map.class)); JavaType refined = introspector.refineDeserializationType(objectMapper.getDeserializationConfig(), accessor, baseType); Class<?> raw = ClassUtils.getRawType(realType.getType()); if (!refined.getRawClass().equals(raw)) { if (GenericTypeReflector.isSuperType(realType.getType(), refined.getRawClass())) { AnnotatedType candidate = GenericTypeReflector.getExactSubType(realType, refined.getRawClass()); if (!ClassUtils.isMissingTypeParameters(candidate.getType())) { return candidate; } } return GenericTypeReflector.updateAnnotations(TypeUtils.toJavaType(refined), realType.getAnnotations()); } } catch (JsonMappingException e) { /*no-op*/ } catch (Exception e) { log.warn("Failed to determine the deserializable type for " + GenericTypeReflector.getTypeName(realType.getType()) + " due to an exception", e); } return realType; }
throw InvalidDefinitionException.from(ctxt.getParser(), ClassUtil.exceptionMessage(e), builderDesc, null); final DeserializationConfig config = ctxt.getConfig(); BeanDeserializerBuilder builder = constructBeanDeserializerBuilder(ctxt, builderDesc); builder.setValueInstantiator(valueInstantiator); addInjectables(ctxt, builderDesc, builder); JsonPOJOBuilder.Value builderConfig = builderDesc.findPOJOBuilderConfig(); final String buildMethodName = (builderConfig == null) ? JsonPOJOBuilder.DEFAULT_BUILD_METHOD : builderConfig.buildMethodName; AnnotatedMethod buildMethod = builderDesc.findMethod(buildMethodName, null); if (buildMethod != null) { // note: can't yet throw error; may be given build method if (config.canOverrideAccessModifiers()) { ClassUtil.checkAndFixAccess(buildMethod.getMember(), config.isEnabled(MapperFeature.OVERRIDE_PUBLIC_ACCESS_MODIFIERS)); if (_factoryConfig.hasDeserializerModifiers()) { for (BeanDeserializerModifier mod : _factoryConfig.deserializerModifiers()) { builder = mod.updateBuilder(config, builderDesc, builder); if (_factoryConfig.hasDeserializerModifiers()) { for (BeanDeserializerModifier mod : _factoryConfig.deserializerModifiers()) { deserializer = mod.modifyDeserializer(config, builderDesc, deserializer);
assertTrue(getDeserializerFactoryConfig(mapper).hasDeserializers()); assertNull(serializers.findSerializer(null, SimpleType.construct(Number.class), null)); Deserializers deserializers = getDeserializerFactoryConfig(mapper).deserializers().iterator().next(); assertSame(deserializer, deserializers.findBeanDeserializer(SimpleType.construct(Date.class), null, null)); assertSame(annotationIntrospector, mapper.getSerializationConfig().getAnnotationIntrospector()); assertSame(annotationIntrospector, mapper.getDeserializationConfig().getAnnotationIntrospector()); assertTrue(mapper.getSerializationConfig().isEnabled(SerializationFeature.FAIL_ON_EMPTY_BEANS)); assertTrue(mapper.getDeserializationConfig().isEnabled(DeserializationFeature.UNWRAP_ROOT_VALUE)); assertTrue(mapper.getFactory().isEnabled(JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER)); assertTrue(mapper.getFactory().isEnabled(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS)); assertFalse(mapper.getSerializationConfig().isEnabled(MapperFeature.AUTO_DETECT_GETTERS)); assertFalse(mapper.getDeserializationConfig().isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION)); assertFalse(mapper.getDeserializationConfig().isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)); assertFalse(mapper.getDeserializationConfig().isEnabled(MapperFeature.AUTO_DETECT_FIELDS)); assertFalse(mapper.getFactory().isEnabled(JsonParser.Feature.AUTO_CLOSE_SOURCE)); assertFalse(mapper.getFactory().isEnabled(JsonGenerator.Feature.QUOTE_FIELD_NAMES));