I am trying to deserialize JSON into a Java POJO using Jackson. Without giving away confidential information, here is an example stack trace when ObjectMapper's deserialization fails:
org.codehaus.jackson.map.JsonMappingException: Can not construct Map key of type com.example.MyEnum from String "coins": not a valid representation: Can not construct Map key of type com.example.MyEnum from String "coins": not one of values for Enum class
My JSON looks like this:
"foo": {
"coins": null,
...
}
And the class I want to deserialize into has this field:
private Map<MyEnum, MyPojo> foo;
And my enum type looks like this:
public enum MyEnum {
COINS("coins"),
...
}
I do realize that I am trying to deserialize a null value. But I believe this should still work: the result of the deserialization should be equivalent to having a Map with foo.put(MyEnum.COINS, null)
, which is indeed a valid Java instruction. Help is much appreciated, thanks in advance.
enum.name()
as the id -- and in this case, name is "COINS", not lowe-case "coins". There are ways around this, as answers point out. – Pennell