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

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

Open
charlie-harvey opened this issue Feb 19, 2015 · 2 comments

Comments

@charlie-harvey
Copy link

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<GreetingType, Greeting> greetings;

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

    public Integer getId() {
        return id;
    }

    public void setGreetings(Map<GreetingType, Greeting> greetings) {
        this.greetings = greetings;
    }

    public Map<GreetingType, Greeting> 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
@cowtowncoder
Copy link
Member

Yup, probably not yet supported. Would be nice to support, for limited set of natively supported (by jackson jr) types.

cowtowncoder added a commit that referenced this issue Feb 20, 2015
@dantesun
Copy link

dantesun commented Aug 5, 2020

EnumReader not working for this case ...

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