Java 如何从自定义Gson JsonSerializer调用另一个序列化程序?
我有自己的自定义类Java 如何从自定义Gson JsonSerializer调用另一个序列化程序?,java,json,serialization,gson,jsonserializer,Java,Json,Serialization,Gson,Jsonserializer,我有自己的自定义类用户: class User { public String name; public int id; public Address address; public Timestamp created; } 我现在为User.class创建一个自定义JsonSerializer: @Override public JsonElement serialize(User src, Type typeOfSrc, JsonSerial
用户
:
class User {
public String name;
public int id;
public Address address;
public Timestamp created;
}
我现在为User.class创建一个自定义JsonSerializer:
@Override
public JsonElement serialize(User src, Type typeOfSrc,
JsonSerializationContext context) {
JsonObject obj = new JsonObject();
obj.addProperty("name", src.name);
obj.addProperty("id", src.id);
obj.addProperty("address", src.address.id);
// Want to invoke another JsonSerializer (TimestampAdapter) for Timestamp
obj.add("created", ???);
return obj;
}
但是我已经有了一个Timestamp.class
使用的Timestamp适配器。如何调用此JsonSerializer
,以便在User.class
中的“已创建”字段上使用
obj.add("created", context.serialize(src.created));
如果您的Timestamp适配器
已向Gson注册Timestamp
类,则JsonSerializationContext
应自动使用它序列化对象并返回jsonement