FasterXML / FasterXML/jackson-core

Can't tell non-blocking parser what charset to use for decoding input

Open
#596 3 comments 0 reactions 0 assignees View on GitHub
documentation
Dominant language
Java
Stars
2.4k
Forks
928
Avg merge
2d 18h
Merged PRs (30d)
24

Description

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:
```java
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 map = mapper.readValue(asyncParser, new TypeReference<>() {});
System.out.println(map);
```
It works fine if the JSON string is encoded with `UTF-8`.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.