Last active
August 29, 2015 14:22
-
-
Save shkschneider/69828ef31fe963d589a6 to your computer and use it in GitHub Desktop.
GsonParsor.getJsonElement(JsonObject, String)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Uses GSON library | |
// getJsonElement(jsonObject, "{errors}{email}[reasons:0]") | |
// Retrieves the first JsonElement of the JsonArray "reasons" of the JsonObject "email" of the JsonObject "errors"... in one line ;) | |
public static JsonElement getJsonElement(@NonNull final JsonObject jsonObject, final String string) { | |
// matches all | |
if (TextUtils.isEmpty(string)) { | |
return jsonObject; | |
} | |
// bad formats | |
if (string.length() - string.replace("\\{", "").length() != string.length() - string.replace("\\}", "").length()) { | |
Log.e(TAG, "Bad {} format: " + string); | |
return null; | |
} | |
if (string.length() - string.replace("\\[", "").length() != string.length() - string.replace("\\]", "").length()) { | |
Log.e(TAG, "Bad [] format: " + string); | |
return null; | |
} | |
if (! string.matches("^([\\{|\\[][^\\{|\\[|\\}|\\]]+(:[0-9]+)?[\\}|\\]])+$")) { | |
Log.e(TAG, "Bad format: " + string); | |
return null; | |
} | |
// tags and paths | |
final Matcher matcherTags = Pattern.compile("([\\{|\\[])").matcher(string); | |
final List<String> tags = new ArrayList<>(); | |
while (matcherTags.find()) { | |
tags.add(matcherTags.group()); | |
} | |
Log.v(TAG, "tags: " + tags.toString()); | |
final Matcher matcherPaths = Pattern.compile("[\\{|\\[]([^\\{|\\[|\\}|\\]]+)[\\}|\\]]").matcher(string); | |
final List<String> paths = new ArrayList<>(); | |
while (matcherPaths.find()) { | |
paths.add(matcherPaths.group(1)); | |
} | |
Log.v(TAG, "paths: " + paths.toString()); | |
// loop | |
JsonObject object = jsonObject; | |
for (int i = 0; i < tags.size(); i++) { | |
// b triggers the end | |
final boolean b = (i == (tags.size() - 1)); | |
final String t = tags.get(i); | |
final String p = paths.get(i); | |
// forced end | |
if (object.isJsonNull()) { | |
Log.w(TAG, "Reached a JsonNull"); | |
return null; | |
} | |
// object | |
if (t.equals("{")) { | |
if (! object.has(p)) { | |
Log.e(TAG, "No such object path: " + p); | |
return null; | |
} | |
final JsonElement jsonElement = object.get(p); | |
// found the result | |
if (b) { | |
return object.get(p); | |
} | |
// keep going so check the type | |
if (! jsonElement.isJsonObject()) { | |
Log.e(TAG, "Invalid type (not JsonObject): " + p); | |
return null; | |
} | |
// prepare next loop | |
object = object.getAsJsonObject(p); | |
} | |
// array | |
else if (t.equals("[")) { | |
final String[] d = p.split(":"); | |
// without index | |
if (d.length == 1) { | |
final String s = d[0]; | |
if (! object.has(s)) { | |
Log.e(TAG, "No such array path: " + p); | |
return null; | |
} | |
// has to be an array | |
if (! object.isJsonArray()) { | |
Log.e(TAG, "Invalid type (not JsonArray): " + p); | |
return null; | |
} | |
// returns as nothing more can be done | |
return object.getAsJsonArray(s); | |
} | |
// with index | |
else if (d.length == 2) { | |
final String s = d[0]; | |
if (! object.has(s)) { | |
Log.e(TAG, "No such array path: " + p); | |
return null; | |
} | |
if (! TextUtils.isDigitsOnly(d[1])) { | |
Log.e(TAG, "Invalid index: " + d[1]); | |
return null; | |
} | |
final int n = Integer.valueOf(d[1]); | |
if (n >= d.length) { | |
Log.e(TAG, "Invalid index: " + n); | |
return null; | |
} | |
final JsonElement jsonElement = object.get(s); | |
final JsonArray array = jsonElement.getAsJsonArray(); | |
// found the result | |
if (b) { | |
return array.get(n); | |
} | |
// keep going so check the type | |
if (! jsonElement.isJsonArray()) { | |
Log.e(TAG, "Invalid type (not JsonArray): " + s); | |
return null; | |
} | |
// prepare next loop | |
object = array.get(n).getAsJsonObject(); | |
} | |
else { | |
Log.e(TAG, "Bad format: " + p); | |
return null; | |
} | |
} | |
} | |
return object; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment