I have a Java class as follows,
public class User {
Integer id;
UserDetails details;
}
The class user details is as follows,
public class UserDetails {
String name;
Integer age;
}
The json String can be defined in both of the following ways,
String jsonString1 = "{\"id\" : 100,\"details\" : {\"name\" : \"ABC\", \"age\": 25}}";
//Here the details object is passed as a string instead as a JSON object in JSON string
String jsonString2 = "{\"id\" : 100,\"details\" : \"{\\\"name\\\" : \\\"ABC\\\", \\\"age\\\": 25}\"}"
I need ObjectMapper.readValue to work on both the strings, i.e.
ObjectMapper mapper = new ObjectMapper();
User user1 = mapper.readValue(jsonString1, User.class);
User user2 = mapper.readValue(jsonString2, User.class);
Is there a way to achieve this in Jackson? Through introducing annotations on UserDetails member and defining some custom Deserializer?