Assume serialization to json includes the class name of the actual object, using this annotation on the Class:
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "@type")
class MyClass {
String foo;
}
So json is for example:
{"@type": "com.example.MyClass", "foo": "bar"}
Can this be deserialized without specifying the type? And I mean not even the super type. Just something like:
objectMapper.readValue(value, Object.class);
which doesn't actually work, it brings back a Map.
Well, it is certainly possible to do that although I have personally never used Jackson that way. You can deserialize it to a JsonNode
object and then convert it to the proper type.
final ObjectMapper objectMapper = new ObjectMapper();
final MyClass myClass = new MyClass();
myClass.foo = "bar";
// Serialize
final String json = objectMapper.writeValueAsString(myClass);
// Deserialize
final JsonNode jsonNode = objectMapper.readTree(json);
// Get the @type
final String type = jsonNode.get("@type").asText();
// Create a Class-object
final Class<?> cls = Class.forName(type);
// And convert it
final Object o = objectMapper.convertValue(jsonNode, cls);
System.out.println(o.getClass());
The output is:
MyClass
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With