Browse Source

ludbo refractor; tests

tags/v1.0.0
Robin Thoni 8 years ago
parent
commit
a4555273c9

+ 20
- 0
luticateutils/src/main/java/com/luticate/utils/dbo/JSONContainer/LuAbstractJsonContainer.java View File

@@ -0,0 +1,20 @@
1
+package com.luticate.utils.dbo.JSONContainer;
2
+
3
+import org.json.JSONArray;
4
+import org.json.JSONException;
5
+import org.json.JSONObject;
6
+
7
+/**
8
+ * Created by robin on 10/21/16.
9
+ */
10
+
11
+public interface LuAbstractJsonContainer {
12
+    int getInt(Object key) throws JSONException;
13
+    String getString(Object key) throws JSONException;
14
+    long getLong(Object key) throws JSONException;
15
+    double getDouble(Object key) throws JSONException;
16
+    boolean getBoolean(Object key) throws JSONException;
17
+    JSONObject getJSONObject(Object key) throws JSONException;
18
+    JSONArray getJSONArray(Object key) throws JSONException;
19
+    boolean isNull(Object key) throws JSONException;
20
+}

+ 58
- 0
luticateutils/src/main/java/com/luticate/utils/dbo/JSONContainer/LuJSONArrayContainer.java View File

@@ -0,0 +1,58 @@
1
+package com.luticate.utils.dbo.JSONContainer;
2
+
3
+import org.json.JSONArray;
4
+import org.json.JSONException;
5
+import org.json.JSONObject;
6
+
7
+/**
8
+ * Created by robin on 10/21/16.
9
+ */
10
+
11
+public class LuJSONArrayContainer implements LuAbstractJsonContainer {
12
+    protected JSONArray _jsonArray;
13
+
14
+    public LuJSONArrayContainer(JSONArray jsonArray)
15
+    {
16
+        _jsonArray = jsonArray;
17
+    }
18
+
19
+    @Override
20
+    public int getInt(Object key) throws JSONException {
21
+        return _jsonArray.getInt((int) key);
22
+    }
23
+
24
+    @Override
25
+    public String getString(Object key) throws JSONException {
26
+        return _jsonArray.getString((int) key);
27
+    }
28
+
29
+    @Override
30
+    public long getLong(Object key) throws JSONException {
31
+        return _jsonArray.getLong((int) key);
32
+    }
33
+
34
+    @Override
35
+    public double getDouble(Object key) throws JSONException {
36
+        return _jsonArray.getDouble((int) key);
37
+    }
38
+
39
+    @Override
40
+    public boolean getBoolean(Object key) throws JSONException {
41
+        return _jsonArray.getBoolean((int) key);
42
+    }
43
+
44
+    @Override
45
+    public JSONObject getJSONObject(Object key) throws JSONException {
46
+        return _jsonArray.getJSONObject((int) key);
47
+    }
48
+
49
+    @Override
50
+    public JSONArray getJSONArray(Object key) throws JSONException {
51
+        return _jsonArray.getJSONArray((int) key);
52
+    }
53
+
54
+    @Override
55
+    public boolean isNull(Object key) throws JSONException {
56
+        return _jsonArray.isNull((int) key);
57
+    }
58
+}

+ 59
- 0
luticateutils/src/main/java/com/luticate/utils/dbo/JSONContainer/LuJSONObjectContainer.java View File

@@ -0,0 +1,59 @@
1
+package com.luticate.utils.dbo.JSONContainer;
2
+
3
+import org.json.JSONArray;
4
+import org.json.JSONException;
5
+import org.json.JSONObject;
6
+
7
+/**
8
+ * Created by robin on 10/21/16.
9
+ */
10
+
11
+public class LuJSONObjectContainer implements LuAbstractJsonContainer {
12
+
13
+    protected JSONObject _jsonObject;
14
+
15
+    public LuJSONObjectContainer(JSONObject jsonObject)
16
+    {
17
+        _jsonObject = jsonObject;
18
+    }
19
+
20
+    @Override
21
+    public int getInt(Object key) throws JSONException {
22
+        return _jsonObject.getInt((String) key);
23
+    }
24
+
25
+    @Override
26
+    public String getString(Object key) throws JSONException {
27
+        return _jsonObject.getString((String) key);
28
+    }
29
+
30
+    @Override
31
+    public long getLong(Object key) throws JSONException {
32
+        return _jsonObject.getLong((String) key);
33
+    }
34
+
35
+    @Override
36
+    public double getDouble(Object key) throws JSONException {
37
+        return _jsonObject.getDouble((String) key);
38
+    }
39
+
40
+    @Override
41
+    public boolean getBoolean(Object key) throws JSONException {
42
+        return _jsonObject.getBoolean((String) key);
43
+    }
44
+
45
+    @Override
46
+    public JSONObject getJSONObject(Object key) throws JSONException {
47
+        return _jsonObject.getJSONObject((String) key);
48
+    }
49
+
50
+    @Override
51
+    public JSONArray getJSONArray(Object key) throws JSONException {
52
+        return _jsonObject.getJSONArray((String) key);
53
+    }
54
+
55
+    @Override
56
+    public boolean isNull(Object key) throws JSONException {
57
+        return _jsonObject.isNull((String) key);
58
+    }
59
+}

+ 17
- 51
luticateutils/src/main/java/com/luticate/utils/dbo/LuDbo.java View File

@@ -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);

Loading…
Cancel
Save