让Jackson对于输入JSON更加友好

发布于 2024-10-31 19:26:30 字数 1938 浏览 4 评论 0原文

我有办法让 Jackson 对输入 JSON 不那么严格吗?例如 JSONObject 提供以下津贴:

构造者在他们接受的文本中更加宽容:

  1. 右大括号之前可能会出现额外的 ,(逗号)。
  2. 字符串可以用 '(单引号)引起来。
  3. 如果字符串不以引号或单引号开头,并且不包含前导空格或尾随空格,并且不包含以下任何字符:{ } [ ],则根本不需要用引号引起来。 / \ : , = ; # 如果它们看起来不像数字并且它们不是保留字 true、false 或 null。*
  4. 按键后可以跟=或=>;以及作者:.
  5. 值后面可以跟; (分号)以及 , (逗号)。
  6. 数字可能带有 0x-(十六进制)前缀。

对我来说最有趣的是第三点。它允许进行以下转换:

new JSONObject("{A : 1}");

...但对于杰克逊,我会收到相同输入 json 的错误:

new ObjectMapper().readTree("{ A : 1}"); // throws an 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)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

过期情话 2024-11-07 19:26:30

非标准 JSON 的扩展列表(即不是 JSON,但足够接近以支持它)可以从以下位置找到: http://wiki.fasterxml.com/JacksonFeaturesNonStandard

从您的列表中,可以完成 (2) 和 (3)(加上一些未列出的其他内容,例如通信网络)。其他不支持;尽管项目增加了对一些常用扩展的支持,但考虑的内容还是有限的。当然,总是可以要求新功能;根据请求、用例添加功能。

在我个人看来,人们要么遵循标准,要么定义新的格式——HTML 是一个很好的例子,它是人们在试图支持“几乎但不完全”有效的事物时所遇到的老鼠洞。调整是没有止境的,互操作性也会受到影响:由于没有标准,所有实现都支持一些不兼容的功能和结构子集。

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.

无语# 2024-11-07 19:26:30

查看相关问题。它展示了如何配置 ObjectMapper 来执行您想要的操作,并且还对您可能不想这样做的原因进行了一些很好的讨论:)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文