diff --git a/src/main/java/org/json/InstanceCreator.java b/src/main/java/org/json/InstanceCreator.java index 4836e23..c8ae05c 100644 --- a/src/main/java/org/json/InstanceCreator.java +++ b/src/main/java/org/json/InstanceCreator.java @@ -5,7 +5,7 @@ package org.json; * * @param the type of instances created */ -public interface InstanceCreator { +interface InstanceCreator { /** * Creates a new instance of type {@code T}. diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index fa16c3a..e0c0337 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -234,6 +234,16 @@ public class JSONObject { 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() { @@ -252,50 +262,6 @@ public class JSONObject { }); } - /** - * Returns the current class-to-function mapping used for type conversions. - * - * @return a map of classes to functions that convert an {@code Object} to that class - */ - public Map, TypeConverter> getClassMapping() { - return classMapping; - } - - /** - * Returns the current collection-to-supplier mapping used for instantiating collections. - * - * @return a map of collection interface types to suppliers of concrete implementations - */ - public Map, InstanceCreator> getCollectionMapping() { - return collectionMapping; - } - - /** - * Adds or updates a type conversion function for a given class. - * - *

This allows users to customize how objects are converted into specific types - * during processing (e.g., JSON deserialization). - * - * @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, TypeConverter function) { - classMapping.put(clazz, function); - } - - /** - * Adds or updates a supplier function for instantiating a collection type. - * - *

This allows customization of which concrete implementation is used for - * interface types like {@code List}, {@code Set}, or {@code Map}. - * - * @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, InstanceCreator function) { - collectionMapping.put(clazz, function); - } - /** * 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 diff --git a/src/main/java/org/json/TypeConverter.java b/src/main/java/org/json/TypeConverter.java index dc07325..d5b4eaf 100644 --- a/src/main/java/org/json/TypeConverter.java +++ b/src/main/java/org/json/TypeConverter.java @@ -6,7 +6,7 @@ package org.json; * * @param the target type to convert to */ -public interface TypeConverter { +interface TypeConverter { /** * Converts the given input object to an instance of type {@code T}. diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 7b81541..7ca6093 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -36,7 +36,6 @@ import org.json.JSONString; 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; @@ -4121,17 +4120,13 @@ public class JSONObjectTest { @Test public void jsonObjectParseFromJson_1() { - JSONObject object = new JSONObject(); - object.setClassMapping(java.time.LocalDateTime.class, new TypeConverter() { - public java.time.LocalDateTime convert(Object input) { - return java.time.LocalDateTime.parse((String) input); - } - }); - java.time.LocalDateTime localDateTime = java.time.LocalDateTime.now(); - object.put("localDate", localDateTime.toString()); - CustomClassA customClassA = object.fromJson(CustomClassA.class); - CustomClassA compareClassClassA = new CustomClassA(localDateTime); - assertEquals(customClassA, compareClassClassA); + JSONObject object = new JSONObject(); + + BigInteger largeInt = new BigInteger("123"); + object.put("largeInt", largeInt.toString()); + CustomClassA customClassA = object.fromJson(CustomClassA.class); + CustomClassA compareClassClassA = new CustomClassA(largeInt); + assertEquals(customClassA, compareClassClassA); } @Test diff --git a/src/test/java/org/json/junit/data/CustomClassA.java b/src/test/java/org/json/junit/data/CustomClassA.java index 275e9a5..08a99d3 100644 --- a/src/test/java/org/json/junit/data/CustomClassA.java +++ b/src/test/java/org/json/junit/data/CustomClassA.java @@ -1,17 +1,19 @@ package org.json.junit.data; +import java.math.BigInteger; + public class CustomClassA { - public java.time.LocalDateTime localDate; + public BigInteger largeInt; public CustomClassA() {} - public CustomClassA(java.time.LocalDateTime localDate) { - this.localDate = localDate; + public CustomClassA(BigInteger largeInt) { + this.largeInt = largeInt; } @Override public boolean equals(Object o) { CustomClassA classA = (CustomClassA) o; - return this.localDate.equals(classA.localDate); + return this.largeInt.equals(classA.largeInt); } }