com.fasterxml.jackson.databind.deser.ValueInstantiator Java Examples

The following examples show how to use com.fasterxml.jackson.databind.deser.ValueInstantiator. 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: PairInstantiators.java    From jackson-datatypes-collections with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unused") // Used from PairInstantiatorsPopulator
static <P> void purePrimitiveInstantiator(
        Class<P> pairClass, Class<?> one, Class<?> two,
        BiFunction<Object, Object, P> factory
) {
    PURE_PRIMITIVE_INSTANTIATORS.put(pairClass, new ValueInstantiator.Base(pairClass) {
        @Override
        public boolean canCreateFromObjectWith() {
            return true;
        }

        @Override
        public SettableBeanProperty[] getFromObjectArguments(DeserializationConfig config) {
            JavaType oneType = config.constructType(one);
            JavaType twoType = config.constructType(two);
            return makeProperties(config, oneType, twoType);
        }

        @Override
        public Object createFromObjectWith(DeserializationContext ctxt, Object[] args) {
            return factory.apply(args[0], args[1]);
        }
    });
}
 
Example #2
Source File: PairInstantiators.java    From jackson-datatypes-collections with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unused") // Used from PairInstantiatorsPopulator
static <P> ValueInstantiator objectPrimitiveInstantiator(
        JavaType inputType, Class<?> two,
        BiFunction<Object, Object, P> factory
) {
    return new ValueInstantiator.Base(inputType) {
        @Override
        public boolean canCreateFromObjectWith() {
            return true;
        }

        @Override
        public SettableBeanProperty[] getFromObjectArguments(DeserializationConfig config) {
            JavaType oneType = inputType.containedType(0);
            JavaType twoType = config.constructType(two);
            return makeProperties(config, oneType, twoType);
        }

        @Override
        public Object createFromObjectWith(DeserializationContext ctxt, Object[] args) {
            return factory.apply(args[0], args[1]);
        }
    };
}
 
Example #3
Source File: PairInstantiators.java    From jackson-datatypes-collections with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unused") // Used from PairInstantiatorsPopulator
static <P> ValueInstantiator primitiveObjectInstantiator(
        JavaType inputType, Class<?> one,
        BiFunction<Object, Object, P> factory
) {
    return new ValueInstantiator.Base(inputType) {
        @Override
        public boolean canCreateFromObjectWith() {
            return true;
        }

        @Override
        public SettableBeanProperty[] getFromObjectArguments(DeserializationConfig config) {
            JavaType oneType = config.constructType(one);
            JavaType twoType = inputType.containedType(0);
            return makeProperties(config, oneType, twoType);
        }

        @Override
        public Object createFromObjectWith(DeserializationContext ctxt, Object[] args) {
            return factory.apply(args[0], args[1]);
        }
    };
}
 
Example #4
Source File: MapDeserializerManager.java    From caravan with Apache License 2.0 6 votes vote down vote up
public MapDeserializerManager(MapCustomizationFactory factory) {
  /**
   * The first parameter is just a placeholder, won't be used.
   * So any element type is ok.
   */
  super(
      CollectionType.construct(ArrayList.class, null, null, null, //
          SimpleType.constructUnsafe(Object.class)), //
      null, null, new ValueInstantiator() {
        @SuppressWarnings("rawtypes")
        @Override
        public Object createUsingDefault(DeserializationContext ctxt) throws IOException {
          return new ArrayList();
        }
      });

  this.factory = factory;
}
 
Example #5
Source File: PropertyBasedCreator.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Factory method used for building actual instances to be used with types
 * OTHER than POJOs.
 * resolves deserializers and checks for "null values".
 *
 * @since 2.9
 */
public static PropertyBasedCreator construct(DeserializationContext ctxt,
        ValueInstantiator valueInstantiator, SettableBeanProperty[] srcCreatorProps,
        boolean caseInsensitive)
    throws JsonMappingException
{
    final int len = srcCreatorProps.length;
    SettableBeanProperty[] creatorProps = new SettableBeanProperty[len];
    for (int i = 0; i < len; ++i) {
        SettableBeanProperty prop = srcCreatorProps[i];
        if (!prop.hasValueDeserializer()) {
            prop = prop.withValueDeserializer(ctxt.findContextualValueDeserializer(prop.getType(), prop));
        }
        creatorProps[i] = prop;
    }
    return new PropertyBasedCreator(ctxt, valueInstantiator, creatorProps, 
            caseInsensitive, false);
}
 
Example #6
Source File: PropertyBasedCreator.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Factory method used for building actual instances to be used with POJOS:
 * resolves deserializers, checks for "null values".
 *
 * @since 2.9
 */
public static PropertyBasedCreator construct(DeserializationContext ctxt,
        ValueInstantiator valueInstantiator, SettableBeanProperty[] srcCreatorProps,
        BeanPropertyMap allProperties)
    throws JsonMappingException
{
    final int len = srcCreatorProps.length;
    SettableBeanProperty[] creatorProps = new SettableBeanProperty[len];
    for (int i = 0; i < len; ++i) {
        SettableBeanProperty prop = srcCreatorProps[i];
        if (!prop.hasValueDeserializer()) {
            prop = prop.withValueDeserializer(ctxt.findContextualValueDeserializer(prop.getType(), prop));
        }
        creatorProps[i] = prop;
    }
    return new PropertyBasedCreator(ctxt, valueInstantiator, creatorProps,
            allProperties.isCaseInsensitive(),
            allProperties.hasAliases());
}
 
Example #7
Source File: SpringHandlerInstantiator.java    From java-technology-stack with MIT License 5 votes vote down vote up
/** @since 4.3 */
@Override
public ValueInstantiator valueInstantiatorInstance(MapperConfig<?> config,
		Annotated annotated, Class<?> implClass) {

	return (ValueInstantiator) this.beanFactory.createBean(implClass);
}
 
Example #8
Source File: ProblemHandlerTest.java    From jackson-modules-base with Apache License 2.0 5 votes vote down vote up
@Override
public Object handleMissingInstantiator(DeserializationContext ctxt,
        Class<?> instClass, ValueInstantiator inst, JsonParser p, String msg)
    throws IOException
{
    p.skipChildren();
    return value;
}
 
Example #9
Source File: CreatorOptimizer.java    From jackson-modules-base with Apache License 2.0 5 votes vote down vote up
public ValueInstantiator createOptimized()
{
    /* [Issue#11]: Need to avoid optimizing if we use delegate- or
     *  property-based creators.
     */
    if (_originalInstantiator.canCreateFromObjectWith()
            || _originalInstantiator.canCreateUsingDelegate()) {
        return null;
    }
    
    // for now, only consider need to handle default creator
    AnnotatedWithParams defaultCreator = _originalInstantiator.getDefaultCreator();
    if (defaultCreator != null) {
        AnnotatedElement elem = defaultCreator.getAnnotated();
        if (elem instanceof Constructor<?>) {
            // First things first: as per [Issue#34], can NOT access private ctors or methods
            Constructor<?> ctor = (Constructor<?>) elem;
            if (!Modifier.isPrivate(ctor.getModifiers())) {
                return createSubclass(ctor, null).with(_originalInstantiator);
            }
        } else if (elem instanceof Method) {
            Method m = (Method) elem;
            int mods = m.getModifiers();
            // and as above, can't access private ones
            if (Modifier.isStatic(mods) && !Modifier.isPrivate(mods)) {
                return createSubclass(null, m).with(_originalInstantiator);
            }
        }
    }
    return null;
}
 
Example #10
Source File: MapDeserializer.java    From caravan with Apache License 2.0 5 votes vote down vote up
public MapDeserializer(JavaType pairType) {
  super(
      CollectionType.construct(ArrayList.class, null, null, null, pairType), //
      null, null, new ValueInstantiator() {
        @Override
        public Object createUsingDefault(DeserializationContext ctxt) throws IOException {
          return new ArrayList();
        }
      });
}
 
Example #11
Source File: CustomCollectionDeserializer.java    From caravan with Apache License 2.0 5 votes vote down vote up
/**
 * Constructor used when creating contextualized instances.
 *
 * @since 2.9
 */
protected CustomCollectionDeserializer(JavaType collectionType,
    JsonDeserializer<Object> valueDeser, TypeDeserializer valueTypeDeser,
    ValueInstantiator valueInstantiator, JsonDeserializer<Object> delegateDeser,
    NullValueProvider nuller, Boolean unwrapSingle) {
  super(collectionType, nuller, unwrapSingle);
  _valueDeserializer = valueDeser;
  _valueTypeDeserializer = valueTypeDeser;
  _valueInstantiator = valueInstantiator;
  _delegateDeserializer = delegateDeser;
}
 
Example #12
Source File: CreatorCollector.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public ValueInstantiator constructValueInstantiator(
        DeserializationConfig config) {
    final JavaType delegateType = _computeDelegateType(
            _creators[C_DELEGATE], _delegateArgs);
    final JavaType arrayDelegateType = _computeDelegateType(
            _creators[C_ARRAY_DELEGATE], _arrayDelegateArgs);
    final JavaType type = _beanDesc.getType();

    // 11-Jul-2016, tatu: Earlier optimization by replacing the whole
    // instantiator did not
    // work well, so let's replace by lower-level check:
    AnnotatedWithParams defaultCtor = StdTypeConstructor
            .tryToOptimize(_creators[C_DEFAULT]);

    StdValueInstantiator inst = new StdValueInstantiator(config, type);
    inst.configureFromObjectSettings(defaultCtor, _creators[C_DELEGATE],
            delegateType, _delegateArgs, _creators[C_PROPS],
            _propertyBasedArgs);
    inst.configureFromArraySettings(_creators[C_ARRAY_DELEGATE],
            arrayDelegateType, _arrayDelegateArgs);
    inst.configureFromStringCreator(_creators[C_STRING]);
    inst.configureFromIntCreator(_creators[C_INT]);
    inst.configureFromLongCreator(_creators[C_LONG]);
    inst.configureFromDoubleCreator(_creators[C_DOUBLE]);
    inst.configureFromBooleanCreator(_creators[C_BOOLEAN]);
    inst.configureIncompleteParameter(_incompleteParameter);
    return inst;
}
 
Example #13
Source File: PropertyBasedCreator.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Deprecated // since 2.9
public static PropertyBasedCreator construct(DeserializationContext ctxt,
        ValueInstantiator valueInstantiator, SettableBeanProperty[] srcCreatorProps)
    throws JsonMappingException
{
    return construct(ctxt, valueInstantiator, srcCreatorProps,
            ctxt.isEnabled(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES));
}
 
Example #14
Source File: FactoryBasedEnumDeserializer.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public FactoryBasedEnumDeserializer(Class<?> cls, AnnotatedMethod f, JavaType paramType,
        ValueInstantiator valueInstantiator, SettableBeanProperty[] creatorProps)
{
    super(cls);
    _factory = f;
    _hasArgs = true;
    // We'll skip case of `String`, as well as no type (zero-args): 
    _inputType = paramType.hasRawClass(String.class) ? null : paramType;
    _deser = null;
    _valueInstantiator = valueInstantiator;
    _creatorProps = creatorProps;
}
 
Example #15
Source File: ArrayBlockingQueueDeserializer.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructor used when creating contextualized instances.
 */
 protected ArrayBlockingQueueDeserializer(JavaType containerType,
        JsonDeserializer<Object> valueDeser, TypeDeserializer valueTypeDeser,
        ValueInstantiator valueInstantiator,
        JsonDeserializer<Object> delegateDeser,
        NullValueProvider nuller, Boolean unwrapSingle)
{
    super(containerType, valueDeser, valueTypeDeser, valueInstantiator, delegateDeser,
            nuller, unwrapSingle);
}
 
Example #16
Source File: EnumDeserializer.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Factory method used when Enum instances are to be deserialized
 * using a creator (static factory method)
 * 
 * @return Deserializer based on given factory method
 *
 * @since 2.8
 */
public static JsonDeserializer<?> deserializerForCreator(DeserializationConfig config,
        Class<?> enumClass, AnnotatedMethod factory,
        ValueInstantiator valueInstantiator, SettableBeanProperty[] creatorProps)
{
    if (config.canOverrideAccessModifiers()) {
        ClassUtil.checkAndFixAccess(factory.getMember(),
                config.isEnabled(MapperFeature.OVERRIDE_PUBLIC_ACCESS_MODIFIERS));
    }
    return new FactoryBasedEnumDeserializer(enumClass, factory,
            factory.getParameterType(0),
            valueInstantiator, creatorProps);
}
 
Example #17
Source File: ContainerDeserializerBase.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override // since 2.9
public Object getEmptyValue(DeserializationContext ctxt) throws JsonMappingException {
    ValueInstantiator vi = getValueInstantiator();
    if (vi == null || !vi.canCreateUsingDefault()) {
        JavaType type = getValueType();
        ctxt.reportBadDefinition(type,
                String.format("Cannot create empty instance of %s, no default Creator", type));
    }
    try {
        return vi.createUsingDefault(ctxt);
    } catch (IOException e) {
        return ClassUtil.throwAsMappingException(ctxt, e);
    }
}
 
Example #18
Source File: EnumMapDeserializer.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @since 2.9
 */
public EnumMapDeserializer(JavaType mapType, ValueInstantiator valueInst,
        KeyDeserializer keyDeser, JsonDeserializer<?> valueDeser, TypeDeserializer vtd,
        NullValueProvider nuller)
{
    super(mapType, nuller, null);
    _enumClass = mapType.getKeyType().getRawClass();
    _keyDeserializer = keyDeser;
    _valueDeserializer = (JsonDeserializer<Object>) valueDeser;
    _valueTypeDeserializer = vtd;
    _valueInstantiator = valueInst;
}
 
Example #19
Source File: SpringHandlerInstantiator.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/** @since 4.3 */
@Override
public ValueInstantiator valueInstantiatorInstance(MapperConfig<?> config,
		Annotated annotated, Class<?> implClass) {

	return (ValueInstantiator) this.beanFactory.createBean(implClass);
}
 
Example #20
Source File: StringCollectionDeserializer.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
protected StringCollectionDeserializer(JavaType collectionType,
        ValueInstantiator valueInstantiator, JsonDeserializer<?> delegateDeser,
        JsonDeserializer<?> valueDeser,
        NullValueProvider nuller, Boolean unwrapSingle)
{
    super(collectionType, nuller, unwrapSingle);
    _valueDeserializer = (JsonDeserializer<String>) valueDeser;
    _valueInstantiator = valueInstantiator;
    _delegateDeserializer = (JsonDeserializer<Object>) delegateDeser;
}
 
Example #21
Source File: SimpleModule.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Method for registering {@link ValueInstantiator} to use when deserializing
 * instances of type <code>beanType</code>.
 *<p>
 * Instantiator is
 * registered when module is registered for <code>ObjectMapper</code>.
 */
public SimpleModule addValueInstantiator(Class<?> beanType, ValueInstantiator inst)
{
    _checkNotNull(beanType, "class to register value instantiator for");
    _checkNotNull(inst, "value instantiator");
    if (_valueInstantiators == null) {
        _valueInstantiators = new SimpleValueInstantiators();
    }
    _valueInstantiators = _valueInstantiators.addValueInstantiator(beanType, inst);
    return this;
}
 
Example #22
Source File: ReferenceTypeDeserializer.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public ReferenceTypeDeserializer(JavaType fullType, ValueInstantiator vi,
        TypeDeserializer typeDeser, JsonDeserializer<?> deser)
{
    super(fullType);
    _valueInstantiator = vi;
    _fullType = fullType;
    _valueDeserializer = (JsonDeserializer<Object>) deser;
    _valueTypeDeserializer = typeDeser;
}
 
Example #23
Source File: SimpleValueInstantiators.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public ValueInstantiator findValueInstantiator(DeserializationConfig config,
        BeanDescription beanDesc, ValueInstantiator defaultInstantiator)
{
    ValueInstantiator inst = _classMappings.get(new ClassKey(beanDesc.getBeanClass()));
    return (inst == null) ? defaultInstantiator : inst;
}
 
Example #24
Source File: BQTimeModule.java    From bootique with Apache License 2.0 4 votes vote down vote up
@Override
public void setupModule(SetupContext context) {
    super.setupModule(context);
    context.addValueInstantiators(new ValueInstantiators.Base() {
        @Override
        public ValueInstantiator findValueInstantiator(DeserializationConfig config,
                                                       BeanDescription beanDesc, ValueInstantiator defaultInstantiator) {
            JavaType type = beanDesc.getType();
            Class<?> raw = type.getRawClass();

            // 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 = AnnotatedClassResolver.resolve(config,
                                config.constructType(ZoneId.class), config);
                    }
                    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;
        }
    });
}
 
Example #25
Source File: SourceMapDeserializer.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
public static SourceMapDeserializer of(final JavaType mapType,
                                       final ValueInstantiator valueInstantiator,
                                       final JsonDeserializer<Object> deserializer)
{
  return of(new MapDeserializer(mapType, valueInstantiator, null, deserializer, null));
}
 
Example #26
Source File: SpringHandlerInstantiator.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/** @since 4.3 */
@Override
public ValueInstantiator valueInstantiatorInstance(MapperConfig<?> config, Annotated annotated, Class<?> implClass) {
	return (ValueInstantiator) this.beanFactory.createBean(implClass);
}
 
Example #27
Source File: HandlerInstantiator.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Method called to construct an instance of ValueInstantiator of specified type.
 */
public ValueInstantiator valueInstantiatorInstance(MapperConfig<?> config,
        Annotated annotated, Class<?> resolverClass) {
    return null;
}
 
Example #28
Source File: SimpleValueInstantiators.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public SimpleValueInstantiators()
{
    _classMappings = new HashMap<ClassKey,ValueInstantiator>();        
}
 
Example #29
Source File: PairInstantiators.java    From jackson-datatypes-collections with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unused") // Used from PairInstantiatorsPopulator
static void add(Class<?> objectKeyOrValuePairClass,
        Function<JavaType, ValueInstantiator> lambda) {
    KEY_OR_VALUE_OBJECT_LAMBDAS.put(objectKeyOrValuePairClass, lambda);
}
 
Example #30
Source File: GuavaOptionalDeserializer.java    From jackson-datatypes-collections with Apache License 2.0 4 votes vote down vote up
public GuavaOptionalDeserializer(JavaType fullType, ValueInstantiator inst,
        TypeDeserializer typeDeser, JsonDeserializer<?> deser)
{
    super(fullType, inst, typeDeser, deser);
}