BeanSerializer { public static Object serialize(Object bean) throws NullPointerException { return serialize(bean, false); }  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 serialize() { TestJsonBean bean = new TestJsonBean(); boolean error = false; try { Map map = (Map) BeanSerializer.serialize(bean); } catch (Exception e) { error = true; } Assert.assertTrue(error); error = false; bean.setName("zzzggg"); try { Map map = (Map) BeanSerializer.serialize(bean, true); Assert.assertEquals(map.get("Name"), "zzzggg"); Assert.assertEquals(map.get("Sex"), false); Assert.assertEquals(map.get("age"), 0); Assert.assertFalse(map.containsKey("friends")); Assert.assertTrue(map.containsKey("Remark")); Assert.assertTrue(map.containsKey(JSON.CLASS_KEY)); map = (Map) BeanSerializer.serialize(bean); Assert.assertEquals(map.get("Name"), "zzzggg"); Assert.assertEquals(map.get("Sex"), false); Assert.assertEquals(map.get("age"), 0); Assert.assertFalse(map.containsKey("friends")); Assert.assertTrue(map.containsKey("Remark")); Assert.assertFalse(map.containsKey(JSON.CLASS_KEY)); } catch (Exception e) { error = true; } Assert.assertFalse(error); bean.setName("zzzgg"); bean.setSex(true); bean.setAge(111); bean.setStep(1234567890l); bean.setFriends(new ArrayList<TestJsonBean>()); bean.setStatus(TestJsonBean.Status.START); error = false; try { Map map = (Map) BeanSerializer.serialize(bean, true); Assert.assertEquals(map.get("Name"), "zzzgg"); Assert.assertEquals(map.get("Sex"), true); Assert.assertEquals(map.get("age"), 111); Assert.assertTrue(map.containsKey("friends")); Assert.assertTrue(map.containsKey("Remark")); Assert.assertTrue(map.containsKey(JSON.CLASS_KEY)); map = (Map) BeanSerializer.serialize(bean); Assert.assertEquals(map.get("Name"), "zzzgg"); Assert.assertEquals(map.get("Sex"), true); Assert.assertEquals(map.get("age"), 111); Assert.assertTrue(map.containsKey("friends")); Assert.assertTrue(map.containsKey("Remark")); Assert.assertFalse(map.containsKey(JSON.CLASS_KEY)); } catch (Exception e) { error = true; } Assert.assertFalse(error); }
@Test public void serializeMap() { boolean error = false; Map<Object, Object> bean = createTestMap(); try { Map map = (Map) BeanSerializer.serialize(bean, true); for (Map.Entry<Object, Object> entry : bean.entrySet()) { if (!map.containsKey(entry.getKey())) { error = true; break; } } Assert.assertFalse(error); map = (Map) BeanSerializer.serialize(bean); for (Map.Entry<Object, Object> entry : bean.entrySet()) { if (!map.containsKey(entry.getKey())) { error = true; break; } } Assert.assertFalse(error); } catch (Exception e) { error = true; } Assert.assertFalse(error); }