Skip to content

Commit

Permalink
Implement #1029 (add support for @JsonAlias)
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Feb 27, 2017
1 parent f66df3d commit 71430dd
Show file tree
Hide file tree
Showing 11 changed files with 366 additions and 80 deletions.
1 change: 1 addition & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Project: jackson-databind
#865: `JsonFormat.Shape.OBJECT` ignored when class implements `Map.Entry`
#888: Allow specifying custom exclusion comparator via `@JsonInclude`,
using `JsonInclude.Include.CUSTOM`
#1029: Add a way to define property name aliases
#1035: `@JsonAnySetter` assumes key of `String`, does not consider declared type.
(reported by Michael F)
#1284: Make `StdKeySerializers` use new `JsonGenerator.writeFieldId()` for `int`/`long` keys
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/com/fasterxml/jackson/databind/BeanProperty.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.fasterxml.jackson.databind;

import java.lang.annotation.Annotation;
import java.util.Collections;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonFormat.Value;
Expand Down Expand Up @@ -164,6 +166,16 @@ public interface BeanProperty extends Named
*/
public JsonInclude.Value findPropertyInclusion(MapperConfig<?> config, Class<?> baseType);

/**
* Method for accessing set of possible alternate names that are accepted
* during deserialization.
*
* @return List (possibly empty) of alternate names; never null
*
* @since 2.9
*/
public List<PropertyName> findAliases(MapperConfig<?> config);

/*
/**********************************************************
/* Schema/introspection support
Expand Down Expand Up @@ -300,6 +312,13 @@ public JsonInclude.Value findPropertyInclusion(MapperConfig<?> config, Class<?>
return v0.withOverrides(v);
}

@Override
public List<PropertyName> findAliases(MapperConfig<?> config) {
// 26-Feb-2017, tatu: Do we really need to allow actual definition?
// For now, let's not.
return Collections.emptyList();
}

@Override public String getName() { return _name.getSimpleName(); }
@Override public PropertyName getFullName() { return _name; }
@Override public JavaType getType() { return _type; }
Expand Down Expand Up @@ -400,6 +419,11 @@ public com.fasterxml.jackson.annotation.JsonInclude.Value findPropertyInclusion(
return null;
}

@Override
public List<PropertyName> findAliases(MapperConfig<?> config) {
return Collections.emptyList();
}

@Override
public void depositSchemaProperty(JsonObjectFormatVisitor objectVisitor,
SerializerProvider provider) throws JsonMappingException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,8 @@ public void resolve(DeserializationContext ctxt)

// And now that we know CreatorProperty instances are also resolved can finally create the creator:
if (creatorProps != null) {
_propertyBasedCreator = PropertyBasedCreator.construct(ctxt, _valueInstantiator, creatorProps);
_propertyBasedCreator = PropertyBasedCreator.construct(ctxt, _valueInstantiator,
creatorProps, _beanProperties);
}

if (extTypes != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,8 @@ public JsonDeserializer<?> build()
_fixAccess(props);

BeanPropertyMap propertyMap = BeanPropertyMap.construct(props,
_config.isEnabled(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES));
_config.isEnabled(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES),
_collectAliases(props));
propertyMap.assignIndexes();

// view processing must be enabled if:
Expand Down Expand Up @@ -416,7 +417,8 @@ public JsonDeserializer<?> buildBuilderBased(JavaType valueType, String expBuild
Collection<SettableBeanProperty> props = _properties.values();
_fixAccess(props);
BeanPropertyMap propertyMap = BeanPropertyMap.construct(props,
_config.isEnabled(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES));
_config.isEnabled(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES),
_collectAliases(props));
propertyMap.assignIndexes();

boolean anyViews = !_config.isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION);
Expand Down Expand Up @@ -449,7 +451,7 @@ public JsonDeserializer<?> buildBuilderBased(JavaType valueType, String expBuild
/**********************************************************
*/

private void _fixAccess(Collection<SettableBeanProperty> mainProps)
protected void _fixAccess(Collection<SettableBeanProperty> mainProps)
{
/* 07-Sep-2016, tatu: Ideally we should be able to avoid forcing
* access to properties that are likely ignored, but due to
Expand Down Expand Up @@ -488,4 +490,26 @@ private void _fixAccess(Collection<SettableBeanProperty> mainProps)
_buildMethod.fixAccess(_config.isEnabled(MapperFeature.OVERRIDE_PUBLIC_ACCESS_MODIFIERS));
}
}

protected Map<String,List<PropertyName>> _collectAliases(Collection<SettableBeanProperty> props)
{
Map<String,List<PropertyName>> mapping = null;
AnnotationIntrospector intr = _config.getAnnotationIntrospector();
if (intr != null) {
for (SettableBeanProperty prop : props) {
List<PropertyName> aliases = intr.findPropertyAliases(prop.getMember());
if ((aliases == null) || aliases.isEmpty()) {
continue;
}
if (mapping == null) {
mapping = new HashMap<>();
}
mapping.put(prop.getName(), aliases);
}
}
if (mapping == null) {
return Collections.emptyMap();
}
return mapping;
}
}
Loading

0 comments on commit 71430dd

Please sign in to comment.