diff --git a/src/main/java/org/json/InstanceCreator.java b/src/main/java/org/json/InstanceCreator.java new file mode 100644 index 0000000..4836e23 --- /dev/null +++ b/src/main/java/org/json/InstanceCreator.java @@ -0,0 +1,16 @@ +package org.json; + +/** + * Interface defining a creator that produces new instances of type {@code T}. + * + * @param the type of instances created + */ +public 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/JSONBuilder.java b/src/main/java/org/json/JSONBuilder.java index 2ee99ca..67c9b94 100644 --- a/src/main/java/org/json/JSONBuilder.java +++ b/src/main/java/org/json/JSONBuilder.java @@ -7,8 +7,6 @@ import java.util.ArrayList; import java.util.Set; import java.util.HashSet; import java.util.Collection; -import java.util.function.Function; -import java.util.function.Supplier; /** * The {@code JSONBuilder} class provides a configurable mechanism for @@ -42,38 +40,88 @@ public class JSONBuilder { *
  • {@code String.class} -> Identity function
  • * */ - private static final Map, Function> classMapping = new HashMap<>(); + private static final Map, TypeConverter> classMapping = new HashMap<>(); /** * A mapping from collection interface types to suppliers that produce * instances of concrete collection implementations. * - *

    Examples of default mappings: - *

      - *
    • {@code List.class} -> {@code ArrayList::new}
    • - *
    • {@code Set.class} -> {@code HashSet::new}
    • - *
    • {@code Map.class} -> {@code HashMap::new}
    • - *
    */ - private static final Map, Supplier> collectionMapping = new HashMap<>(); + private static final Map, InstanceCreator> collectionMapping = new HashMap<>(); // Static initializer block to populate default mappings static { - classMapping.put(int.class, s -> ((Number) s).intValue()); - classMapping.put(Integer.class, s -> ((Number) s).intValue()); - classMapping.put(double.class, s -> ((Number) s).doubleValue()); - classMapping.put(Double.class, s -> ((Number) s).doubleValue()); - classMapping.put(float.class, s -> ((Number) s).floatValue()); - classMapping.put(Float.class, s -> ((Number) s).floatValue()); - classMapping.put(long.class, s -> ((Number) s).longValue()); - classMapping.put(Long.class, s -> ((Number) s).longValue()); - classMapping.put(boolean.class, s -> s); - classMapping.put(Boolean.class, s -> s); - classMapping.put(String.class, s -> s); + 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; + } + }); - collectionMapping.put(List.class, ArrayList::new); - collectionMapping.put(Set.class, HashSet::new); - collectionMapping.put(Map.class, HashMap::new); + 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(); + } + }); } /** @@ -81,7 +129,7 @@ public class JSONBuilder { * * @return a map of classes to functions that convert an {@code Object} to that class */ - public Map, Function> getClassMapping() { + public Map, TypeConverter> getClassMapping() { return this.classMapping; } @@ -90,7 +138,7 @@ public class JSONBuilder { * * @return a map of collection interface types to suppliers of concrete implementations */ - public Map, Supplier> getCollectionMapping() { + public Map, InstanceCreator> getCollectionMapping() { return this.collectionMapping; } @@ -103,7 +151,7 @@ public class JSONBuilder { * @param clazz the target class for which the conversion function is to be set * @param function a function that takes an {@code Object} and returns an instance of {@code clazz} */ - public void setClassMapping(Class clazz, Function function) { + public void setClassMapping(Class clazz, TypeConverter function) { classMapping.put(clazz, function); } @@ -116,7 +164,7 @@ public class JSONBuilder { * @param clazz the collection interface class (e.g., {@code List.class}) * @param function a supplier that creates a new instance of a concrete implementation */ - public void setCollectionMapping(Class clazz, Supplier function) { + public void setCollectionMapping(Class clazz, InstanceCreator function) { collectionMapping.put(clazz, function); } } diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index 496a15a..f5d2bd6 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -3256,7 +3256,7 @@ public class JSONObject { if (this.builder == null) { this.builder = new JSONBuilder(); } - Map, Function> classMapping = this.builder.getClassMapping(); + Map, TypeConverter> classMapping = this.builder.getClassMapping(); for (Field field: clazz.getDeclaredFields()) { field.setAccessible(true); @@ -3265,7 +3265,7 @@ public class JSONObject { Object value = this.get(fieldName); Class pojoClass = field.getType(); if (classMapping.containsKey(pojoClass)) { - field.set(obj, classMapping.get(pojoClass).apply(value)); + field.set(obj, classMapping.get(pojoClass).convert(value)); } else { if (value.getClass() == JSONObject.class) { field.set(obj, fromJson((JSONObject) value, pojoClass)); @@ -3290,10 +3290,10 @@ public class JSONObject { private Collection fromJsonArray(JSONArray jsonArray, Class collectionType, Type elementType) throws JSONException { try { - Map, Function> classMapping = this.builder.getClassMapping(); - Map, Supplier> collectionMapping = this.builder.getCollectionMapping(); + Map, TypeConverter> classMapping = this.builder.getClassMapping(); + Map, InstanceCreator> collectionMapping = this.builder.getCollectionMapping(); Collection collection = (Collection) (collectionMapping.containsKey(collectionType) ? - collectionMapping.get(collectionType).get() + collectionMapping.get(collectionType).create() : collectionType.getDeclaredConstructor().newInstance()); @@ -3312,7 +3312,7 @@ public class JSONObject { for (int i = 0; i < jsonArray.length(); i++) { Object jsonElement = jsonArray.get(i); if (classMapping.containsKey(innerElementClass)) { - collection.add((T) classMapping.get(innerElementClass).apply(jsonElement)); + collection.add((T) classMapping.get(innerElementClass).convert(jsonElement)); } else if (jsonElement.getClass() == JSONObject.class) { collection.add((T) ((JSONObject) jsonElement).fromJson(innerElementClass)); } else if (jsonElement.getClass() == JSONArray.class) { diff --git a/src/main/java/org/json/TypeConverter.java b/src/main/java/org/json/TypeConverter.java new file mode 100644 index 0000000..dc07325 --- /dev/null +++ b/src/main/java/org/json/TypeConverter.java @@ -0,0 +1,18 @@ +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 + */ +public 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); +} diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index e3fb1d8..5a7aedb 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -37,6 +37,7 @@ import org.json.JSONBuilder; import org.json.JSONTokener; import org.json.ParserConfiguration; import org.json.XML; +import org.json.TypeConverter; import org.json.junit.data.BrokenToString; import org.json.junit.data.ExceptionalBean; import org.json.junit.data.Fraction; @@ -4133,7 +4134,11 @@ public class JSONObjectTest { @Test public void jsonObjectParseFromJson_1() { JSONBuilder builder = new JSONBuilder(); - builder.setClassMapping(java.time.LocalDateTime.class, s -> java.time.LocalDateTime.parse((String)s)); + builder.setClassMapping(java.time.LocalDateTime.class, new TypeConverter() { + public java.time.LocalDateTime convert(Object input) { + return java.time.LocalDateTime.parse((String) input); + } + }); JSONObject object = new JSONObject(builder); java.time.LocalDateTime localDateTime = java.time.LocalDateTime.now(); object.put("localDate", localDateTime.toString());