• Jackson中处理map中的null key 或者null value 及实体字段中的null value


    1.map中有null key时的序列化

     当有null key时,jackson序列化会报 Null key for a Map not allowed in JSON (use a converting NullKeySerializer?) 

    处理此异常有两种方式

    • 1.需要自定义一个序列化null key的方法
    • 2. map中直接remove null key

    这里只讨论第一种:

    处理方法为 mapper.getSerializerProvider().setNullKeySerializer(new NullKeySerializer()); 将null key处理为空字符

        @Test
        public void testSerializMapNullKey() throws JsonProcessingException {
            //ignore map null value
            Map<String, Object> map = new HashMap<>();
            map.put(null, "test");
            ObjectMapper mapper = new ObjectMapper();
            mapper.getSerializerProvider().setNullKeySerializer(new NullKeySerializer());
            System.out.println(mapper.writeValueAsString(map));
    
        }
    
    
    static class NullKeySerializer extends StdSerializer<Object> {
            public NullKeySerializer() {
                this(null);
            }
    
            public NullKeySerializer(Class<Object> t) {
                super(t);
            }
    
            @Override
            public void serialize(Object nullKey, JsonGenerator jsonGenerator, SerializerProvider unused)
                    throws IOException, JsonProcessingException {
                jsonGenerator.writeFieldName("");
            }
        }
    {"":"test"}
    
    Process finished with exit code 0

    2. 处理null value的情况

        @Test
        public void testIgnoreMapNullValue() throws JsonProcessingException {
            //ignore null key
            Map<String, Object> map = new HashMap<>();
            map.put("key", "test");
            map.put("key1", null);
            ObjectMapper mapper = new ObjectMapper();
            mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
            System.out.println(mapper.writeValueAsString(map));
    
        }

    输出为:{"key":"test"} 并没有 key1:null

    {"key":"test"}
    
    Process finished with exit code 0

    3. 处理实体中filed中值为null的情况

       使用注解:@JsonInclude(JsonInclude.Include.NON_NULL)

       注意:@JsonInclude(JsonInclude.Include.NON_NULL)即可以在实体上用,也可以在filed中使用,比如在name上用这个注解

        @Test
        public void testIgnoreNullFiled() throws JsonProcessingException {
            //test ignore null filed
            User user = new User();
            user.setName(null);
            user.setHouse("asdf");
            ObjectMapper mapper = new ObjectMapper();
            System.out.println(mapper.writeValueAsString(user));
    
        }
    
        /**
         * ignore null filed
         */
        @JsonInclude(JsonInclude.Include.NON_NULL)
        class User {
            private String name;
            private String house;
    
            public String getName() {
                return name;
            }
    
            public void setName(String name) {
                this.name = name;
            }
    
            public String getHouse() {
                return house;
            }
    
            public void setHouse(String house) {
                this.house = house;
            }
        }

    输出为:

    {"house":"asdf"}
    
    Process finished with exit code 0

    如果设置全局怎么设置呢,使用: mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

        @Test
        public void testIgnoreNullFiledGlobally() throws JsonProcessingException {
            //test ignore null filed
            User user = new User();
            user.setName(null);
            user.setHouse("asdf");
            ObjectMapper mapper = new ObjectMapper();
    
            //Ignore Null Fields Globally
            mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
            System.out.println(mapper.writeValueAsString(user));
    
        }

    参考:1.https://www.baeldung.com/jackson-map-null-values-or-null-key

             2.https://github.com/eugenp/tutorials/tree/master/jackson-simple

  • 相关阅读:
    Appium 自动化测试配置wda的两种方式。
    brew install jenkins
    运算符,流程控制语句,单分支,双分支,多分支
    程序交互,数据类型,格式化输出
    编程语言介绍,变量和常量
    “Hello world! ”
    斐波那锲数列 冒泡排序
    AssetBundle
    animation 老动画
    animator 新动画
  • 原文地址:https://www.cnblogs.com/guanbin-529/p/11441061.html
Copyright © 2020-2023  润新知