com.fasterxml.jackson.databind.util.Converter Java Examples

The following examples show how to use com.fasterxml.jackson.databind.util.Converter. 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: BeanSerializerBase.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Helper method that can be used to see if specified property is annotated
 * to indicate use of a converter for property value (in case of container types,
 * it is container type itself, not key or content type).
 * 
 * @since 2.2
 */
protected JsonSerializer<Object> findConvertingSerializer(SerializerProvider provider,
        BeanPropertyWriter prop)
    throws JsonMappingException
{
    final AnnotationIntrospector intr = provider.getAnnotationIntrospector();
    if (intr != null) {
        AnnotatedMember m = prop.getMember();
        if (m != null) {
            Object convDef = intr.findSerializationConverter(m);
            if (convDef != null) {
                Converter<Object,Object> conv = provider.converterInstance(prop.getMember(), convDef);
                JavaType delegateType = conv.getOutputType(provider.getTypeFactory());
                // [databind#731]: Should skip if nominally java.lang.Object
                JsonSerializer<?> ser = delegateType.isJavaLangObject() ? null
                        : provider.findValueSerializer(delegateType, prop);
                return new StdDelegatingSerializer(conv, delegateType, ser);
            }
        }
    }
    return null;
}
 
Example #2
Source File: StdSerializer.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @deprecated Since 2.9 use {link {@link #findContextualConvertingSerializer} instead
 */
@Deprecated
protected JsonSerializer<?> findConvertingContentSerializer(SerializerProvider provider,
        BeanProperty prop, JsonSerializer<?> existingSerializer)
    throws JsonMappingException
{
    final AnnotationIntrospector intr = provider.getAnnotationIntrospector();
    if (_neitherNull(intr, prop)) {
        AnnotatedMember m = prop.getMember();
        if (m != null) {
            Object convDef = intr.findSerializationContentConverter(m);
            if (convDef != null) {
                Converter<Object,Object> conv = provider.converterInstance(prop.getMember(), convDef);
                JavaType delegateType = conv.getOutputType(provider.getTypeFactory());
                // [databind#731]: Should skip if nominally java.lang.Object
                if ((existingSerializer == null) && !delegateType.isJavaLangObject()) {
                    existingSerializer = provider.findValueSerializer(delegateType);
                }
                return new StdDelegatingSerializer(conv, delegateType, existingSerializer);
            }
        }
    }
    return existingSerializer;
}
 
Example #3
Source File: StdDeserializer.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Helper method that can be used to see if specified property has annotation
 * indicating that a converter is to be used for contained values (contents
 * of structured types; array/List/Map values)
 * 
 * @param existingDeserializer (optional) configured content
 *    serializer if one already exists.
 * 
 * @since 2.2
 */
protected JsonDeserializer<?> findConvertingContentDeserializer(DeserializationContext ctxt,
        BeanProperty prop, JsonDeserializer<?> existingDeserializer)
    throws JsonMappingException
{
    final AnnotationIntrospector intr = ctxt.getAnnotationIntrospector();
    if (_neitherNull(intr, prop)) {
        AnnotatedMember member = prop.getMember();
        if (member != null) {
            Object convDef = intr.findDeserializationContentConverter(member);
            if (convDef != null) {
                Converter<Object,Object> conv = ctxt.converterInstance(prop.getMember(), convDef);
                JavaType delegateType = conv.getInputType(ctxt.getTypeFactory());
                if (existingDeserializer == null) {
                    existingDeserializer = ctxt.findContextualValueDeserializer(delegateType, prop);
                }
                return new StdDelegatingDeserializer<Object>(conv, delegateType, existingDeserializer);
            }
        }
    }
    return existingDeserializer;
}
 
Example #4
Source File: SpringHandlerInstantiator.java    From java-technology-stack with MIT License 5 votes vote down vote up
/** @since 4.3 */
@Override
public Converter<?, ?> converterInstance(MapperConfig<?> config,
		Annotated annotated, Class<?> implClass) {

	return (Converter<?, ?>) this.beanFactory.createBean(implClass);
}
 
Example #5
Source File: RangeSetDeserializer.java    From batfish with Apache License 2.0 5 votes vote down vote up
@Override
protected @Nonnull StdDelegatingDeserializer<RangeSet<?>> withDelegate(
    Converter<Object, RangeSet<?>> converter,
    JavaType delegateType,
    JsonDeserializer<?> delegateDeserializer) {
  return new StdDelegatingDeserializer<>(converter, delegateType, delegateDeserializer);
}
 
Example #6
Source File: JaxbAnnotationIntrospector.java    From jackson-modules-base with Apache License 2.0 5 votes vote down vote up
protected Converter<Object,Object> _converter(MapperConfig<?> config,
        XmlAdapter<?,?> adapter, boolean forSerialization)
{
    TypeFactory tf = config.getTypeFactory();
    JavaType adapterType = tf.constructType(adapter.getClass());
    JavaType[] pt = tf.findTypeParameters(adapterType, XmlAdapter.class);
    // Order of type parameters for Converter is reverse between serializer, deserializer,
    // whereas JAXB just uses single ordering
    if (forSerialization) {
        return new AdapterConverter(adapter, pt[1], pt[0], forSerialization);
    }
    return new AdapterConverter(adapter, pt[0], pt[1], forSerialization);
}
 
Example #7
Source File: DeserializerCache.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected Converter<Object,Object> findConverter(DeserializationContext ctxt,
        Annotated a)
    throws JsonMappingException
{
    Object convDef = ctxt.getAnnotationIntrospector().findDeserializationConverter(a);
    if (convDef == null) {
        return null;
    }
    return ctxt.converterInstance(a, convDef);
}
 
Example #8
Source File: DeserializerCache.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Helper method that will check whether given annotated entity (usually class,
 * but may also be a property accessor) indicates that a {@link Converter} is to
 * be used; and if so, to construct and return suitable serializer for it.
 * If not, will simply return given serializer as is.
 */
protected JsonDeserializer<Object> findConvertingDeserializer(DeserializationContext ctxt,
        Annotated a, JsonDeserializer<Object> deser)
    throws JsonMappingException
{
    Converter<Object,Object> conv = findConverter(ctxt, a);
    if (conv == null) {
        return deser;
    }
    JavaType delegateType = conv.getInputType(ctxt.getTypeFactory());
    return (JsonDeserializer<Object>) new StdDelegatingDeserializer<Object>(conv, delegateType, deser);
}
 
Example #9
Source File: StdDelegatingDeserializer.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Method used for creating resolved contextual instances. Must be
 * overridden when sub-classing.
 */
protected StdDelegatingDeserializer<T> withDelegate(Converter<Object,T> converter,
        JavaType delegateType, JsonDeserializer<?> delegateDeserializer)
{
    ClassUtil.verifyMustOverride(StdDelegatingDeserializer.class, this, "withDelegate");
    return new StdDelegatingDeserializer<T>(converter, delegateType, delegateDeserializer);
}
 
Example #10
Source File: StdDelegatingDeserializer.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public StdDelegatingDeserializer(Converter<Object,T> converter,
        JavaType delegateType, JsonDeserializer<?> delegateDeserializer)
{
    super(delegateType);
    _converter = converter;
    _delegateType = delegateType;
    _delegateDeserializer = (JsonDeserializer<Object>) delegateDeserializer;
}
 
Example #11
Source File: StdDelegatingDeserializer.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public StdDelegatingDeserializer(Converter<?,T> converter)
{
    super(Object.class);
    _converter = (Converter<Object,T>)converter;
    _delegateType = null;
    _delegateDeserializer = null;
}
 
Example #12
Source File: BasicBeanDescription.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
protected Converter<Object,Object> _createConverter(Object converterDef)
{
    if (converterDef == null) {
        return null;
    }
    if (converterDef instanceof Converter<?,?>) {
        return (Converter<Object,Object>) converterDef;
    }
    if (!(converterDef instanceof Class)) {
        throw new IllegalStateException("AnnotationIntrospector returned Converter definition of type "
                +converterDef.getClass().getName()+"; expected type Converter or Class<Converter> instead");
    }
    Class<?> converterClass = (Class<?>)converterDef;
    // there are some known "no class" markers to consider too:
    if (converterClass == Converter.None.class || ClassUtil.isBogusClass(converterClass)) {
        return null;
    }
    if (!Converter.class.isAssignableFrom(converterClass)) {
        throw new IllegalStateException("AnnotationIntrospector returned Class "
                +converterClass.getName()+"; expected Class<Converter>");
    }
    HandlerInstantiator hi = _config.getHandlerInstantiator();
    Converter<?,?> conv = (hi == null) ? null : hi.converterInstance(_config, _classInfo, converterClass);
    if (conv == null) {
        conv = (Converter<?,?>) ClassUtil.createInstance(converterClass,
                _config.canOverrideAccessModifiers());
    }
    return (Converter<Object,Object>) conv;
}
 
Example #13
Source File: BasicBeanDescription.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Converter<Object,Object> findDeserializationConverter()
{
    if (_annotationIntrospector == null) {
        return null;
    }
    return _createConverter(_annotationIntrospector.findDeserializationConverter(_classInfo));
}
 
Example #14
Source File: DatabindContext.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Helper method to use to construct a {@link Converter}, given a definition
 * that may be either actual converter instance, or Class for instantiating one.
 * 
 * @since 2.2
 */
@SuppressWarnings("unchecked")
public Converter<Object,Object> converterInstance(Annotated annotated,
        Object converterDef)
    throws JsonMappingException
{
    if (converterDef == null) {
        return null;
    }
    if (converterDef instanceof Converter<?,?>) {
        return (Converter<Object,Object>) converterDef;
    }
    if (!(converterDef instanceof Class)) {
        throw new IllegalStateException("AnnotationIntrospector returned Converter definition of type "
                +converterDef.getClass().getName()+"; expected type Converter or Class<Converter> instead");
    }
    Class<?> converterClass = (Class<?>)converterDef;
    // there are some known "no class" markers to consider too:
    if (converterClass == Converter.None.class || ClassUtil.isBogusClass(converterClass)) {
        return null;
    }
    if (!Converter.class.isAssignableFrom(converterClass)) {
        throw new IllegalStateException("AnnotationIntrospector returned Class "
                +converterClass.getName()+"; expected Class<Converter>");
    }
    final MapperConfig<?> config = getConfig();
    HandlerInstantiator hi = config.getHandlerInstantiator();
    Converter<?,?> conv = (hi == null) ? null : hi.converterInstance(config, annotated, converterClass);
    if (conv == null) {
        conv = (Converter<?,?>) ClassUtil.createInstance(converterClass,
                config.canOverrideAccessModifiers());
    }
    return (Converter<Object,Object>) conv;
}
 
Example #15
Source File: BasicBeanDescription.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Converter<Object,Object> findSerializationConverter()
{
    if (_annotationIntrospector == null) {
        return null;
    }
    return _createConverter(_annotationIntrospector.findSerializationConverter(_classInfo));
}
 
Example #16
Source File: SpringHandlerInstantiator.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/** @since 4.3 */
@Override
public Converter<?, ?> converterInstance(MapperConfig<?> config,
		Annotated annotated, Class<?> implClass) {

	return (Converter<?, ?>) this.beanFactory.createBean(implClass);
}
 
Example #17
Source File: StdDelegatingSerializer.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Method used for creating resolved contextual instances. Must be
 * overridden when sub-classing.
 */
protected StdDelegatingSerializer withDelegate(Converter<Object,?> converter,
        JavaType delegateType, JsonSerializer<?> delegateSerializer)
{
    ClassUtil.verifyMustOverride(StdDelegatingSerializer.class, this, "withDelegate");
    return new StdDelegatingSerializer(converter, delegateType, delegateSerializer);
}
 
Example #18
Source File: StdDelegatingSerializer.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public StdDelegatingSerializer(Converter<Object,?> converter,
        JavaType delegateType, JsonSerializer<?> delegateSerializer)
{
    super(delegateType);
    _converter = converter;
    _delegateType = delegateType;
    _delegateSerializer = (JsonSerializer<Object>) delegateSerializer;
}
 
Example #19
Source File: StdDelegatingSerializer.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public <T> StdDelegatingSerializer(Class<T> cls, Converter<T,?> converter)
{
    super(cls, false);
    _converter = (Converter<Object,?>)converter;
    _delegateType = null;
    _delegateSerializer = null;
}
 
Example #20
Source File: StdDelegatingSerializer.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public StdDelegatingSerializer(Converter<?,?> converter)
{
    super(Object.class);
    _converter = (Converter<Object,?>)converter;
    _delegateType = null;
    _delegateSerializer = null;
}
 
Example #21
Source File: RangeSetSerializer.java    From batfish with Apache License 2.0 4 votes vote down vote up
@Override
protected @Nonnull StdDelegatingSerializer withDelegate(
    Converter<Object, ?> converter, JavaType delegateType, JsonSerializer<?> delegateSerializer) {
  return this;
}
 
Example #22
Source File: SpringHandlerInstantiator.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/** @since 4.3 */
@Override
public Converter<?, ?> converterInstance(MapperConfig<?> config, Annotated annotated, Class<?> implClass) {
	return (Converter<?, ?>) this.beanFactory.createBean(implClass);
}
 
Example #23
Source File: DeterministicObjectMapper.java    From StubbornJava with MIT License 4 votes vote down vote up
@Override
protected StdDelegatingSerializer withDelegate(Converter<Object,?> converter,
                                               JavaType delegateType, JsonSerializer<?> delegateSerializer)
{
    return new StdDelegatingSerializer(converter, delegateType, delegateSerializer);
}
 
Example #24
Source File: DeterministicObjectMapper.java    From StubbornJava with MIT License 4 votes vote down vote up
private CustomDelegatingSerializerProvider(SerializerProvider serializerProvider,
                                           Converter<?, ?> converter)
{
    super(converter);
    this.serializerProvider = serializerProvider;
}
 
Example #25
Source File: DeterministicObjectMapper.java    From StubbornJava with MIT License 4 votes vote down vote up
@Override
protected StdDelegatingSerializer withDelegate(Converter<Object,?> converter,
                                               JavaType delegateType, JsonSerializer<?> delegateSerializer)
{
    return new StdDelegatingSerializer(converter, delegateType, delegateSerializer);
}
 
Example #26
Source File: DeterministicObjectMapper.java    From StubbornJava with MIT License 4 votes vote down vote up
private CustomDelegatingSerializerProvider(SerializerProvider serializerProvider,
                                           Converter<?, ?> converter)
{
    super(converter);
    this.serializerProvider = serializerProvider;
}
 
Example #27
Source File: DeserializerCache.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Method that does the heavy lifting of checking for per-type annotations,
 * find out full type, and figure out which actual factory method
 * to call.
 */
@SuppressWarnings("unchecked")
protected JsonDeserializer<Object> _createDeserializer(DeserializationContext ctxt,
        DeserializerFactory factory, JavaType type)
    throws JsonMappingException
{
    final DeserializationConfig config = ctxt.getConfig();

    // First things first: do we need to use abstract type mapping?
    if (type.isAbstract() || type.isMapLikeType() || type.isCollectionLikeType()) {
        type = factory.mapAbstractType(config, type);
    }
    BeanDescription beanDesc = config.introspect(type);
    // Then: does type define explicit deserializer to use, with annotation(s)?
    JsonDeserializer<Object> deser = findDeserializerFromAnnotation(ctxt,
            beanDesc.getClassInfo());
    if (deser != null) {
        return deser;
    }

    // If not, may have further type-modification annotations to check:
    JavaType newType = modifyTypeByAnnotation(ctxt, beanDesc.getClassInfo(), type);
    if (newType != type) {
        type = newType;
        beanDesc = config.introspect(newType);
    }

    // We may also have a Builder type to consider...
    Class<?> builder = beanDesc.findPOJOBuilder();
    if (builder != null) {
        return (JsonDeserializer<Object>) factory.createBuilderBasedDeserializer(
        		ctxt, type, beanDesc, builder);
    }

    // Or perhaps a Converter?
    Converter<Object,Object> conv = beanDesc.findDeserializationConverter();
    if (conv == null) { // nope, just construct in normal way
        return (JsonDeserializer<Object>) _createDeserializer2(ctxt, factory, type, beanDesc);
    }
    // otherwise need to do bit of introspection
    JavaType delegateType = conv.getInputType(ctxt.getTypeFactory());
    // One more twist, as per [databind#288]; probably need to get new BeanDesc
    if (!delegateType.hasRawClass(type.getRawClass())) {
        beanDesc = config.introspect(delegateType);
    }
    return new StdDelegatingDeserializer<Object>(conv, delegateType,
            _createDeserializer2(ctxt, factory, delegateType, beanDesc));
}
 
Example #28
Source File: StdDelegatingSerializer.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
protected Converter<Object, ?> getConverter() {
    return _converter;
}
 
Example #29
Source File: BeanSerializerFactory.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Main serializer constructor method. We will have to be careful
 * with respect to ordering of various method calls: essentially
 * we want to reliably figure out which classes are standard types,
 * and which are beans. The problem is that some bean Classes may
 * implement standard interfaces (say, {@link java.lang.Iterable}.
 *<p>
 * Note: sub-classes may choose to complete replace implementation,
 * if they want to alter priority of serializer lookups.
 */
@Override
@SuppressWarnings("unchecked")
public JsonSerializer<Object> createSerializer(SerializerProvider prov,
        JavaType origType)
    throws JsonMappingException
{
    // Very first thing, let's check if there is explicit serializer annotation:
    final SerializationConfig config = prov.getConfig();
    BeanDescription beanDesc = config.introspect(origType);
    JsonSerializer<?> ser = findSerializerFromAnnotation(prov, beanDesc.getClassInfo());
    if (ser != null) {
        return (JsonSerializer<Object>) ser;
    }
    boolean staticTyping;
    // Next: we may have annotations that further indicate actual type to use (a super type)
    final AnnotationIntrospector intr = config.getAnnotationIntrospector();
    JavaType type;

    if (intr == null) {
        type = origType;
    } else {
        try {
            type = intr.refineSerializationType(config, beanDesc.getClassInfo(), origType);
        } catch (JsonMappingException e) {
            return prov.reportBadTypeDefinition(beanDesc, e.getMessage());
        }
    }
    if (type == origType) { // no changes, won't force static typing
        staticTyping = false;
    } else { // changes; assume static typing; plus, need to re-introspect if class differs
        staticTyping = true;
        if (!type.hasRawClass(origType.getRawClass())) {
            beanDesc = config.introspect(type);
        }
    }
    // Slight detour: do we have a Converter to consider?
    Converter<Object,Object> conv = beanDesc.findSerializationConverter();
    if (conv == null) { // no, simple
        return (JsonSerializer<Object>) _createSerializer2(prov, type, beanDesc, staticTyping);
    }
    JavaType delegateType = conv.getOutputType(prov.getTypeFactory());
    
    // One more twist, as per [databind#288]; probably need to get new BeanDesc
    if (!delegateType.hasRawClass(type.getRawClass())) {
        beanDesc = config.introspect(delegateType);
        // [#359]: explicitly check (again) for @JsonSerializer...
        ser = findSerializerFromAnnotation(prov, beanDesc.getClassInfo());
    }
    // [databind#731]: Should skip if nominally java.lang.Object
    if (ser == null && !delegateType.isJavaLangObject()) {
        ser = _createSerializer2(prov, delegateType, beanDesc, true);
    }
    return new StdDelegatingSerializer(conv, delegateType, ser);
}
 
Example #30
Source File: BeanDescription.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Method for finding {@link Converter} used for serializing instances
 * of this class.
 * 
 * @since 2.2
 */
public abstract Converter<Object,Object> findDeserializationConverter();