0

My java app has consumer that gets on input JSON files from server and then I tries convert it using Jackson. But ObjectMapper throws an exception:

com.fasterxml.jackson.databind.JsonMappingException: Invalid UTF-8 middle byte 0x2f

As far as I understand it's is due to incorrect encoding. Can I somehow recognize the encoding and process the server response?

2
  • take a look here stackoverflow.com/questions/6352861/… Commented Feb 6, 2019 at 13:47
  • The server may send a Content-Type header specifying the encoding along with the data. If no encoding is specified, UTF-8 is the default for application/json. In this case fix the server to send correctly encoded Json. Commented Feb 6, 2019 at 14:14

1 Answer 1

0

I was needed to recognize the encoding and correctly process the data. For that I used UniversalDetector from org.mozilla.universalchardet.UniversalDetector

private static final UniversalDetector DETECTOR = new UniversalDetector(null);

private static String getEncode(byte[] data) throws IOException {
    DETECTOR.reset();

    byte[] buf = new byte[data.length];
    InputStream is = new ByteArrayInputStream(data);

    int read;
    while ((read = is.read(buf)) > 0 && !DETECTOR.isDone()) {
        DETECTOR.handleData(buf, 0, read);
    }
    is.close();

    DETECTOR.dataEnd();
    return DETECTOR.getDetectedCharset();
}

And then I read it with correct encode:

private static String readWithEncode(byte[] data, String encoding) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(data), encoding));
    StringBuilder result = new StringBuilder();
    String s;
    while ((s = br.readLine()) != null) {
        result.append(s);
    }
    br.close();
    return result.toString();
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.