/** * Creates a TypeResolverBuilder that performs whitelisting. * @return a TypeResolverBuilder that performs whitelisting. */ private static TypeResolverBuilder<? extends TypeResolverBuilder> createWhitelistedDefaultTyping() { TypeResolverBuilder<? extends TypeResolverBuilder> result = new WhitelistTypeResolverBuilder(ObjectMapper.DefaultTyping.NON_FINAL); result = result.init(JsonTypeInfo.Id.CLASS, null); result = result.inclusion(JsonTypeInfo.As.PROPERTY); return result; }
idRes.init(baseType); b = b.init(info.use(), idRes); inclusion = JsonTypeInfo.As.PROPERTY; b = b.inclusion(inclusion); b = b.typeProperty(info.property()); Class<?> defaultImpl = info.defaultImpl(); b = b.defaultImpl(defaultImpl); b = b.typeIdVisibility(info.visible()); return b;
throws JsonMappingException BeanDescription bean = config.introspectClassAnnotations(baseType.getRawClass()); AnnotatedClass ac = bean.getClassInfo(); AnnotationIntrospector ai = config.getAnnotationIntrospector(); TypeResolverBuilder<?> b = ai.findTypeResolver(config, ac, baseType); b = config.getDefaultTyper(baseType); if (b == null) { return null; if ((b.getDefaultImpl() == null) && baseType.isAbstract()) { JavaType defaultType = mapAbstractType(config, baseType); if ((defaultType != null) && !defaultType.hasRawClass(baseType.getRawClass())) { b = b.defaultImpl(defaultType.getRawClass()); return b.buildTypeDeserializer(config, baseType, subtypes); } catch (IllegalArgumentException e0) { InvalidDefinitionException e = InvalidDefinitionException.from((JsonParser) null,
/** * Method for enabling automatic inclusion of type information -- needed * for proper deserialization of polymorphic types (unless types * have been annotated with {@link com.fasterxml.jackson.annotation.JsonTypeInfo}) -- * using "As.PROPERTY" inclusion mechanism and specified property name * to use for inclusion (default being "@class" since default type information * always uses class name as type identifier) *<p> * NOTE: use of Default Typing can be a potential security risk if incoming * content comes from untrusted sources, and it is recommended that this * is either not done, or, if enabled, use {@link #setDefaultTyping} * passing a custom {@link TypeResolverBuilder} implementation that white-lists * legal types to use. */ public ObjectMapper enableDefaultTypingAsProperty(DefaultTyping applicability, String propertyName) { TypeResolverBuilder<?> typer = new DefaultTypeResolverBuilder(applicability); // we'll always use full class name, when using defaulting typer = typer.init(JsonTypeInfo.Id.CLASS, null); typer = typer.inclusion(JsonTypeInfo.As.PROPERTY); typer = typer.typeProperty(propertyName); return setDefaultTyping(typer); }
/** * Method called to construct a type serializer for values with given declared * base type. This is called for values other than those of bean property * types. */ @Override public TypeSerializer createTypeSerializer(SerializationConfig config, JavaType baseType) { BeanDescription bean = config.introspectClassAnnotations(baseType.getRawClass()); AnnotatedClass ac = bean.getClassInfo(); AnnotationIntrospector ai = config.getAnnotationIntrospector(); TypeResolverBuilder<?> b = ai.findTypeResolver(config, 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 = config.getDefaultTyper(baseType); } else { subtypes = config.getSubtypeResolver().collectAndResolveSubtypesByClass(config, ac); } if (b == null) { return null; } // 10-Jun-2015, tatu: Since not created for Bean Property, no need for post-processing // wrt EXTERNAL_PROPERTY return b.buildTypeSerializer(config, baseType, subtypes); }
/** * 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 called to create a type information serializer for values of given * container property * if one is needed. If not needed (no polymorphic handling configured), should * return null. * * @param containerType Declared type of the container to use as the base type for type information serializer * * @return Type serializer to use for property value contents, if one is needed; null if not. */ public TypeSerializer findPropertyContentTypeSerializer(JavaType containerType, SerializationConfig config, AnnotatedMember accessor) throws JsonMappingException { JavaType contentType = containerType.getContentType(); AnnotationIntrospector ai = config.getAnnotationIntrospector(); TypeResolverBuilder<?> b = ai.findPropertyContentTypeResolver(config, accessor, containerType); TypeSerializer typeSer; // Defaulting: if no annotations on member, check value class if (b == null) { typeSer = createTypeSerializer(config, contentType); } else { Collection<NamedType> subtypes = config.getSubtypeResolver().collectAndResolveSubtypesByClass(config, accessor, contentType); typeSer = b.buildTypeSerializer(config, contentType, subtypes); } return typeSer; }
/** * Method called to find and create a type information deserializer for values of * given container (list, array, map) property, if one is needed. * If not needed (no polymorphic handling configured for property), should return null. *<p> * Note that this method is only called for container bean properties, * and not for values in container types or root values (or non-container properties) * * @param containerType Type of property; must be a container type * @param propertyEntity Field or method that contains container property */ public TypeDeserializer findPropertyContentTypeDeserializer(DeserializationConfig config, JavaType containerType, AnnotatedMember propertyEntity) throws JsonMappingException { AnnotationIntrospector ai = config.getAnnotationIntrospector(); TypeResolverBuilder<?> b = ai.findPropertyContentTypeResolver(config, propertyEntity, containerType); JavaType contentType = containerType.getContentType(); // Defaulting: if no annotations on member, check class if (b == null) { return findTypeDeserializer(config, contentType); } // but if annotations found, may need to resolve subtypes: Collection<NamedType> subtypes = config.getSubtypeResolver().collectAndResolveSubtypesByTypeId( config, propertyEntity, contentType); return b.buildTypeDeserializer(config, contentType, subtypes); }
public JsonMessageSerializer() { actorReferenceModule = new ActorReferenceModule(DefaultDescriptorFactory.get()); mapper.registerModule(actorReferenceModule); mapper.setVisibility(mapper.getSerializationConfig().getDefaultVisibilityChecker() .withFieldVisibility(JsonAutoDetect.Visibility.ANY) .withGetterVisibility(JsonAutoDetect.Visibility.NONE) .withIsGetterVisibility(JsonAutoDetect.Visibility.NONE) .withSetterVisibility(JsonAutoDetect.Visibility.NONE) .withCreatorVisibility(JsonAutoDetect.Visibility.NONE)); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); TypeResolverBuilder<?> typer = new ClassIdTypeResolverBuilder(ObjectMapper.DefaultTyping.JAVA_LANG_OBJECT); typer = typer.init(JsonTypeInfo.Id.NAME, null); typer = typer.inclusion(JsonTypeInfo.As.PROPERTY); mapper.setDefaultTyping(typer); }
/** * Method called to create a type information serializer for values of given * non-container property * if one is needed. If not needed (no polymorphic handling configured), should * return null. * * @param baseType Declared type to use as the base type for type information serializer * * @return Type serializer to use for property values, if one is needed; null if not. */ public TypeSerializer findPropertyTypeSerializer(JavaType baseType, SerializationConfig config, AnnotatedMember accessor) throws JsonMappingException { AnnotationIntrospector ai = config.getAnnotationIntrospector(); TypeResolverBuilder<?> b = ai.findPropertyTypeResolver(config, accessor, baseType); TypeSerializer typeSer; // Defaulting: if no annotations on member, check value class if (b == null) { typeSer = createTypeSerializer(config, baseType); } else { Collection<NamedType> subtypes = config.getSubtypeResolver().collectAndResolveSubtypesByClass( config, accessor, baseType); typeSer = b.buildTypeSerializer(config, baseType, subtypes); } return typeSer; }
AnnotationIntrospector introspectorPair = AnnotationIntrospector.pair(primary, secondary); objectMapper.setConfig( objectMapper.getSerializationConfig() .with(introspectorPair) .with(SerializationFeature.INDENT_OUTPUT)); .with(introspectorPair) .with(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY) .without(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)); customSerializationMapper.setConfig(customSerializationMapper.getDeserializationConfig().with(introspectorPair)); customSerializationMapper.setConfig(customSerializationMapper.getSerializationConfig().with(introspectorPair).with(SerializationFeature.INDENT_OUTPUT)); typer = typer.init(JsonTypeInfo.Id.CLASS, null); typer = typer.inclusion(JsonTypeInfo.As.WRAPPER_OBJECT); customObjectMapper.setDefaultTyping(typer); typer2 = typer2.init(JsonTypeInfo.Id.CLASS, null); typer2 = typer2.inclusion(JsonTypeInfo.As.WRAPPER_OBJECT); deserializeObjectMapper.setDefaultTyping(typer2);
/** * Method called to create a type information deserializer for values of * given non-container property, if one is needed. * If not needed (no polymorphic handling configured for property), should return null. *<p> * Note that this method is only called for non-container bean properties, * and not for values in container types or root values (or container properties) * * @param baseType Declared base type of the value to deserializer (actual * deserializer type will be this type or its subtype) * * @return Type deserializer to use for given base type, if one is needed; null if not. */ public TypeDeserializer findPropertyTypeDeserializer(DeserializationConfig config, JavaType baseType, AnnotatedMember annotated) throws JsonMappingException { AnnotationIntrospector ai = config.getAnnotationIntrospector(); TypeResolverBuilder<?> b = ai.findPropertyTypeResolver(config, annotated, baseType); // Defaulting: if no annotations on member, check value class if (b == null) { return findTypeDeserializer(config, baseType); } // but if annotations found, may need to resolve subtypes: Collection<NamedType> subtypes = config.getSubtypeResolver().collectAndResolveSubtypesByTypeId( config, annotated, baseType); return b.buildTypeDeserializer(config, baseType, subtypes); }
public JsonMessageSerializerModule() { objectMapper = new ObjectMapper(); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); objectMapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false); objectMapper.configure(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY, true); // objectMapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true); objectMapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, true); objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); objectMapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true); objectMapper.enableDefaultTypingAsProperty(DefaultTyping.JAVA_LANG_OBJECT, "_typeName"); TypeResolverBuilder<?> joynrTypeResolverBuilder = objectMapper.getSerializationConfig() .getDefaultTyper(SimpleType.construct(Object.class)); SimpleModule module = new SimpleModule("NonTypedModule", new Version(1, 0, 0, "", "", "")); module.addSerializer(new JoynrEnumSerializer()); module.addSerializer(new JoynrListSerializer()); module.addSerializer(new JoynrArraySerializer()); TypeDeserializer typeDeserializer = joynrTypeResolverBuilder.buildTypeDeserializer(objectMapper.getDeserializationConfig(), SimpleType.construct(Object.class), null); module.addDeserializer(Request.class, new RequestDeserializer(objectMapper)); module.addDeserializer(OneWayRequest.class, new OneWayRequestDeserializer(objectMapper)); module.addDeserializer(Object.class, new JoynrUntypedObjectDeserializer(typeDeserializer)); module.setMixInAnnotation(Throwable.class, ThrowableMixIn.class); objectMapper.registerModule(module); }
/** * Method called to construct a type serializer for values with given declared * base type. This is called for values other than those of bean property * types. */ @Override public TypeSerializer createTypeSerializer(SerializationConfig config, JavaType baseType) { BeanDescription bean = config.introspectClassAnnotations(baseType.getRawClass()); AnnotatedClass ac = bean.getClassInfo(); AnnotationIntrospector ai = config.getAnnotationIntrospector(); TypeResolverBuilder<?> b = ai.findTypeResolver(config, 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 = config.getDefaultTyper(baseType); } else { subtypes = config.getSubtypeResolver().collectAndResolveSubtypes(ac, config, ai); } if (b == null) { return null; } return b.buildTypeSerializer(config, baseType, subtypes); }
/** * 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 called to create a type information serializer for values of given * container property * if one is needed. If not needed (no polymorphic handling configured), should * return null. * * @param containerType Declared type of the container to use as the base type for type information serializer * * @return Type serializer to use for property value contents, if one is needed; null if not. */ public TypeSerializer findPropertyContentTypeSerializer(JavaType containerType, SerializationConfig config, AnnotatedMember accessor) throws JsonMappingException { JavaType contentType = containerType.getContentType(); AnnotationIntrospector ai = config.getAnnotationIntrospector(); TypeResolverBuilder<?> b = ai.findPropertyContentTypeResolver(config, accessor, containerType); // Defaulting: if no annotations on member, check value class if (b == null) { return createTypeSerializer(config, contentType); } Collection<NamedType> subtypes = config.getSubtypeResolver().collectAndResolveSubtypes(accessor, config, ai); return b.buildTypeSerializer(config, contentType, subtypes); }
/** * Method called to find and create a type information deserializer for values of * given container (list, array, map) property, if one is needed. * If not needed (no polymorphic handling configured for property), should return null. *<p> * Note that this method is only called for container bean properties, * and not for values in container types or root values (or non-container properties) * * @param containerType Type of property; must be a container type * @param propertyEntity Field or method that contains container property */ public TypeDeserializer findPropertyContentTypeDeserializer(DeserializationConfig config, JavaType containerType, AnnotatedMember propertyEntity) throws JsonMappingException { AnnotationIntrospector ai = config.getAnnotationIntrospector(); TypeResolverBuilder<?> b = ai.findPropertyContentTypeResolver(config, propertyEntity, containerType); JavaType contentType = containerType.getContentType(); // Defaulting: if no annotations on member, check class if (b == null) { return findTypeDeserializer(config, contentType); } // but if annotations found, may need to resolve subtypes: Collection<NamedType> subtypes = config.getSubtypeResolver().collectAndResolveSubtypesByTypeId( config, propertyEntity, contentType); return b.buildTypeDeserializer(config, contentType, subtypes); }
public JsonMessageSerializer() { actorReferenceModule = new ActorReferenceModule(DefaultDescriptorFactory.get()); mapper.registerModule(actorReferenceModule); mapper.setVisibility(mapper.getSerializationConfig().getDefaultVisibilityChecker() .withFieldVisibility(JsonAutoDetect.Visibility.ANY) .withGetterVisibility(JsonAutoDetect.Visibility.NONE) .withIsGetterVisibility(JsonAutoDetect.Visibility.NONE) .withSetterVisibility(JsonAutoDetect.Visibility.NONE) .withCreatorVisibility(JsonAutoDetect.Visibility.NONE)); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); TypeResolverBuilder<?> typer = new ClassIdTypeResolverBuilder(ObjectMapper.DefaultTyping.JAVA_LANG_OBJECT); typer = typer.init(JsonTypeInfo.Id.NAME, null); typer = typer.inclusion(JsonTypeInfo.As.PROPERTY); mapper.setDefaultTyping(typer); }
/** * Method for enabling automatic inclusion of type information -- needed * for proper deserialization of polymorphic types (unless types * have been annotated with {@link com.fasterxml.jackson.annotation.JsonTypeInfo}) -- * using "As.PROPERTY" inclusion mechanism and specified property name * to use for inclusion (default being "@class" since default type information * always uses class name as type identifier) */ public ObjectMapper enableDefaultTypingAsProperty(DefaultTyping applicability, String propertyName) { TypeResolverBuilder<?> typer = new DefaultTypeResolverBuilder(applicability); // we'll always use full class name, when using defaulting typer = typer.init(JsonTypeInfo.Id.CLASS, null); typer = typer.inclusion(JsonTypeInfo.As.PROPERTY); typer = typer.typeProperty(propertyName); return setDefaultTyping(typer); }