Skip to content

Commit

Permalink
Check null query string
Browse files Browse the repository at this point in the history
  • Loading branch information
maxme committed Sep 29, 2015
1 parent 1735ccb commit a9b52ce
Showing 1 changed file with 14 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
import java.util.ArrayList;

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
Expand All @@ -28,6 +28,10 @@ public static <U> U queryJSON(JSONObject source, String query, U defaultObject)
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 @@ -59,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 @@ -97,6 +98,10 @@ public static <U> U queryJSON(JSONArray source, String query, U defaultObject) {
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 a9b52ce

Please sign in to comment.