I am trying to json-serialize a class MyRootClass with a property that is a collection of elements of a second class MyClass:
public class MyRootClass {
private List<MyInterface> list = new ArrayList<MyInterface>();
// getter / setter
}
public class MyClass implements MyInterface {
private String value = "test";
// getter / setter
}
The following code:
MyRootClass root = new MyRootClass();
root.getList().add(new MyClass());
ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(System.out, root);
Generates this JSON output:
{"list": [ {"value":"test"} ] }
instead of what I need, every object in the collection serialized with a name:
{"list": [ {"myclass": {"value":"test"}} ] }
Is there any way to achieve it using Jackson? I thought about writing a custom serializer, but I've not found anything related to a collection of objects.