- Updating for java 1.6

- Resolving Sonar cube issues.
This commit is contained in:
sk02241994
2025-09-28 19:38:52 +10:00
parent 0521928463
commit 7465da858c
2 changed files with 17 additions and 32 deletions

View File

@@ -258,7 +258,7 @@ public class JSONObject {
* @return a map of classes to functions that convert an {@code Object} to that class * @return a map of classes to functions that convert an {@code Object} to that class
*/ */
public Map<Class<?>, TypeConverter<?>> getClassMapping() { public Map<Class<?>, 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 <T> 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. * 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<Integer, List<String>>) // Map handling (e.g., Map<Integer, List<String>>)
else if (Map.class.isAssignableFrom(rawType)) { else if (Map.class.isAssignableFrom(rawType) && value instanceof JSONObject) {
if (value instanceof JSONObject) { Type[] mapTypes = getMapTypes(targetType);
Type[] mapTypes = getMapTypes(targetType); Type keyType = mapTypes[0];
Type keyType = mapTypes[0]; Type valueType = mapTypes[1];
Type valueType = mapTypes[1]; return convertToMap((JSONObject) value, keyType, valueType, rawType);
return convertToMap((JSONObject) value, keyType, valueType, rawType);
}
} }
// POJO handling (including custom classes like Tuple<Integer, String, Integer>) // POJO handling (including custom classes like Tuple<Integer, String, Integer>)
else if (!rawType.isPrimitive() && !rawType.isEnum()) { else if (!rawType.isPrimitive() && !rawType.isEnum() && value instanceof JSONObject) {
if (value instanceof JSONObject) { // Recurse with the raw class for POJO deserialization
// Recurse with the raw class for POJO deserialization return ((JSONObject) value).fromJson(rawType);
return ((JSONObject) value).fromJson(rawType);
}
} }
// Fallback // Fallback
@@ -3520,7 +3502,7 @@ public class JSONObject {
try { try {
InstanceCreator<?> creator = collectionMapping.getOrDefault(mapType, () -> new HashMap<>()); InstanceCreator<?> creator = collectionMapping.getOrDefault(mapType, () -> new HashMap<>());
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<Object, Object> map = (Map<Object, Object>) creator.create(); Map<Object, Object> createdMap = (Map<Object, Object>) creator.create();
for (Object keyObj : jsonMap.keySet()) { for (Object keyObj : jsonMap.keySet()) {
String keyStr = (String) keyObj; String keyStr = (String) keyObj;
@@ -3529,9 +3511,9 @@ public class JSONObject {
Object convertedKey = convertValue(keyStr, keyType); Object convertedKey = convertValue(keyStr, keyType);
// Convert value recursively (handles nesting) // Convert value recursively (handles nesting)
Object convertedValue = convertValue(mapValue, valueType); Object convertedValue = convertValue(mapValue, valueType);
map.put(convertedKey, convertedValue); createdMap.put(convertedKey, convertedValue);
} }
return map; return createdMap;
} catch (Exception e) { } catch (Exception e) {
throw new JSONException("Failed to convert JSONObject to Map: " + mapType.getName(), e); throw new JSONException("Failed to convert JSONObject to Map: " + mapType.getName(), e);
} }
@@ -3557,7 +3539,11 @@ public class JSONObject {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private <T> Collection<T> fromJsonArray(JSONArray jsonArray, Class<?> collectionType, Type elementType) throws JSONException { private <T> Collection<T> fromJsonArray(JSONArray jsonArray, Class<?> collectionType, Type elementType) throws JSONException {
try { try {
InstanceCreator<?> creator = collectionMapping.getOrDefault(collectionType, () -> new ArrayList<>()); InstanceCreator<?> creator = collectionMapping.getOrDefault(collectionType, new InstanceCreator<List>() {
public List create() {
return new ArrayList();
}
});
Collection<T> collection = (Collection<T>) creator.create(); Collection<T> collection = (Collection<T>) creator.create();
for (int i = 0; i < jsonArray.length(); i++) { for (int i = 0; i < jsonArray.length(); i++) {

View File

@@ -4114,7 +4114,6 @@ public class JSONObjectTest {
object.put("number", 12); object.put("number", 12);
object.put("name", "Alex"); object.put("name", "Alex");
object.put("longNumber", 1500000000L); object.put("longNumber", 1500000000L);
String jsonObject = object.toString();
CustomClass customClass = object.fromJson(CustomClass.class); CustomClass customClass = object.fromJson(CustomClass.class);
CustomClass compareClass = new CustomClass(12, "Alex", 1500000000L); CustomClass compareClass = new CustomClass(12, "Alex", 1500000000L);
assertEquals(customClass, compareClass); assertEquals(customClass, compareClass);