2012-03-25 45 views

回答

0

說明:我是EclipseLink JAXB (MOXy)的領導者,也是JAXB 2 (JSR-222)專家組的成員。

如果您對對象JSON綁定方法感興趣,下面是如何使用MOXy完成的。下面的例子是基於例如一種從JSPON核心規格:

Parent類是對應於JSON的根域對象信息。它有兩個字段,類型爲Child

package forum9862100; 

import javax.xml.bind.annotation.*; 

@XmlAccessorType(XmlAccessType.FIELD) 
public class Parent { 

    protected Child field1; 
    protected Child field2; 

} 

兒童

Child類可以通過其鍵引用。我們將用XmlAdapter來處理這個用例。我們通過@XmlJavaTypeAdapter註釋鏈接到XmlAdapter

package forum9862100; 

import javax.xml.bind.annotation.*; 
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; 

@XmlJavaTypeAdapter(ChildAdapter.class) 
@XmlAccessorType(XmlAccessType.FIELD) 
public class Child { 

    protected String id; 
    protected String foo; 
    protected Integer bar; 

} 

ChildAdapter

下面是XmlAdapter的實施。這XmlAdapter是有狀態的,這意味着我們需要在MarshallerUnmarshaller上設置一個實例。下面

package forum9862100; 

import java.util.*; 

import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.adapters.XmlAdapter; 

public class ChildAdapter extends XmlAdapter<ChildAdapter.AdaptedChild, Child>{ 

    private List<Child> childList = new ArrayList<Child>(); 
    private Map<String, Child> childMap = new HashMap<String, Child>(); 

    public static class AdaptedChild extends Child { 
     @XmlElement(name="$ref") 
     public String reference; 
    } 

    @Override 
    public AdaptedChild marshal(Child child) throws Exception { 
     AdaptedChild adaptedChild = new AdaptedChild(); 
     if(childList.contains(child)) { 
      adaptedChild.reference = child.id; 
     } else { 
      adaptedChild.id = child.id; 
      adaptedChild.foo = child.foo; 
      adaptedChild.bar = child.bar; 
      childList.add(child); 
     } 
     return adaptedChild; 
    } 

    @Override 
    public Child unmarshal(AdaptedChild adaptedChild) throws Exception { 
     Child child = childMap.get(adaptedChild.reference); 
     if(null == child) { 
      child = new Child(); 
      child.id = adaptedChild.id; 
      child.foo = adaptedChild.foo; 
      child.bar = adaptedChild.bar; 
      childMap.put(child.id, child); 
     } 
     return child; 
    } 

} 

演示

的代碼演示如何指定在MarshallerUnmarshaller有狀態XmlAdapter

package forum9862100; 

import java.io.File; 
import javax.xml.bind.*; 
import javax.xml.transform.stream.StreamSource; 

public class Demo { 

    public static void main(String[] args) throws Exception { 
     JAXBContext jc = JAXBContext.newInstance(Parent.class); 

     StreamSource json = new StreamSource(new File("src/forum9862100/input.json")); 
     Unmarshaller unmarshaller = jc.createUnmarshaller(); 
     unmarshaller.setProperty("eclipselink.media-type", "application/json"); 
     unmarshaller.setProperty("eclipselink.json.include-root", false); 
     unmarshaller.setAdapter(new ChildAdapter()); 
     Parent parent = (Parent) unmarshaller.unmarshal(json, Parent.class).getValue(); 

     System.out.println(parent.field1 == parent.field2); 

     Marshaller marshaller = jc.createMarshaller(); 
     marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
     marshaller.setProperty("eclipselink.media-type", "application/json"); 
     marshaller.setProperty("eclipselink.json.include-root", false); 
     marshaller.setAdapter(new ChildAdapter()); 
     marshaller.marshal(parent, System.out); 
    } 

} 

輸出

下面是從運行輸出dem o代碼。請注意0​​的兩個實例是如何通過身份驗證的。

true 
{ 
    "field1" : { 
     "id" : "2", 
     "foo" : "val", 
     "bar" : 4 
    }, 
    "field2" : { 
     "$ref" : "2" 
    }} 

更多信息

+1

這是偉大的,謝謝你的詳細解答! – user842800 2012-05-09 10:56:35

0

我會使用許多Object to JSon序列化庫中的一個。許多庫是可擴展的,但我懷疑添加引用可能會變得複雜,除非您就何時使用這些庫進行務實的選擇。

相關問題