下面列出了怎么用com.fasterxml.jackson.databind.cfg.MapperConfig的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public PropertyName findNameForDeserialization(MapperConfig<?> config, Annotated a)
{
// 16-Sep-2016, tatu: Prior to 2.9 logic her more complicated, on assumption
// that visibility rules may require return of "" if method/fied visible;
// however, that is not required and causes issues so... now simpler:
if (a instanceof AnnotatedMethod) {
AnnotatedMethod am = (AnnotatedMethod) a;
if (!isVisible(am)) {
return null;
}
Class<?> rawType = am.getRawParameterType(0);
return findJaxbPropertyName(am, rawType, BeanUtil.okNameForMutator(am, "set"));
}
if (a instanceof AnnotatedField) {
AnnotatedField af = (AnnotatedField) a;
return isVisible(af)
? findJaxbPropertyName(af, af.getRawType(), null)
: null;
}
return null;
}
@Override
public Boolean hasRequiredMarker(MapperConfig<?> config, AnnotatedMember m) {
// 17-Oct-2017, tatu: [modules-base#32]
// Before 2.9.3, was handling `true` correctly,
// but otherwise used confusing logic (probably in attempt to try to avoid
// reporting not-required for default value case
XmlAttribute attr = m.getAnnotation(XmlAttribute.class);
if (attr != null) {
return attr.required();
}
XmlElement elem = m.getAnnotation(XmlElement.class);
if (elem != null) {
return elem.required();
}
return null;
}
@Override
public PropertyName findNameForDeserialization(MapperConfig<?> config, Annotated a)
{
/* 14-Apr-2014, tatu: Important -- we should NOT introspect name here,
* since we are not using annotations; instead it needs to be done
* in {@link #findParameterSourceName(AnnotatedParameter)}.
*/
/*
PropertyName name = super.findNameForDeserialization(a);
if (name == null) {
if (a instanceof AnnotatedParameter) {
String rawName _paranamer.findParameterName((AnnotatedParameter) a);
if (rawName != null) {
return new PropertyName(rawName);
}
}
}
*/
return null;
}
@Override
public Collection<NamedType> collectAndResolveSubtypesByTypeId(MapperConfig<?> config,
AnnotatedClass baseType)
{
final Class<?> rawBase = baseType.getRawType();
Set<Class<?>> typesHandled = new HashSet<Class<?>>();
Map<String,NamedType> byName = new LinkedHashMap<String,NamedType>();
NamedType rootType = new NamedType(rawBase, null);
_collectAndResolveByTypeId(baseType, rootType, config, typesHandled, byName);
if (_registeredSubtypes != null) {
for (NamedType subtype : _registeredSubtypes) {
// is it a subtype of root type?
if (rawBase.isAssignableFrom(subtype.getType())) { // yes
AnnotatedClass curr = AnnotatedClassResolver.resolveWithoutSuperTypes(config,
subtype.getType());
_collectAndResolveByTypeId(curr, subtype, config, typesHandled, byName);
}
}
}
return _combineNamedAndUnnamed(rawBase, typesHandled, byName);
}
@Override
public PropertyName findNameForDeserialization(MapperConfig<?> config, Annotated a)
{
/* 14-Apr-2014, tatu: Important -- we should NOT introspect name here,
* since we are not using annotations; instead it needs to be done
* in {@link #findParameterSourceName(AnnotatedParameter)}.
*/
/*
PropertyName name = super.findNameForDeserialization(a);
if (name == null) {
if (a instanceof AnnotatedParameter) {
String rawName _paranamer.findParameterName((AnnotatedParameter) a);
if (rawName != null) {
return new PropertyName(rawName);
}
}
}
*/
return null;
}
@Override
public PropertyName findNameForSerialization(MapperConfig<?> config, Annotated a)
{
// 16-Sep-2016, tatu: Prior to 2.9 logic her more complicated, on assumption
// that visibility rules may require return of "" if method/fied visible;
// however, that is not required and causes issues so... now simpler:
if (a instanceof AnnotatedMethod) {
AnnotatedMethod am = (AnnotatedMethod) a;
return isVisible(am)
? findJaxbPropertyName(am, am.getRawType(), BeanUtil.okNameForGetter(am))
: null;
}
if (a instanceof AnnotatedField) {
AnnotatedField af = (AnnotatedField) a;
return isVisible(af)
? findJaxbPropertyName(af, af.getRawType(), null)
: null;
}
return null;
}
protected BeanPropertyWriter _constructVirtualProperty(JsonAppend.Attr attr,
MapperConfig<?> config, AnnotatedClass ac, JavaType type)
{
PropertyMetadata metadata = attr.required() ?
PropertyMetadata.STD_REQUIRED : PropertyMetadata.STD_OPTIONAL;
// could add Index, Description in future, if those matter
String attrName = attr.value();
// allow explicit renaming; if none, default to attribute name
PropertyName propName = _propertyName(attr.propName(), attr.propNamespace());
if (!propName.hasSimpleName()) {
propName = PropertyName.construct(attrName);
}
// now, then, we need a placeholder for member (no real Field/Method):
AnnotatedMember member = new VirtualAnnotatedMember(ac, ac.getRawType(),
attrName, type);
// and with that and property definition
SimpleBeanPropertyDefinition propDef = SimpleBeanPropertyDefinition.construct(config,
member, propName, metadata, attr.include());
// can construct the property writer
return AttributePropertyWriter.construct(attrName, propDef,
ac.getAnnotations(), type);
}
@Override
public JsonFormat.Value findPropertyFormat(MapperConfig<?> config, Class<?> baseType)
{
// 15-Apr-2016, tatu: Let's calculate lazily, retain; assumption being however that
// baseType is always the same
JsonFormat.Value v = _propertyFormat;
if (v == null) {
JsonFormat.Value v1 = config.getDefaultPropertyFormat(baseType);
JsonFormat.Value v2 = null;
AnnotationIntrospector intr = config.getAnnotationIntrospector();
if (intr != null) {
AnnotatedMember member = getMember();
if (member != null) {
v2 = intr.findFormat(member);
}
}
if (v1 == null) {
v = (v2 == null) ? EMPTY_FORMAT : v2;
} else {
v = (v2 == null) ? v1 : v1.withOverrides(v2);
}
_propertyFormat = v;
}
return v;
}
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);
}
/** @since 4.3 */
@Override
public ValueInstantiator valueInstantiatorInstance(MapperConfig<?> config,
Annotated annotated, Class<?> implClass) {
return (ValueInstantiator) this.beanFactory.createBean(implClass);
}
/** @since 4.3 */
@Override
public ObjectIdGenerator<?> objectIdGeneratorInstance(MapperConfig<?> config,
Annotated annotated, Class<?> implClass) {
return (ObjectIdGenerator<?>) this.beanFactory.createBean(implClass);
}
/** @since 4.3 */
@Override
public PropertyNamingStrategy namingStrategyInstance(MapperConfig<?> config,
Annotated annotated, Class<?> implClass) {
return (PropertyNamingStrategy) this.beanFactory.createBean(implClass);
}
/** @since 4.3 */
@Override
public Converter<?, ?> converterInstance(MapperConfig<?> config,
Annotated annotated, Class<?> implClass) {
return (Converter<?, ?>) this.beanFactory.createBean(implClass);
}
/** @since 4.3 */
@Override
public ValueInstantiator valueInstantiatorInstance(MapperConfig<?> config,
Annotated annotated, Class<?> implClass) {
return (ValueInstantiator) this.beanFactory.createBean(implClass);
}
/** @since 4.3 */
@Override
public ObjectIdResolver resolverIdGeneratorInstance(MapperConfig<?> config,
Annotated annotated, Class<?> implClass) {
return (ObjectIdResolver) this.beanFactory.createBean(implClass);
}
/** @since 4.3 */
@Override
public Converter<?, ?> converterInstance(MapperConfig<?> config,
Annotated annotated, Class<?> implClass) {
return (Converter<?, ?>) this.beanFactory.createBean(implClass);
}
AnnotatedClassResolver(MapperConfig<?> config, JavaType type, MixInResolver r) {
_config = config;
_type = type;
_class = type.getRawClass();
_mixInResolver = r;
_bindings = type.getBindings();
_intr = config.isAnnotationProcessingEnabled()
? config.getAnnotationIntrospector() : null;
_primaryMixin = _config.findMixInClassFor(_class);
}
@Override
public String[] findEnumValues(MapperConfig<?> config,
Class<?> enumType, Enum<?>[] enumValues, String[] names) {
HashMap<String,String> expl = null;
for (Field f : enumType.getDeclaredFields()) {
if (!f.isEnumConstant()) {
continue;
}
XmlEnumValue enumValue = f.getAnnotation(XmlEnumValue.class);
if (enumValue == null) {
continue;
}
String n = enumValue.value();
if (n.isEmpty()) {
continue;
}
if (expl == null) {
expl = new HashMap<String,String>();
}
expl.put(f.getName(), n);
}
// and then stitch them together if and as necessary
if (expl != null) {
for (int i = 0, end = enumValues.length; i < end; ++i) {
String defName = enumValues[i].name();
String explValue = expl.get(defName);
if (explValue != null) {
names[i] = explValue;
}
}
}
return names;
}
@Override
public String nameForSetterMethod(MapperConfig<?> config, AnnotatedMethod method,
String defaultName) {
if (method.getDeclaringClass() == this.effectiveType) {
return fieldToJsonMapping
.getOrDefault(defaultName, super.nameForSetterMethod(config, method, defaultName));
} else {
return super.nameForSetterMethod(config, method, defaultName);
}
}
@Override
public JsonFormat.Value findPropertyFormat(MapperConfig<?> config, Class<?> baseType) {
JsonFormat.Value v0 = config.getDefaultPropertyFormat(baseType);
AnnotationIntrospector intr = config.getAnnotationIntrospector();
if ((intr == null) || (_member == null)) {
return v0;
}
JsonFormat.Value v = intr.findFormat(_member);
if (v == null) {
return v0;
}
return v0.withOverrides(v);
}
private boolean adapterTypeMatches(MapperConfig<?> config, XmlAdapter<?,?> adapter,
Class<?> targetType)
{
JavaType adapterType = config.constructType(adapter.getClass());
JavaType[] params = config.getTypeFactory().findTypeParameters(adapterType, XmlAdapter.class);
// should not happen, except if our type resolution has a flaw, but:
Class<?> boundType = (params == null || params.length < 2) ? Object.class
: params[1].getRawClass();
return boundType.isAssignableFrom(targetType);
}
@Override
public Object findSerializationConverter(MapperConfig<?> config, Annotated a)
{
Class<?> serType = _rawSerializationType(a);
// Can apply to both container and regular type; no difference yet here
XmlAdapter<?,?> adapter = findAdapter(config, a, true, serType);
if (adapter != null) {
return _converter(config, adapter, true);
}
return null;
}
@Override
public Collection<NamedType> collectAndResolveSubtypesByTypeId(MapperConfig<?> config,
AnnotatedMember property, JavaType baseType)
{
final AnnotationIntrospector ai = config.getAnnotationIntrospector();
Class<?> rawBase = baseType.getRawClass();
// Need to keep track of classes that have been handled already
Set<Class<?>> typesHandled = new HashSet<Class<?>>();
Map<String,NamedType> byName = new LinkedHashMap<String,NamedType>();
// start with lowest-precedence, which is from type hierarchy
NamedType rootType = new NamedType(rawBase, null);
AnnotatedClass ac = AnnotatedClassResolver.resolveWithoutSuperTypes(config,
rawBase);
_collectAndResolveByTypeId(ac, rootType, config, typesHandled, byName);
// then with definitions from property
if (property != null) {
Collection<NamedType> st = ai.findSubtypes(property);
if (st != null) {
for (NamedType nt : st) {
ac = AnnotatedClassResolver.resolveWithoutSuperTypes(config, nt.getType());
_collectAndResolveByTypeId(ac, nt, config, typesHandled, byName);
}
}
}
// and finally explicit type registrations (highest precedence)
if (_registeredSubtypes != null) {
for (NamedType subtype : _registeredSubtypes) {
// is it a subtype of root type?
if (rawBase.isAssignableFrom(subtype.getType())) { // yes
AnnotatedClass curr = AnnotatedClassResolver.resolveWithoutSuperTypes(config,
subtype.getType());
_collectAndResolveByTypeId(curr, subtype, config, typesHandled, byName);
}
}
}
return _combineNamedAndUnnamed(rawBase, typesHandled, byName);
}
@Override
public String findImplicitPropertyName(MapperConfig<?> config, AnnotatedMember m) {
XmlValue valueInfo = m.getAnnotation(XmlValue.class);
if (valueInfo != null) {
return _xmlValueName;
}
return null;
}
/**
* @since 2.7
*/
public static SimpleBeanPropertyDefinition construct(MapperConfig<?> config,
AnnotatedMember member, PropertyName name, PropertyMetadata metadata,
JsonInclude.Value inclusion) {
return new SimpleBeanPropertyDefinition(config.getAnnotationIntrospector(),
member, name, metadata, inclusion);
}
/**
* Factory method to use for constructing an instance to use for purposes
* other than building serializers or deserializers; will only have information
* on class, not on properties.
*/
public static BasicBeanDescription forOtherUse(MapperConfig<?> config,
JavaType type, AnnotatedClass ac)
{
return new BasicBeanDescription(config, type,
ac, Collections.<BeanPropertyDefinition>emptyList());
}
@Override
public Object findDeserializationConverter(MapperConfig<?> config, Annotated a)
{
// One limitation: for structured types this is done later on
Class<?> deserType = _rawDeserializationType(a);
XmlAdapter<?,?> adapter = findAdapter(config, a, true, deserType);
if (adapter != null) {
return _converter(config, adapter, false);
}
return null;
}
@Override
public TypeResolverBuilder<?> findTypeResolver(MapperConfig<?> config,
AnnotatedClass ac, JavaType baseType)
{
TypeResolverBuilder<?> b = _primary.findTypeResolver(config, ac, baseType);
if (b == null) {
b = _secondary.findTypeResolver(config, ac, baseType);
}
return b;
}
protected BasicBeanDescription _findStdJdkCollectionDesc(MapperConfig<?> cfg, JavaType type)
{
if (_isStdJDKCollection(type)) {
return BasicBeanDescription.forOtherUse(cfg, type,
_resolveAnnotatedClass(cfg, type, cfg));
}
return null;
}
@Override // since 2.9
public JacksonInject.Value findInjectableValue(MapperConfig<?> config, AnnotatedMember m) {
Object id = _findGuiceInjectId(m);
if (id == null) {
return null;
}
return JacksonInject.Value.forId(id);
}