Json使用Java中的引用进行反序列化



我试图从json文件创建java对象。假设我有两个类:

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);
现在假设我有两个json文件:
Parents.json: {"parentId":"1","child":10 } <<10 is the childId used as a reference>>
Children.json: {"childId":10, ...... }

是否有任何java库让我从这两个文件中获得原始的'p'(父)对象?

也xml库有更好的解组能力使用引用比json?在这种情况下,用xml表示数据然后尝试解组数据是否更容易?

更新:我得到了它的工作使用Jaxb在xml感谢这个问题

@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(){ }
}

可以使用JAXB编排XML和Java对象之间的解组。如果你也想使用JSON格式,你可以使用jackson - framework,它可以理解JAXB注释。参见下面的数据实体示例,它适用于JAXBJackson


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;
    }
}

最新更新