I have encountered a very annoying error when trying to marshal a class to JSON using Eclipse Moxy.
I have a an attribute with the following value in one of my domain classes: "the City’s original city site"
which contains the code point u+2019 (’)
When Jaxb attempts to marshal this value, I inexplicable get back a strange control: "Citys original city site"
This results in invalid JSON that returns a null value when decoded. I tried this with Jackson, and receive an ascii escape character, which is still wrong, but it at least makes for valid JSON!
Moxy should be able to output this correctly as ’ is a valid unicode character and is valid within JSON. Is there anything that I can do to output the ’ (and any other unicode character) correctly, and preferably converting this needless character to a regular apostrophe.
Here is my provider class:
@Provider
@Component("customMOXyJsonProvider")
public class CustomMOXyJsonProvider extends MOXyJsonProvider {
@Override
protected void preWriteTo(Object object, Class<?> type, Type genericType,
Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, Object> httpHeaders, Marshaller marshaller)
throws JAXBException {
marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, true);
marshaller.setProperty(Marshaller.JAXB_ENCODING,"UTF-8");
}
}
I am using version 2.5.1 of Moxy.
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.moxy</artifactId>
<version>2.5.1</version>
</dependency>
I have several components in my system that could theoretically screw up the value (postgres,jdbc,hibernate,cxf and tomcat), but I have determined through testing that the value is stored correctly in my domain class -and then corrupted, like Elliot Spitzer visitng a prostitute, at the marshaling step.