转载于:http://code.alibabatech.com/wiki/display/FastJSON/Serial+Features

Fastjson序列化时,可以指定序列化的特性,以满足不同的序列化需求。

SerialFeature类的定义

package com.alibaba.fastjson.serializer;
 
public enum SerializerFeature {
    QuoteFieldNames, UseSingleQuotes, WriteMapNullValue, WriteEnumUsingToString, UseISO8601DateFormat, SkipTransientField
}

使用举例

Date date = new Date(1308841916550L);
 
// 缺省输出
System.out.println(JSON.toJSONString(date)); // 1308841916550
 
// 使用特性 UseISO8601DateFormat
// "2011-06-23T23:11:56.550"
System.out.println(JSON.toJSONString(date, SerializerFeature.UseISO8601DateFormat));
 
// 组合两个Feature(UseISO8601DateFormat和UseSingleQuotes)输出日期
SerializerFeature[] features = {SerializerFeature.UseISO8601DateFormat, SerializerFeature.UseSingleQuotes };
System.out.println(JSON.toJSONString(date, features)); // '2011-06-23T23:11:56.550'

详细说明表格

Featurs 缺省值 说明
QuoteFieldNames true 序列化输出字段,使用引号。例如:
QuoteFieldNames Feature Enabled:

{"id":123,
"name":"张三",
"age":23}

QuoteFieldNames Feature Disabled:

{id:123,
name:
"张三",
age:
23}
UseSingleQuotes false 使用单引号而不是双引号
UseSingleQuotes Feature Enabled:

{'id':123,
'name':'张三',
'age':23}

UseSingleQuotes Feature Disabled:

{"id":123,
"name":"张三",
"age":23}
WriteMapNullValue false 空值是否输出。大多数情况,值为null的属性输出是没有意义的,缺省这个特性是打开的。
WriteMapNullValue Feature Enabled:

{'id':123,
'name':'张三',
'age':23,
birthday :
null}

WriteMapNullValue Feature Disabled:

{"id":123,
"name":"张三",
"age":23}
WriteEnumUsingToString false Enum输出name()或者original

public

static

enum

Type {
    Big,
Medium, Small
}
 
System.out.println(JSON.toJSONString(Type.Big));
//
0
System.out.println(JSON.toJSONString(Type.Medium));
//
1
System.out.println(JSON.toJSONString(Type.Small));
//
2
 
System.out.println(JSON.toJSONString(Type.Big,
SerializerFeature.WriteEnumUsingToString));
//
"Big"
System.out.println(JSON.toJSONString(Type.Medium,
SerializerFeature.WriteEnumUsingToString));
//
"Medium"
System.out.println(JSON.toJSONString(Type.Small,
SerializerFeature.WriteEnumUsingToString));
//
"Small"
UseISO8601DateFormat false Date使用ISO8601格式输出

Date
date =
new

Date(1308841916550L);
System.out.println(JSON.toJSONString(date));
//
1308841916550
 
//
"2011-06-23T23:11:56.550"
System.out.println(JSON.toJSONString(date,
SerializerFeature.UseISO8601DateFormat));
SkipTransientField true 如果是true,类中的Get方法对应的Field是transient,序列化时将会被忽略
WriteNullListAsEmpty false list字段如果为null,输出为[],而不是null
WriteNullNumberAsZero false 数值字段如果为null,输出为0,而不是null
WriteNullBooleanAsFalse false Boolean字段如果为null,输出为false,而不是null
WriteNullStringAsEmpty false 字符类型字段如果为null,输出为"",而不是null
SortField false 按字段名称排序后输出
WriteTabAsSpecial false 把\t做转义输出。
 

Fastjson Feathure的更多相关文章

  1. fastjson 混淆注意事项

    使用fastjson 注意事项,主要表现: 1.加了符号Annotation 的实体类,一使用就会奔溃 2.当有泛型属性时,一使用就奔溃 在调试的时候不会报错,当你要打包签名混淆包的时候,就会出现上述 ...

  2. Java的Json解析包FastJson使用

    阿里巴巴FastJson是一个Json处理工具包,包括“序列化”和“反序列化”两部分,它具备如下特征:速度最快,测试表明,fastjson具有极快的性能,超越任其他的Java Json parser. ...

  3. fastJson使用

    fastjson 是一个性能很好的 Java 语言实现的 JSON 解析器和生成器,由阿里巴巴的工程师开发. 主要特点: 快速FAST (比其它任何基于Java的解析器和生成器更快,包括jackson ...

  4. FASTJSON

    package com.hanqi.test; import java.util.ArrayList;import java.util.Date;import java.util.List; impo ...

  5. Android总结之json解析(FastJson Gson 对比)

    前言: 最近为了统一项目中使用的框架,发现项目中用到了两种json解析框架,他们就是当今非常主流的json解析框架:google的Gson 和阿里巴巴的FastJson,为了废除其中一个所以来个性能和 ...

  6. Android原生json和fastjson的简单使用

    android原生操作json数据 主要是两个类 JSONObject 操作对象     JONSArray操作json数组 对象转json //创建学生对象 Student student=new ...

  7. FastJson的简单实用

    一.FastJson的理解 在工作中,经常客服端需要和服务端进行通信,目前很多项目都采用JSON的方式进行数据传输,简单的参数可以通过手动拼接JSON字符串,但如果请求的参数过多,采用手动拼接JSON ...

  8. Android JSON、GSON、FastJson的封装与解析

    声明: 1.本帖只提供代码,不深入讲解原理.如果读者想要深入了解,那就不要在这个帖子上浪费时间了 2.客户端用的是Google官方的Volley访问服务器,具体了解Volley请戳 这里 3.本帖三种 ...

  9. java分享第十三天(fastjson生成和解析json数据,序列化和反序列化数据)

     fastjson简介:Fastjson是一个Java语言编写的高性能功能完善的JSON库.fastjson采用独创的算法,将parse的速度提升到极致,超过所有json库,包括曾经号称最快的jack ...

随机推荐

  1. Swift - 20 - 字典的基础操作

    //: Playground - noun: a place where people can play import UIKit var dict = [1:"one", 2:& ...

  2. spring web flow 2.0入门(转)

    Spring Web Flow 2.0 入门 一.Spring Web Flow 入门demo(一)简单页面跳转 附源码(转) 二.Spring Web Flow 入门demo(二)与业务结合 附源码 ...

  3. 重新开始学习javase_集合_List

    一,List之ArrayList(转:http://blog.csdn.net/zheng0518/article/details/42198205) 1. ArrayList概述: ArrayLis ...

  4. FingerChaser(3) 解题报告目录

    所有代码都不超过40行... A:http://www.cppblog.com/willing/archive/2010/05/04/114304.html B:http://www.cnblogs. ...

  5. Firefox中Vimperator插件配置

    具体配置什么,同学们可以网上看下善用佳软关于Vimperator的说明,在这里我列出两条我个人觉得最有用的命令 set nextpattern=\s*下一页|下一张|下一篇|下页|后页\s*,^\bn ...

  6. nginx配置无效的问题

    今天修改nginx的一个配置文件,却怎么都没效果,发现是启动nginx指定的配置文件不一样. #ps aux|grep nginx root     17672  0.0  0.0  45856  2 ...

  7. bash shell学习-shell script基础 (笔记)

    A chain no stronger than its weakest link. "一着不慎,满盘皆输" 参考资料:鸟哥的Linux私房菜 基础学习篇(第三版)  Linux ...

  8. 关于jQuery表单校验的应用

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  9. Jssdk微信分享

    <?php require_once "jssdk.php"; $jssdk = new JSSDK("yourAppID", "yourApp ...

  10. js移动设备手机跳转地址代码

    if(/AppleWebKit.*mobile/i.test(navigator.userAgent) || (/MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alc ...