json序列化时忽略值为null的字段2种方式实例
作者:程序猿不脱发2
这篇文章主要给大家介绍了关于json序列化时忽略值为null的字段的2种方式,当对象中某个字段为null时,我们希望将对象转换成json时为null的字段不会被转换到json字符串,里面需要的朋友可以参考下
当一个对象里有些属性值为null 的不想参与json序列化时,比如打印日志等场景进行序列化,null字段会占用日志长度。 可以采用如下两种方式:
方法一:JsonInclude.Include.NON_NULL 注解
在类上面增加 @JsonInclude(JsonInclude.Include.NON_NULL)
示例:
//这个是类注解,表示该类实例化的对象里,值为null的字段不参与序列化 @JsonInclude(JsonInclude.Include.NON_NULL) @Data public class Person { private Long id; private String name; } public static void main(String[] args) { Person p1 = new Person(); p1.setId(1L); p1.setName("test1"); System.out.println("p1: " +JSONObject.toJSON(p1)); Person p2 = new Person(); p2.setId(1L); System.out.println("p2: " +JSONObject.toJSON(p2)); }
输出:
p1: {"name":"test1","id":1}
p2: {"id":1}
方法2:自定义一个ObjectMapper
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
自定义一个JsonUtils工具类:
可以解析字符串,array, map等
@Slf4j public class JsonUtils { private static final String EMPTY_JSON = "{}"; private static final String EMPTY_ARRAY_JSON = "[]"; private static final ObjectMapper MAPPER = new ObjectMapper(); static { MAPPER.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); } public static String toJSON(@Nullable Object obj) { if (obj == null) { return null; } try { return MAPPER.writeValueAsString(obj); } catch (JsonProcessingException e) { log.error("toJson error, ", e); throw new BusinessException(e.getMessage()); } } public static <T> T parseObject(@Nullable String json, Class<T> valueType) { if (json == null) { return null; } try { return MAPPER.readValue(json, valueType); } catch (IOException e) { log.error("parse object error, ", e); throw new BusinessException(e.getMessage()); } } public static <E, T extends Collection<E>> T parseArray(String json, Class<? extends Collection> collectionType, Class<E> valueType) { if (StringUtils.isEmpty(json)) { json = EMPTY_ARRAY_JSON; } try { return MAPPER.readValue(json, defaultInstance().constructCollectionType(collectionType, valueType)); } catch (IOException e) { log.error("parseArray error, ", e); throw new BusinessException(e.getMessage()); } } public static <T> List<T> parseArray(String json, Class<T> valueType) { return parseArray(json, List.class, valueType); } /** * */ public static <K, V, T extends Map<K, V>> T parseMap(String json, Class<? extends Map> mapType, Class<K> keyType, Class<V> valueType) { if (StringUtils.isEmpty(json)) { json = EMPTY_JSON; } try { return MAPPER.readValue(json, defaultInstance().constructMapType(mapType, keyType, valueType)); } catch (IOException e) { log.error("parseMap error, ", e); throw new BusinessException(e.getMessage()); } } public static Map<String, Object> parseMap(String string) { return parseMap(string, Map.class, String.class, Object.class); } }
测试
public static void main(String[] args) { Person p1 = new Person(); p1.setId(1L); p1.setName("test1"); System.out.println("p1: " +JsonUtils.toJSON(p1)); Person p2 = new Person(); p2.setId(1L); System.out.println("p2: " +JsonUtils.toJSON(p2)); } //输出: // p1: {"id":1,"name":"test1"} // p2: {"id":1}
总结
到此这篇关于json序列化时忽略值为null的字段的文章就介绍到这了,更多相关json序列化忽略值为null字段内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!