简体   繁体   中英

Jackson - Use custom deserializer only for specific JSON

We have a class containing multiple Sets of Longs. We want them serialized as arrays, and most clients do so.

However, we have a PHP client that creates sets such that they serialize in an odd way. A set with the number 4 comes in like this:

"setOfNumbers": {
  "4": 0
},

Naturally, Jackson complains about this being an object and not an array. What I would like is to have a custom deserializer that is only invoked if Jackson detects an object where a Set<Long> should be (and ideally only if they are contained in specific classes.)

I've tried this:

    this.addDeserializer(Set.class, new StdDelegatingDeserializer<>(new StdConverter<Map<String, Long>, Set<Long>>() {
        @Override
        public Set<Long> convert(Map<String, Long> set) {
            return parseLongs(set);
        }
    }));

The problem with this is that now it expects an object instead of an array for all Set fields. The class being deserialized is generated, so I can't add any annotations or make other changes.

如果生成的类始终具有相同的名称,则可以尝试使用Json Jackson Mix-in注释,如本所示

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