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
.