1

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(){ }
}
Community
  • 1
  • 1
zambro
  • 414
  • 1
  • 6
  • 17
  • Why you don't keep the children data inside the file: `Parents.json` ? – My-Name-Is Aug 20 '14 at 16:17
  • Several reasons: Objects of both class are too big, Need to avoid duplicate data for Children class (A Child can be referenced in more than one parent). – zambro Aug 20 '14 at 17:21

1 Answers1

0

You can use JAXB to marshall an unmarshall between XML and Java objects. If you want to work with JSON format too you can use the Jackson-Framwork, which understands the JAXB annotations. See the data entity example below, which works for JAXB and Jackson.


import java.io.Serializable;
import java.util.List;
import java.util.Vector;

import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Addresses implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 8612689101962471499L;

    protected List<Address> addresses = new Vector<Address>();

    public Addresses() {

    }

    public Addresses(final List<Address> addresses) {
        setAddresses(addresses);
    }

    /**
     * Unfortunately it isn't possible to return an unmodifiable collection
     * like: {@code return Collections.unmodifiableList(addresses);} since JAXB
     * will call Collection.clear()!
     * 
     * @return
     */
    @XmlElementRef
    public List<Address> getAddresses() {
        return addresses;
    }

    private void setAddresses(List<Address> addresses) {
        /*
         * TODO: Do validation here!
         */
        this.addresses = addresses;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result
                + ((addresses == null) ? 0 : addresses.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Addresses other = (Addresses) obj;
        if (addresses == null) {
            if (other.addresses != null)
                return false;
        } else if (!addresses.equals(other.addresses))
            return false;
        return true;
    }
}
My-Name-Is
  • 4,814
  • 10
  • 44
  • 84