2

I have a json string with SUBSTITUTE () utf-8 character. I'm getting parsing exception when I try to convert json string to java object using jackson. Can you please let me know how to encode and decode utf-8 characters ?

ObjectMapper mapper = new ObjectMapper();
mapper.readValue(jsonString, MY_DOMAIN_OBJECT.class);

jsonString:

{"studentId":"753253-2274", "information":[{"key":"1","value":"Get alerts on your phone(SUBSTITUTE character is present here. Unable to paste it)To subscribe"}]}

enter image description here

Error:

Illegal unquoted character ((CTRL-CHAR, code 26)): has to be escaped using backslash to be included in string value
10
  • Can you please post the actual error? Commented Apr 8, 2019 at 15:18
  • Have you verified that it's really the \u001A that causes trouble? Couldn't it be some mismatch between your domain class and the JSON structure instead? Commented Apr 8, 2019 at 15:19
  • Updated error code. Its not an issue with mismatch. @Mena Commented Apr 8, 2019 at 15:31
  • 1
    Ah, of course. It's a control character. In JSON syntax you have to escape it. The error text actually tells you that, though technically not with a backslash but with a full unicode escape sequence. Commented Apr 8, 2019 at 15:35
  • Have you tried this? Seems the problem is the same. Commented Apr 8, 2019 at 15:37

1 Answer 1

4

Can you try this?

ObjectMapper mapper = new ObjectMapper();
mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
mapper.readValue(jsonString, MY_DOMAIN_OBJECT.class);

I hope it helps you: Javadoc

Feature that determines whether parser will allow JSON Strings to contain unquoted control characters (ASCII characters with value less than 32, including tab and line feed characters) or not. If feature is set false, an exception is thrown if such a character is encountered. Since JSON specification requires quoting for all control characters, this is a non-standard feature, and as such disabled by default.

Sign up to request clarification or add additional context in comments.

2 Comments

This converts my SUBSTITUTE symbol to java encoding (\u001A). But I want to convert to html encoding () since I need to use this information on HTML page. Is there any way to achieve this? @madplay fileformat.info/info/unicode/char/001a/index.htm
Worked for me thanks. Looks like ALLOW_UNQUOTED_CONTROL_CHARS is deprecated, use JsonReadFeature.ALLOW_UNESCAPED_CONTROL_CHARS.mappedFeature() instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.