赞
踩
一:fastjson的自定义序列化
定义一个class---->实现ObjectSerializer接口------>在自定义的class中可以操作数据的序列化
- /**
- * 用于objectId的序列化
- */
- public class ObjectIdSerializer implements ObjectSerializer {
-
- @Override
- public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
- SerializeWriter out = serializer.out;
-
- if (object instanceof ObjectId) {
- ObjectId objectId = (ObjectId) object;
- out.writeString(objectId.toString());
- return;
- }
- }
- }
二:fastjson的自定义反序列化
定义一个class---->实现ObjectDeserializer接口------>在自定义的class中可以操作数据的反序列化
- public class ObjectIdDeserializer implements ObjectDeserializer{
-
- @Override
- public ObjectId deserialze(DefaultJSONParser parser, Type type, Object fieldname){
-
- String idString = parser.getLexer().stringVal();
- return new ObjectId(idString);
- }
-
-
- @Override
- public int getFastMatchToken(){return 0;}
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。