2012-03-25 1 views

答えて

0

注:私はEclipseLink JAXB (MOXy)となり、JAXB 2 (JSR-222)専門家グループのメンバーです。

オブジェクト-JSONバインディングアプローチに興味がある場合は、MOXyを使用してこれを行う方法を以下に示します。例を以下にJSPONコア仕様の一例に基づいている:

Parentクラスは、JSONのルートに対応するドメインオブジェクトでありますメッセージ。それはタイプChildの2つのフィールドを持っています。

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

} 

出力以下

ランニングから出力され、デモoコード。 Childの2つのインスタンスが同一性テストにどのように合格したかに注意してください。

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

+1

これは素晴らしいですが、詳細な回答をありがとう! – user842800

0

多くのJSON直列化ライブラリから1つのオブジェクトを使用します。ライブラリの多くは拡張性がありますが、これらのライブラリをいつ使用するかについて実用的な選択をしない限り、参照の追加は複雑になる可能性があります。

関連する問題

 関連する問題