I am trying to create java objects from json files. Lets say I have two classes:
Class Parent{
int parentId;
.....
.....
Child c;
public Parent(int parentId, Child c){
this.parentId = parentId;
this.c = c;
}
Class Child{
int childId;
.....
.....
public Child(int childId){
this.childId = childId;
}
Child child = new Child(10);
Parent p = new Animal(1,child);
Now imagine I have two json files:
Parents.json: {"parentId":"1","child":10 } <<10 is the childId used as a reference>>
Children.json: {"childId":10, ...... }
Are there any java libraries that let me get back the original 'p' (Parent) object from these two files?
Also does xml libraries have better unmarshalling capablilites using references than json? In this scenario, would it be easier to represent data in xml and then try to unmarshall the data?
UPDATE: I got it working using Jaxb in xml thanks to this question
@XmlRootElement(name="parent")
@XmlAccessorType(XmlAccessType.FIELD)
class Parent {
private String parentID;
@XmlJavaTypeAdapter(ChildAdapter.class)
private Child child;
public Parent(){ }
public Child getChild() {
return child;
}
public void setChild(Child child) {
this.child = child;
}
public String getParentID() {
return parentID;
}
public void setParentID(String parentID) {
this.parentID = parentID;
}
}
@XmlRootElement(name="child")
@XmlAccessorType(XmlAccessType.FIELD)
class Child {
@XmlAttribute
@XmlID
String childID;
String childName;
public Child(){ }
}