aboutsummaryrefslogtreecommitdiff
path: root/libs/utils/WordPressUtils/src/main/java/org/wordpress/android/util/JSONUtil.java
blob: 199fba703db03a2ce62a821058f960a8e9357acd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package org.wordpress.android.util;

import android.text.TextUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.wordpress.android.util.AppLog.T;

import java.util.ArrayList;

public class JSONUtil {
    private static String QUERY_SEPERATOR=".";
    private static String QUERY_ARRAY_INDEX_START="[";
    private static String QUERY_ARRAY_INDEX_END="]";
    private static String QUERY_ARRAY_FIRST="first";
    private static String QUERY_ARRAY_LAST="last";

    private static final String JSON_NULL_STR = "null";

    private static final String TAG="JSONUtil";
    /**
     * Given a JSONObject and a key path (e.g property.child) and a default it will
     * traverse the object graph and pull out the desired property
     */
    public static <U> U queryJSON(JSONObject source, String query, U defaultObject) {
        int nextSeperator = query.indexOf(QUERY_SEPERATOR);
        int nextIndexStart = query.indexOf(QUERY_ARRAY_INDEX_START);
        if (nextSeperator == -1 && nextIndexStart == -1) {
            // last item let's get it
            try {
                if (!source.has(query)) {
                    return defaultObject;
                }
                Object result = source.get(query);
                if (result.getClass().isAssignableFrom(defaultObject.getClass())) {
                    return (U) result;
                } else {
                    return defaultObject;
                }
            } catch (java.lang.ClassCastException e) {
                AppLog.e(T.UTILS, "Unable to cast the object to " + defaultObject.getClass().getName(), e);
                return defaultObject;
            } catch (JSONException e) {
                AppLog.e(T.UTILS, "Unable to get the Key from the input object. Key:" + query, e);
                return defaultObject;
            }
        }
        int endQuery;
        if (nextSeperator == -1 || nextIndexStart == -1) {
            endQuery = Math.max(nextSeperator, nextIndexStart);
        } else {
            endQuery = Math.min(nextSeperator, nextIndexStart);
        }
        String nextQuery = query.substring(endQuery);
        String key = query.substring(0, endQuery);
        try {
            if (source == null) {
                return defaultObject;
            }
            if (nextQuery.indexOf(QUERY_SEPERATOR) == 0) {
                return queryJSON(source.getJSONObject(key), nextQuery.substring(1), defaultObject);
            } else if (nextQuery.indexOf(QUERY_ARRAY_INDEX_START) == 0) {
                return queryJSON(source.getJSONArray(key), nextQuery, defaultObject);
            } else if (!nextQuery.equals("")) {
                return defaultObject;
            }
            Object result = source.get(key);
            if (result.getClass().isAssignableFrom(defaultObject.getClass())) {
                return (U) result;
            } else {
                AppLog.w(T.UTILS, String.format("The returned object type %s is not assignable to the type %s. Using default!",
                        result.getClass(),defaultObject.getClass()));
                return defaultObject;
            }
        } catch (java.lang.ClassCastException e) {
            AppLog.e(T.UTILS, "Unable to cast the object to " + defaultObject.getClass().getName(), e);
            return defaultObject;
        } catch (JSONException e) {
            AppLog.e(T.UTILS, "Unable to get the Key from the input object. Key:" + query, e);
            return defaultObject;
        }
    }

    /**
     * Given a JSONArray and a query (e.g. [0].property) it will traverse the array and
     * pull out the requested property.
     *
     * Acceptable indexes include negative numbers to reference items from the end of
     * the list as well as "last" and "first" as more explicit references to "0" and "-1"
     */
    public static <U> U queryJSON(JSONArray source, String query, U defaultObject){
        // query must start with [ have an index and then have ]
        int indexStart = query.indexOf(QUERY_ARRAY_INDEX_START);
        int indexEnd = query.indexOf(QUERY_ARRAY_INDEX_END);
        if (indexStart == -1 || indexEnd == -1 || indexStart > indexEnd) {
            return defaultObject;
        }
        // get "index" from "[index]"
        String indexStr = query.substring(indexStart + 1, indexEnd);
        int index;
        if (indexStr.equals(QUERY_ARRAY_FIRST)) {
            index = 0;
        } else if (indexStr.equals(QUERY_ARRAY_LAST)) {
            index = -1;
        } else {
            index = Integer.parseInt(indexStr);
        }
        if (index < 0) {
            index = source.length() + index;
        }
        // copy remaining query
        String remainingQuery = query.substring(indexEnd + 1);
        try {
            if (remainingQuery.indexOf(QUERY_ARRAY_INDEX_START) == 0) {
                return queryJSON(source.getJSONArray(index), remainingQuery, defaultObject);
            } else if (remainingQuery.indexOf(QUERY_SEPERATOR) == 0) {
                return queryJSON(source.getJSONObject(index), remainingQuery.substring(1), defaultObject);
            } else if (!remainingQuery.equals("")) {
                // TODO throw an exception since the query isn't valid?
                AppLog.w(T.UTILS, String.format("Incorrect query for next object %s", remainingQuery));
                return defaultObject;
            }
            Object result = source.get(index);
            if (result.getClass().isAssignableFrom(defaultObject.getClass())) {
                return (U) result;
            } else {
                AppLog.w(T.UTILS, String.format("The returned object type %s is not assignable to the type %s. Using default!",
                        result.getClass(),defaultObject.getClass()));
                return defaultObject;
            }
        } catch (java.lang.ClassCastException e) {
            AppLog.e(T.UTILS, "Unable to cast the object to "+defaultObject.getClass().getName(), e);
            return defaultObject;
        } catch (JSONException e) {
            AppLog.e(T.UTILS, "Unable to get the Key from the input object. Key:" + query, e);
            return defaultObject;
        }
    }

    /**
     * Convert a JSONArray (expected to contain strings) in a string list
     */
    public static ArrayList<String> fromJSONArrayToStringList(JSONArray jsonArray) {
        ArrayList<String> stringList = new ArrayList<String>();
        for (int i = 0; i < jsonArray.length(); i++) {
            try {
                stringList.add(jsonArray.getString(i));
            } catch (JSONException e) {
                AppLog.e(T.UTILS, e);
            }
        }
        return stringList;
    }

    /**
     * Convert a string list in a JSONArray
     */
    public static JSONArray fromStringListToJSONArray(ArrayList<String> stringList) {
        JSONArray jsonArray = new JSONArray();
        if (stringList != null) {
            for (int i = 0; i < stringList.size(); i++) {
                jsonArray.put(stringList.get(i));
            }
        }
        return jsonArray;
    }

    /*
     * wrapper for JSONObject.optString() which handles "null" values
     */
    public static String getString(JSONObject json, String name) {
        String value = json.optString(name);
        // return empty string for "null"
        if (JSON_NULL_STR.equals(value))
            return "";
        return value;
    }

    /*
     * use with strings that contain HTML entities
     */
    public static String getStringDecoded(JSONObject json, String name) {
        String value = getString(json, name);
        return HtmlUtils.fastUnescapeHtml(value);
    }

    /*
     * replacement for JSONObject.optBoolean()  - optBoolean() only checks for "true" and "false",
     * but our API sometimes uses "0" to denote false
     */
    public static boolean getBool(JSONObject json, String name) {
        String value = getString(json, name);
        if (TextUtils.isEmpty(value))
            return false;
        if (value.equals("0"))
            return false;
        if (value.equalsIgnoreCase("false"))
            return false;
        return true;
    }

    /*
     * returns the JSONObject child of the passed parent that matches the passed query
     * this is basically an "optJSONObject" that supports nested queries, for example:
     *
     *  getJSONChild("meta/data/site")
     *
     * would find this:
     *
     *  "meta": {
     *       "data": {
     *           "site": {
     *                "ID": 3584907,
     *                "name": "WordPress.com News",
     *           }
     *       }
     *   }
     */
    public static JSONObject getJSONChild(final JSONObject jsonParent, final String query) {
        if (jsonParent == null || TextUtils.isEmpty(query))
            return null;
        String[] names = query.split("/");
        JSONObject jsonChild = null;
        for (int i = 0; i < names.length; i++) {
            if (jsonChild == null) {
                jsonChild = jsonParent.optJSONObject(names[i]);
            } else {
                jsonChild = jsonChild.optJSONObject(names[i]);
            }
            if (jsonChild == null)
                return null;
        }
        return jsonChild;
    }
}