zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    phpexcel Could not open for reading! File does not exist.
    tp5 微信JSAPI支付
    ajax返回数据不跳success
    tp5 paginate带参翻页
    php 小程序 前后端POST通信
    小程序页面跳转不同方法
    小程序添加购物车
    小程序商城数量加减效果
    Include conf/extra/httpd-ssl.conf apache 无法启动
    SQL 实验详细源码参考
  • 原文地址:https://www.cnblogs.com/guanbin-529/p/11441061.html
Copyright © 2011-2022 走看看