Skip to content

Commit 7d28955

Browse files
committed
Updating to work with java 1.6
1 parent 83a0e34 commit 7d28955

File tree

5 files changed

+122
-35
lines changed

5 files changed

+122
-35
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.json;
2+
3+
/**
4+
* Interface defining a creator that produces new instances of type {@code T}.
5+
*
6+
* @param <T> the type of instances created
7+
*/
8+
public interface InstanceCreator<T> {
9+
10+
/**
11+
* Creates a new instance of type {@code T}.
12+
*
13+
* @return a new instance of {@code T}
14+
*/
15+
T create();
16+
}

src/main/java/org/json/JSONBuilder.java

Lines changed: 76 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
import java.util.Set;
88
import java.util.HashSet;
99
import java.util.Collection;
10-
import java.util.function.Function;
11-
import java.util.function.Supplier;
1210

1311
/**
1412
* The {@code JSONBuilder} class provides a configurable mechanism for
@@ -42,46 +40,96 @@ public class JSONBuilder {
4240
* <li>{@code String.class} -> Identity function</li>
4341
* </ul>
4442
*/
45-
private static final Map<Class<?>, Function<Object, ?>> classMapping = new HashMap<>();
43+
private static final Map<Class<?>, TypeConverter<?>> classMapping = new HashMap<>();
4644

4745
/**
4846
* A mapping from collection interface types to suppliers that produce
4947
* instances of concrete collection implementations.
5048
*
51-
* <p>Examples of default mappings:
52-
* <ul>
53-
* <li>{@code List.class} -> {@code ArrayList::new}</li>
54-
* <li>{@code Set.class} -> {@code HashSet::new}</li>
55-
* <li>{@code Map.class} -> {@code HashMap::new}</li>
56-
* </ul>
5749
*/
58-
private static final Map<Class<?>, Supplier<?>> collectionMapping = new HashMap<>();
50+
private static final Map<Class<?>, InstanceCreator<?>> collectionMapping = new HashMap<>();
5951

6052
// Static initializer block to populate default mappings
6153
static {
62-
classMapping.put(int.class, s -> ((Number) s).intValue());
63-
classMapping.put(Integer.class, s -> ((Number) s).intValue());
64-
classMapping.put(double.class, s -> ((Number) s).doubleValue());
65-
classMapping.put(Double.class, s -> ((Number) s).doubleValue());
66-
classMapping.put(float.class, s -> ((Number) s).floatValue());
67-
classMapping.put(Float.class, s -> ((Number) s).floatValue());
68-
classMapping.put(long.class, s -> ((Number) s).longValue());
69-
classMapping.put(Long.class, s -> ((Number) s).longValue());
70-
classMapping.put(boolean.class, s -> s);
71-
classMapping.put(Boolean.class, s -> s);
72-
classMapping.put(String.class, s -> s);
54+
classMapping.put(int.class, new TypeConverter<Integer>() {
55+
public Integer convert(Object input) {
56+
return ((Number) input).intValue();
57+
}
58+
});
59+
classMapping.put(Integer.class, new TypeConverter<Integer>() {
60+
public Integer convert(Object input) {
61+
return ((Number) input).intValue();
62+
}
63+
});
64+
classMapping.put(double.class, new TypeConverter<Double>() {
65+
public Double convert(Object input) {
66+
return ((Number) input).doubleValue();
67+
}
68+
});
69+
classMapping.put(Double.class, new TypeConverter<Double>() {
70+
public Double convert(Object input) {
71+
return ((Number) input).doubleValue();
72+
}
73+
});
74+
classMapping.put(float.class, new TypeConverter<Float>() {
75+
public Float convert(Object input) {
76+
return ((Number) input).floatValue();
77+
}
78+
});
79+
classMapping.put(Float.class, new TypeConverter<Float>() {
80+
public Float convert(Object input) {
81+
return ((Number) input).floatValue();
82+
}
83+
});
84+
classMapping.put(long.class, new TypeConverter<Long>() {
85+
public Long convert(Object input) {
86+
return ((Number) input).longValue();
87+
}
88+
});
89+
classMapping.put(Long.class, new TypeConverter<Long>() {
90+
public Long convert(Object input) {
91+
return ((Number) input).longValue();
92+
}
93+
});
94+
classMapping.put(boolean.class, new TypeConverter<Boolean>() {
95+
public Boolean convert(Object input) {
96+
return (Boolean) input;
97+
}
98+
});
99+
classMapping.put(Boolean.class, new TypeConverter<Boolean>() {
100+
public Boolean convert(Object input) {
101+
return (Boolean) input;
102+
}
103+
});
104+
classMapping.put(String.class, new TypeConverter<String>() {
105+
public String convert(Object input) {
106+
return (String) input;
107+
}
108+
});
73109

74-
collectionMapping.put(List.class, ArrayList::new);
75-
collectionMapping.put(Set.class, HashSet::new);
76-
collectionMapping.put(Map.class, HashMap::new);
110+
collectionMapping.put(List.class, new InstanceCreator<List>() {
111+
public List create() {
112+
return new ArrayList();
113+
}
114+
});
115+
collectionMapping.put(Set.class, new InstanceCreator<Set>() {
116+
public Set create() {
117+
return new HashSet();
118+
}
119+
});
120+
collectionMapping.put(Map.class, new InstanceCreator<Map>() {
121+
public Map create() {
122+
return new HashMap();
123+
}
124+
});
77125
}
78126

79127
/**
80128
* Returns the current class-to-function mapping used for type conversions.
81129
*
82130
* @return a map of classes to functions that convert an {@code Object} to that class
83131
*/
84-
public Map<Class<?>, Function<Object, ?>> getClassMapping() {
132+
public Map<Class<?>, TypeConverter<?>> getClassMapping() {
85133
return this.classMapping;
86134
}
87135

@@ -90,7 +138,7 @@ public class JSONBuilder {
90138
*
91139
* @return a map of collection interface types to suppliers of concrete implementations
92140
*/
93-
public Map<Class<?>, Supplier<?>> getCollectionMapping() {
141+
public Map<Class<?>, InstanceCreator<?>> getCollectionMapping() {
94142
return this.collectionMapping;
95143
}
96144

@@ -103,7 +151,7 @@ public Map<Class<?>, Supplier<?>> getCollectionMapping() {
103151
* @param clazz the target class for which the conversion function is to be set
104152
* @param function a function that takes an {@code Object} and returns an instance of {@code clazz}
105153
*/
106-
public void setClassMapping(Class<?> clazz, Function<Object, ?> function) {
154+
public void setClassMapping(Class<?> clazz, TypeConverter<?> function) {
107155
classMapping.put(clazz, function);
108156
}
109157

@@ -116,7 +164,7 @@ public void setClassMapping(Class<?> clazz, Function<Object, ?> function) {
116164
* @param clazz the collection interface class (e.g., {@code List.class})
117165
* @param function a supplier that creates a new instance of a concrete implementation
118166
*/
119-
public void setCollectionMapping(Class<?> clazz, Supplier<?> function) {
167+
public void setCollectionMapping(Class<?> clazz, InstanceCreator<?> function) {
120168
collectionMapping.put(clazz, function);
121169
}
122170
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3256,7 +3256,7 @@ public <T> T fromJson(Class<T> clazz) {
32563256
if (this.builder == null) {
32573257
this.builder = new JSONBuilder();
32583258
}
3259-
Map<Class<?>, Function<Object, ?>> classMapping = this.builder.getClassMapping();
3259+
Map<Class<?>, TypeConverter<?>> classMapping = this.builder.getClassMapping();
32603260

32613261
for (Field field: clazz.getDeclaredFields()) {
32623262
field.setAccessible(true);
@@ -3265,7 +3265,7 @@ public <T> T fromJson(Class<T> clazz) {
32653265
Object value = this.get(fieldName);
32663266
Class<?> pojoClass = field.getType();
32673267
if (classMapping.containsKey(pojoClass)) {
3268-
field.set(obj, classMapping.get(pojoClass).apply(value));
3268+
field.set(obj, classMapping.get(pojoClass).convert(value));
32693269
} else {
32703270
if (value.getClass() == JSONObject.class) {
32713271
field.set(obj, fromJson((JSONObject) value, pojoClass));
@@ -3290,10 +3290,10 @@ public <T> T fromJson(Class<T> clazz) {
32903290

32913291
private <T> Collection<T> fromJsonArray(JSONArray jsonArray, Class<?> collectionType, Type elementType) throws JSONException {
32923292
try {
3293-
Map<Class<?>, Function<Object, ?>> classMapping = this.builder.getClassMapping();
3294-
Map<Class<?>, Supplier<?>> collectionMapping = this.builder.getCollectionMapping();
3293+
Map<Class<?>, TypeConverter<?>> classMapping = this.builder.getClassMapping();
3294+
Map<Class<?>, InstanceCreator<?>> collectionMapping = this.builder.getCollectionMapping();
32953295
Collection<T> collection = (Collection<T>) (collectionMapping.containsKey(collectionType) ?
3296-
collectionMapping.get(collectionType).get()
3296+
collectionMapping.get(collectionType).create()
32973297
: collectionType.getDeclaredConstructor().newInstance());
32983298

32993299

@@ -3312,7 +3312,7 @@ private <T> Collection<T> fromJsonArray(JSONArray jsonArray, Class<?> collection
33123312
for (int i = 0; i < jsonArray.length(); i++) {
33133313
Object jsonElement = jsonArray.get(i);
33143314
if (classMapping.containsKey(innerElementClass)) {
3315-
collection.add((T) classMapping.get(innerElementClass).apply(jsonElement));
3315+
collection.add((T) classMapping.get(innerElementClass).convert(jsonElement));
33163316
} else if (jsonElement.getClass() == JSONObject.class) {
33173317
collection.add((T) ((JSONObject) jsonElement).fromJson(innerElementClass));
33183318
} else if (jsonElement.getClass() == JSONArray.class) {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.json;
2+
3+
/**
4+
* Interface defining a converter that converts an input {@code Object}
5+
* into an instance of a specific type {@code T}.
6+
*
7+
* @param <T> the target type to convert to
8+
*/
9+
public interface TypeConverter<T> {
10+
11+
/**
12+
* Converts the given input object to an instance of type {@code T}.
13+
*
14+
* @param input the object to convert
15+
* @return the converted instance of type {@code T}
16+
*/
17+
T convert(Object input);
18+
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.json.JSONTokener;
3838
import org.json.ParserConfiguration;
3939
import org.json.XML;
40+
import org.json.TypeConverter;
4041
import org.json.junit.data.BrokenToString;
4142
import org.json.junit.data.ExceptionalBean;
4243
import org.json.junit.data.Fraction;
@@ -4133,7 +4134,11 @@ public boolean equals(Object o) {
41334134
@Test
41344135
public void jsonObjectParseFromJson_1() {
41354136
JSONBuilder builder = new JSONBuilder();
4136-
builder.setClassMapping(java.time.LocalDateTime.class, s -> java.time.LocalDateTime.parse((String)s));
4137+
builder.setClassMapping(java.time.LocalDateTime.class, new TypeConverter<java.time.LocalDateTime>() {
4138+
public java.time.LocalDateTime convert(Object input) {
4139+
return java.time.LocalDateTime.parse((String) input);
4140+
}
4141+
});
41374142
JSONObject object = new JSONObject(builder);
41384143
java.time.LocalDateTime localDateTime = java.time.LocalDateTime.now();
41394144
object.put("localDate", localDateTime.toString());

0 commit comments

Comments
 (0)