麻烦GSON序列化POJO的的ArrayList [英] Trouble with Gson serializing an ArrayList of POJO's

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

问题描述

我是一个新的Java codeR,尽我所能去学习一切我所能。我一直打算使用SimpleXML我的序列化的需求,但想我会尝试的Json了,学习新的东西。这导致最令人沮丧的12小时我曾经在很长一段时间度过。

这是code我使用的尝试和序列化测试POJO的使用GSON 1.7.1的ArrayList。请记住,我非常新的一般序列,所以请如果答案是很明显的温柔。
注:我删除了字符串的读/写器的简化code

 封装测试;进口java.io.IOException异常;
进口的java.util.ArrayList;
进口java.util.Collections中;
进口的java.util.List;进口com.google.gson.Gson;公共类TestGsonSerialDeserialList {
    公共静态无效的主要(字串[] args)抛出IOException
        GSON GSON =新GSON();        //使串行
        字符串s;
        清单<&的TestObject GT;清单= Collections.synchronizedList(新的ArrayList<&的TestObject GT;());
        list.add(新的TestObject());
        list.add(新的TestObject());        S = gson.toJson(列表,ArrayList.class);
        的System.out.println(多个);        //吃串行
        清单<&的TestObject GT;列表2 = Collections.synchronizedList(gson.fromJson(S,ArrayList.class));
        的System.out.println(list2.get(0));
        的System.out.println(list2.get(1));
    }
}

下面是输出我得到:

<$p$p><$c$c>[{\"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

要我新手的眼睛,这看起来是正确的。只是,对象的反序列化列表包含基本对象,而不是那么的TestObject的我序列化。任何人都可以请向我解释什么,如果有的话,我可以做,使这项工作?

感谢。

编辑:

修正测试:感谢ColinD

 封装测试;进口java.io.FileInputStream中;
进口java.io.FileOutputStream中;
进口java.io.IOException异常;
进口java.io.InputStreamReader中;
进口java.io.OutputStreamWriter中;
进口的java.io.Reader;
进口的java.io.Writer;
进口java.lang.reflect.Type;
进口的java.util.ArrayList;
进口java.util.Collections中;
进口的java.util.List;进口com.google.gson.Gson;
进口com.google.gson.reflect.TypeToken;公共类TestGsonSerialDeserialList {
    公共静态无效的主要(字串[] args)抛出IOException
        的System.out.println(---序列化/反序列化开始---);
        字符串文件名=json的\\\\ testList.json        GSON GSON =新GSON();
        键入listOfTestObject =新TypeToken&LT;名单,LT;&的TestObject GT;&GT;(){}的getType()。        //使串行
        作家osWriter =新OutputStreamWriter(新的FileOutputStream(文件名));
        清单&LT;&的TestObject GT;清单= Collections.synchronizedList(新的ArrayList&LT;&的TestObject GT;());
        list.add(新的TestObject());
        list.add(新的TestObject());
        list.add(新的TestObject());
        list.add(新的TestObject());
        gson.toJson(列表,osWriter);
        osWriter.close();
        //吃串行
        读者isReader =新的InputStreamReader(新的FileInputStream((文件名)));
        清单&LT;&的TestObject GT;列表2 = Collections.synchronizedList(
            (列表&LT;&的TestObject GT;)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(---序列化/反序列化端---);
    }
}

输出:

  ---序列化/反序列化开始---
编号:1,I1:12345,名称:ABCDEFG,S []:[Ljava.lang.String; @ 95c083
编号:2,I1:12345,名称:ABCDEFG,S []:[Ljava.lang.String; @ 6791d8c1
编号:3,I1:12345,名称:ABCDEFG,S []:[Ljava.lang.String; @ 182d9c06
编号:4,I1:12345,名称:ABCDEFG,S []:[Ljava.lang.String; @ 5a5e5a50
---序列化/反序列化端---

EDIT2:

老实说,我不知道为什么,但我取代了简单的String镶嵌在我的TestObject与一个ArrayList [],它开始正确的序列化。

  ---序列化/反序列化开始---
编号:1,I1:12345,名称:ABCDEFG,S []:[A,B,C]
编号:2,I1:12345,名称:ABCDEFG,S []:[A,B,C]
编号:3,I1:12345,名称:ABCDEFG,S []:[A,B,C]
编号:4,I1:12345,名称:ABCDEFG,S []:[A,B,C]
---序列化/反序列化端---


解决方案

您需要给具体的泛型类型列表你正在使用的GSON信息(或任何泛型类型你使用它)。特别是反序列化JSON的时候,它需要的信息,以便能够确定什么类型的对象,应该每个数组元素反序列化到。

 键入listOfTestObject =新TypeToken&LT;名单,LT;&的TestObject GT;方式&gt;(){}的getType();
字符串s = gson.toJson(列表,listOfTestObject);
清单&LT;&的TestObject GT;列表2 = gson.fromJson(S,listOfTestObject);

这是在 GSON用户指南记录。

I am a new Java coder, doing everything I can to learn everything I can. I had been planning on using simpleXML for my serialization needs, but figured I would try Json out, to learn something new. This has lead to the most frustrating 12 hours I have spent in a long time.

This is the code I am using to try and serialize an ArrayList of test POJO's using Gson 1.7.1. Keep in mind that I am extremely new to serialization in general, so please be gentle if the answer is very obvious. 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) );
    }
}

Here is the output I get:

[{"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

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?

Thanks.

EDIT:

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 ---");
    }
}

output:

--- 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 ---

EDIT2:

I honestly dont know why, but when I replaced the simple String[] embeded 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 ---

解决方案

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);

This is documented in the Gson user guide.

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

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