I'm trying to parse byte array (retrieved from HttpServletRequest POST 
body) to JsonNode object. Some clients uses dirty encoding that results a 
JsonParseException: 
Invalid UTF-8 middle byte 0x28. If I convert the byte array to string and 
then parse it using the same objectMapper I wouldn't have a problem. But 
this would add extra execution time to the service (small but not 
insignificant for the application I have). The dirty encoding occurs with 
only two fields and it is acceptable for the application to use null for 
these fields when that happens. 

The question: is there a flag to configure Jackson to use null for fields 
that throw this exception (following the code suggests there is none). Or 
is it possible to implement custom parser or encoder for these fields that 
support the required behavior. 

Sample code to reproduce: 


ObjectMapper objectMapper = new ObjectMapper(); 
objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true); 
objectMapper.configure(JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER,
 true); 
String s = "{\"id\":\"33226\",\"name\":\"Some Name\"}"; 
byte[] bytes = s.getBytes(); 

// simulating the dirty encoding
bytes[9] = (byte) 0xc3; 
bytes[10] = (byte) 0x28; 

//converting bytes to String and then parsing it works fine
System.out.println("Parsing String:"); 
JsonNode tree1 = objectMapper.readTree(new String(bytes, "UTF8")); 
System.out.println(tree1); 


try {
  // but parsing the dirty byte array directly throws an Exception
  System.out.println("Parsing byte array:"); 
  JsonNode tree2 = objectMapper.readTree(bytes); 
  System.out.println(tree2); 
} catch (Exception e) { 
  e.printStackTrace(); 
}



Output:
Parsing String:
{"id":"33�(6","name":"Some Name"}
Parsing byte array:
com.fasterxml.jackson.core.JsonParseException: Invalid UTF-8 middle byte 
0x28
 at [Source: (byte[])"{"id":"33�(6","name":"Some Name"}"; line: 1, column: 
12]
 at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:
1798)
 at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(
ParserMinimalBase.java:663)
 at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._reportInvalidOther
(UTF8StreamJsonParser.java:3544)
 at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._reportInvalidOther
(UTF8StreamJsonParser.java:3551)
 at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._decodeUtf8_2(
UTF8StreamJsonParser.java:3324)
 at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._finishString2(
UTF8StreamJsonParser.java:2456)

-- 
You received this message because you are subscribed to the Google Groups 
"jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to