|
8 | 8 | import org.wordpress.android.util.AppLog.T; |
9 | 9 |
|
10 | 10 | import java.util.ArrayList; |
11 | | -import java.util.Iterator; |
12 | 11 |
|
13 | 12 | public class JSONUtils { |
14 | | - private static String QUERY_SEPERATOR="."; |
15 | | - private static String QUERY_ARRAY_INDEX_START="["; |
16 | | - private static String QUERY_ARRAY_INDEX_END="]"; |
17 | | - private static String QUERY_ARRAY_FIRST="first"; |
18 | | - private static String QUERY_ARRAY_LAST="last"; |
| 13 | + private static String QUERY_SEPERATOR = "."; |
| 14 | + private static String QUERY_ARRAY_INDEX_START = "["; |
| 15 | + private static String QUERY_ARRAY_INDEX_END = "]"; |
| 16 | + private static String QUERY_ARRAY_FIRST = "first"; |
| 17 | + private static String QUERY_ARRAY_LAST = "last"; |
19 | 18 |
|
20 | 19 | private static final String JSON_NULL_STR = "null"; |
| 20 | + private static final String TAG = "JSONUtils"; |
21 | 21 |
|
22 | | - private static final String TAG="JSONUtils"; |
23 | 22 | /** |
24 | 23 | * Given a JSONObject and a key path (e.g property.child) and a default it will |
25 | 24 | * traverse the object graph and pull out the desired property |
26 | 25 | */ |
27 | 26 | public static <U> U queryJSON(JSONObject source, String query, U defaultObject) { |
| 27 | + if (source == null) { |
| 28 | + AppLog.e(T.UTILS, "Parameter source is null, can't query a null object"); |
| 29 | + return defaultObject; |
| 30 | + } |
| 31 | + if (query == null) { |
| 32 | + AppLog.e(T.UTILS, "Parameter query is null"); |
| 33 | + return defaultObject; |
| 34 | + } |
28 | 35 | int nextSeperator = query.indexOf(QUERY_SEPERATOR); |
29 | 36 | int nextIndexStart = query.indexOf(QUERY_ARRAY_INDEX_START); |
30 | 37 | if (nextSeperator == -1 && nextIndexStart == -1) { |
@@ -56,9 +63,6 @@ public static <U> U queryJSON(JSONObject source, String query, U defaultObject) |
56 | 63 | String nextQuery = query.substring(endQuery); |
57 | 64 | String key = query.substring(0, endQuery); |
58 | 65 | try { |
59 | | - if (source == null) { |
60 | | - return defaultObject; |
61 | | - } |
62 | 66 | if (nextQuery.indexOf(QUERY_SEPERATOR) == 0) { |
63 | 67 | return queryJSON(source.getJSONObject(key), nextQuery.substring(1), defaultObject); |
64 | 68 | } else if (nextQuery.indexOf(QUERY_ARRAY_INDEX_START) == 0) { |
@@ -89,7 +93,15 @@ public static <U> U queryJSON(JSONObject source, String query, U defaultObject) |
89 | 93 | * Acceptable indexes include negative numbers to reference items from the end of |
90 | 94 | * the list as well as "last" and "first" as more explicit references to "0" and "-1" |
91 | 95 | */ |
92 | | - public static <U> U queryJSON(JSONArray source, String query, U defaultObject){ |
| 96 | + public static <U> U queryJSON(JSONArray source, String query, U defaultObject) { |
| 97 | + if (source == null) { |
| 98 | + AppLog.e(T.UTILS, "Parameter source is null, can't query a null object"); |
| 99 | + return defaultObject; |
| 100 | + } |
| 101 | + if (query == null) { |
| 102 | + AppLog.e(T.UTILS, "Parameter query is null"); |
| 103 | + return defaultObject; |
| 104 | + } |
93 | 105 | // query must start with [ have an index and then have ] |
94 | 106 | int indexStart = query.indexOf(QUERY_ARRAY_INDEX_START); |
95 | 107 | int indexEnd = query.indexOf(QUERY_ARRAY_INDEX_END); |
|
0 commit comments