Jackson serialize Java Object to include class name

Viewed 61

I have these objects:

public class Person {
    private String name;
    private String age;
    private List<Job> list = new ArrayList<>();
    //getters and setters

}

public class Job {
    private String title;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}

using Jackson in main:

ObjectMapper mapper = new ObjectMapper();
mapper.activateDefaultTypingAsProperty( new DefaultBaseTypeLimitingValidator(), 
ObjectMapper.DefaultTyping.NON_FINAL, "class");
String json2 = mapper.writeValueAsString( person );

json2:
{
  "class":"com.johnson.testing.Person",
  "name":"Brandon Johnson","age":50,
  "list":["java.util.ArrayList", 
 [{"class":"com.johnson.testing2.Job",
  "title":"work"}]]
}

I want the "class" key but I do not want the "java.util.ArrayList" field. I am using Jackson to serialize nested objects that are in libraries. I do not have access to the Java Objects to annotate the classes. How can I just have the class with path added to the many nested objects and not the other stuff?

0 Answers
Related