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(JsonNodeDeserializer.java:180)
at org.codehaus.jackson.map.deser.BaseNodeDeserializer.deserializeAny(JsonNodeDeserializer.java:210)
at org.codehaus.jackson.map.deser.JsonNodeDeserializer.deserialize(JsonNodeDeserializer.java:52)
at org.codehaus.jackson.map.deser.JsonNodeDeserializer.deserialize(JsonNodeDeserializer.java:13)
at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:1588)
at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1130)
List of extensions for non-standard JSON (i.e. stuff that is NOT JSON, but is close enough that it can be supported) can be found from: http://wiki.fasterxml.com/JacksonFeaturesNonStandard
From your list, (2) and (3) can be done (plus couple of other things not listed, like commnets). Others are not supported; and although project has added support for some extensions that are commonly used, there are limits to what will be considered. It is always possible to ask for new features of course; features are added based on request, use cases.
In my personal opinion one should either follow the standard, or define new formats -- HTML is a good example of rat holes one gets to when trying to support things that are "almost but not quite" valid. There is no end to tweaks, and interoperability suffers: since there is no standard, all implementations support some non-compatible subsets of features and constructs.
Check out this related question. It shows how to configure an ObjectMapper
to do what you want, and it also has some good discussion about why you might not want to do that :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With