From 8ccf5d7525487226d7a2362f67a36ca606aa6614 Mon Sep 17 00:00:00 2001 From: sk02241994 Date: Thu, 23 Oct 2025 17:32:07 +1100 Subject: [PATCH] Removing the interface classes and simplifying the implementation to use if else instead --- src/main/java/org/json/InstanceCreator.java | 16 -- src/main/java/org/json/JSONObject.java | 177 ++++++-------------- src/main/java/org/json/TypeConverter.java | 18 -- 3 files changed, 49 insertions(+), 162 deletions(-) delete mode 100644 src/main/java/org/json/InstanceCreator.java delete mode 100644 src/main/java/org/json/TypeConverter.java diff --git a/src/main/java/org/json/InstanceCreator.java b/src/main/java/org/json/InstanceCreator.java deleted file mode 100644 index c8ae05c..0000000 --- a/src/main/java/org/json/InstanceCreator.java +++ /dev/null @@ -1,16 +0,0 @@ -package org.json; - -/** - * Interface defining a creator that produces new instances of type {@code T}. - * - * @param the type of instances created - */ -interface InstanceCreator { - - /** - * Creates a new instance of type {@code T}. - * - * @return a new instance of {@code T} - */ - T create(); -} diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index e9e6ff5..934a454 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -157,111 +157,6 @@ public class JSONObject { this.map = new HashMap(); } - /** - * A mapping from Java classes to functions that convert a generic {@code Object} - * into an instance of the target class. - * - *

Examples of default mappings: - *

    - *
  • {@code int.class} or {@code Integer.class} -> Converts a {@code Number} to {@code int}
  • - *
  • {@code boolean.class} or {@code Boolean.class} -> Identity function
  • - *
  • {@code String.class} -> Identity function
  • - *
- */ - private static final Map, TypeConverter> classMapping = new HashMap, TypeConverter>(); - - /** - * A mapping from collection interface types to suppliers that produce - * instances of concrete collection implementations. - * - */ - private static final Map, InstanceCreator> collectionMapping = new HashMap, InstanceCreator>(); - - // Static initializer block to populate default mappings - static { - classMapping.put(int.class, new TypeConverter() { - public Integer convert(Object input) { - return ((Number) input).intValue(); - } - }); - classMapping.put(Integer.class, new TypeConverter() { - public Integer convert(Object input) { - return ((Number) input).intValue(); - } - }); - classMapping.put(double.class, new TypeConverter() { - public Double convert(Object input) { - return ((Number) input).doubleValue(); - } - }); - classMapping.put(Double.class, new TypeConverter() { - public Double convert(Object input) { - return ((Number) input).doubleValue(); - } - }); - classMapping.put(float.class, new TypeConverter() { - public Float convert(Object input) { - return ((Number) input).floatValue(); - } - }); - classMapping.put(Float.class, new TypeConverter() { - public Float convert(Object input) { - return ((Number) input).floatValue(); - } - }); - classMapping.put(long.class, new TypeConverter() { - public Long convert(Object input) { - return ((Number) input).longValue(); - } - }); - classMapping.put(Long.class, new TypeConverter() { - public Long convert(Object input) { - return ((Number) input).longValue(); - } - }); - classMapping.put(boolean.class, new TypeConverter() { - public Boolean convert(Object input) { - return (Boolean) input; - } - }); - classMapping.put(Boolean.class, new TypeConverter() { - public Boolean convert(Object input) { - return (Boolean) input; - } - }); - classMapping.put(String.class, new TypeConverter() { - public String convert(Object input) { - return (String) input; - } - }); - classMapping.put(BigDecimal.class, new TypeConverter() { - public BigDecimal convert(Object input) { - return new BigDecimal((String) input); - } - }); - classMapping.put(BigInteger.class, new TypeConverter() { - public BigInteger convert(Object input) { - return new BigInteger((String) input); - } - }); - - collectionMapping.put(List.class, new InstanceCreator() { - public List create() { - return new ArrayList(); - } - }); - collectionMapping.put(Set.class, new InstanceCreator() { - public Set create() { - return new HashSet(); - } - }); - collectionMapping.put(Map.class, new InstanceCreator() { - public Map create() { - return new HashMap(); - } - }); - } - /** * Construct a JSONObject from a subset of another JSONObject. An array of * strings is used to identify the keys that should be copied. Missing keys @@ -3359,8 +3254,8 @@ public class JSONObject { * *

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 + * 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 @@ -3402,12 +3297,8 @@ public class JSONObject { Object value = get(fieldName); Type fieldType = field.getGenericType(); Class rawType = getRawType(fieldType); - if (classMapping.containsKey(rawType)) { - field.set(obj, classMapping.get(rawType).convert(value)); - } else { - Object convertedValue = convertValue(value, fieldType); - field.set(obj, convertedValue); - } + Object convertedValue = convertValue(value, fieldType); + field.set(obj, convertedValue); } } return obj; @@ -3433,9 +3324,22 @@ public class JSONObject { return value; } - // Use registered type converter - if (classMapping.containsKey(rawType)) { - return classMapping.get(rawType).convert(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 (Boolean) value; + } else if (rawType == String.class) { + return (String) value; + } else if (rawType == BigDecimal.class) { + return new BigDecimal((String) value); + } else if (rawType == BigInteger.class) { + return new BigInteger((String) value); } // Enum conversion @@ -3473,13 +3377,8 @@ public class JSONObject { */ private Map convertToMap(JSONObject jsonMap, Type keyType, Type valueType, Class mapType) throws JSONException { try { - InstanceCreator creator = collectionMapping.get(mapType) != null ? collectionMapping.get(mapType) : new InstanceCreator() { - public Map create() { - return new HashMap(); - } - }; @SuppressWarnings("unchecked") - Map createdMap = (Map) creator.create(); + Map createdMap = new HashMap(); for (Object keyObj : jsonMap.keySet()) { String keyStr = (String) keyObj; @@ -3517,12 +3416,7 @@ public class JSONObject { @SuppressWarnings("unchecked") private Collection fromJsonArray(JSONArray jsonArray, Class collectionType, Type elementType) throws JSONException { try { - InstanceCreator creator = collectionMapping.get(collectionType) != null ? collectionMapping.get(collectionType) : new InstanceCreator() { - public List create() { - return new ArrayList(); - } - }; - Collection collection = (Collection) creator.create(); + Collection collection = getCollection(collectionType); for (int i = 0; i < jsonArray.length(); i++) { Object jsonElement = jsonArray.get(i); @@ -3536,4 +3430,31 @@ public class JSONObject { } } + /** + * 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: + *

    + *
  • {@code List.class}
  • + *
  • {@code ArrayList.class}
  • + *
  • {@code Set.class}
  • + *
  • {@code HashSet.class}
  • + *
+ * 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/main/java/org/json/TypeConverter.java b/src/main/java/org/json/TypeConverter.java deleted file mode 100644 index d5b4eaf..0000000 --- a/src/main/java/org/json/TypeConverter.java +++ /dev/null @@ -1,18 +0,0 @@ -package org.json; - -/** - * Interface defining a converter that converts an input {@code Object} - * into an instance of a specific type {@code T}. - * - * @param the target type to convert to - */ -interface TypeConverter { - - /** - * Converts the given input object to an instance of type {@code T}. - * - * @param input the object to convert - * @return the converted instance of type {@code T} - */ - T convert(Object input); -}