は現在、これを行うことができる任意の利用可能がありますか?あるいは、既存のシリアライザを修正して$ ref表記でオブジェクト参照を処理する方法はありますか?JSPONのシリアル化のためのJavaサポートはありますか?私は<a href="http://www.jspon.org/" rel="nofollow">JSPON specification.</a></p> <p>に応じて参照を処理できるJava JSPONシリアライザを探しています
2
A
答えて
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
はステートフルなので、Marshaller
とUnmarshaller
にインスタンスを設定する必要があります。
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;
}
}
デモ
以下のコードは、Marshaller
とUnmarshaller
上のステートフル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"
}}
詳細情報
0
多くのJSON直列化ライブラリから1つのオブジェクトを使用します。ライブラリの多くは拡張性がありますが、これらのライブラリをいつ使用するかについて実用的な選択をしない限り、参照の追加は複雑になる可能性があります。
関連する問題
- 1. ObjectMapperが、私は<a href="https://github.com/tristanhimmelman/AlamofireObjectMapper" rel="nofollow">AlamofireObjectMapper</a>を使用しています応答
- 2. は、Java用<a href="http://www.magnolia-cms.com/" rel="nofollow">MAGNOLIA</a></p> <p>CMSを使用する
- 3. 私は<a href="https://www.primefaces.org/primeng/#/" rel="nofollow noreferrer">PrimeNG</a><p> </p><a href="https://www.primefaces.org/" rel="nofollow noreferrer">primefaces.org</a>からすべてがうまく機能している使用しています
- 4. は、私は<a href="https://redom.js.org" rel="nofollow noreferrer">redom</a>ライブラリをインポートしようとしています
- 5. 私の<a href=""></a>は機能していませんか?
- 6. Conguring Hound.ciは、私は私のコードのスタイル警官として<strong><a href="https://houndci.com/" rel="nofollow">Hound CI</a></strong>、と私のCSSの命名のための<strong><a href="http://getbem.com/introduction/" rel="nofollow">BEM</a></strong>を使用していますBEM
- 7. のpython3のデフォルトのエンコーディング私は<a href="http://inginious.readthedocs.io" rel="noreferrer">inginious</a>と呼ばれるフレームワークを使用していますし、そのテンプレートをレンダリングする<a href="http://webpy.org/" rel="noreferrer">web.py</a>を使用しています
- 8. JMSクライアントはJavaベースのみにする必要がありますか? <a href="https://en.wikipedia.org/wiki/Java_Message_Service" rel="nofollow noreferrer">wiki</a>リンクあたり
- 9. 私のプロジェクトには<a href="https://github.com/rustyrobot/Murder-traffic/blob/master/Rakefile" rel="nofollow">Rakefile</a>があります。
- 10. 繰り返し処理<a href="https://scrapy.org/" rel="nofollow noreferrer">Scrapy</a>を使用して文字列
- 11. GL_COLORは、私は次のコードスニペットを持って<a href="http://www.swiftless.com/tutorials/glsl/3_glcolor.html" rel="nofollow">http://www.swiftless.com/tutorials/glsl/3_glcolor.html</a>で探していますWebGLの
- 12. 私は唯一のプロビジョニングfail_whenを失敗する方法を参照してください<a href="http://docs.ansible.com/playbooks_error_handling.html" rel="noreferrer">Ansible error handling</a></p> <p>エラー処理についてのドキュメントを見てみると成功
- 13. シングルDSLは、私は別の表現を生成するために、単一のDSLを使用してのアイデアが好きで、その目的のために、私は現在、<a href="https://github.com/caelum/hypertemplate" rel="nofollow">Hypertemplate</a>が<a href="https://github.com/caelum/restfulie" rel="nofollow">Restfulie</a>に詰め込ま使用してい
- 14. は、私は<a href="http://en.wikipedia.org/wiki/Microsoft_Blend" rel="nofollow noreferrer">Expression Blend</a> 4(<a href="https://en.wikipedia.org/wiki/Windows_Phone" rel="nofollow noreferrer">Windows Phone</a> 7のために)、私は(資産)のすべての行動がロードされている起動すると
- 15. ラップしているものにかかわらず、どの "a"タグがクリックされたときにクリックハンドラを処理する方法は?</p> <pre><code><a href="/foo">bar</a> <a href="/"> <div></div> </a> </code></pre> <p>そして、私はANY「」タグが(jQueryを使って)クリックされたときに処理ハンドラを書きたい:
- 16. Cで書かれたCSSセレクタエンジンはありますか?私は私が探しているものの例としてはC.</p> <p>で書かれたオープンソース<a href="http://www.w3.org/TR/css3-selectors/#selectors" rel="nofollow" title="CSS Selectors">CSS selector</a>エンジンの例を探しています
- 17. は私がquandlをインストールする必要がありました<a href="https://www.youtube.com/channel/UCfzlCWGWYyIQ0aLC5w48gBQ" rel="nofollow noreferrer">sentdex</a>チュートリアルについてはMacOSのシエラ
- 18. 私は<a href="https://github.com/redux-observable/redux-observable" rel="nofollow">redux-observable</a>を使用しています
- 19. は私が<a href="http://livevalidation.com" rel="nofollow">livevalidation</a>プラグインを使用しています
- 20. MPAndroidChart:私は<a href="https://github.com/PhilJay/MPAndroidChart" rel="nofollow noreferrer">MPAndroidChart</a>を使用しています
- 21. は、私が<a href="http://apionrails.icalialabs.com/book/chapter_two" rel="nofollow">APIonRails tutorial</a>を使用しています
- 22. 私は<a href="https://tour.dlang.org/" rel="nofollow noreferrer">D language tour</a>を通じてつもり
- 23. は、私は<a href="https://github.com/nvie/gitflow" rel="noreferrer">git flow</a>を使用し始めていると私はやっていることを理解
- 24. はJQueryの.html()私は<a href="http://onsen.io" rel="nofollow noreferrer">Onsen UI</a> + jQueryのを使用してい
- 25. GWT 2.7コンパイルは今、私は私のプロジェクトをコンパイルしています。この<a href="https://developers.google.com/eclipse/" rel="nofollow noreferrer">link.</a></p> <p>を使用してJava 8にGWT 2.7とJava 7に2.6.1
- 26. 無効化デフォルトでは、材料設計<a href="https://material.google.com/components/bottom-navigation.html" rel="nofollow">Bottom navigation</a>のために、私は<a href="https://github.com/roughike/BottomBar/tree/v1" rel="nofollow">BottomBar</a>ライブラリを使用しているタブ
- 27. 私はこれは私が私のプロジェクトでそれを含めています方法です<a href="https://mdbootstrap.com/" rel="nofollow noreferrer">mdbootstrap</a></p> <p>含まれるようにWebPACKのを使用していますWebPACKの
- 28. .htaccessファイルは、私はDebianの8の上だと私は<a href="http://my-domain.unice.fr/social" rel="nofollow">http://my-domain.unice.fr/social</a>でウェブサイトを持っていると私は私が持っている私の.htaccessで<a href="http://my-domain.unice./" rel="nofollow">http://my-domain.unice./</a></p> <p>でこれをリダイレクトしたい
- 29. 2017年に、ATS設定に関してHTTPSを使用しないLAN要求をどのように処理する必要がありますか?</p> <p><a href="https://i.stack.imgur.com/Iq5tl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Iq5tl.png" alt="ATS settings"></a></p> <p>も参照してください:
- 30. ハイライトは、私は次のコードを持っている<a href="https://github.com/mojombo/jekyll" rel="nofollow">jekyll</a></p> <p>で<a href="http://pygments.org/" rel="nofollow">pygments</a>を使用したい
これは素晴らしいですが、詳細な回答をありがとう! – user842800