From 7465da858c921a9e8e791bdaa54df35ea89697da Mon Sep 17 00:00:00 2001 From: sk02241994 Date: Sun, 28 Sep 2025 19:38:52 +1000 Subject: [PATCH] - Updating for java 1.6 - Resolving Sonar cube issues. --- src/main/java/org/json/JSONObject.java | 48 +++++++------------ .../java/org/json/junit/JSONObjectTest.java | 1 - 2 files changed, 17 insertions(+), 32 deletions(-) diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index 52bd2fe..e1dfa47 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -258,7 +258,7 @@ public class JSONObject { * @return a map of classes to functions that convert an {@code Object} to that class */ public Map, TypeConverter> getClassMapping() { - return this.classMapping; + return classMapping; } /** @@ -3445,20 +3445,6 @@ public class JSONObject { } } - /** - * Handles non-primitive types (Enum, Map, JSONObject, JSONArray) during deserialization. - * Now dispatches to the recursive convertValue for consistency. - */ - private void handleNonDataTypes(Class pojoClass, Object value, Field field, T obj) throws JSONException { - try { - Type fieldType = field.getGenericType(); - Object convertedValue = convertValue(value, fieldType); - field.set(obj, convertedValue); - } catch (IllegalAccessException e) { - throw new JSONException("Failed to set field: " + field.getName(), e); - } - } - /** * Recursively converts a value to the target Type, handling nested generics for Collections and Maps. */ @@ -3492,20 +3478,16 @@ public class JSONObject { } } // Map handling (e.g., Map>) - else if (Map.class.isAssignableFrom(rawType)) { - if (value instanceof JSONObject) { - Type[] mapTypes = getMapTypes(targetType); - Type keyType = mapTypes[0]; - Type valueType = mapTypes[1]; - return convertToMap((JSONObject) value, keyType, valueType, rawType); - } + 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()) { - if (value instanceof JSONObject) { - // Recurse with the raw class for POJO deserialization - return ((JSONObject) value).fromJson(rawType); - } + else if (!rawType.isPrimitive() && !rawType.isEnum() && value instanceof JSONObject) { + // Recurse with the raw class for POJO deserialization + return ((JSONObject) value).fromJson(rawType); } // Fallback @@ -3520,7 +3502,7 @@ public class JSONObject { try { InstanceCreator creator = collectionMapping.getOrDefault(mapType, () -> new HashMap<>()); @SuppressWarnings("unchecked") - Map map = (Map) creator.create(); + Map createdMap = (Map) creator.create(); for (Object keyObj : jsonMap.keySet()) { String keyStr = (String) keyObj; @@ -3529,9 +3511,9 @@ public class JSONObject { Object convertedKey = convertValue(keyStr, keyType); // Convert value recursively (handles nesting) Object convertedValue = convertValue(mapValue, valueType); - map.put(convertedKey, convertedValue); + createdMap.put(convertedKey, convertedValue); } - return map; + return createdMap; } catch (Exception e) { throw new JSONException("Failed to convert JSONObject to Map: " + mapType.getName(), e); } @@ -3557,7 +3539,11 @@ public class JSONObject { @SuppressWarnings("unchecked") private Collection fromJsonArray(JSONArray jsonArray, Class collectionType, Type elementType) throws JSONException { try { - InstanceCreator creator = collectionMapping.getOrDefault(collectionType, () -> new ArrayList<>()); + InstanceCreator creator = collectionMapping.getOrDefault(collectionType, new InstanceCreator() { + public List create() { + return new ArrayList(); + } + }); Collection collection = (Collection) creator.create(); for (int i = 0; i < jsonArray.length(); i++) { diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index f853d24..7b81541 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -4114,7 +4114,6 @@ public class JSONObjectTest { object.put("number", 12); object.put("name", "Alex"); object.put("longNumber", 1500000000L); - String jsonObject = object.toString(); CustomClass customClass = object.fromJson(CustomClass.class); CustomClass compareClass = new CustomClass(12, "Alex", 1500000000L); assertEquals(customClass, compareClass);