private static ObjectMapper configureObjectMapper( ObjectMapper objectMapper, boolean shouldIncludeRawResponses) { if (shouldIncludeRawResponses) { SimpleModule module = new SimpleModule(); module.setDeserializerModifier(new BeanDeserializerModifier() { @Override public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config, BeanDescription beanDesc, JsonDeserializer<?> deserializer) { if (Response.class.isAssignableFrom(beanDesc.getBeanClass())) { return new RawResponseDeserializer(deserializer); } return deserializer; } }); objectMapper.registerModule(module); } objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); return objectMapper; }
@Override public void setupModule(final SetupContext context) { super.setupModule(context); context.addBeanDeserializerModifier(new BeanDeserializerModifier() { @Override public JsonDeserializer<?> modifyCollectionDeserializer(final DeserializationConfig config, final CollectionType type, final BeanDescription beanDesc, final JsonDeserializer<?> deserializer) { if (deserializer instanceof CollectionDeserializer) { return new ListDeserializer((CollectionDeserializer) deserializer); } else { return super.modifyCollectionDeserializer(config, type, beanDesc, deserializer); } } }); }
@Override public JsonDeserializer<?> modifyMapDeserializer( DeserializationConfig config, MapType type, BeanDescription beanDesc, JsonDeserializer<?> deserializer) { // statements if (isMapOfStringAndListOfStatements(type)) { return new ModifiedMapDeserializer<String, List<JacksonStatement>>(deserializer); } // labels and descriptions else if (isMapOfStringAndMonolingualTextValue(type)) { return new ModifiedMapDeserializer<String, JacksonMonolingualTextValue>(deserializer); } // sitelinks else if (isMapOfStringAndSitelink(type)) { return new ModifiedMapDeserializer<String, JacksonSiteLink>(deserializer); } // aliases and miscallaneous that does not need this workaround else { return deserializer; } }
@Bean public ObjectMapper objectMapper() { ObjectMapper mapper = new ObjectMapper(); SimpleModule module = new SimpleModule(); module.setDeserializerModifier( new BeanDeserializerModifier() { @Override public JsonDeserializer<Enum> modifyEnumDeserializer( DeserializationConfig config, final JavaType type, BeanDescription beanDesc, final JsonDeserializer<?> deserializer ) { return new JsonDeserializer<Enum>() { @Override public Enum deserialize( JsonParser jp, DeserializationContext ctxt ) throws IOException { Class<? extends Enum> rawClass = (Class<Enum<?>>) type.getRawClass(); return Enum.valueOf( rawClass, jp.getValueAsString().toUpperCase() ); } }; } } ); module.addDeserializer( Expression.class, new ExpressionDeserializer( expressionParser() ) ); module.addDeserializer( Range.class, new RangeDeserializer() ); mapper.registerModule( module ); return mapper; }
@Before public void setup() { typeResolver = mock(TypeResolver.class); configuration = Configuration.build(); instantiator = mock(HandlerInstantiator.class); doReturn(new ResourceDeserializer(Object.class, typeResolver, configuration)) .when(instantiator).deserializerInstance(any(DeserializationConfig.class), any(Annotated.class), eq(ResourceDeserializer.class)); mapper = new ObjectMapper(); mapper.setHandlerInstantiator(instantiator); mapper.registerModule(new Jackson2HalModule()); mapper.registerModule(new TestModule()); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); }
public TypeDeserializer buildTypeDeserializer(DeserializationConfig paramDeserializationConfig, JavaType paramJavaType, Collection<NamedType> paramCollection) { if (this._idType == JsonTypeInfo.Id.NONE) return null; TypeIdResolver localTypeIdResolver = idResolver(paramDeserializationConfig, paramJavaType, paramCollection, false, true); switch (1.$SwitchMap$com$fasterxml$jackson$annotation$JsonTypeInfo$As[this._includeAs.ordinal()]) { default: break; case 1: return new AsArrayTypeDeserializer(paramJavaType, localTypeIdResolver, this._typeProperty, this._typeIdVisible, this._defaultImpl); case 2: return new AsPropertyTypeDeserializer(paramJavaType, localTypeIdResolver, this._typeProperty, this._typeIdVisible, this._defaultImpl); case 3: return new AsWrapperTypeDeserializer(paramJavaType, localTypeIdResolver, this._typeProperty, this._typeIdVisible, this._defaultImpl); case 4: return new AsExternalTypeDeserializer(paramJavaType, localTypeIdResolver, this._typeProperty, this._typeIdVisible, this._defaultImpl); } throw new IllegalStateException("Do not know how to construct standard type serializer for inclusion type: " + this._includeAs); }
@Override public JsonDeserializer<?> deserializerInstance(DeserializationConfig config, Annotated annotated, Class<?> deserClass) { try{ if(MigrationDeserializer.class.equals(deserClass)) { return migrationDeserializer; } else if(None.class.equals(deserClass)) { return null; } System.out.println("DeserializationConfig " + config); System.out.println("Annotated " + annotated); System.out.println("deserClass Class<?> " + deserClass); return (JsonDeserializer<?>) deserClass.newInstance(); }catch (Exception e) { throw new IllegalStateException(e.getMessage(), e); } }
@Override public JsonDeserializer<?> findCollectionDeserializer(CollectionType type, DeserializationConfig config, BeanDescription beanDesc, TypeDeserializer elementTypeDeserializer, JsonDeserializer<?> elementDeserializer) throws JsonMappingException { Class<?> raw = type.getRawClass(); if (CollectionF.class.isAssignableFrom(raw)) { if (Option.class.isAssignableFrom(raw)) { return new OptionDeserializer(type, elementTypeDeserializer, elementDeserializer); } if (ListF.class.isAssignableFrom(raw)) { return new ListFDeserializer(type, elementTypeDeserializer, elementDeserializer); } if (SetF.class.isAssignableFrom(raw)) { return new SetFDeserializer(type, elementTypeDeserializer, elementDeserializer); } return new ListFDeserializer(type, elementTypeDeserializer, elementDeserializer); } return null; }
public ValueInstantiator _valueInstantiatorInstance(DeserializationConfig paramDeserializationConfig, Annotated paramAnnotated, Object paramObject) { if (paramObject == null) return null; if ((paramObject instanceof ValueInstantiator)) return (ValueInstantiator)paramObject; if (!(paramObject instanceof Class)) throw new IllegalStateException("AnnotationIntrospector returned key deserializer definition of type " + paramObject.getClass().getName() + "; expected type KeyDeserializer or Class<KeyDeserializer> instead"); Class localClass = (Class)paramObject; if (localClass == NoClass.class) return null; if (!ValueInstantiator.class.isAssignableFrom(localClass)) throw new IllegalStateException("AnnotationIntrospector returned Class " + localClass.getName() + "; expected Class<ValueInstantiator>"); HandlerInstantiator localHandlerInstantiator = paramDeserializationConfig.getHandlerInstantiator(); if (localHandlerInstantiator != null) { ValueInstantiator localValueInstantiator = localHandlerInstantiator.valueInstantiatorInstance(paramDeserializationConfig, paramAnnotated, localClass); if (localValueInstantiator != null) return localValueInstantiator; } return (ValueInstantiator)ClassUtil.createInstance(localClass, paramDeserializationConfig.canOverrideAccessModifiers()); }
public TypeDeserializer findTypeDeserializer(DeserializationConfig paramDeserializationConfig, JavaType paramJavaType) { AnnotatedClass localAnnotatedClass = paramDeserializationConfig.introspectClassAnnotations(paramJavaType.getRawClass()).getClassInfo(); AnnotationIntrospector localAnnotationIntrospector = paramDeserializationConfig.getAnnotationIntrospector(); Object localObject = localAnnotationIntrospector.findTypeResolver(paramDeserializationConfig, localAnnotatedClass, paramJavaType); Collection localCollection; if (localObject == null) { TypeResolverBuilder localTypeResolverBuilder = paramDeserializationConfig.getDefaultTyper(paramJavaType); localObject = localTypeResolverBuilder; localCollection = null; if (localTypeResolverBuilder == null) return null; } else { localCollection = paramDeserializationConfig.getSubtypeResolver().collectAndResolveSubtypes(localAnnotatedClass, paramDeserializationConfig, localAnnotationIntrospector); } if ((((TypeResolverBuilder)localObject).getDefaultImpl() == null) && (paramJavaType.isAbstract())) { JavaType localJavaType = mapAbstractType(paramDeserializationConfig, paramJavaType); if ((localJavaType != null) && (localJavaType.getRawClass() != paramJavaType.getRawClass())) localObject = ((TypeResolverBuilder)localObject).defaultImpl(localJavaType.getRawClass()); } return ((TypeResolverBuilder)localObject).buildTypeDeserializer(paramDeserializationConfig, paramJavaType, localCollection); }
public static JsonDeserializer<?> deserializerForCreator(DeserializationConfig paramDeserializationConfig, Class<?> paramClass, AnnotatedMethod paramAnnotatedMethod) { Class localClass = paramAnnotatedMethod.getRawParameterType(0); Object localObject; if (localClass == String.class) localObject = null; else if ((localClass == Integer.TYPE) || (localClass == Integer.class)) localObject = Integer.class; else if ((localClass == Long.TYPE) || (localClass == Long.class)) localObject = Long.class; else throw new IllegalArgumentException("Parameter #0 type for factory method (" + paramAnnotatedMethod + ") not suitable, must be java.lang.String or int/Integer/long/Long"); if (paramDeserializationConfig.canOverrideAccessModifiers()) ClassUtil.checkAndFixAccess(paramAnnotatedMethod.getMember()); return new FactoryBasedDeserializer(paramClass, paramAnnotatedMethod, (Class)localObject); }
protected final JsonDeserializer<Object> _createDeserializer(DeserializationContext paramDeserializationContext, DeserializerFactory paramDeserializerFactory, JavaType paramJavaType) { DeserializationConfig localDeserializationConfig = paramDeserializationContext.getConfig(); if ((paramJavaType.isAbstract()) || (paramJavaType.isMapLikeType()) || (paramJavaType.isCollectionLikeType())) paramJavaType = paramDeserializerFactory.mapAbstractType(localDeserializationConfig, paramJavaType); BeanDescription localBeanDescription = localDeserializationConfig.introspect(paramJavaType); JsonDeserializer localJsonDeserializer = findDeserializerFromAnnotation(paramDeserializationContext, localBeanDescription.getClassInfo()); if (localJsonDeserializer != null) return localJsonDeserializer; JavaType localJavaType1 = modifyTypeByAnnotation(paramDeserializationContext, localBeanDescription.getClassInfo(), paramJavaType); if (localJavaType1 != paramJavaType) { paramJavaType = localJavaType1; localBeanDescription = localDeserializationConfig.introspect(localJavaType1); } Class localClass = localBeanDescription.findPOJOBuilder(); if (localClass != null) return paramDeserializerFactory.createBuilderBasedDeserializer(paramDeserializationContext, paramJavaType, localBeanDescription, localClass); Converter localConverter = localBeanDescription.findDeserializationConverter(); if (localConverter == null) return _createDeserializer2(paramDeserializationContext, paramDeserializerFactory, paramJavaType, localBeanDescription); JavaType localJavaType2 = localConverter.getInputType(paramDeserializationContext.getTypeFactory()); if (!localJavaType2.hasRawClass(paramJavaType.getRawClass())) localBeanDescription = localDeserializationConfig.introspect(localJavaType2); return new StdDelegatingDeserializer(localConverter, localJavaType2, _createDeserializer2(paramDeserializationContext, paramDeserializerFactory, localJavaType2, localBeanDescription)); }
@Override public JsonDeserializer<?> modifyEnumDeserializer(DeserializationConfig config, JavaType type, BeanDescription beanDesc, JsonDeserializer<?> deserializer) { if (Arrays.asList(Quality.class, Format.class).contains(type.getRawClass())) { return new EnumDeserializer((Class<? extends Enum>) type.getRawClass()); } return super.modifyEnumDeserializer(config, type, beanDesc, deserializer); }
@Override public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config, BeanDescription beanDesc, JsonDeserializer<?> deserializer) { // We don't use the @JsonDeserialize annotation since we only want the // custom deserializer for the abstract type and not for the actual types. if (Service.class == beanDesc.getBeanClass()) { return new ServiceDeserializer(); } else if (Resource.class == beanDesc.getBeanClass()) { return new ResourceDeserializer(); } else if (Profile.class == beanDesc.getBeanClass()) { return new ProfileDeserializer((JsonDeserializer<Object>) deserializer); } return super.modifyDeserializer(config, beanDesc, deserializer); }
@Override public ValueInstantiator findValueInstantiator(DeserializationConfig config, BeanDescription beanDesc, ValueInstantiator defaultInstantiator) { if (DEFAULT_NO_DESER_CLASS_NAMES.contains(beanDesc.getClassInfo().getRawType().getName())) { throw new IllegalArgumentException("Illegal type " + beanDesc.getClassInfo().getRawType().getName() + " to deserialize: prevented for security reasons"); } return super.findValueInstantiator(config, beanDesc, defaultInstantiator); }
@Override public JsonDeserializer<Enum> modifyEnumDeserializer(DeserializationConfig config, final JavaType type, BeanDescription beanDesc, final JsonDeserializer<?> deserializer) { return new EnumJsonDeserializer(type); }
@Override public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config, BeanDescription beanDesc, JsonDeserializer<?> deserializer) { if (Artifact.class.isAssignableFrom(beanDesc.getType().getRawClass())) { return new ArtifactDeserializer(); } return super.modifyDeserializer(config, beanDesc, deserializer); }
@Override public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config, BeanDescription beanDesc, JsonDeserializer<?> deserializer) { if (BeanAnnotationProcessor.isOwlClassEntity(beanDesc.getBeanClass())) { return new JacksonJsonLdDeserializer(deserializer, beanDesc.getBeanClass()); } return deserializer; }
private void registerForDeserializationIn(ObjectMapper objectMapper) { DeserializationConfig deserializationConfig = objectMapper.getDeserializationConfig(); ClassIntrospector classIntrospector = deserializationConfig.getClassIntrospector(); if (!(classIntrospector instanceof JacksonaticClassIntrospector)) { objectMapper.setConfig(deserializationConfig.with(new JacksonaticClassIntrospector())); } JacksonaticClassIntrospector basicClassIntrospector = (JacksonaticClassIntrospector) objectMapper.getDeserializationConfig().getClassIntrospector(); basicClassIntrospector.register(this); }
/** * Gets a module wrapping this serializer as an adapter for the Jackson * ObjectMapper. * * @param mapper the object mapper for default deserializations * @return a simple module to be plugged onto Jackson ObjectMapper. */ public static SimpleModule getModule(final ObjectMapper mapper) { SimpleModule module = new SimpleModule(); module.setDeserializerModifier(new BeanDeserializerModifier() { @Override public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config, BeanDescription beanDesc, JsonDeserializer<?> deserializer) { if (beanDesc.getBeanClass().getAnnotation(JsonFlatten.class) != null) { return new FlatteningDeserializer(beanDesc.getBeanClass(), deserializer, mapper); } return deserializer; } }); return module; }
@Override public JsonDeserializer<?> findMapDeserializer(MapType type, DeserializationConfig config, BeanDescription beanDesc, KeyDeserializer keyDeserializer, TypeDeserializer elementTypeDeserializer, JsonDeserializer<?> elementDeserializer) { return forJavaType(type); }
@Override public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config, BeanDescription beanDesc, JsonDeserializer<?> deserializer) { if (Metric.class.equals(beanDesc.getBeanClass())) { return new MetricDeserializer(deserializer); } return super.modifyDeserializer(config, beanDesc, deserializer); }
@Before public void setup() { RestOperations restOperations = mock(RestOperations.class); ClientProxyFactory proxyFactory = new JavassistClientProxyFactory(); instantiator = mock(HandlerInstantiator.class); doReturn(new InlineAssociationDeserializer<>(Object.class, restOperations, proxyFactory)) .when(instantiator).deserializerInstance(any(DeserializationConfig.class), any(Annotated.class), eq(InlineAssociationDeserializer.class)); mapper = new ObjectMapper(); mapper.setHandlerInstantiator(instantiator); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); }
@Before public void setup() { HandlerInstantiator instantiator = mock(HandlerInstantiator.class); doReturn(declaredTypeResourceDeserializer()).when(instantiator).deserializerInstance( any(DeserializationConfig.class), any(Annotated.class), eq(ResourceDeserializer.class)); restTemplate = mock(RestTemplate.class); objectMapper = new DefaultObjectMapperFactory().create(instantiator); restOperations = new RestOperations(restTemplate, objectMapper); }
@Override public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config, BeanDescription beanDesc, JsonDeserializer<?> deserializer) { if (deserializer instanceof BeanDeserializerBase) { return new DataBinderDeserializer( (BeanDeserializerBase) deserializer); } // When there is no custom-deserializer implementation returns the // default jackson deserializer return deserializer; }
@Override public TypeDeserializer buildTypeDeserializer(DeserializationConfig config, JavaType baseType, Collection<NamedType> subtypes) { final TypeNameIdResolver typeNameIdResolver = TypeNameIdResolver.construct(config, baseType, subtypes, false, true); return new MetadataTypeDeserializer( baseType, typeNameIdResolver, this.defaultImpl == null ? null : config.getTypeFactory().constructSpecializedType(baseType, this.defaultImpl)); }
/** * Creates a BeanDeserializerModifier that replaces the * SettableBeanProperties in the BeanDeserializerBuilder with * ErrorReportingSettableBeanProperty instances that forward * information about errors when setting bean properties to the * given consumer. (Don't ask ... ) * * @param jsonErrorConsumer The consumer for {@link JsonError}s. * If this is <code>null</code>, then no errors will be reported. * @return The modifier */ private static BeanDeserializerModifier createErrorHandlingBeanDeserializerModifier( Consumer<? super JsonError> jsonErrorConsumer) { return new BeanDeserializerModifier() { @Override public BeanDeserializerBuilder updateBuilder( DeserializationConfig config, BeanDescription beanDesc, BeanDeserializerBuilder builder) { Iterator<SettableBeanProperty> propertiesIterator = builder.getProperties(); while (propertiesIterator.hasNext()) { SettableBeanProperty property = propertiesIterator.next(); SettableBeanProperty wrappedProperty = new ErrorReportingSettableBeanProperty( property, jsonErrorConsumer); builder.addOrReplaceProperty(wrappedProperty, true); } return builder; } }; }
/** * We have plenty of collection types to support... */ @Override public JsonDeserializer<?> findCollectionDeserializer(CollectionType type, DeserializationConfig config, BeanDescription beanDesc, TypeDeserializer elementTypeDeserializer, JsonDeserializer<?> elementDeserializer) throws JsonMappingException { return new SequenceDeserializer(type, elementTypeDeserializer, elementDeserializer); }
@Override public JsonDeserializer<?> findMapDeserializer(MapType type, DeserializationConfig config, BeanDescription beanDesc, KeyDeserializer keyDeserializer, TypeDeserializer elementTypeDeserializer, JsonDeserializer<?> elementDeserializer) throws JsonMappingException { return null; }
@Override public JsonDeserializer<?> findMapLikeDeserializer(MapLikeType type, DeserializationConfig config, BeanDescription beanDesc, KeyDeserializer keyDeserializer, TypeDeserializer elementTypeDeserializer, JsonDeserializer<?> elementDeserializer) throws JsonMappingException { return null; }
@Override public void setupModule(SetupContext context) { super.setupModule(context); context.addValueInstantiators(new ValueInstantiators.Base() { @Override public ValueInstantiator findValueInstantiator(DeserializationConfig config, BeanDescription beanDesc, ValueInstantiator defaultInstantiator) { Class<?> raw = beanDesc.getBeanClass(); // 15-May-2015, tatu: In theory not safe, but in practice we do need to do "fuzzy" matching // because we will (for now) be getting a subtype, but in future may want to downgrade // to the common base type. Even more, serializer may purposefully force use of base type. // So... in practice it really should always work, in the end. :) if (ZoneId.class.isAssignableFrom(raw)) { // let's assume we should be getting "empty" StdValueInstantiator here: if (defaultInstantiator instanceof StdValueInstantiator) { StdValueInstantiator inst = (StdValueInstantiator) defaultInstantiator; // one further complication: we need ZoneId info, not sub-class AnnotatedClass ac; if (raw == ZoneId.class) { ac = beanDesc.getClassInfo(); } else { // we don't need Annotations, so constructing directly is fine here // even if it's not generally recommended ac = AnnotatedClass.construct(ZoneId.class, null, null); } if (!inst.canCreateFromString()) { AnnotatedMethod factory = _findFactory(ac, "of", String.class); if (factory != null) { inst.configureFromStringCreator(factory); } // otherwise... should we indicate an error? } // return ZoneIdInstantiator.construct(config, beanDesc, defaultInstantiator); } } return defaultInstantiator; } }); }
@Override public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config, BeanDescription beanDesc, JsonDeserializer<?> deserializer) { if (deserializer instanceof BeanDeserializer) { BeanDeserializer beanDeserializer = (BeanDeserializer) deserializer; return new EntityBeanDeserializer(beanDeserializer, deserializer.getObjectIdReader()); } return super.modifyDeserializer(config, beanDesc, deserializer); }
@Override public List<BeanPropertyDefinition> updateProperties(DeserializationConfig config, BeanDescription beanDesc, List<BeanPropertyDefinition> propDefs) { List<BeanPropertyDefinition> definitions = super.updateProperties(config, beanDesc, propDefs); List<BeanPropertyDefinition> remove = new ArrayList<>(); List<BeanPropertyDefinition> add = new ArrayList<>(); for (BeanPropertyDefinition definition : definitions) { if (definition.hasGetter() && Collection.class.isAssignableFrom(definition.getGetter().getRawType())) { if (definition instanceof POJOPropertyBuilder) { POJOPropertyBuilder builder = (POJOPropertyBuilder) definition; builder = new POJOPropertyBuilder(builder, builder.getFullName()) { @Override public boolean hasField() { return false; // forces the getter to be used on the collection } }; remove.add(definition); add.add(builder); } } } definitions.removeAll(remove); definitions.addAll(add); return definitions; }
@Override public JsonDeserializer<?> modifyDeserializer(DeserializationConfig deserializationConfig, BeanDescription beanDescription, JsonDeserializer<?> jsonDeserializer) { if (Case.class.isAssignableFrom(beanDescription.getBeanClass())) { return new CaseDeserializer(jsonDeserializer); } return jsonDeserializer; }
@Override public JsonDeserializer<?> modifyDeserializer(DeserializationConfig deserializationConfig, BeanDescription beanDescription, JsonDeserializer<?> jsonDeserializer) { if (Result.class.isAssignableFrom(beanDescription.getBeanClass())) { return new ResultDeserializer(jsonDeserializer); } return jsonDeserializer; }
@Override public KeyDeserializer keyDeserializerInstance( DeserializationConfig deserializationConfig, Annotated annotated, Class<?> keyDeserializerClass) { return null; }