14
Do I have a way to make Jackson less exacting to the input JSON. E.g. JSONObject provides following allowances:
The constructors are more forgiving in the texts they will accept:
- An extra , (comma) may appear just before the closing brace.
- Strings may be quoted with ' (single quote).
- Strings do not need to be quoted at all if they do not begin with a quote or single quote, and if they do not contain leading or trailing spaces, and if they do not contain any of these characters: { } [ ] / \ : , = ; # and if they do not look like numbers and if they are not the reserved words true, false, or null.*
- Keys can be followed by = or => as well as by :.
- Values can be followed by ; (semicolon) as well as by , (comma).
- Numbers may have the 0x- (hex) prefix.
The most interesting for me is 3rd point. It allows to following conversion:
new JSONObject("{A : 1}");
... but for jackson I will get an error with the same input json:
new ObjectMapper().readTree("{ A : 1}"); // throws an exception
Exception:
org.codehaus.jackson.JsonParseException: Unexpected character ('A' (code 65)): was expecting double-quote to start field name
at [Source: java.io.StringReader@26d4f1; line: 1, column: 4]
at org.codehaus.jackson.JsonParser._constructError(JsonParser.java:943)
at org.codehaus.jackson.impl.JsonParserBase._reportError(JsonParserBase.java:636)
at org.codehaus.jackson.impl.JsonParserBase._reportUnexpectedChar(JsonParserBase.java:569)
at org.codehaus.jackson.impl.ReaderBasedParser._handleUnusualFieldName(ReaderBasedParser.java:342)
at org.codehaus.jackson.impl.ReaderBasedParser._parseFieldName(ReaderBasedParser.java:235)
at org.codehaus.jackson.impl.ReaderBasedParser.nextToken(ReaderBasedParser.java:125)
at org.codehaus.jackson.map.deser.BaseNodeDeserializer.deserializeObject