@Test public void test5() { List<LinkedList<SimpleObj>> list = new LinkedList<LinkedList<SimpleObj>>(); LinkedList<SimpleObj> list1 = new LinkedList<SimpleObj>(); for (int j = 0; j < 10; j++) { SimpleObj i = new SimpleObj(); i.setAge(10); i.setId(33442 + j); i.setNumber(30); i.setName("PengtaoQiu\nAlvin"); SimpleObj i2 = new SimpleObj(); i2.setAge(20); i2.setId(12341); i2.setNumber(33); i2.setName("Tom"); i.setContact1(i2); list1.add(i); } list.add(list1); list1 = new LinkedList<SimpleObj>(); for (int j = 0; j < 10; j++) { SimpleObj i = new SimpleObj(); i.setAge(10); i.setId(1000 + j); i.setNumber(30); i.setName("PengtaoQiu\nAlvin"); SimpleObj i2 = new SimpleObj(); i2.setAge(20); i2.setId(12341); i2.setNumber(33); i2.setName("Tom"); i.setContact1(i2); list1.add(i); } list.add(list1); CollectionObj o = new CollectionObj(); o.setList(list); String json = Json.toJson(o); CollectionObj o2 = Json.toObject(json, CollectionObj.class); Assert.assertThat(o2.getList().size(), is(2)); Assert.assertThat(o2.getList().get(0).size(), is(10)); Assert.assertThat(o2.getList().get(0).get(1).getId(), is(33443)); Assert.assertThat(o2.getList().get(1).get(1).getId(), is(1001)); }
@Test public void test2() { SimpleObj i = new SimpleObj(); i.setAge(10); i.setId(33442); i.setNumber(30); i.setName("PengtaoQiu\nAlvin"); SimpleObj i2 = new SimpleObj(); i2.setAge(20); i2.setId(12341); i2.setNumber(33); i2.setName("Tom"); i.setContact1(i2); String jsonStr = Json.toJson(i); SimpleObj temp = Json.toObject(jsonStr, SimpleObj.class); Assert.assertThat(temp.getId(), is(33442)); Assert.assertThat(temp.getContact1().getId(), is(12341)); Assert.assertThat(temp.getContact1().getName(), is("Tom")); Assert.assertThat(temp.getContact1().getAge(), is(20)); Assert.assertThat(temp.getContact2(), nullValue()); }