ObjectMapper { public <T> T convertValue(Object fromValue, Class<T> toValueType) throws IllegalArgumentException { if (fromValue == null) { return null; } if (toValueType != Object.class && toValueType.isAssignableFrom(fromValue.getClass())) { return (T) fromValue; } try { if (fromValue instanceof JSONObject) { return _convertFromJSONObject((JSONObject) fromValue, toValueType); } else if (toValueType == JSONObject.class) { return (T) _convertToJSONObject(fromValue); } else { throw new IllegalArgumentException( "Expecting either fromValue or toValueType to be a JSONObject"); } } catch (NoSuchMethodException e) { throw new IllegalArgumentException(e); } catch (IllegalAccessException e) { throw new IllegalArgumentException(e); } catch (InstantiationException e) { throw new IllegalArgumentException(e); } catch (JSONException e) { throw new IllegalArgumentException(e); } catch (InvocationTargetException e) { throw ExceptionUtil.propagate(e.getCause()); } }  T convertValue(Object fromValue, Class<T> toValueType); JSONArray convertListToJsonArray(Object value);  }
@Test public void testJsonProperty() throws IOException, JSONException { JsonPropertyString c = new JsonPropertyString(); c.testString = "test"; String expected = "{\"testString\":\"test\"}"; JSONObject jsonObject = mObjectMapper.convertValue(c, JSONObject.class); String str = jsonObject.toString(); assertEquals(expected, str); JsonPropertyString jsonPropertyString = mObjectMapper.convertValue( new JSONObject(expected), JsonPropertyString.class); assertEquals(c, jsonPropertyString); }
@Test public void testNestedProperty() throws JSONException { NestedJsonProperty njp = new NestedJsonProperty(); njp.child1 = new JsonPropertyString(); njp.child2 = new JsonPropertyInt(); njp.child1.testString = "testString"; njp.child2.i = 4; String expected7 = "{\"child1\":{\"testString\":\"testString\"},\"child2\":{\"i\":4}}"; String expected8 = "{\"child2\":{\"i\":4},\"child1\":{\"testString\":\"testString\"}}"; NestedJsonProperty parsed7 = mObjectMapper.convertValue( new JSONObject(expected7), NestedJsonProperty.class); assertEquals(njp, parsed7); NestedJsonProperty parsed8 = mObjectMapper.convertValue( new JSONObject(expected8), NestedJsonProperty.class); assertEquals(njp, parsed8); JSONObject jsonObject = mObjectMapper.convertValue(njp, JSONObject.class); assertTrue(expected7.equals(jsonObject.toString()) || expected8.equals(jsonObject.toString())); }
@Test public void testEnumProperty() throws JSONException { JsonPropertyEnum jpe = new JsonPropertyEnum(); jpe.enumValue = TestEnum.VALUE_TWO; String expected = "{\"enumValue\":\"two\"}"; JsonPropertyEnum parsed = mObjectMapper.convertValue( new JSONObject(expected), JsonPropertyEnum.class); assertEquals(jpe, parsed); JSONObject jsonObject = mObjectMapper.convertValue(jpe, JSONObject.class); assertEquals(expected, jsonObject.toString()); }
@Test public void testListString() throws JSONException { JsonPropertyStringList jpsl = new JsonPropertyStringList(); List<String> values = new ArrayList<String>(); jpsl.stringList = values; values.add("one"); values.add("two"); values.add("three"); String expected = "{\"stringList\":[\"one\",\"two\",\"three\"]}"; JsonPropertyStringList jsonPropertyStringList = mObjectMapper.convertValue( new JSONObject(expected), JsonPropertyStringList.class); assertEquals(jpsl, jsonPropertyStringList); JSONObject jsonObject = mObjectMapper.convertValue(jpsl, JSONObject.class); String str = jsonObject.toString(); assertEquals(expected, str); }
@Test public void testSerializeMultitypedList() throws JSONException { List<Object> list = new ArrayList<Object>(); list.add("foo"); list.add(Collections.singletonList("bar")); JsonPropertyMultitypedList javaObj = new JsonPropertyMultitypedList(); javaObj.multitypedList = list; String expected = "{\"multitypedList\":[\"foo\",[\"bar\"]]}"; JSONObject jsonObj = mObjectMapper.convertValue(javaObj, JSONObject.class); String str = jsonObj.toString(); assertEquals(expected, str); }
@Test public void testSerializeListOfLists() throws JSONException { List<List<String>> listOfLists = new ArrayList<List<String>>(); listOfLists.add(Collections.singletonList("foo")); ArrayList<String> sublist2 = new ArrayList<String>(); sublist2.add("1"); sublist2.add("2"); listOfLists.add(sublist2); JsonPropertyListOfLists javaObj = new JsonPropertyListOfLists(); javaObj.listOfLists = listOfLists; String expected = "{\"listOfLists\":[[\"foo\"],[\"1\",\"2\"]]}"; JSONObject jsonObj = mObjectMapper.convertValue(javaObj, JSONObject.class); String str = jsonObj.toString(); assertEquals(expected, str); }
@Test public void testObjectToPrimitive() throws JSONException { ArrayOfPrimitivesContainer container = new ArrayOfPrimitivesContainer(); ArrayList<Object> primitives = container.primitives; primitives.add(Long.MIN_VALUE); primitives.add(Long.MAX_VALUE); primitives.add(Integer.MIN_VALUE); primitives.add(Integer.MAX_VALUE); primitives.add(Float.MIN_VALUE); primitives.add(Float.MAX_VALUE); primitives.add(Double.MIN_VALUE); primitives.add(Double.MAX_VALUE); String json = mObjectMapper.convertValue(container, JSONObject.class).toString(); JSONObject obj = new JSONObject(json); JSONArray array = obj.getJSONArray("primitives"); ArrayList<Object> actual = new ArrayList<>(); for (int i = 0, N = array.length(); i < N; i++) { actual.add(array.get(i)); } assertEquals(primitives.toString(), actual.toString()); }