Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Follow up of Unknown Enum Deserialization issues , Map : <Enum,String> #1882

Closed
isuribb opened this issue Jan 10, 2018 · 6 comments
Closed

Comments

@isuribb
Copy link

isuribb commented Jan 10, 2018

Hi

This is a follow up issue from "issue handling unknown/unmapped Enum keys #1859"

Tried using the "READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE " with the JsonEnumDefaultValue annotation. It worked for the type Enum as expected. Then we have another issue where we are using a Map<Enum,String> object in our Bean class which didnot get resolved with above. When tried debugging, noticed the following code is going inside the class, EnumResolver since we a . It would be good if we can handle the same default value handling done in EnumDeserializer class , on this class as well so that we can use the default value we have defined. Appreciate a quick response.. Thank you.

StdKeyDeserializer :

@Override
public Object _parse(String key, DeserializationContext ctxt) throws IOException
{
if (_factory != null) {
try {
return _factory.call1(key);
} catch (Exception e) {
ClassUtil.unwrapAndThrowAsIAE(e);
}
}
EnumResolver res = ctxt.isEnabled(DeserializationFeature.READ_ENUMS_USING_TO_STRING)
? _getToStringResolver(ctxt) : _byNameResolver;
Enum<?> e = res.findEnum(key);
if ((e == null) && !ctxt.getConfig().isEnabled(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL)) {
return ctxt.handleWeirdKey(_keyClass, key, "not one of values excepted for Enum class: %s",
res.getEnumIds());
// fall-through if problems are collected, not immediately thrown
}
return e;
}
@cowtowncoder
Copy link
Member

As per my comments on the issue, you do need to change type to EnumMap. Handling for regular Map with Enum key will not fully work.

@isuribb
Copy link
Author

isuribb commented Jan 10, 2018

We are unable to use EnumMap since the beans are generated through Apache Thrift, which is only allowing Map "HashMap".

@cowtowncoder
Copy link
Member

@isuribb then you will probably need a custom key deserializer (or deserializer).

@mawifu
Copy link

mawifu commented Jan 10, 2018

It seems like EnumDeserializer works different than StdKeyDeserializer.EnumKD for Enums as keys.
#1877 is another issue which shows another facet of this unbalance.
There EnumMap doesn't solve the problem.

@isuribb
Copy link
Author

isuribb commented Jan 10, 2018

Thanks All... We went with a custom deserilizer and defaulting to a custom value and it resolved our issue. EnumMap also was not an option due to thrift model is not supporting any custom maps except map and tree map.

@cowtowncoder
Copy link
Member

@mawifu true, ordinals are not (for now) supported for keys. But for this issue I EnumMap does matter, wrt filtering (exclusion).

I also do think that for Jackson 3.0 I can do something that helps here, that I realized now: it should be possible to map all Map<ENUMTYPE,V> types into EnumMap<ENUMTYPE,v>, as "abstract type mapping" -- Map otherwise maps to LinkedHashMap by default (since it must map to a concrete type). So if key type is of Enum type, such mapping would make sense.
That would not only help with some of enum-specific handling, but also make things more efficient, and users can still use more specific type (like TreeMap or HashMap) if they really want some other type.

I will file another issue for that work. Thank you for all the input here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants