Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make Jackson more friendly for input JSON

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:

  1. An extra , (comma) may appear just before the closing brace.
  2. Strings may be quoted with ' (single quote).
  3. 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.*
  4. Keys can be followed by = or => as well as by :.
  5. Values can be followed by ; (semicolon) as well as by , (comma).
  6. 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)
like image 658
Raman Avatar asked Apr 12 '11 14:04

Raman


2 Answers

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.

like image 55
StaxMan Avatar answered Oct 16 '22 23:10

StaxMan


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 :)

like image 45
overthink Avatar answered Oct 16 '22 23:10

overthink