代码之家  ›  专栏  ›  技术社区  ›  MiketheCalamity

Jackson预处理反序列化

  •  0
  • MiketheCalamity  · 技术社区  · 8 年前

    当我反序列化一个对象时,我想对json(move/change/add字段)进行一些转换,然后继续处理反序列化的对象。这可能吗?

    输入JSON

    {
        "first": "thing",
        "seconds": [ 55, 67, 12 ]
    }
    

    public class MyObject {
        private String new;
        private int second;
    
        // getters and setters
    }
    

    public class MyObjectDeserializer extends JsonDeserializer<MyObject> {
    
        @Override
        public MyObject deserialize(final JsonParser p, final DeserializationContext ctxt) throws IOException {
            JsonNode json = p.getCodec().readTree(p);
            JsonNode translatedJson = translate(json);
    
            // continue processing MyObject like ObjectMapper#readValue would using the translated json
        }
    
        private JsonNode translate(final JsonNode json) {
            ObjectNode object = (ObjectNode) json;
    
            // Update 'first' to 'new'
            object.put("new", object.get("first").asText()).remove("first");
    
            // Find the max in 'seconds' and add it as 'second'
            JsonNode seconds = object.get("seconds");
            int max = 0;
            for (int i = 0; i < seconds.size(); i++) {
                max = Math.max(max, seconds.get(i).asInt());
            }
            object.put("second", max).remove("seconds");
    
            return object;
        }
    }
    
    2 回复  |  直到 8 年前
        1
  •  2
  •   FrantiÅ¡ek Hartman    8 年前

    ObjectCodec 从 JsonParser :

    public class MyObjectDeserializer extends JsonDeserializer<MyObject> {
    
        @Override
        public MyObject deserialize(final JsonParser p, final DeserializationContext ctxt) throws IOException {
            ObjectCodec codec = p.getCodec();
            JsonNode json = coded.readTree(p);
            JsonNode translatedJson = translate(json);
    
            // continue processing MyObject like ObjectMapper#readValue would using the translated json
            return codec.treeToValue(node, MyObject.class);
        }
    
        private JsonNode translate(final JsonNode json) {
            ObjectNode object = (ObjectNode) json;
    
            // Update 'first' to 'new'
            object.put("new", object.get("first").asText()).remove("first");
    
            // Find the max in 'seconds' and add it as 'second'
            JsonNode seconds = object.get("seconds");
            int max = 0;
            for (int i = 0; i < seconds.size(); i++) {
                max = Math.max(max, seconds.get(i).asInt());
            }
            object.put("second", max).remove("seconds");
    
            return object;
        }
    }
    

    请注意,您将无法使用 new 作为Java类中的字段名,因为它是保留关键字。

        2
  •  2
  •   jaco0646    8 年前

    我更喜欢注释 MyObject 而不是实现一个单独的反序列化程序。下面的示例允许您将数据从JSON往返到Java,然后再往返。

    public static void main(String... args) throws Exception {
        String json = "{\"first\": \"thing\", \"seconds\": [55, 67, 12]}";
        ObjectMapper mapper = new ObjectMapper();
        MyObject mo = mapper.readValue(json, MyObject.class);
        System.out.println(mo);
    }
    
    public static class MyObject {
        private final String news;
        private final List<Integer> seconds;
        private final int max;
    
        @JsonCreator
        public MyObject(@JsonProperty("first") String news,
                        @JsonProperty("seconds") List<Integer> seconds) {
            this.news = news;
            this.seconds = seconds;
            this.max = Collections.max(seconds);
        }
    
        public String getNew() {
            return news;
        }
    
        public int getSecond() {
            return max;
        }
    
        @Override
        public String toString() {
            return "{\"first\": \"" + news + "\", \"seconds\": " + seconds + "}";
        }
    }