7

In my API response, I have control-p character. Jackson parser fails to serialize the character and throws an error

com.fasterxml.jackson.core.JsonParseException: Illegal unquoted character ((CTRL-CHAR, code 16)): has to be escaped using backslash to be included in string value

I have investigated and found that Jackson library actually tries to catch for ctrl-char.

Can anyone suggest solutions or work around for this? Thanks in advance.

June Woo Suk
  • 71
  • 1
  • 1
  • 2

3 Answers3

4

I was able to fix similar problem by setting Feature.ALLOW_UNQUOTED_CONTROL_CHARS (documentation) on JsonParser .

The code in my case looks:

parser.setFeatureMask(parser.getFeatureMask() | JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS.getMask());

As stated by others, such JSON is invalid, but in case you have no chance to change JSON, this should help.

3

Have you tried to configure the mapper to force escape non-ASCII?

This might be enough:

mapper.configure(JsonGenerator.Feature.ESCAPE_NON_ASCII, true);

see documentation

But I agree with StaxMan: the JSON response should be well formatted.

Community
  • 1
  • 1
  • Thanks for the answer, but unfortunately, it does not escape control characters. I agree with both of you, but there is nothing I can do with the source. – June Woo Suk Sep 15 '15 at 23:40
  • You really should try filing a bug against whoever is producing malformed request/content, if at all possible. They may not be aware of the problem, and if so, can't fix. I realize that it may not solve your immediate problem, but without reporting such problems these problems will keep on getting worse for everyone. – StaxMan Sep 16 '15 at 18:11
  • 1
    Setting the mapper.configure() as described above RESOLVED my similar issue: Our user may enter Non-ASCII chars in our UI string input fields (comments, etc.). This mapper configuration forces escaping of non-ASCII characters (neutralize it) thus avoiding the JSONParseException. THANKS! – Panini Luncher Feb 11 '18 at 01:20
0

Content you get is not valid JSON -- as per JSON specification, control characters MUST be escaped within String values, and CAN NOT exist outside of them. So I would recommened getting input data fixed; it is corrupt, and whoever is sending it is not doing good job of cleansing it, or properly escaping.

Barring that, you can write a Reader (or even InputStream) that filters out or converts said control characters.

StaxMan
  • 113,358
  • 34
  • 211
  • 239
  • Thanks for the answer! I realize that it is not a valid JSON, but org.json library actually parses it fine unlike Jackson library. So I was actually looking to find a more sophisticated solution. However, I think you right. It might be only way or I go write a custom Jackson library. – June Woo Suk Sep 15 '15 at 23:06
  • No, not custom Jackson library, just underlying Reader/InputStream that cleanses input. I am surprised that `org.json` does not check for this actually, since it tries to catch spec violations. – StaxMan Sep 16 '15 at 18:10