So I am seeking to deserialise and serialise an object using Jackson. The object has a hierarchy, and deep down in the hierachy there is a
List<T>
where T is either a String, or one of the Java number classes.
Deserialising generic lists has been a common topic on these forums, and I know how to build a mapper that will deserialise or serialise a list using mapper.readValue.
What I don't know how to do is make is so that when I call the mapper on the top level, which doesn't know explicitly about the parametrisation of the classes that it contains, such that it will know how to call the custom deserialiser when it gets to the bottom level class that contains the parameterised list.
A simple example with getters/setters/constructors ommitted:
class A {
String name;
B thing;
}
class B {
String command;
List<C<?>> conditions;
}
class C<T> {
String type;
List<T> parameters
}
And I want to write a Jackson command that serialises A in one go. I have been using a method attached to A:
public String toJSON() throws JsonProcessingException{
ObjectMapper mapper = new ObjectMapper();
mapper.enableDefaultTyping();
return mapper.writeValueAsString(this);
}
but this has the known issue of generic collections losing their type information and not being deserialisable.
I can use the advice here to deserialise a given class with a generic parameter, but I don't see how to combine these solutions. I was hoping that Jackson has some what that I an write a custom deserialiser for C, and it can use that when it reaches that class type, and otherwise it can use the normal serialiser, which works fine for the other classes.