自己写TypeAdapter。例如,参见 javadoc。
用@JsonAdapter注解指定它,或者用 注册它GsonBuilder。
@JsonAdapter(MyClassAdapter.class)
public class MyClass {
public Long timestamp;
public String id;
public HashMap<String, SomeOtherClass> myMap = new HashMap<>();
}
public class MyClassAdapter extends TypeAdapter<MyClass> {
@Override public void write(JsonWriter out, MyClass myClass) throws IOException {
// implement the write method
}
@Override public MyClass read(JsonReader in) throws IOException {
// implement the read method
return ...;
}
}
BIG阳
相关分类