How do I deserialize number to enum? #4291
-
I need to convert integer to enum. public class JacksonDemo {
public static void main(String[] args) throws JsonProcessingException {
String json = "{\"code\":6}";
ObjectMapper mapper = new ObjectMapper();
Note note = mapper.readValue(json, Note.class);
System.out.println(note.code);
}
public static class Note {
@JsonProperty("code")
private Code code;
public void setCode(Code code) {
this.code = code;
}
}
public enum Code {
@JsonProperty("6") AA06;
}
} Output:
If I do the same with public class GsonDemo {
public static void main(String[] args) throws JsonProcessingException {
String json = "{\"code\":6}";
Gson gson = new Gson();
Note note = gson.fromJson(json, Note.class);
System.out.println(note.code);
}
public static class Note {
@SerializedName("code")
private Code code;
public void setCode(Code code) {
this.code = code;
}
}
public enum Code {
@SerializedName("6") AA06;
}
} Output:
So how can I make it work with |
Beta Was this translation helpful? Give feedback.
Answered by
cowtowncoder
Dec 31, 2023
Replies: 1 comment 6 replies
-
This is a volunteer project. There are lots of blogs and already answered questions on stackoverflow.com. use you favourite search engine. Maybe start with https://www.baeldung.com/jackson-serialize-enums |
Beta Was this translation helpful? Give feedback.
6 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ok, no, looks like deserialization from integer numbers is only supported wrt
@JsonValue
or@JsonCreator
annotations.