从JAXB和Jersey生成更多像json的json [英] Generating more json like json from JAXB and Jersey
问题描述
我使用JAXB创建的数据模型,我可以直接生成XML
I work with a datamodel created using JAXB, from that I can generate XML directly
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\
<metadata xmlns="http://musicbrainz.org/ns/mmd-2.0#" xmlns:ext="http://musicbrainz.org/ns/ext#-2.0">
<artist-list offset="0" count="1">
<artist ext:score="100" type="Group" id="4302e264-1cf0-4d1f-aca7-2a6f89e34b36">
<name>Farming Incident</name>
<ipi-list>
<ipi>1001</ipi>
</ipi-list>
</artist>
</artist-list>
</metadata>
并且在泽西的帮助下也使用自然符号生成JSon
and with the help of Jersey also generate JSon using Natural notation
"artist-list":
{"offset":0,
"count":1,
"artist":[
{"score":"100",
"type":"Group",
"id":"4302e264-1cf0-4d1faca7-2a6f89e34b36",
"name":"Farming Incident",
"ipi-list":
{
"ipi":[
"1001"
]
}
}]
}
Xml很好,json几乎没法,除了因为Json直接支持具有像ipi-list和artist-list这样的元素的数组似乎不是json,是否有可能从我的模型生成更多类似json的json?
The Xml is fine, the json is nearly fine except that because Json directly supports arrays having elements like ipi-list and artist-list doesnt seem very json, is it possible to generate more json like json from my model ?
请求的附加信息
json是从这个MMD模式生成的。
http://svn.musicbrainz.org/mmd-schema/trunk/brainz-mmd2-jaxb/src/main/使用JAXB和Jersey的resources / musicbrainz_mmd-2.0.xsd ,
请参阅
http://svn.musicbrainz.org/search_server/trunk/servlet/src/main/java/org/musicbrainz/search/ servlet / mmd2 / ResultsWriter.java 和
http://svn.musicbrainz.org/search_server/trunk/servlet/src/main/java/org/musicbrainz/search/servlet/mmd2/ArtistWriter.java
重点是我希望能够从一个模式中生成Json和XML,但是显然Json看起来并不正确我正在寻找改善它的方法(我不喜欢我自己有json的经验)
The point is that I want to be able to generate Json and XML from one schema with the minimum of fuss, but apparently the Json doesn't look right so Im looking for a way to improve it (I don't really have any experience of json myself)
推荐答案
注意:我是 EclipseLink JAXB(MOXy) 领导和 JAXB(JSR-222) 专家组。
Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.
您可以利用EclipseLink JAXB(MOXy)中的JSON-Binding和外部映射文档来支持您的用例。
You could leverage the JSON-Binding and external mapping document in EclipseLink JAXB (MOXy) to support your use case.
外部映射文件(oxml.xml)
您可以使用 @XmlPath(。)
在MOXy中扩展以展平对象模型的部分。指定路径。
告诉MOXy将引用的对象包含在父节点中。
You can use the @XmlPath(".")
extension in MOXy to flatten parts of your object model. Specify a path of "."
tells MOXy to include the referenced object in the parent node.
<?xml version="1.0"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="forum10699038">
<java-types>
<java-type name="Metadata">
<java-attributes>
<xml-element java-attribute="artistList" xml-path="."/>
</java-attributes>
</java-type>
<java-type name="Artist">
<java-attributes>
<xml-element java-attribute="ipiList" xml-path="."/>
</java-attributes>
</java-type>
</java-types>
</xml-bindings>
jaxb.properties
要将MOXy指定为JAXB提供程序,您需要在与域模型相同的包中添加名为 jaxb.properties
的文件,并使用以下条目。
To specify MOXy as your JAXB provider you need to add a file called jaxb.properties
in the same package as your domain model with the following entry.
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
演示
以下代码填充对象模型您的XML文档,然后编组为JSON。它演示了如何利用外部映射文件并将MOXy置于JSON模式。
The code below populates the object model from your XML document, and then marshalled to JSON. It demonstrates how to leverage the external mapping file and put MOXy in JSON mode.
package forum10699038;
import java.io.File;
import java.util.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextProperties;
public class Demo {
public static void main(String[] args) throws Exception {
// READ FROM XML
JAXBContext jcXML = JAXBContext.newInstance(Metadata.class);
File xml = new File("src/forum10699038/input.xml");
Unmarshaller unmarshaller = jcXML.createUnmarshaller();
Metadata metadata = (Metadata) unmarshaller.unmarshal(xml);
// WRITE TO JSON
Map<String, Object> properties = new HashMap<String, Object>(3);
properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "forum10699038/oxm.xml");
properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
JAXBContext jcJSON = JAXBContext.newInstance(new Class[] {Metadata.class}, properties);
Marshaller marshaller = jcJSON.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(metadata, System.out);
}
}
输出
{
"artist" : [ {
"id" : "4302e264-1cf0-4d1f-aca7-2a6f89e34b36",
"type" : "Group",
"score" : "100",
"name" : "Farming Incident",
"ipi" : [ "1001" ]
} ]
}
MOXy和泽西
您可以在泽西岛等JAXB-RS环境中轻松使用MOXy作为JSON提供者:
You can easily use MOXy as your JSON provider in a JAXB-RS environment such as Jersey:
- http://blog.bdoughan.com/2012/05/moxy-as-your-jax-rs-json-provider.html
其他文件
以下是您的版本我创建的文件是为了确保一切正常。
Below are versions of your files I created to make sure everything worked properly.
input.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<metadata xmlns="http://musicbrainz.org/ns/mmd-2.0#" xmlns:ext="http://musicbrainz.org/ns/ext#-2.0">
<artist-list offset="0" count="1">
<artist ext:score="100" type="Group"
id="4302e264-1cf0-4d1f-aca7-2a6f89e34b36">
<name>Farming Incident</name>
<ipi-list>
<ipi>1001</ipi>
</ipi-list>
</artist>
</artist-list>
</metadata>
元数据
package forum10699038;
import javax.xml.bind.annotation.*;
@XmlRootElement
public class Metadata {
@XmlElement(name="artist-list")
ArtistList artistList;
}
ArtistList
package forum10699038;
import java.util.List;
public class ArtistList {
private List<Artist> artist;
}
艺术家
package forum10699038;
import javax.xml.bind.annotation.*;
@XmlType(propOrder={"name", "ipiList"})
public class Artist {
@XmlAttribute
private String id;
@XmlAttribute
private String type;
@XmlAttribute(namespace="http://musicbrainz.org/ns/ext#-2.0")
private String score;
@XmlElement(name="ipi-list")
private IPIList ipiList;
private String name;
}
IPList
package forum10699038;
import java.util.List;
public class IPIList {
private List<String> ipi;
}
package-info
@XmlSchema(
namespace = "http://musicbrainz.org/ns/mmd-2.0#",
elementFormDefault = XmlNsForm.QUALIFIED,
xmlns={
@XmlNs(prefix="", namespaceURI = "http://musicbrainz.org/ns/mmd-2.0#")
}
)
@XmlAccessorType(XmlAccessType.FIELD)
package forum10699038;
import javax.xml.bind.annotation.*;
这篇关于从JAXB和Jersey生成更多像json的json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!