diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index 257eb1074..4e8b42c97 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -17,6 +17,9 @@ 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 @@ private static JSONException recursivelyDefinedObjectException(String key) { "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. + * + *

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 fromJson(String jsonString, Class clazz) { + JSONObject jsonObject = new JSONObject(jsonString); + return jsonObject.fromJson(clazz); + } + + /** + * Deserializes a JSON string into an instance of the specified class. + * + *

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. + * + *

Note: 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 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 fromJson(Class 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>>) + if (Collection.class.isAssignableFrom(rawType)) { + if (value instanceof JSONArray) { + Type elementType = getElementType(targetType); + return fromJsonArray((JSONArray) value, rawType, elementType); + } + } + // Map handling (e.g., Map>) + 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) + 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 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) + 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 stringToEnum(Class enumClass, String value) throws JSONException { + try { + @SuppressWarnings("unchecked") + Class enumType = (Class) 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 Collection fromJsonArray(JSONArray jsonArray, Class collectionType, Type elementType) throws JSONException { + try { + Collection 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. + *

+ * This method currently supports the following collection types: + *

+ * 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()); + } + } } diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 88c19c7dc..7ca6093b7 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -56,6 +56,17 @@ 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 void jsonObjectParseNullFieldsWithoutParserConfiguration() { 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> 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 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> 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> 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()); + } } diff --git a/src/test/java/org/json/junit/data/CustomClass.java b/src/test/java/org/json/junit/data/CustomClass.java new file mode 100644 index 000000000..9ae405597 --- /dev/null +++ b/src/test/java/org/json/junit/data/CustomClass.java @@ -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)); + } +} + diff --git a/src/test/java/org/json/junit/data/CustomClassA.java b/src/test/java/org/json/junit/data/CustomClassA.java new file mode 100644 index 000000000..08a99d333 --- /dev/null +++ b/src/test/java/org/json/junit/data/CustomClassA.java @@ -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); + } +} + diff --git a/src/test/java/org/json/junit/data/CustomClassB.java b/src/test/java/org/json/junit/data/CustomClassB.java new file mode 100644 index 000000000..688997ec4 --- /dev/null +++ b/src/test/java/org/json/junit/data/CustomClassB.java @@ -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); + } +} + diff --git a/src/test/java/org/json/junit/data/CustomClassC.java b/src/test/java/org/json/junit/data/CustomClassC.java new file mode 100644 index 000000000..9d20aa392 --- /dev/null +++ b/src/test/java/org/json/junit/data/CustomClassC.java @@ -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); + } +} + diff --git a/src/test/java/org/json/junit/data/CustomClassD.java b/src/test/java/org/json/junit/data/CustomClassD.java new file mode 100644 index 000000000..4a858058c --- /dev/null +++ b/src/test/java/org/json/junit/data/CustomClassD.java @@ -0,0 +1,19 @@ +package org.json.junit.data; + +import java.util.List; + +public class CustomClassD { + public List stringList; + + public CustomClassD() {} + public CustomClassD(List stringList) { + this.stringList = stringList; + } + + @Override + public boolean equals(Object o) { + CustomClassD classD = (CustomClassD) o; + return this.stringList.equals(classD.stringList); + } +} + diff --git a/src/test/java/org/json/junit/data/CustomClassE.java b/src/test/java/org/json/junit/data/CustomClassE.java new file mode 100644 index 000000000..807dc5540 --- /dev/null +++ b/src/test/java/org/json/junit/data/CustomClassE.java @@ -0,0 +1,18 @@ +package org.json.junit.data; + +import java.util.List; + +public class CustomClassE { + public List listClassC; + + public CustomClassE() {} + public CustomClassE(List listClassC) { + this.listClassC = listClassC; + } + + @Override + public boolean equals(Object o) { + CustomClassE classE = (CustomClassE) o; + return this.listClassC.equals(classE.listClassC); + } +} diff --git a/src/test/java/org/json/junit/data/CustomClassF.java b/src/test/java/org/json/junit/data/CustomClassF.java new file mode 100644 index 000000000..d85861036 --- /dev/null +++ b/src/test/java/org/json/junit/data/CustomClassF.java @@ -0,0 +1,19 @@ +package org.json.junit.data; + +import java.util.List; + +public class CustomClassF { + public List> listOfString; + + public CustomClassF() {} + public CustomClassF(List> listOfString) { + this.listOfString = listOfString; + } + + @Override + public boolean equals(Object o) { + CustomClassF classF = (CustomClassF) o; + return this.listOfString.equals(classF.listOfString); + } +} + diff --git a/src/test/java/org/json/junit/data/CustomClassG.java b/src/test/java/org/json/junit/data/CustomClassG.java new file mode 100644 index 000000000..c8c9f5784 --- /dev/null +++ b/src/test/java/org/json/junit/data/CustomClassG.java @@ -0,0 +1,18 @@ +package org.json.junit.data; + +import java.util.Map; + +public class CustomClassG { + public Map dataList; + + public CustomClassG () {} + public CustomClassG (Map dataList) { + this.dataList = dataList; + } + + @Override + public boolean equals(Object object) { + CustomClassG classG = (CustomClassG) object; + return this.dataList.equals(classG.dataList); + } +} diff --git a/src/test/java/org/json/junit/data/CustomClassH.java b/src/test/java/org/json/junit/data/CustomClassH.java new file mode 100644 index 000000000..ce9b1af23 --- /dev/null +++ b/src/test/java/org/json/junit/data/CustomClassH.java @@ -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> integerMap; + + public CustomClassH() {} + public CustomClassH(Map> 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())); + } +} diff --git a/src/test/java/org/json/junit/data/CustomClassI.java b/src/test/java/org/json/junit/data/CustomClassI.java new file mode 100644 index 000000000..bd7c4ed89 --- /dev/null +++ b/src/test/java/org/json/junit/data/CustomClassI.java @@ -0,0 +1,12 @@ +package org.json.junit.data; + +import java.util.Map; + +public class CustomClassI { + public Map> integerMap; + + public CustomClassI() {} + public CustomClassI(Map> integerMap) { + this.integerMap = integerMap; + } +}