diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index e1dfa47..fa16c3a 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -3440,7 +3440,7 @@ public class JSONObject { return obj; } catch (NoSuchMethodException e) { throw new JSONException("No no-arg constructor for class: " + clazz.getName(), e); - } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) { + } catch (Exception e) { throw new JSONException("Failed to instantiate or set field for class: " + clazz.getName(), e); } } @@ -3500,7 +3500,11 @@ public class JSONObject { */ private Map convertToMap(JSONObject jsonMap, Type keyType, Type valueType, Class mapType) throws JSONException { try { - InstanceCreator creator = collectionMapping.getOrDefault(mapType, () -> new HashMap<>()); + InstanceCreator creator = collectionMapping.get(mapType) != null ? collectionMapping.get(mapType) : new InstanceCreator() { + public Map create() { + return new HashMap(); + } + }; @SuppressWarnings("unchecked") Map createdMap = (Map) creator.create(); @@ -3522,12 +3526,13 @@ public class JSONObject { /** * Converts a String to an Enum value. */ - private > E stringToEnum(Class enumClass, String value) throws JSONException { + private E stringToEnum(Class enumClass, String value) throws JSONException { try { @SuppressWarnings("unchecked") - Method valueOfMethod = enumClass.getMethod("valueOf", String.class); + Class enumType = (Class) enumClass; + Method valueOfMethod = enumType.getMethod("valueOf", String.class); return (E) valueOfMethod.invoke(null, value); - } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { + } catch (Exception e) { throw new JSONException("Failed to convert string to enum: " + value + " for " + enumClass.getName(), e); } } @@ -3539,11 +3544,11 @@ public class JSONObject { @SuppressWarnings("unchecked") private Collection fromJsonArray(JSONArray jsonArray, Class collectionType, Type elementType) throws JSONException { try { - InstanceCreator creator = collectionMapping.getOrDefault(collectionType, new InstanceCreator() { + InstanceCreator creator = collectionMapping.get(collectionType) != null ? collectionMapping.get(collectionType) : new InstanceCreator() { public List create() { return new ArrayList(); } - }); + }; Collection collection = (Collection) creator.create(); for (int i = 0; i < jsonArray.length(); i++) {