|
@@ -1,19 +1,19 @@
|
1
|
1
|
package com.luticate.utils.dbo;
|
2
|
2
|
|
|
3
|
+import com.luticate.utils.dbo.JSONContainer.LuAbstractJsonContainer;
|
|
4
|
+import com.luticate.utils.dbo.JSONContainer.LuJSONArrayContainer;
|
|
5
|
+import com.luticate.utils.dbo.JSONContainer.LuJSONObjectContainer;
|
|
6
|
+
|
3
|
7
|
import org.json.JSONArray;
|
4
|
|
-import org.json.JSONException;
|
5
|
8
|
import org.json.JSONObject;
|
6
|
9
|
|
7
|
10
|
import java.lang.reflect.Array;
|
8
|
11
|
import java.lang.reflect.Field;
|
9
|
|
-import java.lang.reflect.GenericArrayType;
|
10
|
12
|
import java.lang.reflect.ParameterizedType;
|
11
|
|
-import java.lang.reflect.Type;
|
12
|
13
|
import java.util.AbstractMap;
|
13
|
14
|
import java.util.HashMap;
|
14
|
15
|
import java.util.Iterator;
|
15
|
16
|
import java.util.List;
|
16
|
|
-import java.util.Objects;
|
17
|
17
|
import java.util.Vector;
|
18
|
18
|
|
19
|
19
|
/**
|
|
@@ -43,47 +43,7 @@ public abstract class LuDbo {
|
43
|
43
|
return fieldName;
|
44
|
44
|
}
|
45
|
45
|
|
46
|
|
- public Object getObjectFromJson(Class clazz, JSONObject json, String key) throws Exception {
|
47
|
|
- if (json.isNull(key)) {
|
48
|
|
- return null;
|
49
|
|
- }
|
50
|
|
-
|
51
|
|
- if (clazz == Byte.class || clazz == byte.class) {
|
52
|
|
- return (byte) json.getInt(key);
|
53
|
|
- }
|
54
|
|
- if (clazz == Character.class || clazz == char.class) {
|
55
|
|
- return json.getString(key).charAt(0);
|
56
|
|
- }
|
57
|
|
- if (clazz == Short.class || clazz == short.class) {
|
58
|
|
- return (short) json.getInt(key);
|
59
|
|
- }
|
60
|
|
- if (clazz == Integer.class || clazz == int.class) {
|
61
|
|
- return json.getInt(key);
|
62
|
|
- }
|
63
|
|
- if (clazz == Long.class || clazz == long.class) {
|
64
|
|
- return json.getLong(key);
|
65
|
|
- }
|
66
|
|
- if (clazz == Float.class || clazz == float.class) {
|
67
|
|
- return (float) json.getDouble(key);
|
68
|
|
- }
|
69
|
|
- if (clazz == Double.class || clazz == double.class) {
|
70
|
|
- return json.getDouble(key);
|
71
|
|
- }
|
72
|
|
- if (clazz == Boolean.class || clazz == boolean.class) {
|
73
|
|
- return json.getBoolean(key);
|
74
|
|
- }
|
75
|
|
- if (clazz == String.class) {
|
76
|
|
- return json.getString(key);
|
77
|
|
- }
|
78
|
|
- if (LuDbo.class.isAssignableFrom(clazz)) {
|
79
|
|
- LuDbo dbo = (LuDbo) getInstance(clazz);
|
80
|
|
- dbo.fromJson(json.getJSONObject(key));
|
81
|
|
- return dbo;
|
82
|
|
- }
|
83
|
|
- return null;
|
84
|
|
- }
|
85
|
|
-
|
86
|
|
- public Object getObjectFromJson(Class clazz, JSONArray json, int key) throws Exception {
|
|
46
|
+ public Object getObjectFromJson(Class clazz, LuAbstractJsonContainer json, Object key) throws Exception {
|
87
|
47
|
if (json.isNull(key)) {
|
88
|
48
|
return null;
|
89
|
49
|
}
|
|
@@ -145,7 +105,7 @@ public abstract class LuDbo {
|
145
|
105
|
List list = new Vector();
|
146
|
106
|
|
147
|
107
|
for (Object item : objList) {
|
148
|
|
- list.add(getJsonFromObject(item == null ? null : item.getClass(), item));
|
|
108
|
+ list.add(getJsonFromObject(getClazz(item), item));
|
149
|
109
|
}
|
150
|
110
|
|
151
|
111
|
return list;
|
|
@@ -157,7 +117,7 @@ public abstract class LuDbo {
|
157
|
117
|
for (Object k : objMap.keySet()) {
|
158
|
118
|
if (k.getClass() == String.class) {
|
159
|
119
|
Object item = objMap.get(k);
|
160
|
|
- map.put(k, getJsonFromObject(item == null ? null : item.getClass(), item));
|
|
120
|
+ map.put(k, getJsonFromObject(getClazz(item), item));
|
161
|
121
|
}
|
162
|
122
|
}
|
163
|
123
|
|
|
@@ -186,6 +146,11 @@ public abstract class LuDbo {
|
186
|
146
|
return clazz.newInstance();
|
187
|
147
|
}
|
188
|
148
|
|
|
149
|
+ public Class getClazz(Object obj)
|
|
150
|
+ {
|
|
151
|
+ return obj == null ? null : obj.getClass();
|
|
152
|
+ }
|
|
153
|
+
|
189
|
154
|
public void fromJson(JSONObject json) throws Exception
|
190
|
155
|
{
|
191
|
156
|
for (Field field : getClass().getDeclaredFields()) {
|
|
@@ -193,13 +158,14 @@ public abstract class LuDbo {
|
193
|
158
|
Class clazz = field.getType();
|
194
|
159
|
if (json.has(key)) {
|
195
|
160
|
Object value = null;
|
|
161
|
+
|
196
|
162
|
if (List.class.isAssignableFrom(clazz)) {
|
197
|
163
|
ParameterizedType parameterizedType = (ParameterizedType) field.getGenericType();
|
198
|
164
|
Class type = (Class) parameterizedType.getActualTypeArguments()[0];
|
199
|
165
|
List<Object> list = (List<Object>) getInstance(clazz);
|
200
|
166
|
JSONArray jsonArray = json.getJSONArray(key);
|
201
|
167
|
for (int i = 0; i < jsonArray.length(); ++i) {
|
202
|
|
- list.add(getObjectFromJson(type, jsonArray, i));
|
|
168
|
+ list.add(getObjectFromJson(type, new LuJSONArrayContainer(jsonArray), i));
|
203
|
169
|
}
|
204
|
170
|
value = list;
|
205
|
171
|
}
|
|
@@ -208,7 +174,7 @@ public abstract class LuDbo {
|
208
|
174
|
JSONArray jsonArray = json.getJSONArray(key);
|
209
|
175
|
Object array = Array.newInstance(type, jsonArray.length());
|
210
|
176
|
for (int i = 0; i < jsonArray.length(); ++i) {
|
211
|
|
- Array.set(array, i, getObjectFromJson(type, jsonArray, i));
|
|
177
|
+ Array.set(array, i, getObjectFromJson(type, new LuJSONArrayContainer(jsonArray), i));
|
212
|
178
|
}
|
213
|
179
|
value = array;
|
214
|
180
|
}
|
|
@@ -222,13 +188,13 @@ public abstract class LuDbo {
|
222
|
188
|
Iterator<String> it = obj.keys();
|
223
|
189
|
while (it.hasNext()) {
|
224
|
190
|
String k = it.next();
|
225
|
|
- map.put(k, getObjectFromJson(type, obj, k));
|
|
191
|
+ map.put(k, getObjectFromJson(type, new LuJSONObjectContainer(obj), k));
|
226
|
192
|
}
|
227
|
193
|
value = map;
|
228
|
194
|
}
|
229
|
195
|
}
|
230
|
196
|
else {
|
231
|
|
- value = getObjectFromJson(clazz, json, key);
|
|
197
|
+ value = getObjectFromJson(clazz, new LuJSONObjectContainer(json), key);
|
232
|
198
|
}
|
233
|
199
|
field.setAccessible(true);
|
234
|
200
|
field.set(this, value);
|