FastJSON指南( 三 )


通过编程扩展的方式定制序列化支持以下用于不同常景的定制序列化:
package com.alibaba.fastjson.serializer;/*** @author wenshao[szujobs@hotmail.com]*/public interface PropertyFilter extends SerializeFilter {/*** @param object the owner of the property* @param name the name of the property* @param value the value of the property* @return true if the property will be included, false if to be filtered out* 根据 属性的name与value判断是否进行序列化*/boolean apply(Object object, String name, Object value);}
package com.alibaba.fastjson.serializer;public interface PropertyPreFilter extends SerializeFilter {//根据 object与name判断是否进行序列化boolean apply(JSONSerializer serializer, Object object, String name);}
package com.alibaba.fastjson.serializer;public interface NameFilter extends SerializeFilter {//根据 name与value的值 , 返回json字段key的值String process(Object object, String name, Object value);}
package com.alibaba.fastjson.serializer;public interface ValueFilter extends SerializeFilter {//根据name与value定制输出json的valueObject process(Object object, String name, Object value);}
package com.alibaba.fastjson.serializer;public abstract class BeforeFilter implements SerializeFilter {private static final ThreadLocal serializerLocal = new ThreadLocal();private static final ThreadLocalseperatorLocal= new ThreadLocal();private final static CharacterCOMMA= Character.valueOf(',');final char writeBefore(JSONSerializer serializer, Object object, char seperator) {serializerLocal.set(serializer);seperatorLocal.set(seperator);writeBefore(object);serializerLocal.set(null);return seperatorLocal.get();}protected final void writeKeyValue(String key, Object value) {JSONSerializer serializer = serializerLocal.get();char seperator = seperatorLocal.get();serializer.writeKeyValue(seperator, key, value);if (seperator != ',') {seperatorLocal.set(COMMA);}}//需要实现的方法 , 在实际实现中可以调用writeKeyValue增加json的内容public abstract void writeBefore(Object object);}
package com.alibaba.fastjson.serializer;/*** @since 1.1.35*/public abstract class AfterFilter implements SerializeFilter {private static final ThreadLocal serializerLocal = new ThreadLocal();private static final ThreadLocalseperatorLocal= new ThreadLocal();private final static CharacterCOMMA= Character.valueOf(',');final char writeAfter(JSONSerializer serializer, Object object, char seperator) {serializerLocal.set(serializer);seperatorLocal.set(seperator);writeAfter(object);serializerLocal.set(null);return seperatorLocal.get();}protected final void writeKeyValue(String key, Object value) {JSONSerializer serializer = serializerLocal.get();char seperator = seperatorLocal.get();serializer.writeKeyValue(seperator, key, value);if (seperator != ',') {seperatorLocal.set(COMMA);}}//子类需要实现的方法 , 实际使用的时候可以调用writeKeyValue增加内容public abstract void writeAfter(Object object);}
package com.alibaba.fastjson.serializer;//根据 JsonField配置的label来判断是否进行输出public interface LabelFilter extends SerializeFilter {boolean apply(String label);}
泛型反序列化
通过来实现泛型的反序列化 , 以下是一个简单的例子程序 。首先定义了用于所有DTO的父类 , 代码如下:
package com.ivan.frame.dto.common;import java.io.Serializable;import com.alibaba.fastjson.JSONObject;public class BaseDTO implements Serializable{private static final longserialVersionUID = 2230553030766621644L;@Overridepublic String toString() {return JSONObject.toJSONString(this);}}
用于抽像所有的请求DTO , 里面有个泛型参数 , 代码如下: