I marshaled a class object having an inner class (non-static) using Jackson library which worked absolutely fine. However, I am getting problem while un-marshaling the object. Reason being my inner class doesn't have any default constructor. I have copied sample code here:
public class JsonMarshallUnMarshall {
public static void main(String[] args) {
Person person = new Person();
person.setId(2222);
person.setName("My Name");
Person.Student student = person.new Student(44887);
person.setStudent(student);
String personJsonValue = null;
// Marshalling
System.out.println("====Marshalling====");
ObjectMapper mapper = new ObjectMapper();
mapper.getSerializationConfig().addMixInAnnotations(Person.Student.class, MixIn.class);
mapper.getDeserializationConfig().addMixInAnnotations(Person.Student.class, MixIn.class);
try {
personJsonValue = mapper.writeValueAsString(person);
System.out.println(personJsonValue);
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// UnMarshalling
System.out.println("====Unmarshalling====");
Person unMarshalledPerson = null;
try {
unMarshalledPerson = mapper
.readValue(personJsonValue, Person.class);
System.out.println("id: " + unMarshalledPerson.getId());
System.out.println("name: " + unMarshalledPerson.getName());
System.out.println("student id: " + unMarshalledPerson.getStudent().getStudentId());
} catch (JsonParseException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Person {
private int id;
private String name;
private Student student;
public Person() {
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public class Student {
private int studentId;
public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
public Student(int studentId) {
this.studentId = studentId;
}
}
}
abstract class MixIn {
public MixIn(@JsonProperty("studentId") int newStudentId) {
}
@JsonProperty("studentId")
abstract int getNewStudentId();
}
On executing the code, I get an exception as:
org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type [simple type, class com.tnsi.cnam.batch.utilities.Person$Student]: can not instantiate from JSON object (need to add/enable type information?)
at [Source: java.io.StringReader@1abab88; line: 1, column: 41] (through reference chain: com.tnsi.cnam.batch.utilities.Person["student"])
Is there any way around for this problem... I can't put default constructor because the actual class comes from thirdparty jar.
Sure. But you're probably not going to like it. You need to write your own custom Deserializer. A pretty thorough example of how to do this can be found here.
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