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
*/
public interface InstanceCreator<T> {
interface InstanceCreator<T> {
/**
* Creates a new instance of type {@code T}.

View File

@@ -234,6 +234,16 @@ public class JSONObject {
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>() {
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
* 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
*/
public interface TypeConverter<T> {
interface TypeConverter<T> {
/**
* Converts the given input object to an instance of type {@code T}.