如何使用Gson类型适配器反序列化对象数组?
package tests;
import java.io.IOException;
import java.lang.reflect.Type;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
public class Try03 {
public static class B {
double value;
public B() {
this(0);
}
public B(double value) {
this.value = value;
}
double getValue() {
return value;
}
void setValue(double value) {
this.value = value;
}
}
public static class A {
B[] values = new B[3];
transient double sum;
public A() {
sum = 0;
}
void refresh() {
sum = values[0].getValue() + values[1].getValue() + values[2].getValue();
}
A set(int index, B b) {
if( values[index] != null ) {
sum -= values[index].getValue();
}
values[index] = b;
if( values[index] != null ) {
sum += values[index].getValue();
}
return this;
}
}
public static class Ades implements JsonDeserializer<A> {
@Override
public A deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonObject obj = json.getAsJsonObject();
B[] values = context.deserialize(obj.get("values"), B[].class);
A ans = new A();
ans.values = values;
ans.refresh();
return ans;
}
}
public static class Aadap extends TypeAdapter<A> {
@Override
public void write(JsonWriter out, A value) throws IOException {
}
@Override
public A read(JsonReader in) throws IOException {
in.beginObject();
if( !in.nextName().equals("values") ) {
throw new AssertionError();
}
in.beginArray();
// how to read elements of type B?
in.endArray();
in.endObject();
A ans = new A();
return ans;
}
}
static String[] bsrc = {
"{\n" +
"\"value\": 12.0\n" +
"}",
"{\n" +
"\"value\": 10.0\n" +
"}",
"{\n" +
"\"value\": 7.0\n" +
"}"
};
static String asrc = "{\n" +
"\"values\": [\n" +
"{\n" +
"\"value\": 32.0\n" +
"},\n" +
"{\n" +
"\"value\": 5.0\n" +
"},\n" +
"{\n" +
"\"value\": 8.0\n" +
"}\n" +
"]\n" +
"}";
// works
public static void main01() {
Gson gson = new GsonBuilder().enableComplexMapKeySerialization().serializeNulls().setPrettyPrinting().setVersion(1.0)
.create();
B b = new B();
b.setValue(12);
System.out.println(gson.toJson(b));
}
// works
public static void main02() {
Gson gson = new GsonBuilder().enableComplexMapKeySerialization().serializeNulls().setPrettyPrinting().setVersion(1.0)
.create();
B b = gson.fromJson(bsrc[0], B.class);
System.out.println(b);
}
// works
public static void main03() {
Gson gson = new GsonBuilder().enableComplexMapKeySerialization().serializeNulls().setPrettyPrinting().setVersion(1.0)
.create();
B[] b = { new B(32), new B(5), new B(8) };
A a = new A();
a.set(0, b[0]).set(1, b[1]).set(2, b[2]);
System.out.println(gson.toJson(a));
}
// works but does not initialize transient field
public static void main04() {
Gson gson = new GsonBuilder().enableComplexMapKeySerialization().serializeNulls().setPrettyPrinting().setVersion(1.0)
.create();
A a = gson.fromJson(asrc, A.class);
System.out.println(a);
}
// works and does initialize transient field
// but does not use TypeAdapter
public static void main05() {
Gson gson = new GsonBuilder().enableComplexMapKeySerialization().serializeNulls().setPrettyPrinting().setVersion(1.0)
.registerTypeAdapter(A.class, new Ades())
.create();
A a = gson.fromJson(asrc, A.class);
System.out.println(a);
}
// don't know how to write type adapter
public static void main06() {
Gson gson = new GsonBuilder().enableComplexMapKeySerialization().serializeNulls().setPrettyPrinting().setVersion(1.0)
.registerTypeAdapter(A.class, new Aadap())
.create();
A a = gson.fromJson(asrc, A.class);
System.out.println(a);
}
public static void main(String[] args) {
// main01();
// main02();
// main03();
// main04();
// main05();
main06();
}
}