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
@JsonAnySetterannotation seems to work only with theStringorObjectkey types.The workaround I found is to overload the
addPropertymethod with the key type as String, then convert the String key to Enum type and call theaddPropertymethod with Enum key.Use the
@JsonAnySetterannotation on the overloaded method.Note that I've made the overloaded
addPropertymethodprivateas I don't want to expose this method version.@JsonAnySetterannotation seems to work withprivatemethods.