我无法使用Gson将自定义类实例解析到Json或从Json解析。这是我正在处理的代码的简化版本:
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import java.util.ArrayList;
import java.util.Arrays;
class IntegerArrayList extends ArrayList<Integer> {
public static IntegerArrayList fromJson(String json) {
TypeToken<IntegerArrayList> integerArrayListTypeToken = new TypeToken<IntegerArrayList>() {} ;
IntegerArrayList integerArrayList = new GsonBuilder()
.enableComplexMapKeySerialization()
.create()
.fromJson(json, integerArrayListTypeToken.getType());
return integerArrayList;
}
public String toJson() {
TypeToken<IntegerArrayList> integerArrayListTypeToken = new TypeToken<IntegerArrayList>() {};
return new GsonBuilder()
.enableComplexMapKeySerialization()
.setPrettyPrinting()
.create()
.toJson(this, integerArrayListTypeToken.getType());
}
public IntegerArrayList(Integer... items) {
super();
addAll(Arrays.asList(items));
}
}
public class GsonTest {
public static void main(String[] args) {
IntegerArrayList integerArrayList = new IntegerArrayList(1, 2, 3, 4, 5);
String json = integerArrayList.toJson();
IntegerArrayList integerArrayList1 = IntegerArrayList.fromJson(json);
}
}
我收到此错误:
Exception in thread "main" java.lang.ClassCastException: java.util.ArrayList cannot be cast to IntegerArrayList
at IntegerArrayList.fromJson(GsonTest.java:13)
at GsonTest.main(GsonTest.java:36)
所以这里的问题是,它不是返回一个IntegerArrayList实例,而是返回它继承的ArrayList。
有没有机会直接从Gson获取IntegerArrayList?怎么样?
提前多多感谢!
答案 0 :(得分:0)
好的,我刚刚编写了一个自定义适配器,它工作正常。不过,实际代码会有点复杂,但至少我知道应该由谁来解决这个问题。
以下是代码:
import com.google.gson.GsonBuilder;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
class IntegerArrayList extends ArrayList<Integer> {
String customField = "test";
public IntegerArrayList(Integer... items) {
super();
addAll(Arrays.asList(items));
}
public static IntegerArrayList fromJson(String json) {
IntegerArrayList integerArrayList = new GsonBuilder()
.registerTypeAdapter(IntegerArrayList.class, new JsonAdapter())
.enableComplexMapKeySerialization()
.create()
.fromJson(json, IntegerArrayList.class);
return integerArrayList;
}
public String toJson() {
return new GsonBuilder()
.registerTypeAdapter(IntegerArrayList.class, new JsonAdapter())
.enableComplexMapKeySerialization()
.setPrettyPrinting()
.create()
.toJson(this, IntegerArrayList.class);
}
public static class JsonAdapter extends TypeAdapter<IntegerArrayList> {
@Override
public void write(JsonWriter out, IntegerArrayList value) throws IOException {
out.beginObject();
out.name("customField").value(value.customField);
out.name("items");
out.beginArray();
for (Integer v : value) {
out.value(v);
}
out.endArray();
out.endObject();
}
@Override
public IntegerArrayList read(JsonReader in) throws IOException {
IntegerArrayList rtn = new IntegerArrayList();
in.beginObject();
while (in.hasNext()) {
switch (in.nextName()) {
case "customField":
rtn.customField = in.nextString();
break;
case "items":
in.beginArray();
while (in.hasNext()) {
rtn.add(in.nextInt());
}
in.endArray();
}
}
in.endObject();
return rtn;
}
}
}
public class GsonTest {
public static void main(String[] args) {
IntegerArrayList integerArrayList = new IntegerArrayList(1, 2, 3, 4, 5);
String json = integerArrayList.toJson();
IntegerArrayList integerArrayList1 = IntegerArrayList.fromJson(json);
}
}