Skip to content

Commit c4c2beb

Browse files
committed
Limiting implemetation by removing the new classes.
1 parent 9adea9e commit c4c2beb

File tree

5 files changed

+25
-62
lines changed

5 files changed

+25
-62
lines changed

src/main/java/org/json/InstanceCreator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
* @param <T> the type of instances created
77
*/
8-
public interface InstanceCreator<T> {
8+
interface InstanceCreator<T> {
99

1010
/**
1111
* Creates a new instance of type {@code T}.

src/main/java/org/json/JSONObject.java

Lines changed: 10 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,16 @@ public String convert(Object input) {
234234
return (String) input;
235235
}
236236
});
237+
classMapping.put(BigDecimal.class, new TypeConverter<BigDecimal>() {
238+
public BigDecimal convert(Object input) {
239+
return new BigDecimal((String) input);
240+
}
241+
});
242+
classMapping.put(BigInteger.class, new TypeConverter<BigInteger>() {
243+
public BigInteger convert(Object input) {
244+
return new BigInteger((String) input);
245+
}
246+
});
237247

238248
collectionMapping.put(List.class, new InstanceCreator<List>() {
239249
public List create() {
@@ -252,50 +262,6 @@ public Map create() {
252262
});
253263
}
254264

255-
/**
256-
* Returns the current class-to-function mapping used for type conversions.
257-
*
258-
* @return a map of classes to functions that convert an {@code Object} to that class
259-
*/
260-
public Map<Class<?>, TypeConverter<?>> getClassMapping() {
261-
return classMapping;
262-
}
263-
264-
/**
265-
* Returns the current collection-to-supplier mapping used for instantiating collections.
266-
*
267-
* @return a map of collection interface types to suppliers of concrete implementations
268-
*/
269-
public Map<Class<?>, InstanceCreator<?>> getCollectionMapping() {
270-
return collectionMapping;
271-
}
272-
273-
/**
274-
* Adds or updates a type conversion function for a given class.
275-
*
276-
* <p>This allows users to customize how objects are converted into specific types
277-
* during processing (e.g., JSON deserialization).
278-
*
279-
* @param clazz the target class for which the conversion function is to be set
280-
* @param function a function that takes an {@code Object} and returns an instance of {@code clazz}
281-
*/
282-
public void setClassMapping(Class<?> clazz, TypeConverter<?> function) {
283-
classMapping.put(clazz, function);
284-
}
285-
286-
/**
287-
* Adds or updates a supplier function for instantiating a collection type.
288-
*
289-
* <p>This allows customization of which concrete implementation is used for
290-
* interface types like {@code List}, {@code Set}, or {@code Map}.
291-
*
292-
* @param clazz the collection interface class (e.g., {@code List.class})
293-
* @param function a supplier that creates a new instance of a concrete implementation
294-
*/
295-
public void setCollectionMapping(Class<?> clazz, InstanceCreator<?> function) {
296-
collectionMapping.put(clazz, function);
297-
}
298-
299265
/**
300266
* Construct a JSONObject from a subset of another JSONObject. An array of
301267
* strings is used to identify the keys that should be copied. Missing keys

src/main/java/org/json/TypeConverter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
* @param <T> the target type to convert to
88
*/
9-
public interface TypeConverter<T> {
9+
interface TypeConverter<T> {
1010

1111
/**
1212
* Converts the given input object to an instance of type {@code T}.

src/test/java/org/json/junit/JSONObjectTest.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import org.json.JSONTokener;
3737
import org.json.ParserConfiguration;
3838
import org.json.XML;
39-
import org.json.TypeConverter;
4039
import org.json.junit.data.BrokenToString;
4140
import org.json.junit.data.ExceptionalBean;
4241
import org.json.junit.data.Fraction;
@@ -4121,17 +4120,13 @@ public void jsonObjectParseFromJson_0() {
41214120

41224121
@Test
41234122
public void jsonObjectParseFromJson_1() {
4124-
JSONObject object = new JSONObject();
4125-
object.setClassMapping(java.time.LocalDateTime.class, new TypeConverter<java.time.LocalDateTime>() {
4126-
public java.time.LocalDateTime convert(Object input) {
4127-
return java.time.LocalDateTime.parse((String) input);
4128-
}
4129-
});
4130-
java.time.LocalDateTime localDateTime = java.time.LocalDateTime.now();
4131-
object.put("localDate", localDateTime.toString());
4132-
CustomClassA customClassA = object.fromJson(CustomClassA.class);
4133-
CustomClassA compareClassClassA = new CustomClassA(localDateTime);
4134-
assertEquals(customClassA, compareClassClassA);
4123+
JSONObject object = new JSONObject();
4124+
4125+
BigInteger largeInt = new BigInteger("123");
4126+
object.put("largeInt", largeInt.toString());
4127+
CustomClassA customClassA = object.fromJson(CustomClassA.class);
4128+
CustomClassA compareClassClassA = new CustomClassA(largeInt);
4129+
assertEquals(customClassA, compareClassClassA);
41354130
}
41364131

41374132
@Test
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
package org.json.junit.data;
22

3+
import java.math.BigInteger;
4+
35
public class CustomClassA {
4-
public java.time.LocalDateTime localDate;
6+
public BigInteger largeInt;
57

68
public CustomClassA() {}
7-
public CustomClassA(java.time.LocalDateTime localDate) {
8-
this.localDate = localDate;
9+
public CustomClassA(BigInteger largeInt) {
10+
this.largeInt = largeInt;
911
}
1012

1113
@Override
1214
public boolean equals(Object o) {
1315
CustomClassA classA = (CustomClassA) o;
14-
return this.localDate.equals(classA.localDate);
16+
return this.largeInt.equals(classA.largeInt);
1517
}
1618
}
1719

0 commit comments

Comments
 (0)