Gson 序列化 POJO 的 ArrayList 时遇到问题 [英] Trouble with Gson serializing an ArrayList of POJO's

查看:29
本文介绍了Gson 序列化 POJO 的 ArrayList 时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直计划使用 simpleXML 来满足我的序列化需求,但我想我会尝试使用 JSON 来学习新的东西.

I had been planning on using simpleXML for my serialization needs, but figured I would try JSON out, to learn something new.

这是我用来尝试使用 Gson 1.7.1 序列化测试 POJO 的 ArrayList 的代码.

This is the code I am using to try and serialize an ArrayList of test POJO's using Gson 1.7.1.

注意:我删除了字符串s"的读取器/写入器以简化代码.

Note: I removed the Reader/Writers for a String "s" to simplify the code.

package test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.google.gson.Gson;

public class TestGsonSerialDeserialList {
    public static void main(String[] args) throws IOException{
        Gson gson = new Gson();

        //Make Serial 
        String s;
        List<TestObject> list = Collections.synchronizedList(new ArrayList<TestObject>() );
        list.add(new TestObject());
        list.add(new TestObject());

        s = gson.toJson(list, ArrayList.class);
        System.out.println(s);

        //Eat Serial
        List<TestObject> list2 = Collections.synchronizedList(gson.fromJson(s, ArrayList.class) );
        System.out.println(list2.get(0) );
        System.out.println(list2.get(1) );
    }
}

这是我得到的输出:

[{"objectID":1,"i1":12345,"name":"abcdefg","s":["a","b","c"]},{"objectID":2,"i1":12345,"name":"abcdefg","s":["a","b","c"]}]
java.lang.Object@5c74c3aa
java.lang.Object@75d9fd51

在我的新手眼里,这看起来是正确的.只是,对象的 DeSerialized 列表包含基本对象,而不是 TestObject 的 I 序列化.任何人都可以向我解释一下,如果有的话,我可以做些什么来完成这项工作?

To my newbie eyes this looks correct. Only, the DeSerialized list of objects contains basic Objects, rather then the TestObject's I serialized. Can anyone please explain to me what, if anything, I can do to make this work?

更正测试:感谢 ColinD

Corrected to test: Thanks to ColinD

package test;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class TestGsonSerialDeserialList {
    public static void main(String[] args) throws IOException{
        System.out.println("--- Serialize / Deserialize Started ---");
        String fileName = "json\\testList.json";

        Gson gson = new Gson();
        Type listOfTestObject = new TypeToken<List<TestObject>>(){}.getType();

        //Make Serial 
        Writer osWriter = new OutputStreamWriter( new FileOutputStream(fileName));
        List<TestObject> list = Collections.synchronizedList(new ArrayList<TestObject>() );
        list.add(new TestObject());
        list.add(new TestObject());
        list.add(new TestObject());
        list.add(new TestObject());
        gson.toJson(list, osWriter);
        osWriter.close();


        //Eat Serial
        Reader isReader = new InputStreamReader( new FileInputStream((fileName) ) );
        List<TestObject> list2 = Collections.synchronizedList(
            (List<TestObject>)gson.fromJson(isReader, listOfTestObject) 
        );
        isReader.close();
        System.out.println(list2.get(0) );
        System.out.println(list2.get(1) );
        System.out.println(list2.get(2) );
        System.out.println(list2.get(3) );
        System.out.println("--- Serialize / Deserialize Ended ---");
    }
}

输出:

--- Serialize / Deserialize Started ---
ID#: 1, i1: 12345, name: abcdefg, s[]: [Ljava.lang.String;@95c083
ID#: 2, i1: 12345, name: abcdefg, s[]: [Ljava.lang.String;@6791d8c1
ID#: 3, i1: 12345, name: abcdefg, s[]: [Ljava.lang.String;@182d9c06
ID#: 4, i1: 12345, name: abcdefg, s[]: [Ljava.lang.String;@5a5e5a50
--- Serialize / Deserialize Ended ---

老实说,我不知道为什么,但是当我用 ArrayList 替换嵌入在我的 TestObject 中的简单 String[] 时,它开始正确序列化.

I honestly don't know why, but when I replaced the simple String[] embedded in my TestObject with an ArrayList, it started serializing correctly.

--- Serialize / Deserialize Started ---
ID#: 1, i1: 12345, name: abcdefg, s[]: [a, b, c]
ID#: 2, i1: 12345, name: abcdefg, s[]: [a, b, c]
ID#: 3, i1: 12345, name: abcdefg, s[]: [a, b, c]
ID#: 4, i1: 12345, name: abcdefg, s[]: [a, b, c]
--- Serialize / Deserialize Ended ---

推荐答案

您需要向 Gson 提供有关您正在使用的 List 的特定泛型类型(或与它一起使用的任何泛型类型)的信息).特别是在反序列化 JSON 时,它需要该信息才能确定应该将每个数组元素反序列化为什么类型的对象.

You need to give Gson information on the specific generic type of List you're using (or any generic type you use with it). Particularly when deserializing JSON, it needs that information to be able to determine what type of object it should deserialize each array element to.

Type listOfTestObject = new TypeToken<List<TestObject>>(){}.getType();
String s = gson.toJson(list, listOfTestObject);
List<TestObject> list2 = gson.fromJson(s, listOfTestObject);

这在 Gson 用户指南.

这篇关于Gson 序列化 POJO 的 ArrayList 时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆