Browse Source

fixed some json issues; added yoda time support; swtiched dbo to automatic de/serialization

tags/v1.0.0
Robin Thoni 7 years ago
parent
commit
0478a3fa91

+ 0
- 1
app/build.gradle View File

46
     apt 'com.jakewharton:butterknife-compiler:8.0.1'
46
     apt 'com.jakewharton:butterknife-compiler:8.0.1'
47
     compile project(path: ':luticateutils')
47
     compile project(path: ':luticateutils')
48
     compile 'com.amulyakhare:com.amulyakhare.textdrawable:1.0.1'
48
     compile 'com.amulyakhare:com.amulyakhare.textdrawable:1.0.1'
49
-    compile 'net.danlew:android.joda:2.9.4.2'
50
     compile 'com.google.firebase:firebase-ads:9.6.1'
49
     compile 'com.google.firebase:firebase-ads:9.6.1'
51
     compile 'com.facebook.android:facebook-android-sdk:4.5.0'
50
     compile 'com.facebook.android:facebook-android-sdk:4.5.0'
52
     compile 'com.github.simbiose:Encryption:1.4.0'
51
     compile 'com.github.simbiose:Encryption:1.4.0'

+ 3
- 3
app/src/main/java/com/rthoni/stssaguenay/business/STSBusiness.java View File

52
                 dbo.fromJson(json.getJSONObject(i));
52
                 dbo.fromJson(json.getJSONObject(i));
53
                 dbos.add(dbo);
53
                 dbos.add(dbo);
54
             }
54
             }
55
-        } catch (JSONException e) {
55
+        } catch (Exception e) {
56
             e.printStackTrace();
56
             e.printStackTrace();
57
         }
57
         }
58
         return dbos;
58
         return dbos;
73
         if (str != null) {
73
         if (str != null) {
74
             UserDbo user = new UserDbo();
74
             UserDbo user = new UserDbo();
75
             try {
75
             try {
76
-                user.fromJson(new JSONObject(str));
76
+                user.fromString(str);
77
                 return user;
77
                 return user;
78
-            } catch (JSONException e) {
78
+            } catch (Exception e) {
79
                 e.printStackTrace();
79
                 e.printStackTrace();
80
             }
80
             }
81
         }
81
         }

+ 2
- 2
app/src/main/java/com/rthoni/stssaguenay/business/UsersSettingsBusiness.java View File

29
 
29
 
30
         UsersSettingsDbo settings = new UsersSettingsDbo();
30
         UsersSettingsDbo settings = new UsersSettingsDbo();
31
         try {
31
         try {
32
-            settings.fromJson(new JSONObject(data));
33
-        } catch (JSONException e) {
32
+            settings.fromString(data);
33
+        } catch (Exception e) {
34
             e.printStackTrace();
34
             e.printStackTrace();
35
             return null;
35
             return null;
36
         }
36
         }

+ 2
- 1
app/src/main/java/com/rthoni/stssaguenay/dataaccess/SchedulesDataAccess.java View File

3
 import com.luticate.utils.business.LuPromise;
3
 import com.luticate.utils.business.LuPromise;
4
 import com.luticate.utils.dataaccess.LuDataAccess;
4
 import com.luticate.utils.dataaccess.LuDataAccess;
5
 import com.luticate.utils.dbo.LuDataAccessConfigDbo;
5
 import com.luticate.utils.dbo.LuDataAccessConfigDbo;
6
+import com.luticate.utils.dbo.LuDbo;
6
 import com.rthoni.stssaguenay.dbo.UserFavouriteStopsDbo;
7
 import com.rthoni.stssaguenay.dbo.UserFavouriteStopsDbo;
7
 import com.rthoni.stssaguenay.dbo.RoutesDbo;
8
 import com.rthoni.stssaguenay.dbo.RoutesDbo;
8
 import com.rthoni.stssaguenay.dbo.SchedulesDbo;
9
 import com.rthoni.stssaguenay.dbo.SchedulesDbo;
25
     {
26
     {
26
         HashMap<String, String> map = new HashMap<>();
27
         HashMap<String, String> map = new HashMap<>();
27
         map.put("count", Integer.toString(count));
28
         map.put("count", Integer.toString(count));
28
-        map.put("date", JSONObject.quote(date.toString("y-M-d HH:mm:ss")));
29
+        map.put("date", JSONObject.quote(date.toString(LuDbo.DEFAULT_DATE_TIME_FORMAT)));
29
 
30
 
30
         JSONArray stops = new JSONArray();
31
         JSONArray stops = new JSONArray();
31
         for (UserFavouriteStopsDbo dbo : favouriteStopDbos) {
32
         for (UserFavouriteStopsDbo dbo : favouriteStopDbos) {

+ 17
- 17
app/src/main/java/com/rthoni/stssaguenay/dbo/RoutesDbo.java View File

26
 
26
 
27
     protected String _fgColor;
27
     protected String _fgColor;
28
 
28
 
29
-    @Override
30
-    public void fromJson(JSONObject json) throws JSONException {
31
-        _id = json.getString("id");
32
-        _name = json.getString("name");
33
-        _bgColor = json.getString("bgColor");
34
-        _fgColor = json.getString("fgColor");
35
-    }
36
-
37
-    @Override
38
-    public HashMap<String, Object> toArray() {
39
-        HashMap<String, Object> map = new HashMap<>();
40
-        map.put("id", _id);
41
-        map.put("name", _name);
42
-        map.put("bgColor", _bgColor);
43
-        map.put("fgColor", _fgColor);
44
-        return map;
45
-    }
29
+//    @Override
30
+//    public void fromJson(JSONObject json) throws JSONException {
31
+//        _id = json.getString("id");
32
+//        _name = json.getString("name");
33
+//        _bgColor = json.getString("bgColor");
34
+//        _fgColor = json.getString("fgColor");
35
+//    }
36
+//
37
+//    @Override
38
+//    public HashMap<String, Object> toArray() {
39
+//        HashMap<String, Object> map = new HashMap<>();
40
+//        map.put("id", _id);
41
+//        map.put("name", _name);
42
+//        map.put("bgColor", _bgColor);
43
+//        map.put("fgColor", _fgColor);
44
+//        return map;
45
+//    }
46
 
46
 
47
     public String getId() {
47
     public String getId() {
48
         return _id;
48
         return _id;

+ 15
- 15
app/src/main/java/com/rthoni/stssaguenay/dbo/SchedulesDbo.java View File

31
 
31
 
32
     protected List<LocalDateTime> _schedules;
32
     protected List<LocalDateTime> _schedules;
33
 
33
 
34
-    @Override
35
-    public void fromJson(JSONObject json) throws JSONException {
36
-        _stopId = json.getString("stopId");
37
-        _routeId = json.getString("routeId");
38
-        _schedules = new Vector<>();
39
-        JSONArray schedules = json.getJSONArray("schedules");
40
-        for (int i = 0; i < schedules.length(); ++i) {
41
-            _schedules.add(LocalDateTime.parse(schedules.getString(i), DateTimeFormat.forPattern("y-M-d H:m:s")));
42
-        }
43
-    }
44
-
45
-    @Override
46
-    public HashMap<String, Object> toArray() {
47
-        return null;
48
-    }
34
+//    @Override
35
+//    public void fromJson(JSONObject json) throws JSONException {
36
+//        _stopId = json.getString("stopId");
37
+//        _routeId = json.getString("routeId");
38
+//        _schedules = new Vector<>();
39
+//        JSONArray schedules = json.getJSONArray("schedules");
40
+//        for (int i = 0; i < schedules.length(); ++i) {
41
+//            _schedules.add(LocalDateTime.parse(schedules.getString(i), DateTimeFormat.forPattern("y-M-d H:m:s")));
42
+//        }
43
+//    }
44
+//
45
+//    @Override
46
+//    public HashMap<String, Object> toArray() {
47
+//        return null;
48
+//    }
49
 
49
 
50
     public String getStopId() {
50
     public String getStopId() {
51
         return _stopId;
51
         return _stopId;

+ 23
- 23
app/src/main/java/com/rthoni/stssaguenay/dbo/StopsDbo.java View File

22
     {
22
     {
23
     }
23
     }
24
 
24
 
25
-    @Override
26
-    public void fromJson(JSONObject json) throws JSONException {
27
-        _name = json.getString("name");
28
-        _routes = new Vector<>();
29
-        JSONArray routes = json.getJSONArray("routes");
30
-        for (int i = 0; i < routes.length(); ++i) {
31
-            _routes.add(routes.getString(i));
32
-        }
33
-        _id = json.getString("id");
34
-        _posX = json.getDouble("posX");
35
-        _posY = json.getDouble("posY");
36
-    }
37
-
38
-    @Override
39
-    public HashMap<String, Object> toArray() {
40
-        HashMap<String, Object> map = new HashMap<>();
41
-        map.put("name", _name);
42
-        map.put("routes", _routes);
43
-        map.put("id", _id);
44
-        map.put("posX", _posX);
45
-        map.put("posY", _posY);
46
-        return map;
47
-    }
25
+//    @Override
26
+//    public void fromJson(JSONObject json) throws JSONException {
27
+//        _name = json.getString("name");
28
+//        _routes = new Vector<>();
29
+//        JSONArray routes = json.getJSONArray("routes");
30
+//        for (int i = 0; i < routes.length(); ++i) {
31
+//            _routes.add(routes.getString(i));
32
+//        }
33
+//        _id = json.getString("id");
34
+//        _posX = json.getDouble("posX");
35
+//        _posY = json.getDouble("posY");
36
+//    }
37
+//
38
+//    @Override
39
+//    public HashMap<String, Object> toArray() {
40
+//        HashMap<String, Object> map = new HashMap<>();
41
+//        map.put("name", _name);
42
+//        map.put("routes", _routes);
43
+//        map.put("id", _id);
44
+//        map.put("posX", _posX);
45
+//        map.put("posY", _posY);
46
+//        return map;
47
+//    }
48
 
48
 
49
     protected String _name;
49
     protected String _name;
50
 
50
 

+ 15
- 15
app/src/main/java/com/rthoni/stssaguenay/dbo/UserDbo.java View File

19
 
19
 
20
     protected String _email;
20
     protected String _email;
21
 
21
 
22
-    @Override
23
-    public void fromJson(JSONObject json) throws JSONException {
24
-        _id = json.getString("id");
25
-        _name = json.getString("name");
26
-        _email = json.getString("email");
27
-    }
28
-
29
-    @Override
30
-    public HashMap<String, Object> toArray() {
31
-        HashMap<String, Object> map = new HashMap<>();
32
-        map.put("id", _id);
33
-        map.put("name", _name);
34
-        map.put("email", _email);
35
-        return map;
36
-    }
22
+//    @Override
23
+//    public void fromJson(JSONObject json) throws JSONException {
24
+//        _id = json.getString("id");
25
+//        _name = json.getString("name");
26
+//        _email = json.getString("email");
27
+//    }
28
+//
29
+//    @Override
30
+//    public HashMap<String, Object> toArray() {
31
+//        HashMap<String, Object> map = new HashMap<>();
32
+//        map.put("id", _id);
33
+//        map.put("name", _name);
34
+//        map.put("email", _email);
35
+//        return map;
36
+//    }
37
 
37
 
38
     public String getId() {
38
     public String getId() {
39
         return _id;
39
         return _id;

+ 26
- 26
app/src/main/java/com/rthoni/stssaguenay/dbo/UserFavouriteStopsDbo.java View File

22
 
22
 
23
     protected String _name;
23
     protected String _name;
24
 
24
 
25
-    @Override
26
-    public void fromJson(JSONObject json) throws JSONException {
27
-        _stop = new StopsDbo();
28
-        _stop.fromJson(json.getJSONObject("stop"));
29
-        _routes = new Vector<>();
30
-        JSONArray routes = json.getJSONArray("routes");
31
-        for (int i = 0; i < routes.length(); ++i) {
32
-            RoutesDbo route = new RoutesDbo();
33
-            route.fromJson(routes.getJSONObject(i));
34
-            _routes.add(route);
35
-        }
36
-        _name = json.getString("name");
37
-    }
38
-
39
-    @Override
40
-    public HashMap<String, Object> toArray() {
41
-        HashMap<String, Object> map = new HashMap<>();
42
-        map.put("stop", _stop.toArray());
43
-        List<Object> routes = new Vector<>();
44
-        for (RoutesDbo route : _routes) {
45
-            routes.add(route.toArray());
46
-        }
47
-        map.put("routes", routes);
48
-        map.put("name", _name);
49
-        return map;
50
-    }
25
+//    @Override
26
+//    public void fromJson(JSONObject json) throws JSONException {
27
+//        _stop = new StopsDbo();
28
+//        _stop.fromJson(json.getJSONObject("stop"));
29
+//        _routes = new Vector<>();
30
+//        JSONArray routes = json.getJSONArray("routes");
31
+//        for (int i = 0; i < routes.length(); ++i) {
32
+//            RoutesDbo route = new RoutesDbo();
33
+//            route.fromJson(routes.getJSONObject(i));
34
+//            _routes.add(route);
35
+//        }
36
+//        _name = json.getString("name");
37
+//    }
38
+//
39
+//    @Override
40
+//    public HashMap<String, Object> toArray() {
41
+//        HashMap<String, Object> map = new HashMap<>();
42
+//        map.put("stop", _stop.toArray());
43
+//        List<Object> routes = new Vector<>();
44
+//        for (RoutesDbo route : _routes) {
45
+//            routes.add(route.toArray());
46
+//        }
47
+//        map.put("routes", routes);
48
+//        map.put("name", _name);
49
+//        return map;
50
+//    }
51
 
51
 
52
     public StopsDbo getStop() {
52
     public StopsDbo getStop() {
53
         return _stop;
53
         return _stop;

+ 21
- 21
app/src/main/java/com/rthoni/stssaguenay/dbo/UsersSettingsDbo.java View File

17
 public class UsersSettingsDbo extends LuDbo {
17
 public class UsersSettingsDbo extends LuDbo {
18
     protected List<UserFavouriteStopsDbo> _favouriteStops;
18
     protected List<UserFavouriteStopsDbo> _favouriteStops;
19
 
19
 
20
-    @Override
21
-    public void fromJson(JSONObject json) throws JSONException {
22
-        _favouriteStops = new Vector<>();
23
-        JSONArray stops = json.getJSONArray("favouriteStops");
24
-        for (int i = 0; i < stops.length(); ++i) {
25
-            UserFavouriteStopsDbo stop = new UserFavouriteStopsDbo();
26
-            stop.fromJson(stops.getJSONObject(i));
27
-            _favouriteStops.add(stop);
28
-        }
29
-    }
30
-
31
-    @Override
32
-    public HashMap<String, Object> toArray() {
33
-        HashMap<String, Object> map = new HashMap<>();
34
-        List<HashMap<String, Object>> stops = new Vector<>();
35
-        for (UserFavouriteStopsDbo stop : _favouriteStops) {
36
-            stops.add(stop.toArray());
37
-        }
38
-        map.put("favouriteStops", stops);
39
-        return map;
40
-    }
20
+//    @Override
21
+//    public void fromJson(JSONObject json) throws JSONException {
22
+//        _favouriteStops = new Vector<>();
23
+//        JSONArray stops = json.getJSONArray("favouriteStops");
24
+//        for (int i = 0; i < stops.length(); ++i) {
25
+//            UserFavouriteStopsDbo stop = new UserFavouriteStopsDbo();
26
+//            stop.fromJson(stops.getJSONObject(i));
27
+//            _favouriteStops.add(stop);
28
+//        }
29
+//    }
30
+//
31
+//    @Override
32
+//    public HashMap<String, Object> toArray() {
33
+//        HashMap<String, Object> map = new HashMap<>();
34
+//        List<HashMap<String, Object>> stops = new Vector<>();
35
+//        for (UserFavouriteStopsDbo stop : _favouriteStops) {
36
+//            stops.add(stop.toArray());
37
+//        }
38
+//        map.put("favouriteStops", stops);
39
+//        return map;
40
+//    }
41
 
41
 
42
     public List<UserFavouriteStopsDbo> getFavouriteStops() {
42
     public List<UserFavouriteStopsDbo> getFavouriteStops() {
43
         return _favouriteStops;
43
         return _favouriteStops;

+ 1
- 1
app/src/main/java/com/rthoni/stssaguenay/ui/activities/MainActivity.java View File

150
                                     UserDbo userDbo = new UserDbo();
150
                                     UserDbo userDbo = new UserDbo();
151
                                     userDbo.fromJson(object);
151
                                     userDbo.fromJson(object);
152
                                     onUserLogged(userDbo);
152
                                     onUserLogged(userDbo);
153
-                                } catch (JSONException e) {
153
+                                } catch (Exception e) {
154
                                     e.printStackTrace();
154
                                     e.printStackTrace();
155
                                 }
155
                                 }
156
                             }
156
                             }

+ 1
- 0
build.gradle View File

15
         classpath 'com.android.tools.build:gradle:2.2.2'
15
         classpath 'com.android.tools.build:gradle:2.2.2'
16
         classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
16
         classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
17
         classpath 'com.google.gms:google-services:3.0.0'
17
         classpath 'com.google.gms:google-services:3.0.0'
18
+        classpath 'de.mobilej.unmock:UnMockPlugin:0.5.0'
18
 
19
 
19
         // NOTE: Do not place your application dependencies here; they belong
20
         // NOTE: Do not place your application dependencies here; they belong
20
         // in the individual module build.gradle files
21
         // in the individual module build.gradle files

+ 9
- 1
luticateutils/build.gradle View File

1
 apply plugin: 'com.android.library'
1
 apply plugin: 'com.android.library'
2
+apply plugin: 'de.mobilej.unmock'
2
 
3
 
3
 android {
4
 android {
4
     compileSdkVersion 22
5
     compileSdkVersion 22
18
     }
19
     }
19
 }
20
 }
20
 
21
 
22
+unMock {
23
+    // URI to download the android-all.jar from. e.g. https://oss.sonatype.org/content/groups/public/org/robolectric/android-all/
24
+    downloadFrom 'https://oss.sonatype.org/content/groups/public/org/robolectric/android-all/4.3_r2-robolectric-0/android-all-4.3_r2-robolectric-0.jar'
25
+
26
+    keepStartingWith "org.json."
27
+}
28
+
21
 dependencies {
29
 dependencies {
22
     compile fileTree(dir: 'libs', include: ['*.jar'])
30
     compile fileTree(dir: 'libs', include: ['*.jar'])
23
     testCompile 'junit:junit:4.12'
31
     testCompile 'junit:junit:4.12'
24
-    testCompile 'org.json:json:20160810'
25
     compile 'com.android.volley:volley:1.0.0'
32
     compile 'com.android.volley:volley:1.0.0'
33
+    compile 'net.danlew:android.joda:2.9.4.2'
26
 }
34
 }

+ 43
- 15
luticateutils/src/main/java/com/luticate/utils/dbo/LuDbo.java View File

4
 import com.luticate.utils.dbo.JSONContainer.LuJSONArrayContainer;
4
 import com.luticate.utils.dbo.JSONContainer.LuJSONArrayContainer;
5
 import com.luticate.utils.dbo.JSONContainer.LuJSONObjectContainer;
5
 import com.luticate.utils.dbo.JSONContainer.LuJSONObjectContainer;
6
 
6
 
7
+import org.joda.time.DateTime;
8
+import org.joda.time.LocalDate;
9
+import org.joda.time.LocalDateTime;
10
+import org.joda.time.format.DateTimeFormat;
7
 import org.json.JSONArray;
11
 import org.json.JSONArray;
12
+import org.json.JSONException;
8
 import org.json.JSONObject;
13
 import org.json.JSONObject;
9
 
14
 
10
 import java.lang.reflect.Array;
15
 import java.lang.reflect.Array;
21
  */
26
  */
22
 public abstract class LuDbo {
27
 public abstract class LuDbo {
23
 
28
 
24
-    public void fromString(String data) throws Exception
25
-    {
26
-        JSONObject json = new JSONObject(data);
27
-        fromJson(json);
28
-    }
29
+    public static String DEFAULT_DATE_FORMAT = "yy-MM-dd";
30
+
31
+    public static String DEFAULT_TIME_FORMAT = "HH:mm:ss";
32
+
33
+    public static String DEFAULT_DATE_TIME_FORMAT = DEFAULT_DATE_FORMAT + " " + DEFAULT_TIME_FORMAT;
29
 
34
 
30
     public String getFieldFromJson(String jsonName)
35
     public String getFieldFromJson(String jsonName)
31
     {
36
     {
75
         if (clazz == String.class) {
80
         if (clazz == String.class) {
76
             return json.getString(key);
81
             return json.getString(key);
77
         }
82
         }
83
+        if (clazz == LocalDateTime.class) {
84
+            return LocalDateTime.parse(json.getString(key), DateTimeFormat.forPattern(DEFAULT_DATE_TIME_FORMAT));
85
+        }
86
+        if (clazz == DateTime.class) {
87
+            return DateTime.parse(json.getString(key), DateTimeFormat.forPattern(DEFAULT_DATE_TIME_FORMAT));
88
+        }
78
         if (LuDbo.class.isAssignableFrom(clazz)) {
89
         if (LuDbo.class.isAssignableFrom(clazz)) {
79
             LuDbo dbo = (LuDbo) getInstance(clazz);
90
             LuDbo dbo = (LuDbo) getInstance(clazz);
80
             dbo.fromJson(json.getJSONObject(key));
91
             dbo.fromJson(json.getJSONObject(key));
97
                 || clazz == String.class) {
108
                 || clazz == String.class) {
98
             return obj;
109
             return obj;
99
         }
110
         }
111
+        if (clazz == LocalDateTime.class) {
112
+            return ((LocalDateTime)obj).toString(DateTimeFormat.forPattern(DEFAULT_DATE_TIME_FORMAT));
113
+        }
114
+        if (clazz == DateTime.class) {
115
+            return ((DateTime)obj).toString(DateTimeFormat.forPattern(DEFAULT_DATE_TIME_FORMAT));
116
+        }
100
         if (LuDbo.class.isAssignableFrom(clazz)) {
117
         if (LuDbo.class.isAssignableFrom(clazz)) {
101
-            return ((LuDbo)obj).toArray();
118
+            return ((LuDbo)obj).toJson();
102
         }
119
         }
103
         if (List.class.isAssignableFrom(clazz)) {
120
         if (List.class.isAssignableFrom(clazz)) {
104
             List objList = (List) obj;
121
             List objList = (List) obj;
105
-            List list = new Vector();
122
+            JSONArray list = new JSONArray();
106
 
123
 
107
             for (Object item : objList) {
124
             for (Object item : objList) {
108
-                list.add(getJsonFromObject(getClazz(item), item));
125
+                list.put(getJsonFromObject(getClazz(item), item));
109
             }
126
             }
110
 
127
 
111
             return list;
128
             return list;
112
         }
129
         }
113
         if (AbstractMap.class.isAssignableFrom(clazz)) {
130
         if (AbstractMap.class.isAssignableFrom(clazz)) {
114
             HashMap objMap = (HashMap) obj;
131
             HashMap objMap = (HashMap) obj;
115
-            HashMap map = new HashMap();
132
+            JSONObject map = new JSONObject();
116
 
133
 
117
             for (Object k : objMap.keySet()) {
134
             for (Object k : objMap.keySet()) {
118
                 if (k.getClass() == String.class) {
135
                 if (k.getClass() == String.class) {
119
                     Object item = objMap.get(k);
136
                     Object item = objMap.get(k);
120
-                    map.put(k, getJsonFromObject(getClazz(item), item));
137
+                    try {
138
+                        map.put((String) k, getJsonFromObject(getClazz(item), item));
139
+                    } catch (JSONException ignored) {
140
+                    }
121
                 }
141
                 }
122
             }
142
             }
123
 
143
 
124
             return map;
144
             return map;
125
         }
145
         }
126
         if (clazz.isArray()) {
146
         if (clazz.isArray()) {
127
-            List list = new Vector();
147
+            JSONArray list = new JSONArray();
128
 
148
 
129
             for (int i = 0; i < Array.getLength(obj); ++i) {
149
             for (int i = 0; i < Array.getLength(obj); ++i) {
130
                 Object item = Array.get(obj, i);
150
                 Object item = Array.get(obj, i);
131
-                list.add(getJsonFromObject(item == null ? null : item.getClass(), item));
151
+                list.put(getJsonFromObject(item == null ? null : item.getClass(), item));
132
             }
152
             }
133
 
153
 
134
             return list;
154
             return list;
151
         return obj == null ? null : obj.getClass();
171
         return obj == null ? null : obj.getClass();
152
     }
172
     }
153
 
173
 
174
+    public void fromString(String data) throws Exception
175
+    {
176
+        JSONObject json = new JSONObject(data);
177
+        fromJson(json);
178
+    }
179
+
154
     public void fromJson(JSONObject json) throws Exception
180
     public void fromJson(JSONObject json) throws Exception
155
     {
181
     {
156
         for (Field field : getClass().getDeclaredFields()) {
182
         for (Field field : getClass().getDeclaredFields()) {
214
             Object obj = null;
240
             Object obj = null;
215
             try {
241
             try {
216
                 obj = field.get(this);
242
                 obj = field.get(this);
217
-            } catch (IllegalAccessException e) {
218
-                e.printStackTrace();
243
+            } catch (IllegalAccessException ignored) {
219
             }
244
             }
220
             field.setAccessible(false);
245
             field.setAccessible(false);
221
             map.put(key, getJsonFromObject(clazz, obj));
246
             map.put(key, getJsonFromObject(clazz, obj));
226
 
251
 
227
     public JSONObject toJson()
252
     public JSONObject toJson()
228
     {
253
     {
229
-        return new JSONObject(toArray());
254
+        HashMap<String, Object> array = toArray();
255
+        JSONObject json = new JSONObject(array);
256
+        return json;
230
     }
257
     }
231
 
258
 
232
     @Override
259
     @Override
234
     {
261
     {
235
         return toJson().toString();
262
         return toJson().toString();
236
     }
263
     }
264
+
237
 }
265
 }

+ 26
- 1
luticateutils/src/test/java/com/luticate/utils/TestDbo.java View File

45
     protected HashMap<String, Integer> _hashMap;
45
     protected HashMap<String, Integer> _hashMap;
46
     protected AbstractMap<String, Test2Dbo> _hashMap2;
46
     protected AbstractMap<String, Test2Dbo> _hashMap2;
47
 
47
 
48
+    protected String _aNullString;
49
+    protected Class _object = TestDbo.class;
50
+
48
     public byte getByte() {
51
     public byte getByte() {
49
         return _byte;
52
         return _byte;
50
     }
53
     }
245
         _hashMap2 = hashMap2;
248
         _hashMap2 = hashMap2;
246
     }
249
     }
247
 
250
 
251
+    public String getaNullString() {
252
+        return _aNullString;
253
+    }
254
+
255
+    public void setaNullString(String aNullString) {
256
+        _aNullString = aNullString;
257
+    }
258
+
259
+    public Class getObject() {
260
+        return _object;
261
+    }
262
+
263
+    public void setObject(Class object) {
264
+        _object = object;
265
+    }
266
+
248
     @Override
267
     @Override
249
     public boolean equals(Object o) {
268
     public boolean equals(Object o) {
250
         if (this == o) return true;
269
         if (this == o) return true;
291
         if (!Arrays.equals(_dbos, testDbo._dbos)) return false;
310
         if (!Arrays.equals(_dbos, testDbo._dbos)) return false;
292
         if (_hashMap != null ? !_hashMap.equals(testDbo._hashMap) : testDbo._hashMap != null)
311
         if (_hashMap != null ? !_hashMap.equals(testDbo._hashMap) : testDbo._hashMap != null)
293
             return false;
312
             return false;
294
-        return _hashMap2 != null ? _hashMap2.equals(testDbo._hashMap2) : testDbo._hashMap2 == null;
313
+        if (_hashMap2 != null ? !_hashMap2.equals(testDbo._hashMap2) : testDbo._hashMap2 != null)
314
+            return false;
315
+        if (_aNullString != null ? !_aNullString.equals(testDbo._aNullString) : testDbo._aNullString != null)
316
+            return false;
317
+        return _object != null ? _object.equals(testDbo._object) : testDbo._object == null;
295
 
318
 
296
     }
319
     }
297
 
320
 
325
         result = 31 * result + Arrays.hashCode(_dbos);
348
         result = 31 * result + Arrays.hashCode(_dbos);
326
         result = 31 * result + (_hashMap != null ? _hashMap.hashCode() : 0);
349
         result = 31 * result + (_hashMap != null ? _hashMap.hashCode() : 0);
327
         result = 31 * result + (_hashMap2 != null ? _hashMap2.hashCode() : 0);
350
         result = 31 * result + (_hashMap2 != null ? _hashMap2.hashCode() : 0);
351
+        result = 31 * result + (_aNullString != null ? _aNullString.hashCode() : 0);
352
+        result = 31 * result + (_object != null ? _object.hashCode() : 0);
328
         return result;
353
         return result;
329
     }
354
     }
330
 }
355
 }

+ 9
- 3
luticateutils/src/test/java/com/luticate/utils/TestDboTest.java View File

71
             "\"aString\": \"42 24\"," +
71
             "\"aString\": \"42 24\"," +
72
             "\"aInt\": 4224" +
72
             "\"aInt\": 4224" +
73
             "}" +
73
             "}" +
74
-            "}" +
74
+            "}," +
75
+
76
+            "\"aNullString\": null," +
77
+
78
+            "\"object\": {}" +
75
 
79
 
76
             "}";
80
             "}";
77
 
81
 
138
         assertEquals("42 24", test.getHashMap2().get("an object").getaString());
142
         assertEquals("42 24", test.getHashMap2().get("an object").getaString());
139
         assertEquals(4224, test.getHashMap2().get("an object").getaInt());
143
         assertEquals(4224, test.getHashMap2().get("an object").getaInt());
140
 
144
 
145
+        assertNull(test.getaNullString());
146
+        assertNull(test.getObject());
147
+
141
         assertEquals(test, test);
148
         assertEquals(test, test);
142
     }
149
     }
143
 
150
 
166
     {
173
     {
167
         TestDbo test = new TestDbo();
174
         TestDbo test = new TestDbo();
168
         test.fromString(TEST_DBO_JSON);
175
         test.fromString(TEST_DBO_JSON);
169
-        JSONObject json = test.toJson();
170
 
176
 
171
         TestDbo test2 = new TestDbo();
177
         TestDbo test2 = new TestDbo();
172
-        test2.fromString(json.toString());
178
+        test2.fromString(test.toString());
173
 
179
 
174
         testTestDbo(test2);
180
         testTestDbo(test2);
175
     }
181
     }

Loading…
Cancel
Save