To solve another problem I have moved from using Jersey to EclipseLink MOXy to generate JSON from a JAXB created object model ( created by Sun JAXB 2.1.12). One difference I've noticed is the formatting of the output
that Jersey outputs
{"artist-list":{"offset":0,"count":1,"artist":[{"score":"100","type":"Group","id":"4302e264-1cf0-4d1f-aca7-2a6f89e34b36","name":"Farming Incident","sort-name":"Incident, Farming","gender":"male","country":"AF","disambiguation":"the real one","ipi-list":{"ipi":["1001","1002"]},"life-span":{"begin":"1999-04","ended":"true"},"tag-list":{"tag":[{"count":5,"name":"thrash"},{"count":11,"name":"güth"}]}}]}}
but MOXy gives
"count" : "1",
"offset" : "0",
"artist" : [ {
"id" : "4302e264-1cf0-4d1f-aca7-2a6f89e34b36",
"type" : "Group",
"score" : "100",
"name" : "Farming Incident",
"sort-name" : "Incident, Farming",
"gender" : "male",
"country" : "AF",
"disambiguation" : "the real one",
"ipis" : [ "1001", "1002" ],
"life-span" : {
"begin" : "1999-04",
"ended" : "true"
},
"tags" : [ {
"count" : "5",
"name" : "thrash"
}, {
"count" : "11",
"name" : "güth"
} ]
} ]
}
Moxy is much prettier :) But one of the reasons to move to make our data available via Json is to reduce transmission bandwidth so is it possible to get MOXy to generate all one line, and without the extra spaces around each : ?
By default EclipseLink JAXB (MOXy) will marshal the JSON to one line. To get formatted output you need to set the following property on the
Marshaller
:Root
jaxb.properties
Demo
The following code demonstrates how to specify formatted output:
Output
Below is the output from running the demo code:
JAX-RS
The same behaviour holds for
MOXyJsonProvider
(see: http://blog.bdoughan.com/2012/05/moxy-as-your-jax-rs-json-provider.html).One Line
By default when you include
MOXyJsonProvider
in your JAX-RS application, the output will be marshalled on one line:Formatted Output
You can also configure
MOXyJsonProvider
to produce formatted output: