I’m using jax-rs and adding entity for being marshalled in Jetty by JAXRSOutInterceptor,
but JSON output is being modified with additional empty property which looks like this:
“$”: “”
The JSONProvider is created and configured as here:
JSONProvider jsonProvider = new JSONProvider();
jsonProvider.setConvertTypesToStrings(true);
jsonProvider.setIgnoreNamespaces(true);
jsonProvider.setIgnoreMixedContent(true);
jsonProvider.setUnmarshallAsJaxbElement(true);
providers.add(jsonProvider);
It is also being marshalled to XML which uses namespaces but I don’t want them in JSON output and input.
The object that is being marshalled is similar to this:
@XmlRootElement(name="myObject1")
@XmlAccessorType(XmlAccessType.FIELD)
@SuppressWarnings("serial")
public class MyObject1 implements Serializable {
MyObject2 a;
MyObject2 b;
MyObject2 c;
// includes getters, setters, hashCode, equals, toString,
}
When MyObject2 is:
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@SuppressWarnings("serial")
public class MyObject2 implements Serializable {
String x;
String y;
List<String> z;
// includes getters, setters, hashCode, equals, toString,
}
The rest output is as following:
{
"myObject1": {
"a": {
"x": "value1",
"y": "value2",
"z": "value3",
"$": ""
},
"$": ""
}
}
How do I get rid of the ending “$”: “”
I read that the Jettison (which is the default JSONProvider implementation that I am using) is by default will represent properties mapped with @XmlValue as ‘$’s but there’s no property ?
Is that caused by implementing Serializable?
Note: I’m the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.
Jettison isn’t really a JSON provider but an API that can allow XML libraries to produce/consume JSON. It does a decent job, but there are problems that can occur like the one you are experiencing now. You are also seeing the issues where lists of size one are not marshalled as JSON arrays.
If you can’t find a way to make this work with your current set up. Below is what you could do with MOXy as your JSON provider:
MyObject1
MyObject2
jaxb.properties
To specify MOXy as your JAXB provider you need to include a file called
jaxb.propertiesin the same package as your domain model with the following entry:Demo
Below is some standalone code to demonstrate the reading/writing of the JSON.
input.json/Output
The resulting JSON message contains no
"$"properties and the list of size 1 is property represented as a JSON array.JAX-RS Integration
MOXy includes the MOXyJsonProvider class that makes it easy to configure in your JAX-RS application: