| commit 39: | 733f2fd9721e |
| parent 38: | 78ffeee0ef67 |
| branch: | default |
Rename UnknownJsonType to ObjectJsonType.
12 months ago
Changed (Δ2.5 KB):
raw changeset »
src/main/java/schemaless/serializer/JsonTypeRegistry.java (1 lines added, 1 lines removed)
src/main/java/schemaless/serializer/ObjectJsonType.java (78 lines added, 0 lines removed)
src/main/java/schemaless/serializer/UnknownJsonType.java
Up to file-list src/main/java/schemaless/serializer/JsonTypeRegistry.java:
| … | … | @@ -29,7 +29,7 @@ public class JsonTypeRegistry { |
29 |
29 |
register(UUID.class, new UuidJsonType()); |
30 |
30 |
|
31 |
31 |
enumJsonType = new EnumJsonType(); |
32 |
unknownJsonType = new |
|
32 |
unknownJsonType = new ObjectJsonType(this); |
|
33 |
33 |
} |
34 |
34 |
|
35 |
35 |
private void register(Class<?> javaType, JsonType jsonType) { |
Up to file-list src/main/java/schemaless/serializer/ObjectJsonType.java:
1 |
package schemaless.serializer; |
|
2 |
||
3 |
import java.beans.PropertyDescriptor; |
|
4 |
import java.lang.reflect.InvocationTargetException; |
|
5 |
import java.lang.reflect.Method; |
|
6 |
import java.util.Arrays; |
|
7 |
import java.util.HashSet; |
|
8 |
import java.util.Set; |
|
9 |
||
10 |
import org.apache.commons.beanutils.PropertyUtils; |
|
11 |
import org.json.JSONException; |
|
12 |
import org.json.JSONObject; |
|
13 |
import org.json.JSONWriter; |
|
14 |
||
15 |
public class ObjectJsonType extends NullAwareJsonType { |
|
16 |
static final Set<String> DEFAULT_EXCLUDES = new HashSet<String>(); |
|
17 |
static { |
|
18 |
DEFAULT_EXCLUDES.addAll(Arrays.asList("class")); |
|
19 |
} |
|
20 |
private final JsonTypeRegistry registry; |
|
21 |
||
22 |
public ObjectJsonType(JsonTypeRegistry registry) { |
|
23 |
this.registry = registry; |
|
24 |
} |
|
25 |
||
26 |
public void setNotNull(JSONWriter json, Class<?> javaType, String name, Object value) throws JSONException { |
|
27 |
try { |
|
28 |
json.key(name).object(); |
|
29 |
for (PropertyDescriptor each : PropertyUtils.getPropertyDescriptors(javaType)) { |
|
30 |
if (DEFAULT_EXCLUDES.contains(each.getName())) { |
|
31 |
continue; |
|
32 |
} |
|
33 |
Method getter = each.getReadMethod(); |
|
34 |
if (getter != null) { |
|
35 |
getter.setAccessible(true); |
|
36 |
Object propertyValue = getter.invoke(value); |
|
37 |
registry.find(each.getPropertyType()).set(json, each.getPropertyType(), each.getName(), propertyValue); |
|
38 |
} |
|
39 |
} |
|
40 |
json.endObject(); |
|
41 |
} catch (IllegalAccessException e) { |
|
42 |
throw new JSONException(e); |
|
43 |
} catch (IllegalArgumentException e) { |
|
44 |
throw new JSONException(e); |
|
45 |
} catch (InvocationTargetException e) { |
|
46 |
throw new JSONException(e); |
|
47 |
} |
|
48 |
} |
|
49 |
||
50 |
public Object getNotNull(JSONObject json, Class<?> javaType, String name) throws JSONException { |
|
51 |
try { |
|
52 |
JSONObject jsonValue = json.getJSONObject(name); |
|
53 |
Object value = javaType.newInstance(); |
|
54 |
for (PropertyDescriptor each : PropertyUtils.getPropertyDescriptors(javaType)) { |
|
55 |
if (DEFAULT_EXCLUDES.contains(each.getName())) { |
|
56 |
continue; |
|
57 |
} |
|
58 |
Method setter = each.getWriteMethod(); |
|
59 |
if (setter != null) { |
|
60 |
setter.setAccessible(true); |
|
61 |
Object propertyValue = registry.find(each.getPropertyType()).get(jsonValue, each.getPropertyType(), each.getName()); |
|
62 |
if (value != null || !each.getPropertyType().isPrimitive()) { |
|
63 |
setter.invoke(value, propertyValue); |
|
64 |
} |
|
65 |
} |
|
66 |
} |
|
67 |
return value; |
|
68 |
} catch (InstantiationException e) { |
|
69 |
throw new JSONException(e); |
|
70 |
} catch (IllegalAccessException e) { |
|
71 |
throw new JSONException(e); |
|
72 |
} catch (IllegalArgumentException e) { |
|
73 |
throw new JSONException(e); |
|
74 |
} catch (InvocationTargetException e) { |
|
75 |
throw new JSONException(e); |
|
76 |
} |
|
77 |
} |
|
78 |
} |
