FastJson生成jsonObject时null字段不显示的处理方法
微wx笑 2019-07-04【库&框架】 14 0关键字: FastJson
初次使用FastJson的小伙伴,可能遇到这样一种情况,生成的JSON字符串少了很多属性,一查发现缺少的属性有一个共同特征,就是值都是Null。
FastJson生成jsonObject时null字段不显示的处理方法
初次使用FastJson的小伙伴,可能遇到这样一种情况,生成的JSON字符串少了很多属性,一查发现缺少的属性有一个共同特征,就是值都是Null。
解决方法:
使用String str =JSONObject.toJSONString(data,SerializerFeature.WriteMapNullValue);
如果是注入的时候呢?参考以下代码:
@Bean public HttpMessageConverters fastJsonHttpMessageConverters(){ //创建FastJson信息转换对象 FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter(); //创建Fastjosn对象并设定序列化规则 FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteMapNullValue); // 中文乱码解决方案 List<MediaType> mediaTypes = new ArrayList<>(); mediaTypes.add(MediaType.APPLICATION_JSON_UTF8);//设定json格式且编码为UTF-8 fastJsonHttpMessageConverter.setSupportedMediaTypes(mediaTypes); //规则赋予转换对象 fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig); return new HttpMessageConverters(fastJsonHttpMessageConverter); }
通过 FastJsonConfig 的 setSerializerFeature 方法,可以设置很多序列化规则。
具体的通过以下源码可以了解到:
/* * Copyright 1999-2018 Alibaba Group. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.alibaba.fastjson.serializer; /** * @author wenshao[szujobs@hotmail.com] */ public enum SerializerFeature { QuoteFieldNames, /** * */ UseSingleQuotes, /** * */ WriteMapNullValue, /** * 用枚举toString()值输出 */ WriteEnumUsingToString, /** * 用枚举name()输出 */ WriteEnumUsingName, /** * */ UseISO8601DateFormat, /** * @since 1.1 */ WriteNullListAsEmpty, /** * @since 1.1 */ WriteNullStringAsEmpty, /** * @since 1.1 */ WriteNullNumberAsZero, /** * @since 1.1 */ WriteNullBooleanAsFalse, /** * @since 1.1 */ SkipTransientField, /** * @since 1.1 */ SortField, /** * @since 1.1.1 */ @Deprecated WriteTabAsSpecial, /** * @since 1.1.2 */ PrettyFormat, /** * @since 1.1.2 */ WriteClassName, /** * @since 1.1.6 */ DisableCircularReferenceDetect, // 32768 /** * @since 1.1.9 */ WriteSlashAsSpecial, /** * @since 1.1.10 */ BrowserCompatible, /** * @since 1.1.14 */ WriteDateUseDateFormat, /** * @since 1.1.15 */ NotWriteRootClassName, /** * @since 1.1.19 * @deprecated */ DisableCheckSpecialChar, /** * @since 1.1.35 */ BeanToArray, /** * @since 1.1.37 */ WriteNonStringKeyAsString, /** * @since 1.1.42 */ NotWriteDefaultValue, /** * @since 1.2.6 */ BrowserSecure, /** * @since 1.2.7 */ IgnoreNonFieldGetter, /** * @since 1.2.9 */ WriteNonStringValueAsString, /** * @since 1.2.11 */ IgnoreErrorGetter, /** * @since 1.2.16 */ WriteBigDecimalAsPlain, /** * @since 1.2.27 */ MapSortField; SerializerFeature(){ mask = (1 << ordinal()); } public final int mask; public final int getMask() { return mask; } public static boolean isEnabled(int features, SerializerFeature feature) { return (features & feature.mask) != 0; } public static boolean isEnabled(int features, int fieaturesB, SerializerFeature feature) { int mask = feature.mask; return (features & mask) != 0 || (fieaturesB & mask) != 0; } public static int config(int features, SerializerFeature feature, boolean state) { if (state) { features |= feature.mask; } else { features &= ~feature.mask; } return features; } public static int of(SerializerFeature[] features) { if (features == null) { return 0; } int value = 0; for (SerializerFeature feature: features) { value |= feature.mask; } return value; } public final static SerializerFeature[] EMPTY = new SerializerFeature[0]; public static final int WRITE_MAP_NULL_FEATURES = WriteMapNullValue.getMask() | WriteNullBooleanAsFalse.getMask() | WriteNullListAsEmpty.getMask() | WriteNullNumberAsZero.getMask() | WriteNullStringAsEmpty.getMask() ; }
本文由 微wx笑 创作,采用 署名-非商业性使用-相同方式共享 4.0 许可协议,转载请附上原文出处链接及本声明。
原文链接:https://www.ivu4e.cn/blog/lib-frame/2019-07-04/24.html
上一篇:返回列表