fastjson自动过滤null值及解决方案
最近在做接口自动化的测试,在调试一个接口的时候出现以下报错
{"error":"/aftersale/create => JSON parse error: Unrecognized token 'channel': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false'); nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'channel': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')\n at [Source: (PushbackInputStream); line: 1, column: 9]","data":null,"return_code":500}
传入的json格式的参数如下
{ "channel": 2, "type": 1, "order_no": "288826819878912", "child_order_no": "288826819878912", "shipment_order_no": "", "user_no": "cszy6NFHGIw50oK2hhT8uO6Du78oJUu92EXgsQMRJic=", "operate_from": 2, "member_phone": "15225951038", "flaw_info_list": [{ "id": null, "images": ["https://XXXXXXXXXX.XXXXXXXX.com/goods_pic_test/c7a087bc23ee04f90bdbfcd2dbf14fa9.jpg", "https://customer-service-center.oss-cn-beijing.aliyuncs.com/goods_pic_test/4b80c480d779849de84c8cebb5f1a2d6.jpg", "https://customer-service-center.oss-cn-beijing.aliyuncs.com/goods_pic_test/c2cb4891a24ed7bf3520bf8000a7e9a5.jpg", "https://customer-service-center.oss-cn-beijing.aliyuncs.com/goods_pic_test/d6926dabb14e936f762678f7db1d9a33.jpg"], "remark": "2", "product_code": "200500374", "erp_code": "6941448605003", "product_name": null, "series_name": null, "new_series_name": null, "problem_pos": 1, "new_product_code": null, "new_erp_code": null, "new_product_name": null }], "send_type": 1, "receiver_info": { "province": "河南省", "city": "洛阳市", "district": "老城区", "detail_address": "测试", "name": "测试-不用发货", "phone": "15225951038" }, "sender_info": { "province": "北京市", "city": "北京市", "district": "朝阳区", "detail_address": "测试地址", "name": "测试-不用发货", "phone": "15225951038", "take_time_end": "2020-10-20 10:00:00", "take_time_start": "2020-10-20 08:00:00" } }
查看请求的request请求信息,发现value为null的key,在转换成map或者jsonObject时,被自动过滤掉了,发现是fastjson自动把value为null的字段过滤了。
解决办法:
设置SerializerFeature序列化属性 SerializerFeature.WriteMapNullValue,输出值为null的字段。
String bodyStr=JSONObject.toJSONString(body, SerializerFeature.WriteMapNullValue);
更多属性
上文中用到的 JSON.toJSONString(Object object, SerializerFeature… features) 方法 SerializerFeature 的属性值还有如下
SerializerFeature属性 | 含义 |
---|---|
UseSingleQuotes | 使用单引号而不是双引号,默认为false |
WriteMapNullValue | 使用输出为null的参数,默认为false |
当value为null时,JSONObject.toJSONString()返回的json字符串将不展示对应的key,这明显不是我们想要的,所以可以使用
SONObject.toJSONString(Object object, SerializerFeature… features) 来获取我们想要的值,SerializerFeature属性对应的值和含义如下:
名称 | 含义 |
---|---|
QuoteFieldNames | 输出key时是否使用双引号,默认为true |
UseSingleQuotes | 使用单引号而不是双引号,默认为false |
WriteMapNullValue | 是否输出值为null的字段,默认为false |
WriteEnumUsingToString | Enum输出name()或者original,默认为false |
UseISO8601DateFormat | Date使用ISO8601格式输出,默认为false |
WriteNullListAsEmpty | List字段如果为null,输出为[],而非null |
WriteNullStringAsEmpty | 字符类型字段如果为null,输出为”“,而非null |
WriteNullNumberAsZero | 数值字段如果为null,输出为0,而非null |
WriteNullBooleanAsFalse | Boolean字段如果为null,输出为false,而非null |
SkipTransientField | 如果是true,类中的Get方法对应的Field是transient,序列化时将会被忽略。默认为true |
SortField | 按字段名称排序后输出。默认为false |
WriteTabAsSpecial | 把\t做转义输出,默认为false 不推荐 |
PrettyFormat | 结果是否格式化,默认为false |
WriteClassName | 序列化时写入类型信息,默认为false。反序列化是需用到 |
DisableCircularReferenceDetect | 消除对同一对象循环引用的问题,默认为false |
WriteSlashAsSpecial | 对斜杠’/’进行转义 |
BrowserCompatible | 将中文都会序列化为\uXXXX格式,字节数会多一些,但是能兼容IE6,默认为false |
WriteDateUseDateFormat | 全局修改日期格式,默认为false。JSON.DEFFAULT_DATE_FORMAT = “yyyy-MM-dd”;JSON.toJSONString(obj,SerializerFeature.WriteDateUseDateFormat); |
DisableCheckSpecialChar | 一个对象的字符串属性中如果有特殊字符如双引号,将会在转成json时带有反斜杠转移符。如果不需要转义,可以使用这个属性。默认为false |
NotWriteRootClassName | 含义 |
BeanToArray | 将对象转为array输出 |
WriteNonStringKeyAsString | 含义 |
NotWriteDefaultValue | 含义 |
BrowserSecure | 含义 |
IgnoreNonFieldGetter | 含义 |
WriteEnumUsingName | 含义 |