Java源码示例:com.fasterxml.jackson.databind.util.Converter

示例1
/**
 * 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;
}
 
示例2
/**
 * @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;
}
 
示例3
/**
 * 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;
}
 
示例4
/** @since 4.3 */
@Override
public Converter<?, ?> converterInstance(MapperConfig<?> config,
		Annotated annotated, Class<?> implClass) {

	return (Converter<?, ?>) this.beanFactory.createBean(implClass);
}
 
示例5
/** @since 4.3 */
@Override
public Converter<?, ?> converterInstance(MapperConfig<?> config,
		Annotated annotated, Class<?> implClass) {

	return (Converter<?, ?>) this.beanFactory.createBean(implClass);
}
 
示例6
@SuppressWarnings("unchecked")
public StdDelegatingSerializer(Converter<?,?> converter)
{
    super(Object.class);
    _converter = (Converter<Object,?>)converter;
    _delegateType = null;
    _delegateSerializer = null;
}
 
示例7
@SuppressWarnings("unchecked")
public <T> StdDelegatingSerializer(Class<T> cls, Converter<T,?> converter)
{
    super(cls, false);
    _converter = (Converter<Object,?>)converter;
    _delegateType = null;
    _delegateSerializer = null;
}
 
示例8
@SuppressWarnings("unchecked")
public StdDelegatingSerializer(Converter<Object,?> converter,
        JavaType delegateType, JsonSerializer<?> delegateSerializer)
{
    super(delegateType);
    _converter = converter;
    _delegateType = delegateType;
    _delegateSerializer = (JsonSerializer<Object>) delegateSerializer;
}
 
示例9
/**
 * 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);
}
 
示例10
/**
 * 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;
}
 
示例11
@Override
public Converter<Object,Object> findSerializationConverter()
{
    if (_annotationIntrospector == null) {
        return null;
    }
    return _createConverter(_annotationIntrospector.findSerializationConverter(_classInfo));
}
 
示例12
@Override
public Converter<Object,Object> findDeserializationConverter()
{
    if (_annotationIntrospector == null) {
        return null;
    }
    return _createConverter(_annotationIntrospector.findDeserializationConverter(_classInfo));
}
 
示例13
@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;
}
 
示例14
@SuppressWarnings("unchecked")
public StdDelegatingDeserializer(Converter<?,T> converter)
{
    super(Object.class);
    _converter = (Converter<Object,T>)converter;
    _delegateType = null;
    _delegateDeserializer = null;
}
 
示例15
@SuppressWarnings("unchecked")
public StdDelegatingDeserializer(Converter<Object,T> converter,
        JavaType delegateType, JsonDeserializer<?> delegateDeserializer)
{
    super(delegateType);
    _converter = converter;
    _delegateType = delegateType;
    _delegateDeserializer = (JsonDeserializer<Object>) delegateDeserializer;
}
 
示例16
/**
 * 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);
}
 
示例17
/**
 * 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);
}
 
示例18
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);
}
 
示例19
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);
}
 
示例20
@Override
protected @Nonnull StdDelegatingDeserializer<RangeSet<?>> withDelegate(
    Converter<Object, RangeSet<?>> converter,
    JavaType delegateType,
    JsonDeserializer<?> delegateDeserializer) {
  return new StdDelegatingDeserializer<>(converter, delegateType, delegateDeserializer);
}
 
示例21
/** @since 4.3 */
@Override
public Converter<?, ?> converterInstance(MapperConfig<?> config, Annotated annotated, Class<?> implClass) {
	return (Converter<?, ?>) this.beanFactory.createBean(implClass);
}
 
示例22
protected Converter<Object, ?> getConverter() {
    return _converter;
}
 
示例23
/**
 * 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);
}
 
示例24
/**
 * 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));
}
 
示例25
private CustomDelegatingSerializerProvider(SerializerProvider serializerProvider,
                                           Converter<?, ?> converter)
{
    super(converter);
    this.serializerProvider = serializerProvider;
}
 
示例26
@Override
protected StdDelegatingSerializer withDelegate(Converter<Object,?> converter,
                                               JavaType delegateType, JsonSerializer<?> delegateSerializer)
{
    return new StdDelegatingSerializer(converter, delegateType, delegateSerializer);
}
 
示例27
private CustomDelegatingSerializerProvider(SerializerProvider serializerProvider,
                                           Converter<?, ?> converter)
{
    super(converter);
    this.serializerProvider = serializerProvider;
}
 
示例28
@Override
protected StdDelegatingSerializer withDelegate(Converter<Object,?> converter,
                                               JavaType delegateType, JsonSerializer<?> delegateSerializer)
{
    return new StdDelegatingSerializer(converter, delegateType, delegateSerializer);
}
 
示例29
@Override
protected @Nonnull StdDelegatingSerializer withDelegate(
    Converter<Object, ?> converter, JavaType delegateType, JsonSerializer<?> delegateSerializer) {
  return this;
}
 
示例30
/**
 * Method called to construct a Converter instance used for specified class.
 * 
 * @since 2.2
 */
public Converter<?,?> converterInstance(MapperConfig<?> config,
        Annotated annotated, Class<?> implClass) {
    return null;
}