MOXy JAXB outputs invalid control characters for Unicode (u + 2019) when encoding UTF-8

I came across a very annoying error when trying to marshal the JSON class using Eclipse Moxy.

I have 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 tries to marshal this value, I inexplicably return a weird control: "Citys original city site"

This results in invalid JSON, which returns a null value when decoding. I tried this with Jackson and got the escape ascii character, which is still wrong, but it at least makes JSON valid!

Moxy should be able to output this correctly as a valid Unicode character and valid in JSON. Is there anything I can do to output (and any other Unicode character) correctly and preferably convert this unnecessary 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 from Moxy.

  <dependency> <groupId>org.eclipse.persistence</groupId> <artifactId>org.eclipse.persistence.moxy</artifactId> <version>2.5.1</version> </dependency> 

I have several components on my system that could theoretically mess up the value (postgres, jdbc, hibernate, cxf and tomcat), but I decided through testing that the value is correctly stored in my domain class and then corrupted like Elliot Spitzer visited a prostitute at the marshaling stage.

+6
source share
1 answer

Note. I am an EclipseLink JAXB (MOXy) and a member of the JAXB Group (JSR-222) .

UPDATE # 3

The issue is now fixed in EclipseLink 2.5.2 and 2.6.0 threads. You can download the nightly build starting October 10, 2013 from the following location:

Or from Maven using

 <dependency> <groupId>org.eclipse.persistence</groupId> <artifactId>org.eclipse.persistence.moxy</artifactId> <version>2.5.2-SNAPSHOT</version> </dependency> 

and

 <repository> <id>oss.sonatype.org</id> <name>OSS Sonatype Staging</name> <url>https://oss.sonatype.org/content/groups/staging</url> </repository> 

UPDATE # 2

The following error can be used to track our progress on this issue:


UPDATE # 1

You are using business work in EclipseLink 2.5.0. The performance fix we made in EclipseLink 2.5.1 crashes:


ORIGNAL ANSWER

It seems that the error in our marshalling of OutputStream missing in our marshalling for Writer for JSON (XML is working correctly). Below is my quick inquiry. I will update my answer as soon as I have additional information.

Java Model

 public class Foo { private String bar; public String getBar() { return bar; } public void setBar(String bar) { this.bar = bar; } } 

Demo code

 import java.io.OutputStreamWriter; import java.util.*; import javax.xml.bind.*; import javax.xml.bind.Marshaller; import org.eclipse.persistence.jaxb.JAXBContextProperties; public class Demo { public static void main(String[] args) throws Exception { Map<String, Object> properties = new HashMap<String, Object>(); properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json"); properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false); JAXBContext jc = JAXBContext.newInstance(new Class[] {Foo.class}, properties); Foo foo = new Foo(); foo.setBar("the City's original city site"); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); // Broken marshaller.marshal(foo, System.out); // Works marshaller.marshal(foo, new OutputStreamWriter(System.out)); } } 

Output

 { "bar" : "the Citys original city site" }{ "bar" : "the City's original city site" } 
+3
source

Source: https://habr.com/ru/post/955526/


All Articles