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格式,字节数会多一些,但是能兼容IE 6,默认为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 含义  

示例

准备

  • User、Word来模拟各种数据类型。
  • SerializerFeatureTest:JSON部分示例的示例方法。
package com.fastjson;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import org.junit.Test; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature; import lombok.AllArgsConstructor;
import lombok.Data; public class FastjsonFormat {
private static Word word; private static void init() {
word = new Word();
word.setA("a");
word.setB(2);
word.setC(true);
word.setD("d");
word.setE("");
word.setF(null);
word.setDate(new Date()); List<User> list = new ArrayList<User>();
User user1 = new User();
user1.setId(1);
user1.setOld("11");
user1.setName("用户1");
user1.setAdd("北京");
User user2 = new User();
user2.setId(2);
user2.setOld("22");
user2.setName("用户2");
user2.setAdd("上海");
User user3 = new User();
user3.setId(3);
user3.setOld("33");
user3.setName("用户3");
user3.setAdd("广州"); list.add(user3);
list.add(user2);
list.add(null);
list.add(user1); word.setList(list); Map<String, Object> map = new HashMap<>();
map.put("mapa", "mapa");
map.put("mapo", "mapo");
map.put("mapz", "mapz");
map.put("user1", user1);
map.put("user3", user3);
map.put("user4", null);
map.put("list", list);
word.setMap(map);
} public static void main(String[] args) {
init();
useSingleQuotes();
writeMapNullValue();
useISO8601DateFormat();
writeNullListAsEmpty();
writeNullStringAsEmpty();
sortField();
prettyFormat();
writeDateUseDateFormat();
beanToArray();
showJsonBySelf(); Person person = new Person(null, 28, new String[] { "", "明明" },
new BodyInfo[] { new Height(188), new Weight(150) });
System.out.println(person); // 去null值
System.out.println(JSON.toJSONString(person)); // 美化,format
System.out.println(JSON.toJSONString(person, SerializerFeature.PrettyFormat));
} /**
* 9:自定义 格式化输出 显示值为null的字段 将为null的字段值显示为""
* DisableCircularReferenceDetect:消除循环引用
*/
private static void showJsonBySelf() {
System.out.println(JSON.toJSONString(word));
System.out.println(JSON.toJSONString(word, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.DisableCircularReferenceDetect,
SerializerFeature.WriteNullListAsEmpty));
} /**
* 8: 将对象转为array输出
*/
private static void beanToArray() {
word.setMap(null);
word.setList(null);
System.out.println(JSON.toJSONString(word));
System.out.println(JSON.toJSONString(word, SerializerFeature.BeanToArray));
} /**
* 7: WriteDateUseDateFormat:全局修改日期格式,默认为false。
*/
private static void writeDateUseDateFormat() {
word.setMap(null);
word.setList(null);
System.out.println(JSON.toJSONString(word));
JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd";
System.out.println(JSON.toJSONString(word, SerializerFeature.WriteDateUseDateFormat));
} /**
* 6: PrettyFormat
*/
private static void prettyFormat() {
word.setMap(null);
word.setList(null);
System.out.println(JSON.toJSONString(word));
System.out.println(JSON.toJSONString(word, SerializerFeature.PrettyFormat));
} /**
* SortField:按字段名称排序后输出。默认为false 这里使用的是fastjson:为了更好使用sort field
* martch优化算法提升parser的性能,fastjson序列化的时候,
* 缺省把SerializerFeature.SortField特性打开了。 反序列化的时候也缺省把SortFeidFastMatch的选项打开了。
* 这样,如果你用fastjson序列化的文本,输出的结果是按照fieldName排序输出的,parser时也能利用这个顺序进行优化读取。
* 这种情况下,parser能够获得非常好的性能。
*/
private static void sortField() {
System.out.println(JSON.toJSONString(word));
System.out.println(JSON.toJSONString(word, SerializerFeature.SortField));
} /**
* 5: WriteNullStringAsEmpty:字符类型字段如果为null,输出为"",而非null
* 需要配合WriteMapNullValue使用,现将null输出
*/
private static void writeNullStringAsEmpty() {
word.setE(null);
System.out.println(JSONObject.toJSONString(word));
System.out.println("设置WriteMapNullValue后:");
System.out.println(JSONObject.toJSONString(word, SerializerFeature.WriteMapNullValue));
System.out.println("设置WriteMapNullValue、WriteNullStringAsEmpty后:");
System.out.println(JSONObject.toJSONString(word, SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteNullStringAsEmpty));
} /**
* 4: WriteNullListAsEmpty:List字段如果为null,输出为[],而非null
* 需要配合WriteMapNullValue使用,现将null输出
*/
private static void writeNullListAsEmpty() {
word.setList(null);
System.out.println(JSONObject.toJSONString(word));
System.out.println("设置WriteNullListAsEmpty后:");
System.out.println(JSONObject.toJSONString(word, SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteNullListAsEmpty));
} /**
* 3: UseISO8601DateFormat:Date使用ISO8601格式输出,默认为false
*/
private static void useISO8601DateFormat() {
System.out.println(JSONObject.toJSONString(word));
System.out.println("设置UseISO8601DateFormat后:");
System.out.println(JSONObject.toJSONString(word, SerializerFeature.UseISO8601DateFormat));
} /**
* 2: WriteMapNullValue:是否输出值为null的字段,默认为false
*/
private static void writeMapNullValue() {
System.out.println(JSONObject.toJSONString(word));
System.out.println("设置WriteMapNullValue后:");
System.out.println(JSONObject.toJSONString(word, SerializerFeature.WriteMapNullValue));
} /**
* 1: UseSingleQuotes:使用单引号而不是双引号,默认为false
*/
private static void useSingleQuotes() {
System.out.println(JSONObject.toJSONString(word));
System.out.println("设置useSingleQuotes后:");
System.out.println(JSONObject.toJSONString(word, SerializerFeature.UseSingleQuotes));
} } interface BodyInfo { } @Data
@AllArgsConstructor
class Height implements BodyInfo {
int height;
} @Data
@AllArgsConstructor
class Person {
String name;
int age; String[] nickNames; BodyInfo[] bodyInfos;
} @Data
@AllArgsConstructor
class Weight implements BodyInfo {
int weight;
} @Data
class User {
private int id;
private String name;
private String add;
private String old;
} @Data
class Word {
private String d;
private String e;
private String f;
private String a;
private int b;
private boolean c;
private Date date;
private Map<String, Object> map;
private List<User> list;
}

fastjson数据格式转换 SerializerFeature属性详解的更多相关文章

  1. Ext.tree.TreePanel 属性详解

    Ext.tree.TreePanel 属性详解 2013-06-09 11:02:47|  分类: ExtJs|举报|字号 订阅  原文地址:http://blog.163.com/zzf_fly/b ...

  2. 【CSS3 transform属性和过渡属性详解】

    CSS3transform属性详解 transform字面上就是变形,改变的意思. 在CSS3中transform主要包括以下几种:旋转rotate.扭曲skew.缩放scale和移动translat ...

  3. Android笔记-2-TextView的属性详解

    [Android 基础]TextView的属性详解 android:autoLink :设置是否当文本为URL链接/email/电话号码/map时,文本显示为可点击的链接.可选值(none/web / ...

  4. fiddler请求报文的headers属性详解

    fiddler请求报文的headers属性详解 headers的属性包含以下几部分. (1)Cache头域 在Cache头域中,通常会出现以下属性. 1. Cache-Control 用来指定Resp ...

  5. Android零基础入门第80节:Intent 属性详解(下)

    上一期学习了Intent的前三个属性,本期接着学习其余四个属性,以及Android系统常用内置组件的启动. 四.Data和Type属性 Data属性通常用于向Action属性提供操作的数据.Data属 ...

  6. css 12-CSS3属性详解:动画详解

    12-CSS3属性详解:动画详解 #前言 本文主要内容: 过渡:transition 2D 转换 transform 3D 转换 transform 动画:animation #过渡:transiti ...

  7. android:exported 属性详解

    属性详解 标签: android 2015-06-11 17:47 27940人阅读 评论(7) 收藏 举报 分类: Android(95) 项目点滴(25) 昨天在用360扫描应用漏洞时,扫描结果, ...

  8. OutputCache属性详解(一)一Duration、VaryByParam

    目录 OutputCache概念学习 OutputCache属性详解(一) OutputCache属性详解(二) OutputCache属性详解(三) OutputCache属性详解(四)— SqlD ...

  9. OutputCache属性详解(二)一 Location

    目录 OutputCache概念学习 OutputCache属性详解(一) OutputCache属性详解(二) OutputCache属性详解(三) OutputCache属性详解(四)— SqlD ...

随机推荐

  1. mybatis关联查询resultmap的使用详解resultmap

    因为该案例比较典型,所以记录一下,恐后期有所疑问,以便用时便于会议. 案例典型在 关联关系典型 主表一张业务模板表 TABLE_NAME  COLUMN_NAME COMMENTS YMIT_BIZ_ ...

  2. UVA-806 Spatial Structures (四分树)

    题目大意:将一块图像上的黑点在两种表示法之间转换. 题目分析:递归下去... 注意:输出时要注意细节!!! 代码如下: # include<iostream> # include<c ...

  3. Vue.js学习笔记:在元素 和 template 中使用 v-if 指令

    f 指令 语法比较简单,直接上代码: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" " ...

  4. Swift 标签控制器(tabbar添加提醒和控制器)

    // Override point for customization after application launch. //初始化window, 大小为设备物理大小 self.window = U ...

  5. HDU3861The King’s Problem

    HDU3861   kosaraju缩点+最小路径覆盖 为什么是最小路径覆盖呢,我们假设有一个如下DAG图 目前我们1出发到了3处,对于3的儿子4.5.6,肯定是不能彼此到达的.所以最好的情况3只能延 ...

  6. 使用cmd命令打开文件夹

    在命令行中输入你想要打开文件所在的磁盘,这里我以打开E:\homework\1.jpg来给大家做示范.在命令行中输入  E:   输入后按下enter键.就进入E盘中,效果如图所示!   如果你想要查 ...

  7. deno学习二 基本代码

    deno 介绍是安全的ts 运行时 简单的代码 使用js(app.js) console.log("demoapp") 输出 dalongdemo 使用ts(app.ts) con ...

  8. 一个filebeat实例 设置多topic设置

    方法1:一实例多topic: https://discuss.elastic.co/t/filebeat-5-0-output-to-kafka-multiple-topics/67934 The d ...

  9. php 文件上传$_FILES中error返回值详解

    用PHP上传文件时,我们会用程序去监听浏览器发送过来的文件信息,首先会通 过$_FILES[fieldName]['error']的不同数值来判断此欲上传的文件状态是否正常.$_FILES[field ...

  10. 使用ntp从时间同步服务器更新centos系统时间的方法

    CentOS系统时间同步的步骤如下: 复制代码 代码如下: cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtimentpdate us.pool.ntp ...