Fastjson Feathure

转载于: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缺省值说明
QuoteFieldNamestrue序列化输出字段,使用引号。例如:
QuoteFieldNames Feature Enabled:
{ "id" : 123 , "name" : "张三" , "age" : 23 }

QuoteFieldNames Feature Disabled:

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

UseSingleQuotes Feature Disabled:

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

WriteMapNullValue Feature Disabled:

{ "id" : 123 , "name" : "张三" , "age" : 23 }
WriteEnumUsingToStringfalseEnum输出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"
UseISO8601DateFormatfalseDate使用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));
SkipTransientFieldtrue如果是true,类中的Get方法对应的Field是transient,序列化时将会被忽略
WriteNullListAsEmptyfalselist字段如果为null,输出为[],而不是null
WriteNullNumberAsZerofalse数值字段如果为null,输出为0,而不是null
WriteNullBooleanAsFalsefalseBoolean字段如果为null,输出为false,而不是null
WriteNullStringAsEmptyfalse字符类型字段如果为null,输出为"",而不是null
SortFieldfalse按字段名称排序后输出
WriteTabAsSpecialfalse把\t做转义输出。
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值