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.
What went wrong? JSON.parse () parses a string as JSON. This string has to be valid JSON and will throw this error if incorrect syntax was encountered. Both lines will throw a SyntaxError: Omit the trailing commas to parse the JSON correctly: You cannot use single-quotes around properties, like 'foo'.
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
Control characters. If the source data contains control characters, the FOR JSON clause encodes them in the JSON output in u<code> format, as shown in the following table.
How FOR JSON escapes special characters and control characters (SQL Server) 1 Escaping of special characters. If the source data contains special characters, the FOR JSON clause escapes them in the JSON output with \, as shown in the following table. 2 Control characters. ... 3 Learn more about JSON in SQL Server and Azure SQL Database
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With