@Test public void test4() { SimpleObj2 so2 = new SimpleObj2(); so2.setId(334); User user = new User(); user.setId(2434L); user.setName("Pengtao"); so2.setUser(user); Book book = new Book(); book.setId(23424); book.setPrice(3.4); book.setSell(true); book.setText("cccccccc"); book.setTitle("ddddd"); so2.setBook(book); String jsonStr = Json.toJson(so2); SimpleObj2 temp = Json.toObject(jsonStr, SimpleObj2.class); Assert.assertThat(temp.getBook().getPrice(), is(3.4)); Assert.assertThat(temp.getBook().getTitle(), nullValue()); Assert.assertThat(temp.getId(), is(334)); }
@Test public void test11() { SimpleObj2 obj = new SimpleObj2(); obj.setSex('c'); obj.setSymbol("测试一下".toCharArray()); String json = Json.toJson(obj); SimpleObj2 obj2 = Json.toObject(json, SimpleObj2.class); Assert.assertThat(obj2.getSex(), is('c')); Assert.assertThat(new String(obj2.getSymbol()), is("测试一下")); }
@Test public void testBigNumber() { SimpleObj2 s = new SimpleObj2(); s.setBigDecimal(new BigDecimal("-3.34")); s.setBigInteger(new BigInteger("-4")); String json = Json.toJson(s); System.out.println(json); SimpleObj2 s2 = Json.toObject(json, SimpleObj2.class); Assert.assertThat(s2.getBigDecimal(), is(new BigDecimal("-3.34"))); Assert.assertThat(s2.getBigInteger(), is(new BigInteger("-4"))); }