@Test public void testJsonToObject() throws Exception { String json = '{' + "hello:'world'," + "count:10," + "someStatic:'foo'," + "testEnum:'bar'," + "children:[{hello:'world-2',count:11},{hello:'world-3',count:12}]}"; TestObject object = beanJsonConverter.convertToObject(json, TestObject.class); assertEquals("world", object.hello); assertEquals(10, object.count); assertEquals("world-2", object.children.get(0).hello); assertEquals(11, object.children.get(0).count); assertEquals("world-3", object.children.get(1).hello); assertEquals(12, object.children.get(1).count); assertNull("Should not set static values", TestObject.staticValue); assertEquals(TestObject.TestEnum.bar, object.testEnum); }
@Test public void testJsonToNestedGeneric() throws Exception { String jsonActivity = "{key0:[0,1,2],key1:[3,4,5]}"; Map<String, List<Integer>> data = beanJsonConverter.convertToObject(jsonActivity, new TypeLiteral<Map<String, List<Integer>>>(){}.getType()); assertEquals(2, data.size()); assertEquals(Arrays.asList(0, 1, 2), data.get("key0")); assertEquals(Arrays.asList(3, 4, 5), data.get("key1")); }
@Test public void testEmptyJsonMap() throws Exception { String emptyMap = "{}"; Map<String, String> data = beanJsonConverter.convertToObject(emptyMap, new TypeLiteral<Map<String,String>>(){}.getType()); assertTrue(data.isEmpty()); } }
@Test public void testJsonToPrimitives() throws Exception { String simpleJson = "{hello:'world',count:10}"; Object object = beanJsonConverter.convertToObject(simpleJson, null); Map<?, ?> map = (Map<?, ?>) object; assertEquals("world", map.get("hello")); assertEquals(10, map.get("count")); }
@Test public void testEmptyJsonMap() throws Exception { String emptyMap = "{}"; Map<String, String> data = beanJsonConverter.convertToObject(emptyMap, new TypeLiteral<Map<String,String>>(){}.getType()); assertTrue(data.isEmpty()); }
@Test public void testJsonToObject() throws Exception { String json = '{' + "hello:'world'," + "count:10," + "someStatic:'foo'," + "testEnum:'bar'," + "children:[{hello:'world-2',count:11},{hello:'world-3',count:12}]}"; TestObject object = beanJsonConverter.convertToObject(json, TestObject.class); assertEquals("world", object.hello); assertEquals(10, object.count); assertEquals("world-2", object.children.get(0).hello); assertEquals(11, object.children.get(0).count); assertEquals("world-3", object.children.get(1).hello); assertEquals(12, object.children.get(1).count); assertNull("Should not set static values", TestObject.staticValue); assertEquals(TestObject.TestEnum.bar, object.testEnum); }
@Test public void testJsonToNestedGeneric() throws Exception { String jsonActivity = "{key0:[0,1,2],key1:[3,4,5]}"; Map<String, List<Integer>> data = beanJsonConverter.convertToObject(jsonActivity, new TypeLiteral<Map<String, List<Integer>>>(){}.getType()); assertEquals(2, data.size()); assertEquals(Arrays.asList(0, 1, 2), data.get("key0")); assertEquals(Arrays.asList(3, 4, 5), data.get("key1")); }
@Test public void testEmptyJsonMap() throws Exception { String emptyMap = "{}"; Map<String, String> data = beanJsonConverter.convertToObject(emptyMap, new TypeLiteral<Map<String,String>>(){}.getType()); assertTrue(data.isEmpty()); } }
@Test public void testJsonToObject() throws Exception { String json = '{' + "hello:'world'," + "count:10," + "someStatic:'foo'," + "testEnum:'bar'," + "children:[{hello:'world-2',count:11},{hello:'world-3',count:12}]}"; TestObject object = beanJsonConverter.convertToObject(json, TestObject.class); assertEquals("world", object.hello); assertEquals(10, object.count); assertEquals("world-2", object.children.get(0).hello); assertEquals(11, object.children.get(0).count); assertEquals("world-3", object.children.get(1).hello); assertEquals(12, object.children.get(1).count); assertNull("Should not set static values", TestObject.staticValue); assertEquals(TestObject.TestEnum.bar, object.testEnum); }
@Test public void testJsonToNestedGeneric() throws Exception { String jsonActivity = "{key0:[0,1,2],key1:[3,4,5]}"; Map<String, List<Integer>> data = beanJsonConverter.convertToObject(jsonActivity, new TypeLiteral<Map<String, List<Integer>>>(){}.getType()); assertEquals(2, data.size()); assertEquals(Arrays.asList(0, 1, 2), data.get("key0")); assertEquals(Arrays.asList(3, 4, 5), data.get("key1")); }
@Test public void testEmptyJsonMap() throws Exception { String emptyMap = "{}"; Map<String, String> data = beanJsonConverter.convertToObject(emptyMap, new TypeLiteral<Map<String,String>>(){}.getType()); assertTrue(data.isEmpty()); }
@Test public void testJsonToObject() throws Exception { String json = '{' + "hello:'world'," + "count:10," + "someStatic:'foo'," + "testEnum:'bar'," + "children:[{hello:'world-2',count:11},{hello:'world-3',count:12}]}"; TestObject object = beanJsonConverter.convertToObject(json, TestObject.class); assertEquals("world", object.hello); assertEquals(10, object.count); assertEquals("world-2", object.children.get(0).hello); assertEquals(11, object.children.get(0).count); assertEquals("world-3", object.children.get(1).hello); assertEquals(12, object.children.get(1).count); assertNull("Should not set static values", TestObject.staticValue); assertEquals(TestObject.TestEnum.bar, object.testEnum); }
@Test public void testJsonToPrimitives() throws Exception { String simpleJson = "{hello:'world',count:10}"; Object object = beanJsonConverter.convertToObject(simpleJson, null); Map<?, ?> map = (Map<?, ?>) object; assertEquals("world", map.get("hello")); assertEquals(10, map.get("count")); }
@Test public void testEmptyJsonMap() throws Exception { String emptyMap = "{}"; Map<String, String> data = beanJsonConverter.convertToObject(emptyMap, new TypeLiteral<Map<String,String>>(){}.getType()); assertTrue(data.isEmpty()); }
@Test public void testJsonToObject() throws Exception { String json = '{' + "hello:'world'," + "count:10," + "someStatic:'foo'," + "testEnum:'bar'," + "children:[{hello:'world-2',count:11},{hello:'world-3',count:12}]}"; TestObject object = beanJsonConverter.convertToObject(json, TestObject.class); assertEquals("world", object.hello); assertEquals(10, object.count); assertEquals("world-2", object.children.get(0).hello); assertEquals(11, object.children.get(0).count); assertEquals("world-3", object.children.get(1).hello); assertEquals(12, object.children.get(1).count); assertNull("Should not set static values", TestObject.staticValue); assertEquals(TestObject.TestEnum.bar, object.testEnum); }
@Test public void testJsonToNestedGeneric() throws Exception { String jsonActivity = "{key0:[0,1,2],key1:[3,4,5]}"; Map<String, List<Integer>> data = beanJsonConverter.convertToObject(jsonActivity, new TypeLiteral<Map<String, List<Integer>>>(){}.getType()); assertEquals(2, data.size()); assertEquals(Arrays.asList(0, 1, 2), data.get("key0")); assertEquals(Arrays.asList(3, 4, 5), data.get("key1")); }
@Test public void testEmptyExtendableBean() throws Exception { String emptyMap = "{}"; ExtendableTestObject data = beanJsonConverter.convertToObject(emptyMap, ExtendableTestObject.class); assertTrue(data.isEmpty()); }
@Test public void testGetPropertyNameOfBridgeMethod() throws NoSuchMethodException, SecurityException { Method bridgeSetter = GenericObject.class.getMethod("setT", Object.class); assertNull(BeanJsonConverter.getPropertyName(bridgeSetter)); Method realSetter = GenericObject.class.getMethod("setT", String.class);; assertEquals("t", BeanJsonConverter.getPropertyName(realSetter)); } }
@Test public void testJsonToPrimitives() throws Exception { String simpleJson = "{hello:'world',count:10}"; Object object = beanJsonConverter.convertToObject(simpleJson, null); Map<?, ?> map = (Map<?, ?>) object; assertEquals("world", map.get("hello")); assertEquals(10, map.get("count")); }
@Test public void testEmptyExtendableBean() throws Exception { String emptyMap = "{}"; ExtendableTestObject data = beanJsonConverter.convertToObject(emptyMap, ExtendableTestObject.class); assertTrue(data.isEmpty()); }