BeanSerializer { public static Object deserialize(Object object) { return deserializeByType(object, Object.class); }  static Object serialize(Object bean); static Object serialize(Object bean, boolean addType); static T deserializeByType(Object src, Class<T> clazz); static Object deserialize(Object object);  }
@Test public void deserialize() throws Exception { List list3 = new ArrayList(); list3.add(1111); list3.add("ll222"); Map map4 = new HashMap(); map4.put("key1", "111"); map4.put("key2", 222); Map beanmap5 = new HashMap(); try { BeanSerializer.deserializeByType(beanmap5, TestJsonBean.class); Assert.fail(); } catch (Exception e) { } beanmap5.put("Name", "zzzzggg"); beanmap5.put("Sex", true); beanmap5.put("age", 22); beanmap5.put("friends", new ArrayList<TestJsonBean>()); beanmap5.put("vips", new HashSet<TestJsonBean>()); beanmap5.put("remark", "hehehe"); TestJsonBean bean = BeanSerializer.deserializeByType(beanmap5, TestJsonBean.class); Assert.assertEquals(beanmap5.get("Name"), bean.getName()); Assert.assertEquals(beanmap5.get("Sex"), bean.isSex()); Assert.assertEquals(beanmap5.get("age"), bean.getAge()); Assert.assertEquals(beanmap5.get("friends"), bean.getFriends()); Assert.assertEquals(beanmap5.get("Remark"), bean.getRemark()); TestJsonBean bean1 = new TestJsonBean(); beanmap5.put("friends", new TestJsonBean[] { bean1 }); bean = BeanSerializer.deserializeByType(beanmap5, TestJsonBean.class); Assert.assertEquals(bean1, bean.getFriends().get(0)); beanmap5.put("friends", null); bean = BeanSerializer.deserializeByType(beanmap5, TestJsonBean.class); Assert.assertTrue(CommonUtils.isEmpty(bean.getFriends())); beanmap5.put("friends", "xxxx"); try { BeanSerializer.deserializeByType(beanmap5, TestJsonBean.class); Assert.fail(); } catch (Exception e) { } beanmap5.put("friends", new ArrayList<TestJsonBean>()); beanmap5.put("vips", new TestJsonBean[] { bean1 }); bean = BeanSerializer.deserializeByType(beanmap5, TestJsonBean.class); Assert.assertEquals(bean1, new ArrayList<TestJsonBean>(bean.getVips()).get(0)); beanmap5.put("vips", new HashSet<TestJsonBean>()); beanmap5.put(JSON.CLASS_KEY, "com.alipay.sofa.rpc.common.json.TestJsonBean"); TestJsonBean bean2 = (TestJsonBean) BeanSerializer.deserializeByType(beanmap5, Object.class); Assert.assertEquals(beanmap5.get("Name"), bean2.getName()); Assert.assertEquals(beanmap5.get("Sex"), bean2.isSex()); Assert.assertEquals(beanmap5.get("age"), bean2.getAge()); Assert.assertEquals(beanmap5.get("friends"), bean2.getFriends()); Assert.assertEquals(beanmap5.get("Remark"), bean2.getRemark()); map4.put("bean", beanmap5); list3.add(map4); list3.add(beanmap5); int[] ss6 = new int[] { 11, 22, 33, 44 }; Integer[] ss7 = new Integer[] { 55, 66 }; Object[] args = new Object[] { "11", 22, true, list3, map4, beanmap5, ss6, ss7 }; Object[] os = BeanSerializer.deserializeByType(args, Object[].class); Assert.assertEquals(os[0], "11"); Assert.assertEquals(os[1], 22); Assert.assertEquals(os[2], true); Assert.assertEquals(((List) os[3]).get(0), 1111); Assert.assertEquals(((List) os[3]).get(1), "ll222"); Assert.assertTrue(((List) os[3]).get(2) instanceof Map); Assert.assertTrue(((List) os[3]).get(3) instanceof TestJsonBean); TestJsonBean actualBean3_3 = (TestJsonBean) ((List) os[3]).get(3); Assert.assertEquals(beanmap5.get("Name"), actualBean3_3.getName()); Assert.assertEquals(beanmap5.get("Sex"), actualBean3_3.isSex()); Assert.assertEquals(beanmap5.get("age"), actualBean3_3.getAge()); Assert.assertEquals(beanmap5.get("friends"), actualBean3_3.getFriends()); Assert.assertEquals(beanmap5.get("Remark"), actualBean3_3.getRemark()); Assert.assertTrue(os[4] instanceof Map); Map actualMap4 = (Map) os[4]; Assert.assertTrue(actualMap4.get("bean") instanceof TestJsonBean); Assert.assertTrue(os[5] instanceof TestJsonBean); TestJsonBean actualBean5 = (TestJsonBean) os[5]; Assert.assertEquals(beanmap5.get("Name"), actualBean5.getName()); Assert.assertEquals(beanmap5.get("Sex"), actualBean5.isSex()); Assert.assertEquals(beanmap5.get("age"), actualBean5.getAge()); Assert.assertEquals(beanmap5.get("friends"), actualBean5.getFriends()); Assert.assertEquals(beanmap5.get("Remark"), actualBean5.getRemark()); Assert.assertArrayEquals((int[]) os[6], ss6); Assert.assertArrayEquals((Integer[]) os[7], ss7); List<Map> list8 = new ArrayList<Map>(); for (int i = 0; i < 2; i++) { Map beanmap1 = new HashMap(); beanmap1.put("Name", "zzzzggg"); beanmap1.put("Sex", true); beanmap1.put("age", 22); beanmap1.put("friends", new ArrayList<TestJsonBean>()); beanmap1.put("remark", "hehehe"); list8.add(beanmap1); } List jsonBean1 = (List) BeanSerializer.deserialize(list8); List jsonBean2 = BeanSerializer.deserializeByType(list8, List.class); Assert.assertEquals(jsonBean1.size(), jsonBean2.size()); Assert.assertEquals(jsonBean1.get(0).getClass(), jsonBean2.get(0).getClass()); }