下面列出了怎么用com.fasterxml.jackson.databind.util.BeanUtil的API类实例代码及写法,或者点击链接到github查看源代码。
@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;
}
@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;
}
protected void _addSetterMethod(Map<String, POJOPropertyBuilder> props,
AnnotatedMethod m, AnnotationIntrospector ai)
{
String implName; // from naming convention
boolean visible;
PropertyName pn = (ai == null) ? null : ai.findNameForDeserialization(m);
boolean nameExplicit = (pn != null);
if (!nameExplicit) { // no explicit name; must follow naming convention
implName = (ai == null) ? null : ai.findImplicitPropertyName(m);
if (implName == null) {
implName = BeanUtil.okNameForMutator(m, _mutatorPrefix, _stdBeanNaming);
}
if (implName == null) { // if not, must skip
return;
}
visible = _visibilityChecker.isSetterVisible(m);
} else { // explicit indication of inclusion, but may be empty
// we still need implicit name to link with other pieces
implName = (ai == null) ? null : ai.findImplicitPropertyName(m);
if (implName == null) {
implName = BeanUtil.okNameForMutator(m, _mutatorPrefix, _stdBeanNaming);
}
// if not regular getter name, use method name as is
if (implName == null) {
implName = m.getName();
}
if (pn.isEmpty()) {
// !!! TODO: use PropertyName for implicit names too
pn = _propNameFromSimple(implName);
nameExplicit = false;
}
visible = true;
}
boolean ignore = (ai == null) ? false : ai.hasIgnoreMarker(m);
_property(props, implName).addSetter(m, pn, nameExplicit, visible, ignore);
}
@Override
public PropertyName findWrapperName(MapperConfig<?> config, Annotated ann)
{
XmlElementWrapper w = findAnnotation(XmlElementWrapper.class, ann, false, false, false);
if (w != null) {
/* 18-Sep-2013, tatu: As per [jaxb-annotations#24], need to take special care with empty
* String, as that should indicate here "use underlying unmodified
* property name" (that is, one NOT overridden by @JsonProperty)
*/
PropertyName name = _combineNames(w.name(), w.namespace(), "");
// clumsy, yes, but has to do:
if (!name.hasSimpleName()) {
if (ann instanceof AnnotatedMethod) {
AnnotatedMethod am = (AnnotatedMethod) ann;
String str;
if (am.getParameterCount() == 0) {
str = BeanUtil.okNameForGetter(am);
} else {
str = BeanUtil.okNameForMutator(am, "set");
}
if (str != null) {
return name.withSimpleName(str);
}
}
return name.withSimpleName(ann.getName());
}
return name;
}
return null;
}
@Override
public String findImplicitPropertyName(AnnotatedMember member) {
JsonProperty property = member.getAnnotation(JsonProperty.class);
if (property == null) {
if (member instanceof AnnotatedMethod) {
AnnotatedMethod method = (AnnotatedMethod) member;
String fieldName = BeanUtil.okNameForGetter(method);
return getJacksonPropertyName(member.getDeclaringClass(), fieldName);
}
} else if (!property.value().equals("")) {
return property.value();
}
return null;
}
@Override
public JsonSerializer<?> createContextual(SerializerProvider provider,
BeanProperty property) throws JsonMappingException
{
TypeSerializer typeSer = _valueTypeSerializer;
if (typeSer != null) {
typeSer = typeSer.forProperty(property);
}
// First: do we have an annotation override from property?
JsonSerializer<?> ser = findAnnotatedContentSerializer(provider, property);
if (ser == null) {
// If not, use whatever was configured by type
ser = _valueSerializer;
if (ser == null) {
// A few conditions needed to be able to fetch serializer here:
if (_useStatic(provider, property, _referredType)) {
ser = _findSerializer(provider, _referredType, property);
}
} else {
ser = provider.handlePrimaryContextualization(ser, property);
}
}
// First, resolve wrt property, resolved serializers
ReferenceTypeSerializer<?> refSer;
if ((_property == property)
&& (_valueTypeSerializer == typeSer) && (_valueSerializer == ser)) {
refSer = this;
} else {
refSer = withResolved(property, typeSer, ser, _unwrapper);
}
// and then see if we have property-inclusion overrides
if (property != null) {
JsonInclude.Value inclV = property.findPropertyInclusion(provider.getConfig(), handledType());
if (inclV != null) {
JsonInclude.Include incl = inclV.getContentInclusion();
if (incl != JsonInclude.Include.USE_DEFAULTS) {
Object valueToSuppress;
boolean suppressNulls;
switch (incl) {
case NON_DEFAULT:
valueToSuppress = BeanUtil.getDefaultValue(_referredType);
suppressNulls = true;
if (valueToSuppress != null) {
if (valueToSuppress.getClass().isArray()) {
valueToSuppress = ArrayBuilders.getArrayComparator(valueToSuppress);
}
}
break;
case NON_ABSENT:
suppressNulls = true;
valueToSuppress = _referredType.isReferenceType() ? MARKER_FOR_EMPTY : null;
break;
case NON_EMPTY:
suppressNulls = true;
valueToSuppress = MARKER_FOR_EMPTY;
break;
case CUSTOM:
valueToSuppress = provider.includeFilterInstance(null, inclV.getContentFilter());
if (valueToSuppress == null) { // is this legal?
suppressNulls = true;
} else {
suppressNulls = provider.includeFilterSuppressNulls(valueToSuppress);
}
break;
case NON_NULL:
valueToSuppress = null;
suppressNulls = true;
break;
case ALWAYS: // default
default:
valueToSuppress = null;
suppressNulls = false;
break;
}
if ((_suppressableValue != valueToSuppress)
|| (_suppressNulls != suppressNulls)) {
refSer = refSer.withContentInclusion(valueToSuppress, suppressNulls);
}
}
}
}
return refSer;
}
@Override
public JsonSerializer<?> createContextual(SerializerProvider provider,
BeanProperty property) throws JsonMappingException
{
JsonSerializer<?> ser = null;
JsonSerializer<?> keySer = null;
final AnnotationIntrospector intr = provider.getAnnotationIntrospector();
final AnnotatedMember propertyAcc = (property == null) ? null : property.getMember();
// First: if we have a property, may have property-annotation overrides
if (propertyAcc != null && intr != null) {
Object serDef = intr.findKeySerializer(propertyAcc);
if (serDef != null) {
keySer = provider.serializerInstance(propertyAcc, serDef);
}
serDef = intr.findContentSerializer(propertyAcc);
if (serDef != null) {
ser = provider.serializerInstance(propertyAcc, serDef);
}
}
if (ser == null) {
ser = _valueSerializer;
}
// [databind#124]: May have a content converter
ser = findContextualConvertingSerializer(provider, property, ser);
if (ser == null) {
// 30-Sep-2012, tatu: One more thing -- if explicit content type is annotated,
// we can consider it a static case as well.
// 20-Aug-2013, tatu: Need to avoid trying to access serializer for java.lang.Object tho
if (_valueTypeIsStatic && !_valueType.isJavaLangObject()) {
ser = provider.findValueSerializer(_valueType, property);
}
}
if (keySer == null) {
keySer = _keySerializer;
}
if (keySer == null) {
keySer = provider.findKeySerializer(_keyType, property);
} else {
keySer = provider.handleSecondaryContextualization(keySer, property);
}
Object valueToSuppress = _suppressableValue;
boolean suppressNulls = _suppressNulls;
if (property != null) {
JsonInclude.Value inclV = property.findPropertyInclusion(provider.getConfig(), null);
if (inclV != null) {
JsonInclude.Include incl = inclV.getContentInclusion();
if (incl != JsonInclude.Include.USE_DEFAULTS) {
switch (incl) {
case NON_DEFAULT:
valueToSuppress = BeanUtil.getDefaultValue(_valueType);
suppressNulls = true;
if (valueToSuppress != null) {
if (valueToSuppress.getClass().isArray()) {
valueToSuppress = ArrayBuilders.getArrayComparator(valueToSuppress);
}
}
break;
case NON_ABSENT:
suppressNulls = true;
valueToSuppress = _valueType.isReferenceType() ? MARKER_FOR_EMPTY : null;
break;
case NON_EMPTY:
suppressNulls = true;
valueToSuppress = MARKER_FOR_EMPTY;
break;
case CUSTOM:
valueToSuppress = provider.includeFilterInstance(null, inclV.getContentFilter());
if (valueToSuppress == null) { // is this legal?
suppressNulls = true;
} else {
suppressNulls = provider.includeFilterSuppressNulls(valueToSuppress);
}
break;
case NON_NULL:
valueToSuppress = null;
suppressNulls = true;
break;
case ALWAYS: // default
default:
valueToSuppress = null;
// 30-Sep-2016, tatu: Should not need to check global flags here,
// if inclusion forced to be ALWAYS
suppressNulls = false;
break;
}
}
}
}
MapEntrySerializer mser = withResolved(property, keySer, ser,
valueToSuppress, suppressNulls);
// but note: no (full) filtering or sorting (unlike Maps)
return mser;
}
protected void _addGetterMethod(Map<String, POJOPropertyBuilder> props,
AnnotatedMethod m, AnnotationIntrospector ai)
{
// Very first thing: skip if not returning any value
if (!m.hasReturnType()) {
return;
}
// any getter?
// @JsonAnyGetter?
if (Boolean.TRUE.equals(ai.hasAnyGetter(m))) {
if (_anyGetters == null) {
_anyGetters = new LinkedList<AnnotatedMember>();
}
_anyGetters.add(m);
return;
}
// @JsonValue?
if (Boolean.TRUE.equals(ai.hasAsValue(m))) {
if (_jsonValueAccessors == null) {
_jsonValueAccessors = new LinkedList<>();
}
_jsonValueAccessors.add(m);
return;
}
String implName; // from naming convention
boolean visible;
PropertyName pn = ai.findNameForSerialization(m);
boolean nameExplicit = (pn != null);
if (!nameExplicit) { // no explicit name; must consider implicit
implName = ai.findImplicitPropertyName(m);
if (implName == null) {
implName = BeanUtil.okNameForRegularGetter(m, m.getName(), _stdBeanNaming);
}
if (implName == null) { // if not, must skip
implName = BeanUtil.okNameForIsGetter(m, m.getName(), _stdBeanNaming);
if (implName == null) {
return;
}
visible = _visibilityChecker.isIsGetterVisible(m);
} else {
visible = _visibilityChecker.isGetterVisible(m);
}
} else { // explicit indication of inclusion, but may be empty
// we still need implicit name to link with other pieces
implName = ai.findImplicitPropertyName(m);
if (implName == null) {
implName = BeanUtil.okNameForGetter(m, _stdBeanNaming);
}
// if not regular getter name, use method name as is
if (implName == null) {
implName = m.getName();
}
if (pn.isEmpty()) {
// !!! TODO: use PropertyName for implicit names too
pn = _propNameFromSimple(implName);
nameExplicit = false;
}
visible = true;
}
boolean ignore = ai.hasIgnoreMarker(m);
_property(props, implName).addGetter(m, pn, nameExplicit, visible, ignore);
}