I've added the @JsonAnySetter
annotation to addProperty
method
Map<MyEnum, String> mymap = new EnumMap<>(MyEnum.class);
@JsonAnySetter
public void addProperty(MyEnum key, Object value) {
mymap.put(key, value);
}
but while deserializing I'm getting the below error
com.fasterxml.jackson.databind.JsonMappingException: Invalid 'any-setter' annotation on method 'addProperty()': first argument not of type String or Object, but java.lang.Enum
If I change the map to simple HashMap with key of type String then it works fine.
Could someone please let me know what nneds to be done for deserializing to EnumMap
The @JsonAnySetter
annotation seems to work only with the String
or Object
key types.
The workaround I found is to overload the addProperty
method with the key type as String, then convert the String key to Enum type and call the addProperty
method with Enum key.
Use the @JsonAnySetter
annotation on the overloaded method.
Map<MyEnum, String> mymap = new EnumMap<>(MyEnum.class);
public void addProperty(MyEnum key, Object value) {
mymap.put(key, value);
}
@JsonAnySetter
private void addProperty(String key, Object value) {
addProperty(MyEnum.valueOf(key), value);
}
Note that I've made the overloaded addProperty
method private
as I don't want to expose this method version.
@JsonAnySetter
annotation seems to work with private
methods.