Skip to content

Commit 7465da8

Browse files
committed
- Updating for java 1.6
- Resolving Sonar cube issues.
1 parent 0521928 commit 7465da8

File tree

2 files changed

+17
-32
lines changed

2 files changed

+17
-32
lines changed

src/main/java/org/json/JSONObject.java

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ public Map create() {
258258
* @return a map of classes to functions that convert an {@code Object} to that class
259259
*/
260260
public Map<Class<?>, TypeConverter<?>> getClassMapping() {
261-
return this.classMapping;
261+
return classMapping;
262262
}
263263

264264
/**
@@ -3445,20 +3445,6 @@ public <T> T fromJson(Class<T> clazz) {
34453445
}
34463446
}
34473447

3448-
/**
3449-
* Handles non-primitive types (Enum, Map, JSONObject, JSONArray) during deserialization.
3450-
* Now dispatches to the recursive convertValue for consistency.
3451-
*/
3452-
private <T> void handleNonDataTypes(Class<?> pojoClass, Object value, Field field, T obj) throws JSONException {
3453-
try {
3454-
Type fieldType = field.getGenericType();
3455-
Object convertedValue = convertValue(value, fieldType);
3456-
field.set(obj, convertedValue);
3457-
} catch (IllegalAccessException e) {
3458-
throw new JSONException("Failed to set field: " + field.getName(), e);
3459-
}
3460-
}
3461-
34623448
/**
34633449
* Recursively converts a value to the target Type, handling nested generics for Collections and Maps.
34643450
*/
@@ -3492,20 +3478,16 @@ private Object convertValue(Object value, Type targetType) throws JSONException
34923478
}
34933479
}
34943480
// Map handling (e.g., Map<Integer, List<String>>)
3495-
else if (Map.class.isAssignableFrom(rawType)) {
3496-
if (value instanceof JSONObject) {
3497-
Type[] mapTypes = getMapTypes(targetType);
3498-
Type keyType = mapTypes[0];
3499-
Type valueType = mapTypes[1];
3500-
return convertToMap((JSONObject) value, keyType, valueType, rawType);
3501-
}
3481+
else if (Map.class.isAssignableFrom(rawType) && value instanceof JSONObject) {
3482+
Type[] mapTypes = getMapTypes(targetType);
3483+
Type keyType = mapTypes[0];
3484+
Type valueType = mapTypes[1];
3485+
return convertToMap((JSONObject) value, keyType, valueType, rawType);
35023486
}
35033487
// POJO handling (including custom classes like Tuple<Integer, String, Integer>)
3504-
else if (!rawType.isPrimitive() && !rawType.isEnum()) {
3505-
if (value instanceof JSONObject) {
3506-
// Recurse with the raw class for POJO deserialization
3507-
return ((JSONObject) value).fromJson(rawType);
3508-
}
3488+
else if (!rawType.isPrimitive() && !rawType.isEnum() && value instanceof JSONObject) {
3489+
// Recurse with the raw class for POJO deserialization
3490+
return ((JSONObject) value).fromJson(rawType);
35093491
}
35103492

35113493
// Fallback
@@ -3520,7 +3502,7 @@ else if (!rawType.isPrimitive() && !rawType.isEnum()) {
35203502
try {
35213503
InstanceCreator<?> creator = collectionMapping.getOrDefault(mapType, () -> new HashMap<>());
35223504
@SuppressWarnings("unchecked")
3523-
Map<Object, Object> map = (Map<Object, Object>) creator.create();
3505+
Map<Object, Object> createdMap = (Map<Object, Object>) creator.create();
35243506

35253507
for (Object keyObj : jsonMap.keySet()) {
35263508
String keyStr = (String) keyObj;
@@ -3529,9 +3511,9 @@ else if (!rawType.isPrimitive() && !rawType.isEnum()) {
35293511
Object convertedKey = convertValue(keyStr, keyType);
35303512
// Convert value recursively (handles nesting)
35313513
Object convertedValue = convertValue(mapValue, valueType);
3532-
map.put(convertedKey, convertedValue);
3514+
createdMap.put(convertedKey, convertedValue);
35333515
}
3534-
return map;
3516+
return createdMap;
35353517
} catch (Exception e) {
35363518
throw new JSONException("Failed to convert JSONObject to Map: " + mapType.getName(), e);
35373519
}
@@ -3557,7 +3539,11 @@ private <E extends Enum<E>> E stringToEnum(Class<?> enumClass, String value) thr
35573539
@SuppressWarnings("unchecked")
35583540
private <T> Collection<T> fromJsonArray(JSONArray jsonArray, Class<?> collectionType, Type elementType) throws JSONException {
35593541
try {
3560-
InstanceCreator<?> creator = collectionMapping.getOrDefault(collectionType, () -> new ArrayList<>());
3542+
InstanceCreator<?> creator = collectionMapping.getOrDefault(collectionType, new InstanceCreator<List>() {
3543+
public List create() {
3544+
return new ArrayList();
3545+
}
3546+
});
35613547
Collection<T> collection = (Collection<T>) creator.create();
35623548

35633549
for (int i = 0; i < jsonArray.length(); i++) {

src/test/java/org/json/junit/JSONObjectTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4114,7 +4114,6 @@ public void jsonObjectParseFromJson_0() {
41144114
object.put("number", 12);
41154115
object.put("name", "Alex");
41164116
object.put("longNumber", 1500000000L);
4117-
String jsonObject = object.toString();
41184117
CustomClass customClass = object.fromJson(CustomClass.class);
41194118
CustomClass compareClass = new CustomClass(12, "Alex", 1500000000L);
41204119
assertEquals(customClass, compareClass);

0 commit comments

Comments
 (0)