Reputation: 1262
im Trying to serialize an object ( from an Enum ) in the client-side of my application and deserialize it in the other side ( server-side ) to the same Object using jackson . i have this enum in my client-side :
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "@type")
public enum Request{
Signup,
Login
}
which using the @jsonTypeInfo annotaion of jackson .
and on the server-side im doing this to deserialize the object :
ObjectMapper mapper = new ObjectMapper();
final JsonNode jsonNode;
jsonNode = mapper.readTree(json); //json is the return String from the convertObjectToJson(Request.Signup);
//i get the error on this line
String type = jsonNode.get("@type").asText();
as i've commented in the code above im getting the NullException on the jsonNode.get("@type").asText()
while the json is : [ "Request", "Signup" ]
and this is the function that serialize the objects :
public static String convertObjectToJson(Object object) throws IOException {
ObjectWriter objectWriter = new ObjectMapper().writer().withDefaultPrettyPrinter();
String json = objectWriter.writeValueAsString(object);
return json;
}
where is the problem ?
Upvotes: 0
Views: 645
Reputation: 2468
Jackson by default will represent java enums as simple strings and for that reason you are unable to deserialize the object
Take a look here, it could help: http://www.baeldung.com/jackson-serialize-enums
Upvotes: 2