Skip to content

Commit

Permalink
Fix #80 handling for 3.0 as well (can/need to use PropertyNameMatcher)
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed May 14, 2021
1 parent 620f045 commit 48725ba
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class BeanReader
protected final Map<String, String> _aliasMapping;

protected final Set<String> _ignorableNames;
protected final boolean _caseInsensitive;

protected final Constructor<?> _defaultCtor;
protected final Constructor<?> _stringCtor;
Expand All @@ -49,7 +50,8 @@ public class BeanReader
*/
protected BeanReader(Class<?> type, Map<String,BeanPropertyReader> propsByName,
Constructor<?> defaultCtor, Constructor<?> stringCtor, Constructor<?> longCtor,
Set<String> ignorableNames, Map<String, String> aliasMapping)
Set<String> ignorableNames, Map<String, String> aliasMapping,
boolean caseInsensitive)
{
super(type);
_propsByName = propsByName;
Expand All @@ -58,6 +60,7 @@ protected BeanReader(Class<?> type, Map<String,BeanPropertyReader> propsByName,
_longCtor = longCtor;
_ignorableNames = ignorableNames;
_aliasMapping = aliasMapping;
_caseInsensitive = caseInsensitive;
}

/**
Expand All @@ -69,7 +72,6 @@ protected void initPropertyMatcher(TokenStreamFactory streamFactory)
// 26-Jan-2020, tatu: One complication are aliases, if any
Map<String,BeanPropertyReader> byName = _aliasMapping.isEmpty()
? _propsByName : _mixInAliases(_propsByName, _aliasMapping);

final int size = byName.size();
List<Named> names = new ArrayList<>(size);
_propValueReaders = new BeanPropertyReader[size];
Expand All @@ -82,14 +84,14 @@ protected void initPropertyMatcher(TokenStreamFactory streamFactory)
// except for one problem: when we cache readers we cache matcher... so would
// need to figure out what to do with that -- can not support dynamic change
// easily.
/*
// 14-May-2021, tatu: Global case-insensitivity added via [jackson-jr#80], so:
if (_caseInsensitive) {
_propNameMatcher = streamFactory.constructCINameMatcher(names, true);
// false -> Strings not already intern()ed
_propNameMatcher = streamFactory.constructCINameMatcher(names, false,
Locale.getDefault());
} else {
_propNameMatcher = streamFactory.constructNameMatcher(names, true);
_propNameMatcher = streamFactory.constructNameMatcher(names, false);
}
*/
_propNameMatcher = streamFactory.constructNameMatcher(names, true);
}

private final Map<String,BeanPropertyReader> _mixInAliases(Map<String,BeanPropertyReader> props,
Expand All @@ -116,7 +118,8 @@ private final Map<String,BeanPropertyReader> _mixInAliases(Map<String,BeanProper
*/
public static BeanReader construct(Class<?> type, Map<String, BeanPropertyReader> props,
Constructor<?> defaultCtor, Constructor<?> stringCtor, Constructor<?> longCtor,
Set<String> ignorableProps, Map<String, String> aliasMapping)
Set<String> ignorableProps, Map<String, String> aliasMapping,
boolean caseInsensitive)
{
if (ignorableProps == null) {
ignorableProps = Collections.<String>emptySet();
Expand All @@ -125,7 +128,7 @@ public static BeanReader construct(Class<?> type, Map<String, BeanPropertyReader
aliasMapping = Collections.emptyMap();
}
return new BeanReader(type, props, defaultCtor, stringCtor, longCtor,
ignorableProps, aliasMapping);
ignorableProps, aliasMapping, caseInsensitive);
}

public Map<String,BeanPropertyReader> propertiesByName() { return _propsByName; }
Expand Down Expand Up @@ -319,13 +322,21 @@ private final Object _readWithUnknown(JSONReader r, JsonParser p,
final Object bean, String propName)
throws JacksonException
{
// first, skip current property
handleUnknown(r, p, propName);

final Object[] valueBuf = r._setterBuffer;

// first, check if current property still found (can this
// really occur?)
BeanPropertyReader prop = findProperty(propName);
if (prop == null) {
handleUnknown(r, p, propName);
} else {
valueBuf[0] = prop.getReader().readNext(r, p);
prop.setValueFor(bean, valueBuf);
}

// then do the rest with looping
for (; (propName = p.nextName()) != null; ) {
BeanPropertyReader prop = findProperty(propName);
prop = findProperty(propName);
if (prop == null) {
handleUnknown(r, p, propName);
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -471,8 +471,6 @@ protected BeanReader _resolveBeanForDeser(Class<?> raw, POJODefinition beanDef)
longCtor.setAccessible(true);
}
}
final boolean caseInsensitive = JSON.Feature.ACCEPT_CASE_INSENSITIVE_PROPERTIES.isEnabled(_features);

final List<POJODefinition.Prop> rawProps = beanDef.getProperties();
final int len = rawProps.size();
final Map<String, BeanPropertyReader> propMap;
Expand All @@ -481,10 +479,7 @@ protected BeanReader _resolveBeanForDeser(Class<?> raw, POJODefinition beanDef)
if (len == 0) {
propMap = Collections.emptyMap();
} else {
propMap = caseInsensitive
? new TreeMap<String, BeanPropertyReader>(String.CASE_INSENSITIVE_ORDER)
// 13-May-2021, tatu: Let's retain ordering here:
: new LinkedHashMap<String, BeanPropertyReader>();
propMap = new LinkedHashMap<String, BeanPropertyReader>();
final boolean useFields = JSON.Feature.USE_FIELDS.isEnabled(_features);
for (int i = 0; i < len; ++i) {
POJODefinition.Prop rawProp = rawProps.get(i);
Expand Down Expand Up @@ -519,18 +514,17 @@ protected BeanReader _resolveBeanForDeser(Class<?> raw, POJODefinition beanDef)
// we must link via name of primary property, unfortunately:
if (rawProp.hasAliases()) {
if (aliasMapping == null) {
aliasMapping = caseInsensitive
? new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER)
: new HashMap<String, String>();
aliasMapping = new LinkedHashMap<String, String>();
}
for (String alias : rawProp.aliases()) {
aliasMapping.put(alias, rawProp.name);
}
}
}
}
final boolean caseInsensitive = JSON.Feature.ACCEPT_CASE_INSENSITIVE_PROPERTIES.isEnabled(_features);
return BeanReader.construct(raw, propMap, defaultCtor, stringCtor, longCtor,
beanDef.getIgnorableNames(), aliasMapping);
beanDef.getIgnorableNames(), aliasMapping, caseInsensitive);
}

private TypeBindings _bindings(Class<?> ctxt) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ public void testSimpleBean() throws Exception

public void testSimpleBeanCaseInsensitive() throws Exception
{
final String INPUT = aposToQuotes("{'NaMe':{'FIRST':'Bob','last':'Burger'},'x':13, 'optioN': 'opTIOn1'}");
final String INPUT = aposToQuotes(
"{'NaMe':{'FIRST':'Bob','last':'Burger'},'x':13, 'optioN': 'opTIOn1'}");
TestBean bean =
JSON.builder()
.enable(JSON.Feature.ACCEPT_CASE_INSENSITIVE_PROPERTIES)
Expand All @@ -93,10 +94,10 @@ public void testSimpleBeanCaseInsensitive() throws Exception

assertNotNull(bean);
assertEquals(13, bean.x);
assertEquals(Option.Option1, bean.option);
assertNotNull(bean.name);
assertEquals("Bob", bean.name.first);
assertEquals("Burger", bean.name.last);
assertEquals(Option.Option1, bean.option);
}

public void testUnknownProps() throws Exception
Expand Down

0 comments on commit 48725ba

Please sign in to comment.