简体   繁体   中英

Case-insensitive JsonNode in Jackson

I need to deserialize JSON objects and access the fields in a case-insensitive manner. Example:

String s = "{\"FOO\": 123}";
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree(s);
node.get("foo"); // this should return the "FOO" field

This needs to be performant, so calling getFieldNames() and lowercasing the results is not a good solution.

没有自动化的方法,但您可以通过创建自定义 JsonNodeFactory 来创建自定义 ObjectNodes —— 然后您可以覆盖用于添加和访问条目的方法。

This feature is available as of 2.5 (Jan 2015):

ACCEPT_CASE_INSENSITIVE_PROPERTIES

Feature that will allow for more forgiving deserialization of incoming JSON. If enabled, the bean properties will be matched using their lower-case equivalents, meaning that any case-combination (incoming and matching names are canonicalized by lower-casing) should work. Note that there is additional performance overhead since incoming property names need to be lower-cased before comparison, for cases where there are upper-case letters. Overhead for names that are already lower-case should be negligible however.

Feature is disabled by default.

https://fasterxml.github.io/jackson-databind/javadoc/2.5/com/fasterxml/jackson/databind/MapperFeature.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM