I'm using Jackson's non-blocking parser to implement a BodySubscriber for use with Java's non-blocking HTTP client. The parser is created by JsonFactory#createNonBlockingByteArrayParser() using the factory instance associated with the ObjectMapper . It's working like a charm, but it seems that it uses UTF-8 by default and there is no way of telling it other encodings to use (such as the encoding specified by the response headers other than UTF-8).
I figured it might auto-detect the response body's encoding like it's the case with other parsers, but it turned out that it assumes all input is UTF-8. For example, this snippet would crash:
ObjectMapper mapper = new JsonMapper();
byte[] jsonBytes = "{\"Psst!\": \"I'm not UTF-8\"}".getBytes(StandardCharsets.UTF_16);
JsonParser asyncParser = mapper.getFactory().createNonBlockingByteArrayParser();
ByteArrayFeeder feeder = ((ByteArrayFeeder) asyncParser.getNonBlockingInputFeeder());
feeder.feedInput(jsonBytes, 0, jsonBytes.length);
feeder.endOfInput();
Map<String, String> map = mapper.readValue(asyncParser, new TypeReference<>() {});
System.out.println(map);
It works fine if the JSON string is encoded with UTF-8.
I'm using Jackson's non-blocking parser to implement a
BodySubscriberfor use with Java's non-blocking HTTP client. The parser is created byJsonFactory#createNonBlockingByteArrayParser()using the factory instance associated with theObjectMapper. It's working like a charm, but it seems that it usesUTF-8by default and there is no way of telling it other encodings to use (such as the encoding specified by the response headers other thanUTF-8).I figured it might auto-detect the response body's encoding like it's the case with other parsers, but it turned out that it assumes all input is
UTF-8. For example, this snippet would crash:It works fine if the JSON string is encoded with
UTF-8.