3

I am trying to parse a JSON which is obtained from rest service via RestTemplate.

When I try to parse a JSON which contains a newline or tab character, it is throwing the following exception:

org.codehaus.jackson.JsonParseException: Illegal unquoted character ((CTRL-CHAR, code 13)): has to be escaped using backslash to be included in string value
at [Source: java.io.StringReader@a8f373; line: 1, column: 663]
at org.codehaus.jackson.JsonParser._constructError(JsonParser.java:1433)
at org.codehaus.jackson.impl.JsonParserMinimalBase._reportError(JsonParserMinimalBase.java:521)
at org.codehaus.jackson.impl.JsonParserMinimalBase._throwUnquotedSpace(JsonParserMinimalBase.java:482)
at org.codehaus.jackson.impl.ReaderBasedParser._skipString(ReaderBasedParser.java:1416)
at org.codehaus.jackson.impl.ReaderBasedParser.nextToken(ReaderBasedParser.java:366)
at com.demo.myapp.JsonParsingUtil.parseJson(JsonParsingUtil.java:67)  at java.lang.Thread.run(Thread.java:745)

The code is working fine in my local server [Windows OS, JBoss server].

When I deploy the code in the [Linux OS, JBoss server], this parsing exception pops out.

The issue scenario is:

  • User enters some data via webapp in Windows environment.
  • I access the data via REST service
  • Data is parsed to retrieve some information and new JSON created with the info and passes to some other team.

I am using JsonParser to parse the JSON, and I have set the features as:

JsonFactory jsonFactory = new MappingJsonFactory();
JsonParser jsonParser = jsonFactory.createJsonParser(jsonString);
jsonParser.configure(JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER, true);
jsonParser.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);

I have also tried like this:

byte[] byteArray = jsonData.getBody().getBytes(); 
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(byteArray.length); 
byteArrayOutputStream.write(byteArray, 0, byteArray.length); 
jsonString = new String(byteArray, Charset.forName("UTF-8"));

I have set UTF-8 encoding in RestTemplate object, which is used in obtaining the JSON from the REST service:

RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));

Since my JSON contains a lot of {key:values} using JsonGenerator is not a viable option. I hope somebody can show me the right direction to solve the issue.

Please find the code on which I'm working :

        JsonFactory jsonFactory = new MappingJsonFactory();
        JsonParser jsonParser = jsonFactory.createJsonParser(jsonString);
        jsonParser.configure(JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER, true);
        jsonParser.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
        while (!jsonParser.isClosed()) {
            JsonToken jsonToken = jsonParser.nextToken();
            if (JsonToken.FIELD_NAME.equals(jsonToken)) {
                String fieldName = jsonParser.getCurrentName();
                if (fieldName.equals("Comments")) {
                    String code = jsonParser.nextTextValue();
                }
            }
        }

And jsonString obtaining from REST template is:

{"Comments":"Approved.
            Please proceed"}

which is

Approved. NewLine Char Please proceed

NOTE: I am using Codehaus jackson-1.9.13 library for JsonParsing.

informatik01
  • 16,038
  • 10
  • 74
  • 104
RahulArackal
  • 944
  • 12
  • 28
  • Not answering your actual issue, but you probably want to upgrade Jackson to a more recent version. It hasn't been on codehaus for a while now. – Mena Nov 21 '16 at 13:09
  • Also, you may want to share a minimal example of the payload failing parsing. – Mena Nov 21 '16 at 13:10
  • @Mena I have updated my problem with code and sample input. And my manager may oppose changing to other jackson libraries. But all inputs are welcome :) – RahulArackal Nov 21 '16 at 13:29
  • Your JSON payload seems invalid as is. To use newlines or line feeds, you can escape the value, e.g. with `%0A` or `\n` etc. – Mena Nov 21 '16 at 13:36
  • I have tried to replace all "\\r\\n" with "\n", but it doesn't seem to work. And also the json is from a third party and i receive it as given above. – RahulArackal Nov 21 '16 at 14:33

0 Answers0