Deserialize with Jackson with reference to an existing object - java

JSON
{
"schools": [
{
"id": 1,
"name": "School A"
},
{
"id": 2,
"name": "School B"
}
],
"students": [
{
"id": 1,
"name": "Bobby",
"school": 1
}
]
}
How would I map the JSON into the following classes such that Bobby's school is mapped to the already instantiated School A.
public class School {
private Integer id;
private String name;
}
public class Student {
private Integer id;
private String name;
private School school;
}
I've tried some weird stuff with the Student class...
public class Student {
private Integer id;
private String name;
private School school;
#JsonProperty("school")
public void setSchool(Integer sid) {
for (School school : getSchools()) {
if (school.id == sid) {
this.school = school;
break;
}
}
}
}
The problem I'm having is that both the schools and the students are being parsed from the JSON at the same time, so I'm not sure how to get a list of the schools. Maybe I should parse these separately so I have the list of schools first?

Jackson will do it for you. Just annotate your objects with #JsonIdentityInfo:
#JsonIdentityInfo(scope=School.class, generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
public class School {
private Integer id;
private String name;
public School() {
}
public School(Integer id, String name) {
this.id = id;
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
#JsonIdentityInfo(scope=Student.class, generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
public class Student {
private Integer id;
private String name;
private School school;
public Student() {
}
public Student(Integer id, String name, School school) {
this.id = id;
this.name = name;
this.school = school;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public School getSchool() {
return school;
}
public void setSchool(School school) {
this.school = school;
}
}
public static void main(String[] args) throws IOException {
School school = new School(1, "St Magdalene's");
Student mary = new Student(1, "Mary", school);
Student bob = new Student(2, "Bob", school);
Student[] students = new Student[] {mary, bob};
// Write out
String serialized = mapper.writeValueAsString(students);
System.out.println("Serialized: " + serialized);
// Read in
Student[] deserialized = mapper.readValue(serialized, Student[].class);
}

With classes defined as below:
#JsonIdentityInfo(property = "id", generator = ObjectIdGenerators.PropertyGenerator.class)
class School {
public Integer id;
public String name;
}
class Student {
public Integer id;
public String name;
#JsonIdentityReference(alwaysAsId = true)
public School school;
}
class All {
public List<School> schools;
public List<Student> students;
}
This works exactly as You intended:
#Test
public void test() throws JsonProcessingException {
var json = "{" +
"\"schools\":[" +
"{\"id\":1,\"name\":\"School A\"}," +
"{\"id\":2,\"name\":\"School B\"}" +
"]," +
"\"students\":[" +
"{\"id\":1,\"name\":\"Bobby\",\"school\":1}" +
"]" +
"}";
var mapper = new ObjectMapper();
var all = mapper.readValue(json, All.class);
Assertions.assertThat(all.students.get(0).school).isSameAs(all.schools.get(0));
}

Related

#JsonUnwrapped and #JsonCreate for same field causes issues

When I use #JsonUnwrapped on nested field:
public class Person{
private int id;
#JsonUnwrapped
private Father father
//getters/setters
#Data
#AllArgsConstructor
private static class Father {
private String name;
private String surname;
}
And at the same time I use the #JsonCreator:
#JsonCreator // DESERIALIZATION: JSON -> POJO
public Person(...
#JsonProperty("name") String name,
#JsonProperty("surname") String surname) {
(...)
this.father = new Father(name, surname);
with Father being nested class.
I get the error:
Father` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creat
But when I remove the #JsonUnwrapped the field gets deserialised ok but is not flatten during serialisation.
How to assure that Father field will be serialised and deserialised flatten at the same time?
EDIT:
I paste full code:
#Data
#JsonIgnoreProperties(ignoreUnknown = true)
public class Person {
private int id;
private String firstName;
private String lastName;
private boolean active;
private Address address;
private String[] languages;
#JsonIgnore private boolean isTheKing;
#JsonUnwrapped // SERIALIZATIONL POJO -> JSON
private Father father;
#JsonCreator // DESERIALIZATION: JSON -> POJO
public Student(
#JsonProperty("id") int id,
#JsonProperty("firstName") String firstName,
#JsonProperty("lastName") String lastName,
#JsonProperty("active") boolean active,
#JsonProperty("address") Address address,
#JsonProperty("languages") String[] languages,
#JsonProperty("isTheKing") boolean isTheKing,
#JsonProperty("name") String name,
#JsonProperty("surname") String surname) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.active = active;
this.address = address;
this.languages = languages;
this.isTheKing = isTheKing;
this.father = new Father(name, surname);
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String[] getLanguages() {
return languages;
}
public void setLanguages(String[] languages) {
this.languages = languages;
}
public Father getFather() {
return father;
}
public void setFather(Father father) {
this.father = father;
}
#Data
#AllArgsConstructor
static class Father {
private String name;
private String surname;
}
}
the following main method fails:
ObjectMapper mapper = new ObjectMapper();
Person myStudent =
mapper.readValue(new File("src/main/resources/data/rest/studentIN.json"), Person.class);
System.out.println(myStudent);
with error:
Exception in thread "main"
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot
construct instance of com.example.demo.Person$Father
(no Creators, like default construct, exist): cannot deserialize from
Object value (no delegate- or property-based Creator)
I use lombok:
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
</dependency>
It should work for simple POJO model. Father class should be public:
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
public class JsonApp {
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
Person.Father father = new Person.Father();
father.setName("Wit");
father.setSurname("Pil");
Person person = new Person();
person.setId(1909);
person.setFather(father);
String json = mapper.writeValueAsString(person);
System.out.println(json);
System.out.println(mapper.readValue(json, Person.class));
}
}
class Person {
private int id;
#JsonUnwrapped
private Father father;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Father getFather() {
return father;
}
public void setFather(Father father) {
this.father = father;
}
#Override
public String toString() {
return "Person{" +
"id=" + id +
", father=" + father +
'}';
}
static class Father {
private String name;
private String surname;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
#Override
public String toString() {
return "Father{" +
"name='" + name + '\'' +
", surname='" + surname + '\'' +
'}';
}
}
}
Above code prints:
{
"id" : 1909,
"name" : "Wit",
"surname" : "Pil"
}
Person{id=1909, father=Father{name='Wit', surname='Pil'}}
I've tested it for many version since 2.6.7 and it works fine.

Complex Java Object to Json

I have a java class like :
public class Sclass {
private Student student;
private Teacher teacher;
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
}
public class Teacher {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Student {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
I want to use Sclass in a rest service and thus want a json for the same, please assist.
Example using gson:
Student student = new Student();
student.setName("Student 1");
student.setAge(18);
Teacher teacher = new Teacher();
teacher.setName("Teacher 1");
Sclass sclass = new Sclass();
sclass.setStudent(student);
sclass.setTeacher(teacher);
Gson gson = new Gson();
String json = gson.toJson(sclass);
System.out.println(json);
Output:
{"student":{"name":"Student 1","age":18},"teacher":{"name":"Teacher 1"}}

getter setter in inner private class in java [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I want to create bean in java corresponding to below json
{
"name": "",
"id": "",
"dept": {
"deptId": "",
"deptName": "",
"course": {
"courseId": "",
}
}
}
My idea is to create parent class and keep dept and course as inner private classes and then have getters setters to get or set data and form parent bean. But I am getting error "Change visibility to the public"
How can I access private fields of inner private class to get and set data?
try this way its will work
public class firstClass{
private String name;
private String id;
Department dept;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Department getDept() {
return dept;
}
public void setDept(Department dept) {
this.dept = dept;
}
}
class Department{
private int departId;
private String deptName;
Course course;
public int getDepartId() {
return departId;
}
public void setDepartId(int departId) {
this.departId = departId;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
public Course getCourse() {
return course;
}
public void setCourse(Course course) {
this.course = course;
}
}
class Course{
private int courseId;
public int getCourseId() {
return courseId;
}
public void setCourseId(int courseId) {
this.courseId = courseId;
}
}
You can't access private fields. Why don't you create a getter and setter for the inner class private fields?
And, maybe you should consider using gson library.
You at least have to make say nested public interfaces, say Dept and Course, with your private (static) nested private classes DeptImpl and SourceImpl.
public class X {
public interface Dept { ... }
private static class DeptImpl extends Dept { ... }
public Dept getDept() { ... }
public Dept createDept(...) {
DeptImpl dept = new DeptImpl(...); ...
return dept;
}
Maybe you need to provide a factory method createDept.
In some cases the implementing class can be anonymous new Dept() { ... }.
You can use Builder Design pattern with immutable Objects:
public class Class {
private final String name;
private final int id;
private final Department dept;
private Class(ClassBuilder classBuilder){
this.name = classBuilder.getName();
this.id = classBuilder.getId();
this.dept = classBuilder.getDept();
}
public String getName() {
return name;
}
public int getId() {
return id;
}
public Department getDept() {
return dept;
}
private static class Department{
private final int deptId;
private final String deptName;
private final Course course;
private Department(DepartmentBuilder departmentBuilder){
this.deptId = departmentBuilder.getDeptId();
this.deptName = departmentBuilder.getDeptName();
this.course = departmentBuilder.getCourse();
}
public int getDeptId() {
return deptId;
}
public String getDeptName() {
return deptName;
}
public Course getCourse() {
return course;
}
private static class Course{
private final int courseId;
private Course(CourseBuilder courseBuilder){
this.courseId = courseBuilder.getCourseId();
}
public int getCourseId() {
return courseId;
}
}
}
public static class ClassBuilder{
private final String name;
private final int id;
private final Department dept;
public ClassBuilder(String name, int id, Department dept){
this.name = name;
this.id = id;
this.dept = dept;
}
public Department getDept() {
return dept;
}
public String getName() {
return name;
}
public int getId() {
return id;
}
public Class build(){
return new Class(this);
}
}
public static class DepartmentBuilder {
private final int deptId;
private final String deptName;
private final Department.Course course;
public DepartmentBuilder(int deptId, String deptName, Department.Course course ){
this.deptId = deptId;
this.deptName = deptName;
this.course = course;
}
public int getDeptId() {
return deptId;
}
public String getDeptName() {
return deptName;
}
public Department.Course getCourse() {
return course;
}
public Department build(){
return new Department(this);
}
}
public static class CourseBuilder{
private final int courseId ;
public CourseBuilder(int courseId){
this.courseId = courseId;
}
public int getCourseId() {
return courseId;
}
public Department.Course build(){
return new Department.Course(this);
}
}
}
public class Sample {
public static void main(String ... strings){
Class clazz = new Class.ClassBuilder("ClassName", 1, new Class.DepartmentBuilder(1, "departmentName", new Class.CourseBuilder(2).build()).build()).build();
System.out.println(clazz.getDept());
}
}

Getting Null object in HashMap implementation

Iam trying t get values from a HashMap but when i call him and setText the value i always get Null, let me show the code:
MyValues.class
private List<ItemsBean> items;
public List<ItemsBean> getItems() { return items;}
public void setItems(List<ItemsBean> items) { this.items = items; }
public static class ItemsBean {
private Map<String, leakBean> Gitt;
public Map<String, leakBean> getGitt() { return Gitt;}
public void setGitt(Map<String, leakBean> Gitt) { this.Gitt = Gitt;}
public static class leakBean {
private int id;
private String dev;
public int getId() {return id; }
public String getDev(){return dev;}
public void setId(int id) { this.id = id;}
public void setDev(String dev){this.dev = dev;}
}
I´m using Gson so for get the values and use it for .setText or Toast im trying to access like this:
MyValues object;
txt1.setText(String.valueOf( object.getItems().get(0).getGitt().get("id")));
Here i get null, can someone helpme with this? i just need to access to values, also the items.size(); return 1 and must return 3
here is hte JSON:
{
"id": 1001,
"name": "Super1",
"user": {
"name": "The Super 1"
},
"items": [
{
"987987M7812b163eryrt": {
"id": 1,
"dev": "seed"
},
"90812bn120893juuh": {
"id": 2,
"dev": "none"
},
"981273jn19203nj123rg": {
"id": 3,
"dev": "mine"
}
}
]}
Try with these POJOs:
MyValues.java
public class MyValues {
private int id;
private String name;
private User user;
private List<HashMap<String, ItemsBean>> items;
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 User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public List<HashMap<String, ItemsBean>> getItems() {
return items;
}
public void setItems(List<HashMap<String, ItemsBean>> items) {
this.items = items;
}
}
User.java
public class User {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
ItemsBean.java
public class ItemsBean {
private int id;
private String dev;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDev() {
return dev;
}
public void setDev(String dev) {
this.dev = dev;
}
}
You can no access the values like so:
object.getItems().get(0).get("987987M7812b163eryrt").getId();

deep cloning with out serialization

I am very new and learning java,I want to perform deep cloning without serialization ,I read some articles from internet and still in doubt about deep cloning without serialization.So i want to know is there any other rules that I have to follow to do deep cloning, below is my program
Department.java
package com.deepclone;
public class Department {
private int id;
private String name;
public Department(int id, String name) {
this.id = id;
this.name = name;
}
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;
}
}
Employee.java
package com.deepclone;
public class Employee implements Cloneable {
private String employeeId;
private String empName;
private Department department;
public Employee(String employeeId, String empName, Department department) {
this.employeeId = employeeId;
this.empName = empName;
this.department = department;
}
#Override
protected Object clone() throws CloneNotSupportedException {
Employee employee = new Employee(employeeId, empName, new Department(
department.getId(), department.getName()));
return employee;
}
public String getEmployeeId() {
return employeeId;
}
public void setEmployeeId(String employeeId) {
this.employeeId = employeeId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
}
TestCloning.java
package com.deepclone;
public class TestClonning1 {
public static void main(String[] args) throws CloneNotSupportedException {
Department hrDepartment = new Department(10, "HR");
Employee employee = new Employee("1", "rajeev", hrDepartment);
System.out.println(employee.getDepartment().getName());
Employee cloneEmployee = (Employee) employee.clone();
System.out.println(cloneEmployee.getDepartment().getName());
cloneEmployee.getDepartment().setName("it");
System.out.println(employee.getDepartment().getName());
System.out.println(cloneEmployee.getDepartment().getName());
}
}
output
HR
HR
HR
it
is there any other alternative to achive deep cloning without serialization...if yes then give link.
Try this Java Deep-Cloning Library
Cloner cloner = new Cloner();
MyClass other = ...;
MyClass clone = cloner.deepClone(other);

Categories