|
@@ -11,7 +11,9 @@ import java.lang.reflect.ParameterizedType;
|
11
|
11
|
import java.lang.reflect.Type;
|
12
|
12
|
import java.util.AbstractMap;
|
13
|
13
|
import java.util.HashMap;
|
|
14
|
+import java.util.Iterator;
|
14
|
15
|
import java.util.List;
|
|
16
|
+import java.util.Objects;
|
15
|
17
|
import java.util.Vector;
|
16
|
18
|
|
17
|
19
|
/**
|
|
@@ -25,7 +27,7 @@ public abstract class LuDbo {
|
25
|
27
|
fromJson(json);
|
26
|
28
|
}
|
27
|
29
|
|
28
|
|
- public static String getFieldFromJson(String jsonName)
|
|
30
|
+ public String getFieldFromJson(String jsonName)
|
29
|
31
|
{
|
30
|
32
|
if (!jsonName.startsWith("_")) {
|
31
|
33
|
return String.format("_%s", jsonName);
|
|
@@ -33,7 +35,7 @@ public abstract class LuDbo {
|
33
|
35
|
return jsonName;
|
34
|
36
|
}
|
35
|
37
|
|
36
|
|
- public static String getJsonFromField(String fieldName)
|
|
38
|
+ public String getJsonFromField(String fieldName)
|
37
|
39
|
{
|
38
|
40
|
if (fieldName.startsWith("_")) {
|
39
|
41
|
return fieldName.substring(1);
|
|
@@ -41,7 +43,7 @@ public abstract class LuDbo {
|
41
|
43
|
return fieldName;
|
42
|
44
|
}
|
43
|
45
|
|
44
|
|
- public static Object getObjectFromJson(Class clazz, JSONObject json, String key) throws Exception {
|
|
46
|
+ public Object getObjectFromJson(Class clazz, JSONObject json, String key) throws Exception {
|
45
|
47
|
if (json.isNull(key)) {
|
46
|
48
|
return null;
|
47
|
49
|
}
|
|
@@ -74,14 +76,14 @@ public abstract class LuDbo {
|
74
|
76
|
return json.getString(key);
|
75
|
77
|
}
|
76
|
78
|
if (LuDbo.class.isAssignableFrom(clazz)) {
|
77
|
|
- LuDbo dbo = (LuDbo) clazz.newInstance();
|
|
79
|
+ LuDbo dbo = (LuDbo) getInstance(clazz);
|
78
|
80
|
dbo.fromJson(json.getJSONObject(key));
|
79
|
81
|
return dbo;
|
80
|
82
|
}
|
81
|
83
|
return null;
|
82
|
84
|
}
|
83
|
85
|
|
84
|
|
- public static Object getObjectFromJson(Class clazz, JSONArray json, int key) throws Exception {
|
|
86
|
+ public Object getObjectFromJson(Class clazz, JSONArray json, int key) throws Exception {
|
85
|
87
|
if (json.isNull(key)) {
|
86
|
88
|
return null;
|
87
|
89
|
}
|
|
@@ -114,13 +116,23 @@ public abstract class LuDbo {
|
114
|
116
|
return json.getString(key);
|
115
|
117
|
}
|
116
|
118
|
if (LuDbo.class.isAssignableFrom(clazz)) {
|
117
|
|
- LuDbo dbo = (LuDbo) clazz.newInstance();
|
|
119
|
+ LuDbo dbo = (LuDbo) getInstance(clazz);
|
118
|
120
|
dbo.fromJson(json.getJSONObject(key));
|
119
|
121
|
return dbo;
|
120
|
122
|
}
|
121
|
123
|
return null;
|
122
|
124
|
}
|
123
|
125
|
|
|
126
|
+ public Object getInstance(Class clazz) throws Exception {
|
|
127
|
+ if (clazz == List.class) {
|
|
128
|
+ return new Vector<>();
|
|
129
|
+ }
|
|
130
|
+ if (clazz == AbstractMap.class) {
|
|
131
|
+ return new HashMap<>();
|
|
132
|
+ }
|
|
133
|
+ return clazz.newInstance();
|
|
134
|
+ }
|
|
135
|
+
|
124
|
136
|
public void fromJson(JSONObject json) throws Exception
|
125
|
137
|
{
|
126
|
138
|
for (Field field : getClass().getDeclaredFields()) {
|
|
@@ -131,7 +143,7 @@ public abstract class LuDbo {
|
131
|
143
|
if (List.class.isAssignableFrom(clazz)) {
|
132
|
144
|
ParameterizedType parameterizedType = (ParameterizedType) field.getGenericType();
|
133
|
145
|
Class type = (Class) parameterizedType.getActualTypeArguments()[0];
|
134
|
|
- List<Object> list = (clazz == List.class ? new Vector<>() : (List<Object>) clazz.newInstance());
|
|
146
|
+ List<Object> list = (List<Object>) getInstance(clazz);
|
135
|
147
|
JSONArray jsonArray = json.getJSONArray(key);
|
136
|
148
|
for (int i = 0; i < jsonArray.length(); ++i) {
|
137
|
149
|
list.add(getObjectFromJson(type, jsonArray, i));
|
|
@@ -147,13 +159,21 @@ public abstract class LuDbo {
|
147
|
159
|
}
|
148
|
160
|
value = array;
|
149
|
161
|
}
|
150
|
|
-// else if (AbstractMap.class.isAssignableFrom(clazz)) {
|
151
|
|
-// ParameterizedType parameterizedType = (ParameterizedType) field.getGenericType();
|
152
|
|
-// Class type = (Class) parameterizedType.getActualTypeArguments()[0];
|
153
|
|
-// if (type == String.class) {
|
154
|
|
-// type = (Class) parameterizedType.getActualTypeArguments()[1];
|
155
|
|
-// }
|
156
|
|
-// }
|
|
162
|
+ else if (AbstractMap.class.isAssignableFrom(clazz)) {
|
|
163
|
+ ParameterizedType parameterizedType = (ParameterizedType) field.getGenericType();
|
|
164
|
+ Class type = (Class) parameterizedType.getActualTypeArguments()[0];
|
|
165
|
+ if (type == String.class) {
|
|
166
|
+ type = (Class) parameterizedType.getActualTypeArguments()[1];
|
|
167
|
+ AbstractMap<String, Object> map = (AbstractMap<String, Object>) getInstance(clazz);
|
|
168
|
+ JSONObject obj = json.getJSONObject(key);
|
|
169
|
+ Iterator<String> it = obj.keys();
|
|
170
|
+ while (it.hasNext()) {
|
|
171
|
+ String k = it.next();
|
|
172
|
+ map.put(k, getObjectFromJson(type, obj, k));
|
|
173
|
+ }
|
|
174
|
+ value = map;
|
|
175
|
+ }
|
|
176
|
+ }
|
157
|
177
|
else {
|
158
|
178
|
value = getObjectFromJson(clazz, json, key);
|
159
|
179
|
}
|
|
@@ -168,6 +188,11 @@ public abstract class LuDbo {
|
168
|
188
|
{
|
169
|
189
|
HashMap<String, Object> map = new HashMap<>();
|
170
|
190
|
|
|
191
|
+ for (Field field : getClass().getDeclaredFields()) {
|
|
192
|
+ String key = getJsonFromField(field.getName());
|
|
193
|
+ Class clazz = field.getType();
|
|
194
|
+ }
|
|
195
|
+
|
171
|
196
|
return map;
|
172
|
197
|
}
|
173
|
198
|
|