com.fasterxml.jackson.databind.jsontype.TypeIdResolver Java Examples

The following examples show how to use com.fasterxml.jackson.databind.jsontype.TypeIdResolver. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: UnknownPluginHandler.java    From milkman with MIT License 6 votes vote down vote up
@Override
public JavaType handleUnknownTypeId(DeserializationContext ctxt, JavaType baseType, String subTypeId,
		TypeIdResolver idResolver, String failureMsg) throws IOException {
	if (baseType.hasRawClass(RequestAspect.class)) {
		log.error("Unknown AspectType found: " + subTypeId + ".");
		return ReferenceType.construct(UnknownRequestAspect.class);
	}
	if (baseType.hasRawClass(OptionsObject.class)) {
		log.error("Unknown OptionsObject found: " + subTypeId + ".");
		return ReferenceType.construct(UnknownOptionsObject.class);
	}
	if (baseType.hasRawClass(RequestContainer.class)) {
		log.error("Unknown RequestContainer found: " + subTypeId + ".");
		return ReferenceType.construct(UnknownRequestContainer.class);
	}
	return null;
}
 
Example #2
Source File: AbstractTypedJacksonModule.java    From presto with Apache License 2.0 5 votes vote down vote up
public InternalTypeDeserializer(Class<T> baseClass, TypeIdResolver typeIdResolver)
{
    super(baseClass);
    this.typeDeserializer = new AsPropertyTypeDeserializer(
            TypeFactory.defaultInstance().constructType(baseClass),
            typeIdResolver,
            TYPE_PROPERTY,
            false,
            null);
}
 
Example #3
Source File: JacksonHandlerInstantiatorTest.java    From log4j2-elasticsearch with Apache License 2.0 5 votes vote down vote up
@Test
public void typeIdResolverInstanceReturnsNull() {

    // given
    VirtualProperty[] customProperties = new VirtualProperty[0];
    Log4j2Lookup valueResolver = new Log4j2Lookup(null);
    JacksonHandlerInstantiator handlerInstantiator = createTestHandlerInstantiator(customProperties, valueResolver);

    // when
    TypeIdResolver result = handlerInstantiator.typeIdResolverInstance(null, null, null);

    // then
    Assert.assertNull(result);

}
 
Example #4
Source File: ProblemHandlerTest.java    From jackson-modules-base with Apache License 2.0 5 votes vote down vote up
@Override
public JavaType handleUnknownTypeId(DeserializationContext ctxt,
        JavaType baseType, String subTypeId, TypeIdResolver idResolver,
        String failureMsg)
    throws IOException
{
    return ctxt.constructType(raw);
}
 
Example #5
Source File: ProblemHandlerTest.java    From jackson-modules-base with Apache License 2.0 5 votes vote down vote up
@Override
public JavaType handleMissingTypeId(DeserializationContext ctxt,
        JavaType baseType, TypeIdResolver idResolver,
        String failureMsg)
    throws IOException
{
    return ctxt.constructType(raw);
}
 
Example #6
Source File: AsPropertyTypeDeserializer.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @since 2.8
 */
public AsPropertyTypeDeserializer(JavaType bt, TypeIdResolver idRes,
        String typePropertyName, boolean typeIdVisible, JavaType defaultImpl,
        As inclusion)
{
    super(bt, idRes, typePropertyName, typeIdVisible, defaultImpl);
    _inclusion = inclusion;
}
 
Example #7
Source File: TypeDeserializerBase.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @since 2.8
 */
protected TypeDeserializerBase(JavaType baseType, TypeIdResolver idRes,
        String typePropertyName, boolean typeIdVisible, JavaType defaultImpl)
{
    _baseType = baseType;
    _idResolver = idRes;
    _typePropertyName = ClassUtil.nonNullString(typePropertyName);
    _typeIdVisible = typeIdVisible;
    // defaults are fine, although shouldn't need much concurrency
    _deserializers = new ConcurrentHashMap<String, JsonDeserializer<Object>>(16, 0.75f, 2);
    _defaultImpl = defaultImpl;
    _property = null;
}
 
Example #8
Source File: MapperConfig.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Method that can be called to obtain an instance of <code>TypeIdResolver</code> of
 * specified type.
 */
public TypeIdResolver typeIdResolverInstance(Annotated annotated,
        Class<? extends TypeIdResolver> resolverClass)
{
    HandlerInstantiator hi = getHandlerInstantiator();
    if (hi != null) {
        TypeIdResolver builder = hi.typeIdResolverInstance(this, annotated, resolverClass);
        if (builder != null) {
            return builder;
        }
    }
    return (TypeIdResolver) ClassUtil.createInstance(resolverClass, canOverrideAccessModifiers());
}
 
Example #9
Source File: DeserializationContext.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
     * @since 2.9
     */
    public JavaType handleMissingTypeId(JavaType baseType,
            TypeIdResolver idResolver, String extraDesc) throws IOException
    {
        LinkedNode<DeserializationProblemHandler> h = _config.getProblemHandlers();
        while (h != null) {
            // Can bail out if it's handled
            JavaType type = h.value().handleMissingTypeId(this, baseType, idResolver, extraDesc);
            if (type != null) {
                if (type.hasRawClass(Void.class)) {
                    return null;
                }
                // But ensure there's type compatibility
                if (type.isTypeOrSubTypeOf(baseType.getRawClass())) {
                    return type;
                }
                throw invalidTypeIdException(baseType, null,
                        "problem handler tried to resolve into non-subtype: "+type);
            }
            h = h.next();
        }
        // 09-Mar-2017, tatu: We may want to consider yet another feature at some
        //    point to allow returning `null`... but that seems bit risky for now
//        if (!isEnabled(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE)) {
//            return null;
//        }
        throw missingTypeIdException(baseType, extraDesc);
    }
 
Example #10
Source File: NsTypeResolverBuilder.java    From components with Apache License 2.0 5 votes vote down vote up
@Override
protected TypeIdResolver idResolver(MapperConfig<?> config, JavaType baseType,
                                    PolymorphicTypeValidator subtypeValidator, Collection<NamedType> subtypes,
                                    boolean forSer, boolean forDeser) {
    if (_idType == null) {
        throw new IllegalStateException("Can not build, 'init()' not yet called");
    }

    return new NsTypeIdResolver(baseType, config.getTypeFactory(), basicMetaData);
}
 
Example #11
Source File: OdataTypeDiscriminatorTypeResolver.java    From azure-libraries-for-java with MIT License 5 votes vote down vote up
@Override
public TypeSerializer buildTypeSerializer(SerializationConfig config, JavaType baseType, Collection<NamedType> subtypes) {
    //Copied this code from parent class, StdTypeResolverBuilder with same method name
    TypeIdResolver idRes = this.idResolver(config, baseType, subtypes, true, false);
    // have to escape "." in the middle of the "odata.type" otherwise it will be serialized to "odata": { "type":"Value"} JSON
    String escapedString = this._typeProperty.replace(".", "\\.");
    return new AsPropertyTypeSerializer(idRes, (BeanProperty) null, escapedString);
}
 
Example #12
Source File: DeserializationContext.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Method that deserializers should call if they encounter a type id
 * (for polymorphic deserialization) that cannot be resolved to an
 * actual type; usually since there is no mapping defined.
 * Default implementation will try to call {@link DeserializationProblemHandler#handleUnknownTypeId}
 * on configured handlers, if any, to allow for recovery; if recovery does not
 * succeed, will throw exception constructed with {@link #invalidTypeIdException}.
 *
 * @param baseType Base type from which resolution starts
 * @param id Type id that could not be converted
 * @param extraDesc Additional problem description to add to default exception message,
 *    if resolution fails.
 *
 * @return {@link JavaType} that id resolves to
 *
 * @throws IOException To indicate unrecoverable problem, if resolution cannot
 *    be made to work
 *
 * @since 2.8
 */
public JavaType handleUnknownTypeId(JavaType baseType, String id,
        TypeIdResolver idResolver, String extraDesc) throws IOException
{
    LinkedNode<DeserializationProblemHandler> h = _config.getProblemHandlers();
    while (h != null) {
        // Can bail out if it's handled
        JavaType type = h.value().handleUnknownTypeId(this, baseType, id, idResolver, extraDesc);
        if (type != null) {
            if (type.hasRawClass(Void.class)) {
                return null;
            }
            // But ensure there's type compatibility
            if (type.isTypeOrSubTypeOf(baseType.getRawClass())) {
                return type;
            }
            throw invalidTypeIdException(baseType, id,
                    "problem handler tried to resolve into non-subtype: "+type);
        }
        h = h.next();
    }
    // 24-May-2016, tatu: Actually we may still not want to fail quite yet
    if (!isEnabled(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE)) {
        return null;
    }
    throw invalidTypeIdException(baseType, id, extraDesc);
}
 
Example #13
Source File: AbstractTypedJacksonModule.java    From presto with Apache License 2.0 5 votes vote down vote up
protected AbstractTypedJacksonModule(
        Class<T> baseClass,
        Function<T, String> nameResolver,
        Function<String, Class<? extends T>> classResolver)
{
    super(baseClass.getSimpleName() + "Module", Version.unknownVersion());

    TypeIdResolver typeResolver = new InternalTypeResolver<>(nameResolver, classResolver);

    addSerializer(baseClass, new InternalTypeSerializer<>(baseClass, typeResolver));
    addDeserializer(baseClass, new InternalTypeDeserializer<>(baseClass, typeResolver));
}
 
Example #14
Source File: JacksonHandlerInstantiator.java    From log4j2-elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public TypeIdResolver typeIdResolverInstance(MapperConfig<?> config, Annotated annotated, Class<?> resolverClass) {
    return null;
}
 
Example #15
Source File: ServerInstanceListSerializerTest.java    From pinpoint with Apache License 2.0 4 votes vote down vote up
@Override
public TypeIdResolver typeIdResolverInstance(MapperConfig<?> config, Annotated annotated, Class<?> resolverClass) {
    return null;
}
 
Example #16
Source File: JacksonAnnotationIntrospector.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Helper method called to construct and initialize instance of {@link TypeResolverBuilder}
 * if given annotated element indicates one is needed.
 */
@SuppressWarnings("deprecation")
protected TypeResolverBuilder<?> _findTypeResolver(MapperConfig<?> config,
        Annotated ann, JavaType baseType)
{
    // First: maybe we have explicit type resolver?
    TypeResolverBuilder<?> b;
    JsonTypeInfo info = _findAnnotation(ann, JsonTypeInfo.class);
    JsonTypeResolver resAnn = _findAnnotation(ann, JsonTypeResolver.class);
    
    if (resAnn != null) {
        if (info == null) {
            return null;
        }
        /* let's not try to force access override (would need to pass
         * settings through if we did, since that's not doable on some
         * platforms)
         */
        b = config.typeResolverBuilderInstance(ann, resAnn.value());
    } else { // if not, use standard one, if indicated by annotations
        if (info == null) {
            return null;
        }
        // bit special; must return 'marker' to block use of default typing:
        if (info.use() == JsonTypeInfo.Id.NONE) {
            return _constructNoTypeResolverBuilder();
        }
        b = _constructStdTypeResolverBuilder();
    }
    // Does it define a custom type id resolver?
    JsonTypeIdResolver idResInfo = _findAnnotation(ann, JsonTypeIdResolver.class);
    TypeIdResolver idRes = (idResInfo == null) ? null
            : config.typeIdResolverInstance(ann, idResInfo.value());
    if (idRes != null) {
        idRes.init(baseType);
    }
    b = b.init(info.use(), idRes);
    /* 13-Aug-2011, tatu: One complication; external id
     *   only works for properties; so if declared for a Class, we will need
     *   to map it to "PROPERTY" instead of "EXTERNAL_PROPERTY"
     */
    JsonTypeInfo.As inclusion = info.include();
    if (inclusion == JsonTypeInfo.As.EXTERNAL_PROPERTY && (ann instanceof AnnotatedClass)) {
        inclusion = JsonTypeInfo.As.PROPERTY;
    }
    b = b.inclusion(inclusion);
    b = b.typeProperty(info.property());
    Class<?> defaultImpl = info.defaultImpl();

    // 08-Dec-2014, tatu: To deprecate `JsonTypeInfo.None` we need to use other placeholder(s);
    //   and since `java.util.Void` has other purpose (to indicate "deser as null"), we'll instead
    //   use `JsonTypeInfo.class` itself. But any annotation type will actually do, as they have no
    //   valid use (cannot instantiate as default)
    if (defaultImpl != JsonTypeInfo.None.class && !defaultImpl.isAnnotation()) {
        b = b.defaultImpl(defaultImpl);
    }
    b = b.typeIdVisibility(info.visible());
    return b;
}
 
Example #17
Source File: SpringHandlerInstantiator.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public TypeIdResolver typeIdResolverInstance(MapperConfig<?> config,
		Annotated annotated, Class<?> resolverClass) {
	return (TypeIdResolver) this.beanFactory.createBean(resolverClass);
}
 
Example #18
Source File: RestOperationsFactory.java    From bowman with Apache License 2.0 4 votes vote down vote up
@Override
public TypeIdResolver typeIdResolverInstance(MapperConfig<?> config, Annotated annotated,
		Class<?> resolverClass) {
	return (TypeIdResolver) findHandlerInstance(resolverClass);
}
 
Example #19
Source File: MetadataTypeResolver.java    From fahrschein with Apache License 2.0 4 votes vote down vote up
@Override
public MetadataTypeResolver init(JsonTypeInfo.Id idType, TypeIdResolver res) {
    return this;
}
 
Example #20
Source File: YahooTypeResolver.java    From cloudstreetmarket.com with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected TypeIdResolver idResolver(MapperConfig<?> config, JavaType baseType, Collection<NamedType> subtypes, boolean forSer, boolean forDeser) {
    return new YahooTypeIdResolver(config);
}
 
Example #21
Source File: ResolverInstantiator.java    From requery with Apache License 2.0 4 votes vote down vote up
@Override
public TypeIdResolver typeIdResolverInstance(MapperConfig<?> config, Annotated annotated, Class<?> resolverClass) {
    return null;
}
 
Example #22
Source File: SpringHandlerInstantiator.java    From cloud-config with MIT License 4 votes vote down vote up
@Override
public TypeIdResolver typeIdResolverInstance(MapperConfig<?> config,
                                             Annotated annotated, Class<?> resolverClass) {
    return (TypeIdResolver) this.beanFactory.createBean(resolverClass);
}
 
Example #23
Source File: ClassAliasTypeResolverBuilder.java    From simple-spring-memcached with MIT License 4 votes vote down vote up
@Override
protected TypeIdResolver idResolver(final MapperConfig<?> config, final JavaType baseType, final Collection<NamedType> subtypes,
        final boolean forSer, final boolean forDeser) {
    return new ClassAliasIdResolver(baseType, config.getTypeFactory(), idToClass, classToId);
}
 
Example #24
Source File: MarshallerService.java    From dawnsci with Eclipse Public License 1.0 4 votes vote down vote up
@Override
protected TypeIdResolver idResolver(MapperConfig<?> config,
JavaType baseType, Collection<NamedType> subtypes,
boolean forSer, boolean forDeser) {
	return new RegisteredClassIdResolver(baseType, config.getTypeFactory(), registry);
}
 
Example #25
Source File: AsWrapperTypeDeserializer.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * @since 2.8
 */
public AsWrapperTypeDeserializer(JavaType bt, TypeIdResolver idRes,
        String typePropertyName, boolean typeIdVisible, JavaType defaultImpl)
{
    super(bt, idRes, typePropertyName, typeIdVisible, defaultImpl);
}
 
Example #26
Source File: AbstractTypedJacksonModule.java    From presto with Apache License 2.0 4 votes vote down vote up
public InternalTypeSerializer(Class<T> baseClass, TypeIdResolver typeIdResolver)
{
    super(baseClass);
    this.typeSerializer = new AsPropertyTypeSerializer(typeIdResolver, null, TYPE_PROPERTY);
}
 
Example #27
Source File: SpringHandlerInstantiator.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public TypeIdResolver typeIdResolverInstance(MapperConfig<?> config, Annotated annotated, Class<?> implClass) {
	return (TypeIdResolver) this.beanFactory.createBean(implClass);
}
 
Example #28
Source File: SpringHandlerInstantiator.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public TypeIdResolver typeIdResolverInstance(MapperConfig<?> config, Annotated annotated, Class<?> implClass) {
	return (TypeIdResolver) this.beanFactory.createBean(implClass);
}
 
Example #29
Source File: SpringHandlerInstantiator.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public TypeIdResolver typeIdResolverInstance(MapperConfig<?> config, Annotated annotated, Class<?> implClass) {
	return (TypeIdResolver) this.beanFactory.createBean(implClass);
}
 
Example #30
Source File: AsPropertyTypeDeserializer.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * @since 2.8
 */
public AsPropertyTypeDeserializer(JavaType bt, TypeIdResolver idRes,
        String typePropertyName, boolean typeIdVisible, JavaType defaultImpl)
{
    this(bt, idRes, typePropertyName, typeIdVisible, defaultImpl, As.PROPERTY);
}