使用GSON注册多个适配器不起作用

day*_*005 16 java json gson

我在使用GsonBuilder注册多个typeAdapter时遇到了问题.似乎只有一个会启动,它永远不会考虑第二个.如果我自己每个人做它似乎工作正常.但我需要他们与两者合作,似乎我做错了什么.我目前正在使用GSON v2.2.4.

Zip对象简单形式:

public class Zip {

  private String zipCode;
  private String city;
  private String state;

  public Zip(){}                                            
}
Run Code Online (Sandbox Code Playgroud)

ZipSerializer:

public class ZipSerializer implements JsonSerializer<Zip>{

    @Override
    public JsonElement serialize(Zip obj, Type type, JsonSerializationContext jsc) {
        Gson gson = new Gson();
        JsonObject jObj = (JsonObject)gson.toJsonTree(obj);
        jObj.remove("state");        
        return jObj;
    }

}
Run Code Online (Sandbox Code Playgroud)

JsonResponse对象简单形式:

public class JsonResponse {

    private String jsonrpc = "2.0"; 
    private Object result = null;
    private String id = null;

    public JsonResponse(){}

}
Run Code Online (Sandbox Code Playgroud)

JsonResponseSerializer:

public class JsonResponseSerializer implements JsonSerializer<JsonResponse> {

    @Override
    public JsonElement serialize(JsonResponse obj, Type type, JsonSerializationContext jsc) {
        Gson gson = new Gson();
        JsonObject jObj = (JsonObject)gson.toJsonTree(obj);
        jObj.remove("id");

        return jObj;
    }

}
Run Code Online (Sandbox Code Playgroud)

测试示例:

public class Test {

    public static void main(String[] args) {
        Zip zip = new Zip();
        zip.setCity("testcity");
        zip.setState("OH");
        zip.setZipCode("12345");

        JsonResponse resp = new JsonResponse();
        resp.setId("1");
        resp.setResult(zip);
        resp.setJsonrpc("2.0");

        Gson gson1 = new GsonBuilder()
        .registerTypeAdapter(JsonResponse.class, new JsonResponseSerializer())
        .registerTypeAdapter(Zip.class, new ZipSerializer())
        .setPrettyPrinting()
        .create();


        String json = gson1.toJson(resp);
        System.out.println(json);               

    }

}
Run Code Online (Sandbox Code Playgroud)

输出:

{
  "jsonrpc": "2.0",
  "result": {
    "zipCode": "12345",
    "city": "testcity",
    "state": "OH"
  }
}
Run Code Online (Sandbox Code Playgroud)

预期输出:(注意ZipSerializer没有关闭)

{
  "jsonrpc": "2.0",
  "result": {
    "zipCode": "12345",
    "city": "testcity"
  }
}
Run Code Online (Sandbox Code Playgroud)

为此简化了测试用例.我知道我可以在这个例子中使用exclusionStrategy来实现结果,但真正的问题要复杂得多,这是我描绘和复制我的问题的最佳方式.

谢谢

<---------------------------- SOLUTION -------------------- ----------->

我设法使用TypeAdapter在一个对象中读取一个变量(很多)的Gson自定义seralizer,它帮助了我很大的帮助.

我创建了一个基本customTypeAdapterFactory,然后为每个需要特殊序列化的类扩展它.

CustomizedTypeAdapterFactory

public abstract class CustomizedTypeAdapterFactory<C> implements TypeAdapterFactory {

    private final Class<C> customizedClass;

    public CustomizedTypeAdapterFactory(Class<C> customizedClass) {
        this.customizedClass = customizedClass;
    }

    @SuppressWarnings("unchecked") // we use a runtime check to guarantee that 'C' and 'T' are equal
    public final <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
        return type.getRawType() == customizedClass
                ? (TypeAdapter<T>) customizeMyClassAdapter(gson, (TypeToken<C>) type)
                : null;
    }

    private TypeAdapter<C> customizeMyClassAdapter(Gson gson, TypeToken<C> type) {
        final TypeAdapter<C> delegate = gson.getDelegateAdapter(this, type);
        final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class);

        return new TypeAdapter<C>() {

            @Override public void write(JsonWriter out, C value) throws IOException {               
                JsonElement tree = delegate.toJsonTree(value);
                beforeWrite(value, tree);
                elementAdapter.write(out, tree);
            }


            @Override public C read(JsonReader in) throws IOException {
                JsonElement tree = elementAdapter.read(in);
                afterRead(tree);
                return delegate.fromJsonTree(tree);
            }
        };
    }

    /**
    * Override this to muck with {@code toSerialize} before it is written to
    * the outgoing JSON stream.
    */
    protected void beforeWrite(C source, JsonElement toSerialize) {
    }

    /**
    * Override this to muck with {@code deserialized} before it parsed into
    * the application type.
    */
    protected void afterRead(JsonElement deserialized) {
    }
}
Run Code Online (Sandbox Code Playgroud)

ZipTypeAdapterFactory(与JsonResponseTypeAdapterFactory一起使用)

public class ZipTypeAdapterFactory  extends CustomizedTypeAdapterFactory<Zip> {

    ZipTypeAdapterFactory() {
        super(Zip.class);
    }

    @Override
    protected void beforeWrite(Zip source, JsonElement toSerialize) {
        JsonObject obj = (JsonObject) toSerialize;
        obj.remove("state");
    }

}
Run Code Online (Sandbox Code Playgroud)

测试代码:

Gson gson1 = new GsonBuilder()
        .registerTypeAdapterFactory(new JsonResponseTypeAdapterFactory())       
        .registerTypeAdapterFactory(new ZipTypeAdapterFactory())
        .setPrettyPrinting()
        .create();


        String json = gson1.toJson(resp);
        System.out.println(json);
Run Code Online (Sandbox Code Playgroud)

谢谢大家的帮助.

Pom*_*Pom 5

问题出在你的身上JsonResponseSerializer.您创建一个新的Gson实例,然后在此新实例ZipSerializer上未注册.这就是你永远不会调用你的第二个序列化器的原因.

如果您想实现委派和复杂的序列化,请查看TypeAdapterFactory.

正如你所说,如果你只是想从序列化中过滤字段定义一个ExclusionStrategy.