mirror of
https://github.com/stleary/JSON-java.git
synced 2026-01-25 00:00:38 -05:00
Limiting implemetation by removing the new classes.
This commit is contained in:
@@ -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}.
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -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}.
|
||||||
|
|||||||
@@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user