Skip to content

Commit

Permalink
Merge branch 'release/4.6' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyr59h committed Sep 30, 2015
2 parents 07511b7 + 5f1a47c commit 82f2229
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.wordpress.android.util;

import android.test.InstrumentationTestCase;

import org.json.JSONArray;
import org.json.JSONObject;

public class JSONUtilsTest extends InstrumentationTestCase {
public void testQueryJSONNullSource1() {
JSONUtils.queryJSON((JSONObject) null, "", "");
}

public void testQueryJSONNullSource2() {
JSONUtils.queryJSON((JSONArray) null, "", "");
}

public void testQueryJSONNullQuery1() {
JSONUtils.queryJSON(new JSONObject(), null, "");
}

public void testQueryJSONNullQuery2() {
JSONUtils.queryJSON(new JSONArray(), null, "");
}

public void testQueryJSONNullReturnValue1() {
JSONUtils.queryJSON(new JSONObject(), "", null);
}

public void testQueryJSONNullReturnValue2() {
JSONUtils.queryJSON(new JSONArray(), "", null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,30 @@
import org.wordpress.android.util.AppLog.T;

import java.util.ArrayList;
import java.util.Iterator;

public class JSONUtils {
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 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 = "JSONUtils";

private static final String TAG="JSONUtils";
/**
* 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) {
if (source == null) {
AppLog.e(T.UTILS, "Parameter source is null, can't query a null object");
return defaultObject;
}
if (query == null) {
AppLog.e(T.UTILS, "Parameter query is null");
return defaultObject;
}
int nextSeperator = query.indexOf(QUERY_SEPERATOR);
int nextIndexStart = query.indexOf(QUERY_ARRAY_INDEX_START);
if (nextSeperator == -1 && nextIndexStart == -1) {
Expand Down Expand Up @@ -56,9 +63,6 @@ public static <U> U queryJSON(JSONObject source, String query, U defaultObject)
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) {
Expand Down Expand Up @@ -89,7 +93,15 @@ public static <U> U queryJSON(JSONObject source, String query, U defaultObject)
* 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){
public static <U> U queryJSON(JSONArray source, String query, U defaultObject) {
if (source == null) {
AppLog.e(T.UTILS, "Parameter source is null, can't query a null object");
return defaultObject;
}
if (query == null) {
AppLog.e(T.UTILS, "Parameter query is null");
return 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);
Expand Down

0 comments on commit 82f2229

Please sign in to comment.