List of usage examples for com.google.gson JsonArray size
public int size()
From source file:com.facebook.ads.sdk.VideoThumbnail.java
License:Open Source License
public static APINodeList<VideoThumbnail> parseResponse(String json, APIContext context, APIRequest request) throws MalformedResponseException { APINodeList<VideoThumbnail> videoThumbnails = new APINodeList<VideoThumbnail>(request, json); JsonArray arr; JsonObject obj;//from ww w. j a va2 s. com JsonParser parser = new JsonParser(); Exception exception = null; try { JsonElement result = parser.parse(json); if (result.isJsonArray()) { // First, check if it's a pure JSON Array arr = result.getAsJsonArray(); for (int i = 0; i < arr.size(); i++) { videoThumbnails.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; return videoThumbnails; } else if (result.isJsonObject()) { obj = result.getAsJsonObject(); if (obj.has("data")) { if (obj.has("paging")) { JsonObject paging = obj.get("paging").getAsJsonObject().get("cursors").getAsJsonObject(); String before = paging.has("before") ? paging.get("before").getAsString() : null; String after = paging.has("after") ? paging.get("after").getAsString() : null; videoThumbnails.setPaging(before, after); } if (obj.get("data").isJsonArray()) { // Second, check if it's a JSON array with "data" arr = obj.get("data").getAsJsonArray(); for (int i = 0; i < arr.size(); i++) { videoThumbnails.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; } else if (obj.get("data").isJsonObject()) { // Third, check if it's a JSON object with "data" obj = obj.get("data").getAsJsonObject(); boolean isRedownload = false; for (String s : new String[] { "campaigns", "adsets", "ads" }) { if (obj.has(s)) { isRedownload = true; obj = obj.getAsJsonObject(s); for (Map.Entry<String, JsonElement> entry : obj.entrySet()) { videoThumbnails.add(loadJSON(entry.getValue().toString(), context)); } break; } } if (!isRedownload) { videoThumbnails.add(loadJSON(obj.toString(), context)); } } return videoThumbnails; } else if (obj.has("images")) { // Fourth, check if it's a map of image objects obj = obj.get("images").getAsJsonObject(); for (Map.Entry<String, JsonElement> entry : obj.entrySet()) { videoThumbnails.add(loadJSON(entry.getValue().toString(), context)); } return videoThumbnails; } else { // Fifth, check if it's an array of objects indexed by id boolean isIdIndexedArray = true; for (Map.Entry entry : obj.entrySet()) { String key = (String) entry.getKey(); if (key.equals("__fb_trace_id__")) { continue; } JsonElement value = (JsonElement) entry.getValue(); if (value != null && value.isJsonObject() && value.getAsJsonObject().has("id") && value.getAsJsonObject().get("id") != null && value.getAsJsonObject().get("id").getAsString().equals(key)) { videoThumbnails.add(loadJSON(value.toString(), context)); } else { isIdIndexedArray = false; break; } } if (isIdIndexedArray) { return videoThumbnails; } // Sixth, check if it's pure JsonObject videoThumbnails.clear(); videoThumbnails.add(loadJSON(json, context)); return videoThumbnails; } } } catch (Exception e) { exception = e; } throw new MalformedResponseException("Invalid response string: " + json, exception); }
From source file:com.facebook.ads.sdk.WebAppLink.java
License:Open Source License
public static APINodeList<WebAppLink> parseResponse(String json, APIContext context, APIRequest request) throws MalformedResponseException { APINodeList<WebAppLink> webAppLinks = new APINodeList<WebAppLink>(request, json); JsonArray arr; JsonObject obj;//from w w w.jav a 2 s . c o m JsonParser parser = new JsonParser(); Exception exception = null; try { JsonElement result = parser.parse(json); if (result.isJsonArray()) { // First, check if it's a pure JSON Array arr = result.getAsJsonArray(); for (int i = 0; i < arr.size(); i++) { webAppLinks.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; return webAppLinks; } else if (result.isJsonObject()) { obj = result.getAsJsonObject(); if (obj.has("data")) { if (obj.has("paging")) { JsonObject paging = obj.get("paging").getAsJsonObject().get("cursors").getAsJsonObject(); String before = paging.has("before") ? paging.get("before").getAsString() : null; String after = paging.has("after") ? paging.get("after").getAsString() : null; webAppLinks.setPaging(before, after); } if (obj.get("data").isJsonArray()) { // Second, check if it's a JSON array with "data" arr = obj.get("data").getAsJsonArray(); for (int i = 0; i < arr.size(); i++) { webAppLinks.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; } else if (obj.get("data").isJsonObject()) { // Third, check if it's a JSON object with "data" obj = obj.get("data").getAsJsonObject(); boolean isRedownload = false; for (String s : new String[] { "campaigns", "adsets", "ads" }) { if (obj.has(s)) { isRedownload = true; obj = obj.getAsJsonObject(s); for (Map.Entry<String, JsonElement> entry : obj.entrySet()) { webAppLinks.add(loadJSON(entry.getValue().toString(), context)); } break; } } if (!isRedownload) { webAppLinks.add(loadJSON(obj.toString(), context)); } } return webAppLinks; } else if (obj.has("images")) { // Fourth, check if it's a map of image objects obj = obj.get("images").getAsJsonObject(); for (Map.Entry<String, JsonElement> entry : obj.entrySet()) { webAppLinks.add(loadJSON(entry.getValue().toString(), context)); } return webAppLinks; } else { // Fifth, check if it's an array of objects indexed by id boolean isIdIndexedArray = true; for (Map.Entry entry : obj.entrySet()) { String key = (String) entry.getKey(); if (key.equals("__fb_trace_id__")) { continue; } JsonElement value = (JsonElement) entry.getValue(); if (value != null && value.isJsonObject() && value.getAsJsonObject().has("id") && value.getAsJsonObject().get("id") != null && value.getAsJsonObject().get("id").getAsString().equals(key)) { webAppLinks.add(loadJSON(value.toString(), context)); } else { isIdIndexedArray = false; break; } } if (isIdIndexedArray) { return webAppLinks; } // Sixth, check if it's pure JsonObject webAppLinks.clear(); webAppLinks.add(loadJSON(json, context)); return webAppLinks; } } } catch (Exception e) { exception = e; } throw new MalformedResponseException("Invalid response string: " + json, exception); }
From source file:com.facebook.ads.sdk.WindowsAppLink.java
License:Open Source License
public static APINodeList<WindowsAppLink> parseResponse(String json, APIContext context, APIRequest request) throws MalformedResponseException { APINodeList<WindowsAppLink> windowsAppLinks = new APINodeList<WindowsAppLink>(request, json); JsonArray arr; JsonObject obj;/*from www . j a v a 2 s. c om*/ JsonParser parser = new JsonParser(); Exception exception = null; try { JsonElement result = parser.parse(json); if (result.isJsonArray()) { // First, check if it's a pure JSON Array arr = result.getAsJsonArray(); for (int i = 0; i < arr.size(); i++) { windowsAppLinks.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; return windowsAppLinks; } else if (result.isJsonObject()) { obj = result.getAsJsonObject(); if (obj.has("data")) { if (obj.has("paging")) { JsonObject paging = obj.get("paging").getAsJsonObject().get("cursors").getAsJsonObject(); String before = paging.has("before") ? paging.get("before").getAsString() : null; String after = paging.has("after") ? paging.get("after").getAsString() : null; windowsAppLinks.setPaging(before, after); } if (obj.get("data").isJsonArray()) { // Second, check if it's a JSON array with "data" arr = obj.get("data").getAsJsonArray(); for (int i = 0; i < arr.size(); i++) { windowsAppLinks.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; } else if (obj.get("data").isJsonObject()) { // Third, check if it's a JSON object with "data" obj = obj.get("data").getAsJsonObject(); boolean isRedownload = false; for (String s : new String[] { "campaigns", "adsets", "ads" }) { if (obj.has(s)) { isRedownload = true; obj = obj.getAsJsonObject(s); for (Map.Entry<String, JsonElement> entry : obj.entrySet()) { windowsAppLinks.add(loadJSON(entry.getValue().toString(), context)); } break; } } if (!isRedownload) { windowsAppLinks.add(loadJSON(obj.toString(), context)); } } return windowsAppLinks; } else if (obj.has("images")) { // Fourth, check if it's a map of image objects obj = obj.get("images").getAsJsonObject(); for (Map.Entry<String, JsonElement> entry : obj.entrySet()) { windowsAppLinks.add(loadJSON(entry.getValue().toString(), context)); } return windowsAppLinks; } else { // Fifth, check if it's an array of objects indexed by id boolean isIdIndexedArray = true; for (Map.Entry entry : obj.entrySet()) { String key = (String) entry.getKey(); if (key.equals("__fb_trace_id__")) { continue; } JsonElement value = (JsonElement) entry.getValue(); if (value != null && value.isJsonObject() && value.getAsJsonObject().has("id") && value.getAsJsonObject().get("id") != null && value.getAsJsonObject().get("id").getAsString().equals(key)) { windowsAppLinks.add(loadJSON(value.toString(), context)); } else { isIdIndexedArray = false; break; } } if (isIdIndexedArray) { return windowsAppLinks; } // Sixth, check if it's pure JsonObject windowsAppLinks.clear(); windowsAppLinks.add(loadJSON(json, context)); return windowsAppLinks; } } } catch (Exception e) { exception = e; } throw new MalformedResponseException("Invalid response string: " + json, exception); }
From source file:com.facebook.ads.sdk.WindowsPhoneAppLink.java
License:Open Source License
public static APINodeList<WindowsPhoneAppLink> parseResponse(String json, APIContext context, APIRequest request) throws MalformedResponseException { APINodeList<WindowsPhoneAppLink> windowsPhoneAppLinks = new APINodeList<WindowsPhoneAppLink>(request, json); JsonArray arr; JsonObject obj;/*from w w w .j a v a 2 s . co m*/ JsonParser parser = new JsonParser(); Exception exception = null; try { JsonElement result = parser.parse(json); if (result.isJsonArray()) { // First, check if it's a pure JSON Array arr = result.getAsJsonArray(); for (int i = 0; i < arr.size(); i++) { windowsPhoneAppLinks.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; return windowsPhoneAppLinks; } else if (result.isJsonObject()) { obj = result.getAsJsonObject(); if (obj.has("data")) { if (obj.has("paging")) { JsonObject paging = obj.get("paging").getAsJsonObject().get("cursors").getAsJsonObject(); String before = paging.has("before") ? paging.get("before").getAsString() : null; String after = paging.has("after") ? paging.get("after").getAsString() : null; windowsPhoneAppLinks.setPaging(before, after); } if (obj.get("data").isJsonArray()) { // Second, check if it's a JSON array with "data" arr = obj.get("data").getAsJsonArray(); for (int i = 0; i < arr.size(); i++) { windowsPhoneAppLinks.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; } else if (obj.get("data").isJsonObject()) { // Third, check if it's a JSON object with "data" obj = obj.get("data").getAsJsonObject(); boolean isRedownload = false; for (String s : new String[] { "campaigns", "adsets", "ads" }) { if (obj.has(s)) { isRedownload = true; obj = obj.getAsJsonObject(s); for (Map.Entry<String, JsonElement> entry : obj.entrySet()) { windowsPhoneAppLinks.add(loadJSON(entry.getValue().toString(), context)); } break; } } if (!isRedownload) { windowsPhoneAppLinks.add(loadJSON(obj.toString(), context)); } } return windowsPhoneAppLinks; } else if (obj.has("images")) { // Fourth, check if it's a map of image objects obj = obj.get("images").getAsJsonObject(); for (Map.Entry<String, JsonElement> entry : obj.entrySet()) { windowsPhoneAppLinks.add(loadJSON(entry.getValue().toString(), context)); } return windowsPhoneAppLinks; } else { // Fifth, check if it's an array of objects indexed by id boolean isIdIndexedArray = true; for (Map.Entry entry : obj.entrySet()) { String key = (String) entry.getKey(); if (key.equals("__fb_trace_id__")) { continue; } JsonElement value = (JsonElement) entry.getValue(); if (value != null && value.isJsonObject() && value.getAsJsonObject().has("id") && value.getAsJsonObject().get("id") != null && value.getAsJsonObject().get("id").getAsString().equals(key)) { windowsPhoneAppLinks.add(loadJSON(value.toString(), context)); } else { isIdIndexedArray = false; break; } } if (isIdIndexedArray) { return windowsPhoneAppLinks; } // Sixth, check if it's pure JsonObject windowsPhoneAppLinks.clear(); windowsPhoneAppLinks.add(loadJSON(json, context)); return windowsPhoneAppLinks; } } } catch (Exception e) { exception = e; } throw new MalformedResponseException("Invalid response string: " + json, exception); }
From source file:com.facebook.buck.json.RawParser.java
License:Apache License
/** * @return One of: String, Boolean, Long, Number, List<Object>, Map<String, Object>. *//*from w ww .j a va 2 s .c o m*/ @Nullable @VisibleForTesting static Object toRawTypes(JsonElement json) { // Cases are ordered from most common to least common. if (json.isJsonPrimitive()) { JsonPrimitive primitive = json.getAsJsonPrimitive(); if (primitive.isString()) { return interner.intern(primitive.getAsString()); } else if (primitive.isBoolean()) { return primitive.getAsBoolean(); } else if (primitive.isNumber()) { Number number = primitive.getAsNumber(); // Number is likely an instance of class com.google.gson.internal.LazilyParsedNumber. if (number.longValue() == number.doubleValue()) { return number.longValue(); } else { return number; } } else { throw new IllegalStateException("Unknown primitive type: " + primitive); } } else if (json.isJsonArray()) { JsonArray array = json.getAsJsonArray(); List<Object> out = Lists.newArrayListWithCapacity(array.size()); for (JsonElement item : array) { out.add(toRawTypes(item)); } return out; } else if (json.isJsonObject()) { Map<String, Object> out = new LinkedHashMap<>(json.getAsJsonObject().entrySet().size()); for (Map.Entry<String, JsonElement> entry : json.getAsJsonObject().entrySet()) { // On a large project, without invoking intern(), we have seen `buck targets` OOM. When this // happened, according to the .hprof file generated using -XX:+HeapDumpOnOutOfMemoryError, // 39.6% of the memory was spent on char[] objects while 14.5% was spent on Strings. // (Another 10.5% was spent on java.util.HashMap$Entry.) Introducing intern() stopped the // OOM from happening. out.put(interner.intern(entry.getKey()), toRawTypes(entry.getValue())); } return out; } else if (json.isJsonNull()) { return null; } else { throw new IllegalStateException("Unknown type: " + json); } }
From source file:com.flipkart.android.proteus.toolbox.ColorUtils.java
License:Apache License
private static ColorStateList inflateFromJson(Context context, JsonObject jsonObject) { ColorStateList result = null;// w ww.j a v a 2 s . co m JsonElement type = jsonObject.get("type"); if (null != type && type.isJsonPrimitive()) { String colorType = type.getAsString(); if (TextUtils.equals(colorType, "selector")) { JsonElement childrenElement = jsonObject.get("children"); if (null != childrenElement && childrenElement.isJsonArray()) { JsonArray children = childrenElement.getAsJsonArray(); int listAllocated = 20; int listSize = 0; int[] colorList = new int[listAllocated]; int[][] stateSpecList = new int[listAllocated][]; for (int idx = 0; idx < children.size(); idx++) { JsonElement itemObject = children.get(idx); if (!itemObject.isJsonObject()) { continue; } Set<Map.Entry<String, JsonElement>> entrySet = ((JsonObject) itemObject).entrySet(); if (entrySet.size() == 0) { continue; } int j = 0; Integer baseColor = null; float alphaMod = 1.0f; int[] stateSpec = new int[entrySet.size() - 1]; boolean ignoreItem = false; for (Map.Entry<String, JsonElement> entry : entrySet) { if (ignoreItem) { break; } if (!entry.getValue().isJsonPrimitive()) { continue; } Integer attributeId = getAttribute(entry.getKey()); if (null != attributeId) { switch (attributeId) { case android.R.attr.type: if (!TextUtils.equals("item", entry.getValue().getAsString())) { ignoreItem = true; } break; case android.R.attr.color: String colorRes = entry.getValue().getAsString(); if (!TextUtils.isEmpty(colorRes)) { baseColor = getColorFromAttributeValue(context, colorRes); } break; case android.R.attr.alpha: String alphaStr = entry.getValue().getAsString(); if (!TextUtils.isEmpty(alphaStr)) { alphaMod = Float.parseFloat(alphaStr); } break; default: stateSpec[j++] = entry.getValue().getAsBoolean() ? attributeId : -attributeId; break; } } } if (!ignoreItem) { stateSpec = StateSet.trimStateSet(stateSpec, j); if (null == baseColor) { throw new IllegalStateException("No Color Specified"); } if (listSize + 1 >= listAllocated) { listAllocated = idealIntArraySize(listSize + 1); int[] ncolor = new int[listAllocated]; System.arraycopy(colorList, 0, ncolor, 0, listSize); int[][] nstate = new int[listAllocated][]; System.arraycopy(stateSpecList, 0, nstate, 0, listSize); colorList = ncolor; stateSpecList = nstate; } final int color = modulateColorAlpha(baseColor, alphaMod); colorList[listSize] = color; stateSpecList[listSize] = stateSpec; listSize++; } } if (listSize > 0) { int[] colors = new int[listSize]; int[][] stateSpecs = new int[listSize][]; System.arraycopy(colorList, 0, colors, 0, listSize); System.arraycopy(stateSpecList, 0, stateSpecs, 0, listSize); result = new ColorStateList(stateSpecs, colors); } } } } return result; }
From source file:com.flipkart.android.proteus.toolbox.Utils.java
License:Apache License
public static Result readJson(String path, JsonObject data, int index) { // replace INDEX reference with index value if (ProteusConstants.INDEX.equals(path)) { path = path.replace(ProteusConstants.INDEX, String.valueOf(index)); return Result.success(new JsonPrimitive(path)); } else {//from w w w . j ava 2s. c om StringTokenizer tokenizer = new StringTokenizer(path, ProteusConstants.DATA_PATH_DELIMITERS); JsonElement elementToReturn = data; JsonElement tempElement; JsonArray tempArray; while (tokenizer.hasMoreTokens()) { String segment = tokenizer.nextToken(); if (elementToReturn == null) { return Result.NO_SUCH_DATA_PATH_EXCEPTION; } if (elementToReturn.isJsonNull()) { return Result.JSON_NULL_EXCEPTION; } if ("".equals(segment)) { continue; } if (elementToReturn.isJsonArray()) { tempArray = elementToReturn.getAsJsonArray(); if (ProteusConstants.INDEX.equals(segment)) { if (index < tempArray.size()) { elementToReturn = tempArray.get(index); } else { return Result.NO_SUCH_DATA_PATH_EXCEPTION; } } else if (ProteusConstants.ARRAY_DATA_LENGTH_REFERENCE.equals(segment)) { elementToReturn = new JsonPrimitive(tempArray.size()); } else if (ProteusConstants.ARRAY_DATA_LAST_INDEX_REFERENCE.equals(segment)) { if (tempArray.size() == 0) { return Result.NO_SUCH_DATA_PATH_EXCEPTION; } elementToReturn = tempArray.get(tempArray.size() - 1); } else { try { index = Integer.parseInt(segment); } catch (NumberFormatException e) { return Result.INVALID_DATA_PATH_EXCEPTION; } if (index < tempArray.size()) { elementToReturn = tempArray.get(index); } else { return Result.NO_SUCH_DATA_PATH_EXCEPTION; } } } else if (elementToReturn.isJsonObject()) { tempElement = elementToReturn.getAsJsonObject().get(segment); if (tempElement != null) { elementToReturn = tempElement; } else { return Result.NO_SUCH_DATA_PATH_EXCEPTION; } } else if (elementToReturn.isJsonPrimitive()) { return Result.INVALID_DATA_PATH_EXCEPTION; } else { return Result.NO_SUCH_DATA_PATH_EXCEPTION; } } if (elementToReturn.isJsonNull()) { return Result.JSON_NULL_EXCEPTION; } return Result.success(elementToReturn); } }
From source file:com.flipkart.android.proteus.toolbox.Utils.java
License:Apache License
public static JsonElement merge(JsonElement oldJson, JsonElement newJson, boolean useCopy, Gson gson) { JsonElement newDataElement;/*from ww w.ja va 2 s .c om*/ JsonArray oldArray; JsonArray newArray; JsonElement oldArrayItem; JsonElement newArrayItem; JsonObject oldObject; if (oldJson == null || oldJson.isJsonNull()) { return useCopy ? gson.fromJson(newJson, JsonElement.class) : newJson; } if (newJson == null || newJson.isJsonNull()) { newJson = JsonNull.INSTANCE; return newJson; } if (newJson.isJsonPrimitive()) { JsonPrimitive value; if (!useCopy) { return newJson; } if (newJson.getAsJsonPrimitive().isBoolean()) { value = new JsonPrimitive(newJson.getAsBoolean()); } else if (newJson.getAsJsonPrimitive().isNumber()) { value = new JsonPrimitive(newJson.getAsNumber()); } else if (newJson.getAsJsonPrimitive().isString()) { value = new JsonPrimitive(newJson.getAsString()); } else { value = newJson.getAsJsonPrimitive(); } return value; } if (newJson.isJsonArray()) { if (!oldJson.isJsonArray()) { return useCopy ? gson.fromJson(newJson, JsonArray.class) : newJson; } else { oldArray = oldJson.getAsJsonArray(); newArray = newJson.getAsJsonArray(); if (oldArray.size() > newArray.size()) { while (oldArray.size() > newArray.size()) { oldArray.remove(oldArray.size() - 1); } } for (int index = 0; index < newArray.size(); index++) { if (index < oldArray.size()) { oldArrayItem = oldArray.get(index); newArrayItem = newArray.get(index); oldArray.set(index, merge(oldArrayItem, newArrayItem, useCopy, gson)); } else { oldArray.add(newArray.get(index)); } } } } else if (newJson.isJsonObject()) { if (!oldJson.isJsonObject()) { return useCopy ? gson.fromJson(newJson, JsonObject.class) : newJson; } else { oldObject = oldJson.getAsJsonObject(); for (Map.Entry<String, JsonElement> entry : newJson.getAsJsonObject().entrySet()) { newDataElement = merge(oldObject.get(entry.getKey()), entry.getValue(), useCopy, gson); oldObject.add(entry.getKey(), newDataElement); } } } else { return useCopy ? gson.fromJson(newJson, JsonElement.class) : newJson; } return oldJson; }
From source file:com.flipkart.android.proteus.toolbox.Utils.java
License:Apache License
public static String getStringFromArray(JsonArray array, String delimiter) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < array.size(); i++) { if (array.get(i).isJsonPrimitive()) { sb.append(array.get(i).getAsString()); } else {//from w w w .j av a 2 s .c om sb.append(array.get(i).toString()); } if (i < array.size() - 1) { sb.append(delimiter).append(" "); } } return sb.toString(); }
From source file:com.flipkart.android.proteus.view.manager.ProteusViewManagerImpl.java
License:Apache License
private void updateChildrenFromData() { JsonArray dataList = new JsonArray(); ViewGroup parent = ((ViewGroup) view); Result result = Utils.readJson(dataPathForChildren, dataContext.getData(), dataContext.getIndex()); if (result.isSuccess() && null != result.element && result.element.isJsonArray()) { dataList = result.element.getAsJsonArray(); }/* ww w . j av a2 s.c o m*/ int childCount = parent.getChildCount(); View child; if (childCount > dataList.size()) { while (childCount > dataList.size()) { childCount--; child = parent.getChildAt(childCount); if (child instanceof ProteusView) { ((ProteusView) child).getViewManager().destroy(); } parent.removeViewAt(childCount); } } JsonObject data = dataContext.getData(); ProteusView childView; childCount = parent.getChildCount(); for (int index = 0; index < dataList.size(); index++) { if (index < childCount) { child = parent.getChildAt(index); if (child instanceof ProteusView) { ((ProteusView) child).getViewManager().update(data); } } else if (childLayout != null) { childView = layoutBuilder.build(parent, childLayout, data, dataContext.getIndex(), styles); layoutHandler.addView((ProteusView) view, childView); } } }