threejeez
threejeez

Reputation: 2324

ObjectMapper Not Handling UTF-8 Properly?

I'm using ObjectMapper to serialize posts in my system to json. These posts contain entries from all over the world and contain utf-8 characters. The problem is that the ObjectMapper doesn't seem to be handling these characters properly. For example, the string "Musée d'Orsay" gets serialized as "Mus?©e d'Orsay".

Here's my code that's doing the serialization:

public static String toJson(List<Post> posts) {
        ObjectMapper objectMapper = new ObjectMapper()
            .configure(Feature.USE_ANNOTATIONS, true);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            objectMapper.writeValue(out, posts);
        } catch (JsonGenerationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JsonMappingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return new String(out.toByteArray());
    }

Interestingly, the exact same List<Post> posts gets serialized just fine when I return it via a request handler using @ResponseBody using the following configuration:

public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    ObjectMapper m = new ObjectMapper()
        .enable(Feature.USE_ANNOTATIONS)
        .disable(Feature.FAIL_ON_UNKNOWN_PROPERTIES);
    MappingJacksonHttpMessageConverter c = new MappingJacksonHttpMessageConverter();
    c.setObjectMapper(m);
    converters.add(c);
    super.configureMessageConverters(converters);
}

Any help greatly appreciated!

Upvotes: 3

Views: 25719

Answers (2)

StaxMan
StaxMan

Reputation: 116630

Aside from conversions, how about simplifying the process to:

return objectMapper.writeValueAsString(posts);

which speeds up the process (no need to go from POJO to byte to array to decode to char to build String) as well as (more importantly) shortens code.

Upvotes: 2

threejeez
threejeez

Reputation: 2324

Not 10 minutes later and I found the problem. The issue wasn't with the ObjectMapper, it was with how I was turning the ByteArrayOutputStream into a string. I changed the code as follows and everything started working:

try {
        return out.toString("utf-8");
    } catch (UnsupportedEncodingException e) {
        return out.toString();
    }

Upvotes: 1

Related Questions