Merge pull request #1006 from sk02241994/feature-1003

1003: Implement JSONObject.fromJson() with unit tests
This commit is contained in:
Sean Leary
2025-10-31 11:25:03 -05:00
committed by GitHub
12 changed files with 588 additions and 0 deletions

View File

@@ -17,6 +17,9 @@ import java.math.BigInteger;
import java.util.*;
import java.util.Map.Entry;
import java.util.regex.Pattern;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.GenericArrayType;
/**
* A JSONObject is an unordered collection of name/value pairs. Its external
@@ -3207,4 +3210,250 @@ public class JSONObject {
"JavaBean object contains recursively defined member variable of key " + quote(key)
);
}
/**
* Helper method to extract the raw Class from Type.
*/
private Class<?> getRawType(Type type) {
if (type instanceof Class) {
return (Class<?>) type;
} else if (type instanceof ParameterizedType) {
return (Class<?>) ((ParameterizedType) type).getRawType();
} else if (type instanceof GenericArrayType) {
return Object[].class; // Simplified handling for arrays
}
return Object.class; // Fallback
}
/**
* Extracts the element Type for a Collection Type.
*/
private Type getElementType(Type type) {
if (type instanceof ParameterizedType) {
Type[] args = ((ParameterizedType) type).getActualTypeArguments();
return args.length > 0 ? args[0] : Object.class;
}
return Object.class;
}
/**
* Extracts the key and value Types for a Map Type.
*/
private Type[] getMapTypes(Type type) {
if (type instanceof ParameterizedType) {
Type[] args = ((ParameterizedType) type).getActualTypeArguments();
if (args.length == 2) {
return args;
}
}
return new Type[]{Object.class, Object.class}; // Default: String keys, Object values
}
/**
* Deserializes a JSON string into an instance of the specified class.
*
* <p>This method attempts to map JSON key-value pairs to the corresponding fields
* of the given class. It supports basic data types including int, double, float,
* long, and boolean (as well as their boxed counterparts). The class must have a
* no-argument constructor, and the field names in the class must match the keys
* in the JSON string.
*
* @param jsonString json in string format
* @param clazz the class of the object to be returned
* @return an instance of Object T with fields populated from the JSON string
*/
public static <T> T fromJson(String jsonString, Class<T> clazz) {
JSONObject jsonObject = new JSONObject(jsonString);
return jsonObject.fromJson(clazz);
}
/**
* Deserializes a JSON string into an instance of the specified class.
*
* <p>This method attempts to map JSON key-value pairs to the corresponding fields
* of the given class. It supports basic data types including {@code int}, {@code double},
* {@code float}, {@code long}, and {@code boolean}, as well as their boxed counterparts.
* The target class must have a no-argument constructor, and its field names must match
* the keys in the JSON string.
*
* <p><strong>Note:</strong> Only classes that are explicitly supported and registered within
* the {@code JSONObject} context can be deserialized. If the provided class is not among those,
* this method will not be able to deserialize it. This ensures that only a limited and
* controlled set of types can be instantiated from JSON for safety and predictability.
*
* @param clazz the class of the object to be returned
* @param <T> the type of the object
* @return an instance of type {@code T} with fields populated from the JSON string
* @throws IllegalArgumentException if the class is not supported for deserialization
*/
@SuppressWarnings("unchecked")
public <T> T fromJson(Class<T> clazz) {
try {
T obj = clazz.getDeclaredConstructor().newInstance();
for (Field field : clazz.getDeclaredFields()) {
field.setAccessible(true);
String fieldName = field.getName();
if (has(fieldName)) {
Object value = get(fieldName);
Type fieldType = field.getGenericType();
Object convertedValue = convertValue(value, fieldType);
field.set(obj, convertedValue);
}
}
return obj;
} catch (NoSuchMethodException e) {
throw new JSONException("No no-arg constructor for class: " + clazz.getName(), e);
} catch (Exception e) {
throw new JSONException("Failed to instantiate or set field for class: " + clazz.getName(), e);
}
}
/**
* Recursively converts a value to the target Type, handling nested generics for Collections and Maps.
*/
private Object convertValue(Object value, Type targetType) throws JSONException {
if (value == null) {
return null;
}
Class<?> rawType = getRawType(targetType);
// Direct assignment
if (rawType.isAssignableFrom(value.getClass())) {
return value;
}
if (rawType == int.class || rawType == Integer.class) {
return ((Number) value).intValue();
} else if (rawType == double.class || rawType == Double.class) {
return ((Number) value).doubleValue();
} else if (rawType == float.class || rawType == Float.class) {
return ((Number) value).floatValue();
} else if (rawType == long.class || rawType == Long.class) {
return ((Number) value).longValue();
} else if (rawType == boolean.class || rawType == Boolean.class) {
return value;
} else if (rawType == String.class) {
return value;
} else if (rawType == BigDecimal.class) {
return new BigDecimal((String) value);
} else if (rawType == BigInteger.class) {
return new BigInteger((String) value);
}
// Enum conversion
if (rawType.isEnum() && value instanceof String) {
return stringToEnum(rawType, (String) value);
}
// Collection handling (e.g., List<List<Map<String, Integer>>>)
if (Collection.class.isAssignableFrom(rawType)) {
if (value instanceof JSONArray) {
Type elementType = getElementType(targetType);
return fromJsonArray((JSONArray) value, rawType, elementType);
}
}
// Map handling (e.g., Map<Integer, List<String>>)
else if (Map.class.isAssignableFrom(rawType) && value instanceof JSONObject) {
Type[] mapTypes = getMapTypes(targetType);
Type keyType = mapTypes[0];
Type valueType = mapTypes[1];
return convertToMap((JSONObject) value, keyType, valueType, rawType);
}
// POJO handling (including custom classes like Tuple<Integer, String, Integer>)
else if (!rawType.isPrimitive() && !rawType.isEnum() && value instanceof JSONObject) {
// Recurse with the raw class for POJO deserialization
return ((JSONObject) value).fromJson(rawType);
}
// Fallback
return value.toString();
}
/**
* Converts a JSONObject to a Map with the specified generic key and value Types.
* Supports nested types via recursive convertValue.
*/
private Map<?, ?> convertToMap(JSONObject jsonMap, Type keyType, Type valueType, Class<?> mapType) throws JSONException {
try {
@SuppressWarnings("unchecked")
Map<Object, Object> createdMap = new HashMap();
for (Object keyObj : jsonMap.keySet()) {
String keyStr = (String) keyObj;
Object mapValue = jsonMap.get(keyStr);
// Convert key (e.g., String to Integer for Map<Integer, ...>)
Object convertedKey = convertValue(keyStr, keyType);
// Convert value recursively (handles nesting)
Object convertedValue = convertValue(mapValue, valueType);
createdMap.put(convertedKey, convertedValue);
}
return createdMap;
} catch (Exception e) {
throw new JSONException("Failed to convert JSONObject to Map: " + mapType.getName(), e);
}
}
/**
* Converts a String to an Enum value.
*/
private <E> E stringToEnum(Class<?> enumClass, String value) throws JSONException {
try {
@SuppressWarnings("unchecked")
Class<E> enumType = (Class<E>) enumClass;
Method valueOfMethod = enumType.getMethod("valueOf", String.class);
return (E) valueOfMethod.invoke(null, value);
} catch (Exception e) {
throw new JSONException("Failed to convert string to enum: " + value + " for " + enumClass.getName(), e);
}
}
/**
* Deserializes a JSONArray into a Collection, supporting nested generics.
* Uses recursive convertValue for elements.
*/
@SuppressWarnings("unchecked")
private <T> Collection<T> fromJsonArray(JSONArray jsonArray, Class<?> collectionType, Type elementType) throws JSONException {
try {
Collection<T> collection = getCollection(collectionType);
for (int i = 0; i < jsonArray.length(); i++) {
Object jsonElement = jsonArray.get(i);
// Recursively convert each element using the full element Type (handles nesting)
Object convertedValue = convertValue(jsonElement, elementType);
collection.add((T) convertedValue);
}
return collection;
} catch (Exception e) {
throw new JSONException("Failed to convert JSONArray to Collection: " + collectionType.getName(), e);
}
}
/**
* Creates and returns a new instance of a supported {@link Collection} implementation
* based on the specified collection type.
* <p>
* This method currently supports the following collection types:
* <ul>
* <li>{@code List.class}</li>
* <li>{@code ArrayList.class}</li>
* <li>{@code Set.class}</li>
* <li>{@code HashSet.class}</li>
* </ul>
* If the provided type does not match any of the supported types, a {@link JSONException}
* is thrown.
*
* @param collectionType the {@link Class} object representing the desired collection type
* @return a new empty instance of the specified collection type
* @throws JSONException if the specified type is not a supported collection type
*/
private Collection getCollection(Class<?> collectionType) throws JSONException {
if (collectionType == List.class || collectionType == ArrayList.class) {
return new ArrayList();
} else if (collectionType == Set.class || collectionType == HashSet.class) {
return new HashSet();
} else {
throw new JSONException("Unsupported Collection type: " + collectionType.getName());
}
}
}

View File

@@ -56,6 +56,17 @@ import org.json.junit.data.RecursiveBeanEquals;
import org.json.junit.data.Singleton;
import org.json.junit.data.SingletonEnum;
import org.json.junit.data.WeirdList;
import org.json.junit.data.CustomClass;
import org.json.junit.data.CustomClassA;
import org.json.junit.data.CustomClassB;
import org.json.junit.data.CustomClassC;
import org.json.junit.data.CustomClassD;
import org.json.junit.data.CustomClassE;
import org.json.junit.data.CustomClassF;
import org.json.junit.data.CustomClassG;
import org.json.junit.data.CustomClassH;
import org.json.junit.data.CustomClassI;
import org.json.JSONObject;
import org.junit.After;
import org.junit.Ignore;
import org.junit.Test;
@@ -4095,4 +4106,128 @@ public class JSONObjectTest {
assertTrue("JSONObject should be empty", jsonObject.isEmpty());
}
@Test
public void jsonObjectParseFromJson_0() {
JSONObject object = new JSONObject();
object.put("number", 12);
object.put("name", "Alex");
object.put("longNumber", 1500000000L);
CustomClass customClass = object.fromJson(CustomClass.class);
CustomClass compareClass = new CustomClass(12, "Alex", 1500000000L);
assertEquals(customClass, compareClass);
}
@Test
public void jsonObjectParseFromJson_1() {
JSONObject object = new JSONObject();
BigInteger largeInt = new BigInteger("123");
object.put("largeInt", largeInt.toString());
CustomClassA customClassA = object.fromJson(CustomClassA.class);
CustomClassA compareClassClassA = new CustomClassA(largeInt);
assertEquals(customClassA, compareClassClassA);
}
@Test
public void jsonObjectParseFromJson_2() {
JSONObject object = new JSONObject();
object.put("number", 12);
JSONObject classC = new JSONObject();
classC.put("stringName", "Alex");
classC.put("longNumber", 123456L);
object.put("classC", classC);
CustomClassB customClassB = object.fromJson(CustomClassB.class);
CustomClassC classCObject = new CustomClassC("Alex", 123456L);
CustomClassB compareClassB = new CustomClassB(12, classCObject);
assertEquals(customClassB, compareClassB);
}
@Test
public void jsonObjectParseFromJson_3() {
JSONObject object = new JSONObject();
JSONArray array = new JSONArray();
array.put("test1");
array.put("test2");
array.put("test3");
object.put("stringList", array);
CustomClassD customClassD = object.fromJson(CustomClassD.class);
CustomClassD compareClassD = new CustomClassD(Arrays.asList("test1", "test2", "test3"));
assertEquals(customClassD, compareClassD);
}
@Test
public void jsonObjectParseFromJson_4() {
JSONObject object = new JSONObject();
JSONArray array = new JSONArray();
array.put(new CustomClassC("test1", 1L).toJSON());
array.put(new CustomClassC("test2", 2L).toJSON());
object.put("listClassC", array);
CustomClassE customClassE = object.fromJson(CustomClassE.class);
CustomClassE compareClassE = new CustomClassE(java.util.Arrays.asList(
new CustomClassC("test1", 1L),
new CustomClassC("test2", 2L)));
assertEquals(customClassE, compareClassE);
}
@Test
public void jsonObjectParseFromJson_5() {
JSONObject object = new JSONObject();
JSONArray array = new JSONArray();
array.put(Arrays.asList("A", "B", "C"));
array.put(Arrays.asList("D", "E"));
object.put("listOfString", array);
CustomClassF customClassF = object.fromJson(CustomClassF.class);
List<List<String>> listOfString = new ArrayList<>();
listOfString.add(Arrays.asList("A", "B", "C"));
listOfString.add(Arrays.asList("D", "E"));
CustomClassF compareClassF = new CustomClassF(listOfString);
assertEquals(customClassF, compareClassF);
}
@Test
public void jsonObjectParseFromJson_6() {
JSONObject object = new JSONObject();
Map<String, String> dataList = new HashMap<>();
dataList.put("A", "Aa");
dataList.put("B", "Bb");
dataList.put("C", "Cc");
object.put("dataList", dataList);
CustomClassG customClassG = object.fromJson(CustomClassG.class);
CustomClassG compareClassG = new CustomClassG(dataList);
assertEquals(customClassG, compareClassG);
}
@Test
public void jsonObjectParseFromJson_7() {
JSONObject object = new JSONObject();
Map<String, List<Integer>> dataList = new HashMap<>();
dataList.put("1", Arrays.asList(1, 2, 3, 4));
dataList.put("2", Arrays.asList(2, 3, 4, 5));
object.put("integerMap", dataList);
CustomClassH customClassH = object.fromJson(CustomClassH.class);
CustomClassH compareClassH = new CustomClassH(dataList);
assertEquals(customClassH.integerMap.toString(), compareClassH.integerMap.toString());
}
@Test
public void jsonObjectParseFromJson_8() {
JSONObject object = new JSONObject();
Map<String, Map<String, Integer>> dataList = new HashMap<>();
dataList.put("1", Collections.singletonMap("1", 1));
dataList.put("2", Collections.singletonMap("2", 2));
object.put("integerMap", dataList);
CustomClassI customClassI = object.fromJson(CustomClassI.class);
CustomClassI compareClassI = new CustomClassI(dataList);
assertEquals(customClassI.integerMap.toString(), compareClassI.integerMap.toString());
}
}

View File

@@ -0,0 +1,23 @@
package org.json.junit.data;
public class CustomClass {
public int number;
public String name;
public Long longNumber;
public CustomClass() {}
public CustomClass (int number, String name, Long longNumber) {
this.number = number;
this.name = name;
this.longNumber = longNumber;
}
@Override
public boolean equals(Object o) {
CustomClass customClass = (CustomClass) o;
return (this.number == customClass.number
&& this.name.equals(customClass.name)
&& this.longNumber.equals(customClass.longNumber));
}
}

View File

@@ -0,0 +1,19 @@
package org.json.junit.data;
import java.math.BigInteger;
public class CustomClassA {
public BigInteger largeInt;
public CustomClassA() {}
public CustomClassA(BigInteger largeInt) {
this.largeInt = largeInt;
}
@Override
public boolean equals(Object o) {
CustomClassA classA = (CustomClassA) o;
return this.largeInt.equals(classA.largeInt);
}
}

View File

@@ -0,0 +1,20 @@
package org.json.junit.data;
public class CustomClassB {
public int number;
public CustomClassC classC;
public CustomClassB() {}
public CustomClassB(int number, CustomClassC classC) {
this.number = number;
this.classC = classC;
}
@Override
public boolean equals(Object o) {
CustomClassB classB = (CustomClassB) o;
return this.number == classB.number
&& this.classC.equals(classB.classC);
}
}

View File

@@ -0,0 +1,34 @@
package org.json.junit.data;
import org.json.JSONObject;
public class CustomClassC {
public String stringName;
public Long longNumber;
public CustomClassC() {}
public CustomClassC(String stringName, Long longNumber) {
this.stringName = stringName;
this.longNumber = longNumber;
}
public JSONObject toJSON() {
JSONObject object = new JSONObject();
object.put("stringName", this.stringName);
object.put("longNumber", this.longNumber);
return object;
}
@Override
public boolean equals(Object o) {
CustomClassC classC = (CustomClassC) o;
return this.stringName.equals(classC.stringName)
&& this.longNumber.equals(classC.longNumber);
}
@Override
public int hashCode() {
return java.util.Objects.hash(stringName, longNumber);
}
}

View File

@@ -0,0 +1,19 @@
package org.json.junit.data;
import java.util.List;
public class CustomClassD {
public List<String> stringList;
public CustomClassD() {}
public CustomClassD(List<String> stringList) {
this.stringList = stringList;
}
@Override
public boolean equals(Object o) {
CustomClassD classD = (CustomClassD) o;
return this.stringList.equals(classD.stringList);
}
}

View File

@@ -0,0 +1,18 @@
package org.json.junit.data;
import java.util.List;
public class CustomClassE {
public List<CustomClassC> listClassC;
public CustomClassE() {}
public CustomClassE(List<CustomClassC> listClassC) {
this.listClassC = listClassC;
}
@Override
public boolean equals(Object o) {
CustomClassE classE = (CustomClassE) o;
return this.listClassC.equals(classE.listClassC);
}
}

View File

@@ -0,0 +1,19 @@
package org.json.junit.data;
import java.util.List;
public class CustomClassF {
public List<List<String>> listOfString;
public CustomClassF() {}
public CustomClassF(List<List<String>> listOfString) {
this.listOfString = listOfString;
}
@Override
public boolean equals(Object o) {
CustomClassF classF = (CustomClassF) o;
return this.listOfString.equals(classF.listOfString);
}
}

View File

@@ -0,0 +1,18 @@
package org.json.junit.data;
import java.util.Map;
public class CustomClassG {
public Map<String, String> dataList;
public CustomClassG () {}
public CustomClassG (Map<String, String> dataList) {
this.dataList = dataList;
}
@Override
public boolean equals(Object object) {
CustomClassG classG = (CustomClassG) object;
return this.dataList.equals(classG.dataList);
}
}

View File

@@ -0,0 +1,22 @@
package org.json.junit.data;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;
public class CustomClassH {
public Map<String, List<Integer>> integerMap;
public CustomClassH() {}
public CustomClassH(Map<String, List<Integer>> integerMap) {
this.integerMap = integerMap;
}
@Override
public boolean equals(Object object) {
CustomClassH classH = (CustomClassH) object;
return this.integerMap.size() == classH.integerMap.size()
&& this.integerMap.keySet().equals(classH.integerMap.keySet())
&& new ArrayList<>(this.integerMap.values()).equals(new ArrayList<>(classH.integerMap.values()));
}
}

View File

@@ -0,0 +1,12 @@
package org.json.junit.data;
import java.util.Map;
public class CustomClassI {
public Map<String, Map<String, Integer>> integerMap;
public CustomClassI() {}
public CustomClassI(Map<String, Map<String, Integer>> integerMap) {
this.integerMap = integerMap;
}
}