Limiting implemetation by removing the new classes.

This commit is contained in:
sk02241994
2025-10-16 14:19:19 +11:00
parent 9adea9e12d
commit c4c2beb874
5 changed files with 25 additions and 62 deletions

View File

@@ -5,7 +5,7 @@ package org.json;
* *
* @param <T> the type of instances created * @param <T> the type of instances created
*/ */
public interface InstanceCreator<T> { interface InstanceCreator<T> {
/** /**
* Creates a new instance of type {@code T}. * Creates a new instance of type {@code T}.

View File

@@ -234,6 +234,16 @@ public class JSONObject {
return (String) input; return (String) input;
} }
}); });
classMapping.put(BigDecimal.class, new TypeConverter<BigDecimal>() {
public BigDecimal convert(Object input) {
return new BigDecimal((String) input);
}
});
classMapping.put(BigInteger.class, new TypeConverter<BigInteger>() {
public BigInteger convert(Object input) {
return new BigInteger((String) input);
}
});
collectionMapping.put(List.class, new InstanceCreator<List>() { collectionMapping.put(List.class, new InstanceCreator<List>() {
public List create() { 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<Class<?>, 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<Class<?>, InstanceCreator<?>> getCollectionMapping() {
return collectionMapping;
}
/**
* Adds or updates a type conversion function for a given class.
*
* <p>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.
*
* <p>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 * 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 * strings is used to identify the keys that should be copied. Missing keys

View File

@@ -6,7 +6,7 @@ package org.json;
* *
* @param <T> the target type to convert to * @param <T> the target type to convert to
*/ */
public interface TypeConverter<T> { interface TypeConverter<T> {
/** /**
* Converts the given input object to an instance of type {@code T}. * Converts the given input object to an instance of type {@code T}.

View File

@@ -36,7 +36,6 @@ import org.json.JSONString;
import org.json.JSONTokener; import org.json.JSONTokener;
import org.json.ParserConfiguration; import org.json.ParserConfiguration;
import org.json.XML; import org.json.XML;
import org.json.TypeConverter;
import org.json.junit.data.BrokenToString; import org.json.junit.data.BrokenToString;
import org.json.junit.data.ExceptionalBean; import org.json.junit.data.ExceptionalBean;
import org.json.junit.data.Fraction; import org.json.junit.data.Fraction;
@@ -4122,15 +4121,11 @@ public class JSONObjectTest {
@Test @Test
public void jsonObjectParseFromJson_1() { public void jsonObjectParseFromJson_1() {
JSONObject object = new JSONObject(); JSONObject object = new JSONObject();
object.setClassMapping(java.time.LocalDateTime.class, new TypeConverter<java.time.LocalDateTime>() {
public java.time.LocalDateTime convert(Object input) { BigInteger largeInt = new BigInteger("123");
return java.time.LocalDateTime.parse((String) input); object.put("largeInt", largeInt.toString());
}
});
java.time.LocalDateTime localDateTime = java.time.LocalDateTime.now();
object.put("localDate", localDateTime.toString());
CustomClassA customClassA = object.fromJson(CustomClassA.class); CustomClassA customClassA = object.fromJson(CustomClassA.class);
CustomClassA compareClassClassA = new CustomClassA(localDateTime); CustomClassA compareClassClassA = new CustomClassA(largeInt);
assertEquals(customClassA, compareClassClassA); assertEquals(customClassA, compareClassClassA);
} }

View File

@@ -1,17 +1,19 @@
package org.json.junit.data; package org.json.junit.data;
import java.math.BigInteger;
public class CustomClassA { public class CustomClassA {
public java.time.LocalDateTime localDate; public BigInteger largeInt;
public CustomClassA() {} public CustomClassA() {}
public CustomClassA(java.time.LocalDateTime localDate) { public CustomClassA(BigInteger largeInt) {
this.localDate = localDate; this.largeInt = largeInt;
} }
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
CustomClassA classA = (CustomClassA) o; CustomClassA classA = (CustomClassA) o;
return this.localDate.equals(classA.localDate); return this.largeInt.equals(classA.largeInt);
} }
} }