Gson无法反序列化List <serializable> </serializable>

时间:2013-08-27 08:53:15

标签: java json gson

我试图将从服务器获得的JSON反序列化为其原始POJO表单。我知道原始的对象(我有服务器代码的来源),但似乎我错过了一些东西。

这是我可以组装的最小代码示例,用于说明我的问题:

package mycompany.mypackage;
import java.io.Serializable;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.InstanceCreator;

public class GsonSerializeTest {

    /* main method to illustrate the problem */
    public static void main(String[] args) {
        Serializable[] identifiers= {"ITEM",12345678,"abc.def.ghijkl.mnopqr",87654321};
        EntityUid uid = new GsonSerializeTest().new EntityUid(identifiers);
        Gson converter = new GsonBuilder().registerTypeAdapter(Serializable.class, new GsonSerializeTest().new SerializableInstanceCreator()).create();
        String json = converter.toJson(uid);
        System.out.println("Converted to string: " + json);
        EntityUid uid2 = converter.fromJson(json, EntityUid.class); // ERROR
        System.out.println("Converted back to object: " + uid2);
    }

    /* the POJO that gets serialized and fails while deserializing */
    public class EntityUid implements Serializable {
        private final List<Serializable> identifier = new ArrayList<Serializable>();

        public EntityUid(final Serializable... identifier) {
            for (Serializable partialIdentifier : identifier) {
                this.identifier.add(partialIdentifier);
            }
        }
    }

    /* Class for generating Serializable instances */
    public class SerializableInstanceCreator implements InstanceCreator<Serializable> {
        public Serializable createInstance(Type arg0) {
            return new String();
        }
    }
}

这会准备EntityUid对象,将其序列化(就像服务器一样):

Converted to string: {"identifier":["ITEM",12345678,"abc.def.ghijkl.mnopqr",87654321]}

然后尝试将其反序列化为原始对象,但失败并显示以下内容:

Exception in thread "main" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 17
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:176)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40)
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:81)
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:60)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:93)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:172)
    at com.google.gson.Gson.fromJson(Gson.java:803)
    at com.google.gson.Gson.fromJson(Gson.java:768)
    at com.google.gson.Gson.fromJson(Gson.java:717)
    at com.google.gson.Gson.fromJson(Gson.java:689)
    at mycompany.mypackage.GsonSerializeTest.main(GsonSerializeTest.java:19)
Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 17
    at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:374)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:165)
    ... 10 more

有人可以给我一个关于我可能遗失的内容的指针吗?非常感谢提前!

2 个答案:

答案 0 :(得分:2)

请阅读Serializing and Deserializing Collection with Objects of Arbitrary Types

以下代码可以使用。

import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class GsonSerializeTest {

    /* main method to illustrate the problem */
    public static void main(String[] args) {
        Object[] identifiers= {12345678,"ITEM","abc.def.ghijkl.mnopqr",87654321};
        EntityUid uid = new EntityUid(identifiers);
        Gson converter = new GsonBuilder().create();
        String json = converter.toJson(uid);
        System.out.println("Converted to string: " + json);
        EntityUid uid2 = converter.fromJson(json, EntityUid.class); // ERROR
        System.out.println("Converted back to object: " + uid2);
    }

    /* the POJO that gets serialized and fails while deserializing */
    public static class EntityUid  {
        private final List<Object> identifier = new ArrayList<Object>();

        public EntityUid(final Object... identifier) {
            for (Object partialIdentifier : identifier) {
                this.identifier.add(partialIdentifier);
            }
        }

        @Override
        public String toString() {
            return "EntityUid [identifier=" + identifier + "]";
        }


    }
}

答案 1 :(得分:0)

通过以下帖子,它会对你有帮助。

Android JSon error "Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2"

收藏限制:

可以序列化任意对象的集合但不能从中反序列化因为用户无法指示生成的对象的类型在反序列化时,Collection必须是特定的泛型类型所有这些都是有意义的,并且是遵循良好的Java编码实践时很少出现问题

相关问题