gson在自定义的反序列化器中调用标准的反序列化
是否有可能在gson中编写一个json反序列化器,首先调用默认行为,然后我可以对我的对象做一些后期处理。 例如:
public class FooDeserializer implements JsonDeserializer<Foo> { public Foo deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { Foo foo = context.deserialize(json, typeOfT);//Standard deserialization call????? foo.doSomething(); return foo(); } }
我正在使用GSON 1.3(我不能使用任何其他版本,因为我只能使用公司存储库中的版本)
谢谢
你可以通过为你的对象(如CustomClass.class)实现自定义的TypeAdapterFactory来进行反序列化,如下所示。
public class CustomTypeAdapterFactory implements TypeAdapterFactory { public final TypeAdapter create(Gson gson, TypeToken type) { return new TypeAdapter() { @Override public void write(JsonWriter out, Object value) throws IOException { JsonElement tree = delegate.toJsonTree(value); //add code for writing object } @Override public Object read(JsonReader in) throws IOException { JsonElement tree = elementAdapter.read(in); //Add code for reading object } }; } }
然后把它注册为Gson
Gson gson = new GsonBuilder().registerTypeAdapter(CustomClass.class,new CustomTypeAdapterFactory()).create();
这是我做的一个库,它扩展了Gson来处理Post-serialization和Post-deserialization之类的情况
另外它还有许多其他很酷的function,我一直需要Gson。
public class YourDeserializer<Foo> extends FooDeserializer<Foo> { public Foo deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { Foo foo = super.deserialize(json, typeOfT,context); foo.doSomething(); //put logic return foo(); } }
以下是由@ user1556622提供的不完整答案和code.google.com/p/google-gson/issues/detail?id=43中的讨论的完整实现。
因此,我们可以序列化抽象Field
对象的列表,并顺序地将其反序列化,以独立于具体Field
具体实现及其层次深度。
class MyClass { //class which we would like to serialiaze/deserialize List<Field> fields; //field is an hierarchy of classes } /** * Purpose of this adapter is simple: * 1) put during serialization in all Field objects additional property describing class * 2) during deserialization invoke (based on class info) necessary deserializer to create class */ public class FieldTypeAdapterFactory implements TypeAdapterFactory { private static final String CLASS_META_KEY="clz"; Gson gson; TypeToken<?> type; TypeAdapter<Field> fieldAdapter; TypeAdapter<JsonElement> elementAdapter; TypeAdapterFactory taf; public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { if (!Field.class.isAssignableFrom(type.getRawType())) return null; // this class only serializes 'Field' and its subtypes this.type=type; this.gson=gson; this.taf=this; fieldAdapter = gson.getDelegateAdapter(taf, TypeToken.get(Field.class)); elementAdapter = gson.getAdapter(JsonElement.class); TypeAdapter<T> result = new FieldTypeAdapter<T>(); result.nullSafe(); return result; } class FieldTypeAdapter<T> extends TypeAdapter<T> { public FieldTypeAdapter() { } @Override public void write(JsonWriter out, Object value) throws IOException { if(value instanceof Field) { JsonObject object = fieldAdapter.toJsonTree((Field )value).getAsJsonObject(); object.addProperty(CLASS_META_KEY, value.getClass().getCanonicalName()); elementAdapter.write(out, object); } else { elementAdapter.write(out, (JsonElement) value); } } @Override public T read(JsonReader in) throws IOException { JsonObject object = elementAdapter.read(in).getAsJsonObject(); if (object.has(CLASS_META_KEY)) { String className=object.get(CLASS_META_KEY).getAsString(); try { Class<?> clz = Class.forName(className); TypeAdapter<?> adapter = gson.getDelegateAdapter(taf, TypeToken.get(clz)); return (T) adapter.fromJsonTree(object); } catch (Exception e) { return (T )fieldAdapter.fromJsonTree(object); } } else return (T )elementAdapter.fromJsonTree(object); } } }
工厂注册:
Gson gson = new GsonBuilder() .registerTypeAdapterFactory(new FieldTypeAdapterFactory()) .create();
public class FooDeserializer implements JsonDeserializer<Foo> { public Foo deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { Foo foo=new Gson().fromJson(json, Foo.class); // use default Gson object foo.doSomething(); return foo; }