fastjson的SerializerFeature
2016-02-25 10:57
405 查看
0X00
先来一下SerializerFeature的源码吧。/* * Copyright 1999-2101 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,//输出key时是否使用双引号,默认为true /** * */ UseSingleQuotes,//使用单引号而不是双引号,默认为false /** * */ WriteMapNullValue,//是否输出值为null的字段,默认为false /** * */ WriteEnumUsingToString,//Enum输出name()或者original,默认为false /** * */ UseISO8601DateFormat,//Date使用ISO8601格式输出,默认为false /** * @since 1.1 */ WriteNullListAsEmpty,//List字段如果为null,输出为[],而非null /** * @since 1.1 */ WriteNullStringAsEmpty,//字符类型字段如果为null,输出为"",而非null /** * @since 1.1 */ WriteNullNumberAsZero,//数值字段如果为null,输出为0,而非null /** * @since 1.1 */ WriteNullBooleanAsFalse,//Boolean字段如果为null,输出为false,而非null /** * @since 1.1 */ SkipTransientField,//如果是true,类中的Get方法对应的Field是transient,序列化时将会被忽略。默认为true /** * @since 1.1 */ SortField,//按字段名称排序后输出。默认为false /** * @since 1.1.1 */ @Deprecated WriteTabAsSpecial,//把\t做转义输出,默认为false /** * @since 1.1.2 */ PrettyFormat,//结果是否格式化,默认为false /** * @since 1.1.2 */ WriteClassName,//序列化时写入类型信息,默认为false。反序列化是需用到 /** * @since 1.1.6 */ DisableCircularReferenceDetect,//消除对同一对象循环引用的问题,默认为false /** * @since 1.1.9 */ WriteSlashAsSpecial,//对斜杠'/'进行转义 /** * @since 1.1.10 */ BrowserCompatible,//将中文都会序列化为\uXXXX格式,字节数会多一些,但是能兼容IE 6,默认为false /** * @since 1.1.14 */ WriteDateUseDateFormat,//全局修改日期格式,默认为false。JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd";JSON.toJSONString(obj, SerializerFeature.WriteDateUseDateFormat); /** * @since 1.1.15 */ NotWriteRootClassName,//暂不知,求告知 /** * @since 1.1.19 */ DisableCheckSpecialChar,//一个对象的字符串属性中如果有特殊字符如双引号,将会在转成json时带有反斜杠转移符。如果不需要转义,可以使用这个属性。默认为false /** * @since 1.1.35 */ BeanToArray //暂不知,求告知 ; private SerializerFeature(){ mask = (1 << ordinal()); } private final int mask; public final int getMask() { return mask; } public static boolean isEnabled(int features, SerializerFeature feature) { return (features & feature.getMask()) != 0; } public static int config(int features, SerializerFeature feature, boolean state) { if (state) { features |= feature.getMask(); } else { features &= ~feature.getMask(); } return features; } }
0X01
大致用法先来张图(图是盗的,链接在参考3):
简单的代码示例
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.serializer.SerializerFeature; private static SerializerFeature[] features = { SerializerFeature.WriteNullNumberAsZero, SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.DisableCircularReferenceDetect }; JSONObject result = new JSONObject(); result.put("errorCode", 4000 1); result.put("errorMessage", cause);//Map<String, String> cause JSON.toJSONString(result, features);
参考
【fastjson的SerializerFeature用法】【SerialFeature】
【FastJSON 简单使用】
【fastjson常见问题】
【fastjson中SerializerFeature解析】
【fastjson wiki】
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树