FasterXML / FasterXML/jackson-jr

Enum as key of nested Map gets set as String when converting from string to Object

Open
#21 2 comments 1 reaction 0 assignees View on GitHub
Dominant language
Java
Stars
266
Forks
34
Avg merge
3h 7m
Merged PRs (30d)
1

Description

``` java
public enum GreetingType {
ENGLISH, CHINESE, HAWAIIAN;
}

public class Greeting {
private String word;

public void setWord(String word) {
this.word = word;
}

public String getWord() {
return word;
}
}

public class MyObj {
private Integer id;
private Map greetings;

public void setId(Integer id) {
this.id = id;
}

public Integer getId() {
return id;
}

public void setGreetings(Map greetings) {
this.greetings = greetings;
}

public Map getGreetings() {
return greetings;
}
}

// DOES NOT WORK:
String json = "{\"id\": \"1\", \"greetings\": {\"ENGLISH\": {\"word\": \"hello\"} }, {\"CHINESE\": {\"word\": \"nihao\"} }, {\"HAWAIIAN\": {\"word\": \"aloha\"} } }";
MyObj myObj = JSON.std.beanFrom(MyObj.class, json);
// keys of map greetings map are strings - not enums
myObj.getGreetings().get(GreetingType.ENGLISH); // -> Error
myObj.getGreetings().get("ENGLISH"); // -> Success

// DATABIND ObjectMapper WORKS AS EXPECTED:
ObjectMapper mapper = new ObjectMapper();
MyObj myObj = mapper.readValue(json, MyObj.class);
myObj.getGreetings().get(GreetingType.ENGLISH); // -> Success
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.