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>
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"
]
}
}]
}
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 ?
Additional Information as Requested
The json is generated from this MMD schema
http://svn.musicbrainz.org/mmd-schema/trunk/brainz-mmd2-jaxb/src/main/resources/musicbrainz_mmd-2.0.xsd using JAXB and Jersey ,
see
http://svn.musicbrainz.org/search_server/trunk/servlet/src/main/java/org/musicbrainz/search/servlet/mmd2/ResultsWriter.java and
http://svn.musicbrainz.org/search_server/trunk/servlet/src/main/java/org/musicbrainz/search/servlet/mmd2/ArtistWriter.java
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)
Note: I’m the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.
You could leverage the JSON-Binding and external mapping document in EclipseLink JAXB (MOXy) to support your use case.
External Mapping File (oxml.xml)
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.jaxb.properties
To specify MOXy as your JAXB provider you need to add a file called
jaxb.propertiesin the same package as your domain model with the following entry.Demo
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.
Output
MOXy and Jersey
You can easily use MOXy as your JSON provider in a JAXB-RS environment such as Jersey:
OTHER FILES
Below are versions of your files I created to make sure everything worked properly.
input.xml
Metadata
ArtistList
Artist
IPList
package-info