FastJSON指南( 二 )

package com.alibaba.fastjson.annotation;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;import com.alibaba.fastjson.parser.Feature;import com.alibaba.fastjson.serializer.SerializerFeature;@Retention(RetentionPolicy.RUNTIME)@Target({ ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER })public @interface JSONField {// 配置序列化和反序列化的顺序int ordinal() default 0;// 指定字段的名称String name() default "";// 指定字段的格式 , 对日期格式有用String format() default "";// 是否序列化boolean serialize() default true;// 是否反序列化boolean deserialize() default true;//字段级别的SerializerFeatureSerializerFeature[] serialzeFeatures() default {};//Feature[] parseFeatures() default {};//给属性打上标签 ,  相当于给属性进行了分组String label() default "";boolean jsonDirect() default false;//制定属性的序列化类Class serializeUsing() default Void.class;//制定属性的反序列化类Class deserializeUsing() default Void.class;String[] alternateNames() default {};boolean unwrapped() default false;}
其中与可以用于对字段的序列化与反序列化进行定制化 。比如我们在User实体上加上个sex属性 , 类型为 。下面分别定义了序列化类与反序列化类 , 序列化类代码如下:
package com.ivan.json.converter;import java.io.IOException;import java.lang.reflect.Type;import com.alibaba.fastjson.serializer.JSONSerializer;import com.alibaba.fastjson.serializer.ObjectSerializer;public class SexSerializer implements ObjectSerializer {public void write(JSONSerializer serializer,Object object,Object fieldName,Type fieldType,int features)throws IOException {Boolean value = http://www.kingceram.com/post/(Boolean) object;String text ="女";if (value != null && value =http://www.kingceram.com/post/= true) {text ="男";}serializer.write(text);}}
反序列化类代码如下:
package com.ivan.json.converter;import java.lang.reflect.Type;import com.alibaba.fastjson.parser.DefaultJSONParser;import com.alibaba.fastjson.parser.JSONToken;import com.alibaba.fastjson.parser.deserializer.ObjectDeserializer;public class SexDeserialize implements ObjectDeserializer {public T deserialze(DefaultJSONParser parser,Type type,Object fieldName) {String sex = parser.parseObject(String.class);if ("男".equals(sex)) {return (T) Boolean.TRUE;} else {return (T) Boolean.FALSE;}}public int getFastMatchToken() {return JSONToken.UNDEFINED;}}
提供了用于类级别的定制化, 的源码如下:
package com.alibaba.fastjson.annotation;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;import com.alibaba.fastjson.PropertyNamingStrategy;import com.alibaba.fastjson.parser.Feature;import com.alibaba.fastjson.serializer.SerializeFilter;import com.alibaba.fastjson.serializer.SerializerFeature;@Retention(RetentionPolicy.RUNTIME)//需要标注在类上@Target({ ElementType.TYPE })public @interface JSONType {boolean asm() default true;//这里可以定义输出json的字段顺序String[] orders() default {};//包含的字段String[] includes() default {};//不包含的字段String[] ignores() default {};//类级别的序列化特性定义SerializerFeature[] serialzeFeatures() default {};Feature[] parseFeatures() default {};//按字母顺序进行输出boolean alphabetic() default true;Class mappingTo() default Void.class;Class builder() default Void.class;String typeName() default "";String typeKey() default "";Class[] seeAlso() default{};//序列化类Class serializer() default Void.class;//反序列化类Class deserializer() default Void.class;boolean serializeEnumAsJavaBean() default false;PropertyNamingStrategy naming() default PropertyNamingStrategy.CamelCase;Class[] serialzeFilters() default {};}