Java源码示例:com.fasterxml.jackson.databind.deser.ValueInstantiator
示例1
/**
* 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());
}
示例2
/**
* 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);
}
示例3
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;
}
示例4
@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]);
}
};
}
示例5
@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]);
}
};
}
示例6
@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]);
}
});
}
示例7
/** @since 4.3 */
@Override
public ValueInstantiator valueInstantiatorInstance(MapperConfig<?> config,
Annotated annotated, Class<?> implClass) {
return (ValueInstantiator) this.beanFactory.createBean(implClass);
}
示例8
/** @since 4.3 */
@Override
public ValueInstantiator valueInstantiatorInstance(MapperConfig<?> config,
Annotated annotated, Class<?> implClass) {
return (ValueInstantiator) this.beanFactory.createBean(implClass);
}
示例9
/**
* 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;
}
示例10
@Override
public ValueInstantiator findValueInstantiator(DeserializationConfig config,
BeanDescription beanDesc, ValueInstantiator defaultInstantiator)
{
ValueInstantiator inst = _classMappings.get(new ClassKey(beanDesc.getBeanClass()));
return (inst == null) ? defaultInstantiator : inst;
}
示例11
@SuppressWarnings("unchecked")
public ReferenceTypeDeserializer(JavaType fullType, ValueInstantiator vi,
TypeDeserializer typeDeser, JsonDeserializer<?> deser)
{
super(fullType);
_valueInstantiator = vi;
_fullType = fullType;
_valueDeserializer = (JsonDeserializer<Object>) deser;
_valueTypeDeserializer = typeDeser;
}
示例12
@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;
}
示例13
/**
* @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;
}
示例14
@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);
}
}
示例15
/**
* 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);
}
示例16
/**
* 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);
}
示例17
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;
}
示例18
@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));
}
示例19
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;
}
示例20
/**
* 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;
}
示例21
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();
}
});
}
示例22
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;
}
示例23
@Override
public Object handleMissingInstantiator(DeserializationContext ctxt,
Class<?> instClass, ValueInstantiator inst, JsonParser p, String msg)
throws IOException
{
p.skipChildren();
return value;
}
示例24
/** @since 4.3 */
@Override
public ValueInstantiator valueInstantiatorInstance(MapperConfig<?> config, Annotated annotated, Class<?> implClass) {
return (ValueInstantiator) this.beanFactory.createBean(implClass);
}
示例25
/**
* Method called to construct an instance of ValueInstantiator of specified type.
*/
public ValueInstantiator valueInstantiatorInstance(MapperConfig<?> config,
Annotated annotated, Class<?> resolverClass) {
return null;
}
示例26
public SimpleValueInstantiators()
{
_classMappings = new HashMap<ClassKey,ValueInstantiator>();
}
示例27
public SimpleValueInstantiators addValueInstantiator(Class<?> forType,
ValueInstantiator inst)
{
_classMappings.put(new ClassKey(forType), inst);
return this;
}
示例28
public StringCollectionDeserializer(JavaType collectionType,
JsonDeserializer<?> valueDeser, ValueInstantiator valueInstantiator)
{
this(collectionType, valueInstantiator, null, valueDeser, valueDeser, null);
}
示例29
@Override
public ValueInstantiator getValueInstantiator() {
return _valueInstantiator;
}
示例30
protected final NullValueProvider _findNullProvider(DeserializationContext ctxt,
BeanProperty prop, Nulls nulls, JsonDeserializer<?> valueDeser)
throws JsonMappingException
{
if (nulls == Nulls.FAIL) {
if (prop == null) {
return NullsFailProvider.constructForRootValue(ctxt.constructType(valueDeser.handledType()));
}
return NullsFailProvider.constructForProperty(prop);
}
if (nulls == Nulls.AS_EMPTY) {
// cannot deal with empty values if there is no value deserializer that
// can indicate what "empty value" is:
if (valueDeser == null) {
return null;
}
// Let's first do some sanity checking...
// NOTE: although we could use `ValueInstantiator.Gettable` in general,
// let's not since that would prevent being able to use custom impls:
if (valueDeser instanceof BeanDeserializerBase) {
ValueInstantiator vi = ((BeanDeserializerBase) valueDeser).getValueInstantiator();
if (!vi.canCreateUsingDefault()) {
final JavaType type = prop.getType();
ctxt.reportBadDefinition(type,
String.format("Cannot create empty instance of %s, no default Creator", type));
}
}
// Second: can with pre-fetch value?
{
AccessPattern access = valueDeser.getEmptyAccessPattern();
if (access == AccessPattern.ALWAYS_NULL) {
return NullsConstantProvider.nuller();
}
if (access == AccessPattern.CONSTANT) {
return NullsConstantProvider.forValue(valueDeser.getEmptyValue(ctxt));
}
}
return new NullsAsEmptyProvider(valueDeser);
}
if (nulls == Nulls.SKIP) {
return NullsConstantProvider.skipper();
}
return null;
}