【1】保留value为null的对象
默认情况下,fastJSON是过滤调value为null的对象的。可使用如下语法使其保留:
JSON.DEFAULT_GENERATE_FEATURE |= SerializerFeature.WriteMapNullValue.getMask(); or JSON.toJSONString(parseObject, SerializerFeature.WriteMapNullValue); or JSONObject.toJSONString(parseObject, SerializerFeature.WriteMapNullValue);
实例如下:
@Test public void testMd5(){ JSONObject jsonObject = new JSONObject(); jsonObject.put("key1", 12354); jsonObject.put("key2", null); System.out.println(jsonObject.toJSONString()); System.out.println(JSONObject.toJSONString(jsonObject,SerializerFeature.WriteMapNullValue)); System.out.println(JSON.toJSONString(jsonObject,SerializerFeature.WriteMapNullValue)); }
输出结果如下:
{"key1":12354} {"key1":12354,"key2":null} {"key1":12354,"key2":null}
【2】fastJSON中引用的割裂
在进行参数传递时,有时会需要用到参数,但是又不能改变原有参数,这时就需要进行引用割裂。
实例如下:
@Test public void testJSONObject(){ JSONObject params = new JSONObject(); JSONObject result = new JSONObject(); String appid="111"; String secret="222"; String js_code="333"; result.put("appid",appid); result.put("secret",secret); result.put("js_code",js_code); params.put("RESULT",result); //userInfo 从params中脱离出来 String userInfo = params.getString("RESULT"); //user 与 userInfo 不是同一个对象 JSONObject user = JSON.parseObject(userInfo); user.put("ID","11111"); System.out.println("userInfo---"+userInfo); System.out.println("user---"+user.toJSONString()); System.out.println("params---"+params.toJSONString()); JSONObject params2 = JSON.parseObject(params.toJSONString()); params2.put("NAME","JANE"); System.out.println("params2---"+params2.toJSONString()); }
打印结果如下:
【3】JSON中value为正则时,出现"unclosed string XXX"异常
实例如下,将一个字符串解析为JSONObject对象时,fastjson抛出异常:
@Test public void testFastJSON(){ //^.*\d+.*$ String jsonStr = "{\"bo\":\"baseinfo\",\"bc\":\"SYS_DICT_VALUE\",\"DICT_CODE\":\"1111\",\"DICT_VALUE\":\"^.*\\d+.*$\",\"DICT_VALUE_NAME\":\"密码需要包含数字\",\"TENANT_CODE\":\"{TENANT_CODE}\"}"; JSONObject jsonObject = JSON.parseObject(jsonStr); System.out.println(jsonObject); }
解决方案
双重转义让其表示自身(这是思想),修改后如下:
@Test public void testFastJSON(){ //^.*\d+.*$ String jsonStr = "{\"bo\":\"baseinfo\",\"DICT_VALUE\":\"^.*\\\\d+.*$\",\"DICT_VALUE_NAME\":\"密码需要包含数字\"}"; JSONObject jsonObject = JSON.parseObject(jsonStr); System.out.println(jsonObject); System.out.println(jsonObject.toString()); System.out.println(jsonObject.get("DICT_VALUE")); System.out.println(jsonObject.getString("DICT_VALUE")); }
测试结果如下:
{"DICT_VALUE_NAME":"密码需要包含数字","bo":"baseinfo","DICT_VALUE":"^.*\\d+.*$"} {"DICT_VALUE_NAME":"密码需要包含数字","bo":"baseinfo","DICT_VALUE":"^.*\\d+.*$"} ^.*\d+.*$ ^.*\d+.*$
为什么要打印四个结果呢?这里有些细节需要注意。
- System.out.println打印默认会调用String.valueOf(x);方法获取一个字符串并打印;
- String.valueOf(obj);又调用了obj.toString();
所以前面两个打印时会看到最后得到的对象中正则多了一个\
,后面两个结果正常!
原因
为什么会抛出该异常?自行查看JSONLexerBase.scanString()方法!
后续会分析该原因。
【4】springboot下配置fastjson
方式一注入bean
@Bean FastJsonHttpMessageConverter fastJsonHttpMessageConverter(){ FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter(); FastJsonConfig config = new FastJsonConfig(); config.setDateFormat("yyyy-MM-dd HH:mm:ss"); config.setCharset(Charset.forName("UTF-8")); config.setSerializerFeatures( //输出类名 SerializerFeature.WriteClassName, //输出map中value为null的数据 SerializerFeature.WriteMapNullValue, //json格式化 SerializerFeature.PrettyFormat, //输出空list为[],而不是null SerializerFeature.WriteNullListAsEmpty, //输出空string为"",而不是null SerializerFeature.WriteNullStringAsEmpty ); converter.setFastJsonConfig(config); return converter; }
方式二方法覆盖
@Configuration public class MyWebMvcConfig implements WebMvcConfigurer { @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter(); FastJsonConfig config = new FastJsonConfig(); config.setDateFormat("yyyy-MM-dd HH:mm:ss"); config.setCharset(Charset.forName("UTF-8")); config.setSerializerFeatures( //输出类名 SerializerFeature.WriteClassName, //输出map中value为null的数据 SerializerFeature.WriteMapNullValue, //json格式化 SerializerFeature.PrettyFormat, //输出空list为[],而不是null SerializerFeature.WriteNullListAsEmpty, //输出空string为"",而不是null SerializerFeature.WriteNullStringAsEmpty ); converter.setFastJsonConfig(config); converters.add(converter); } }
【4】SerializerFeature那些特性
public static void toJson(Tool result, HttpServletResponse response)throws Exception { response.setContentType("text/json"); response.setHeader("Cache-Control", "no-cache"); response.setCharacterEncoding("UTF-8"); PrintWriter writer = response.getWriter(); JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss"; String json = JSONObject.toJSONString(result, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullNumberAsZero, SerializerFeature.WriteNullListAsEmpty, SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteNullBooleanAsFalse, SerializerFeature.WriteDateUseDateFormat, SerializerFeature.DisableCircularReferenceDetect); writer.write(json); writer.close(); }