List of usage examples for com.google.gson JsonElement isJsonObject
public boolean isJsonObject()
From source file:com.facebook.ads.sdk.UserLeadGenFieldData.java
License:Open Source License
public static APINodeList<UserLeadGenFieldData> parseResponse(String json, APIContext context, APIRequest request) throws MalformedResponseException { APINodeList<UserLeadGenFieldData> userLeadGenFieldDatas = new APINodeList<UserLeadGenFieldData>(request, json);//from w ww .j a va 2 s .c o m JsonArray arr; JsonObject obj; 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++) { userLeadGenFieldDatas.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; return userLeadGenFieldDatas; } 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; userLeadGenFieldDatas.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++) { userLeadGenFieldDatas.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()) { userLeadGenFieldDatas.add(loadJSON(entry.getValue().toString(), context)); } break; } } if (!isRedownload) { userLeadGenFieldDatas.add(loadJSON(obj.toString(), context)); } } return userLeadGenFieldDatas; } 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()) { userLeadGenFieldDatas.add(loadJSON(entry.getValue().toString(), context)); } return userLeadGenFieldDatas; } 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)) { userLeadGenFieldDatas.add(loadJSON(value.toString(), context)); } else { isIdIndexedArray = false; break; } } if (isIdIndexedArray) { return userLeadGenFieldDatas; } // Sixth, check if it's pure JsonObject userLeadGenFieldDatas.clear(); userLeadGenFieldDatas.add(loadJSON(json, context)); return userLeadGenFieldDatas; } } } catch (Exception e) { exception = e; } throw new MalformedResponseException("Invalid response string: " + json, exception); }
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;/*ww w. j a v a 2s. c om*/ JsonObject obj; 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;/* w ww.ja v a 2s . c o m*/ JsonObject obj; 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;//from w ww . ja v a2 s .c o m JsonObject obj; 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;/*from w w w .j av a 2 s .c om*/ JsonObject obj; 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.apple.XctoolOutputParsing.java
License:Apache License
private static void dispatchEventCallback(Gson gson, JsonElement element, XctoolEventCallback eventCallback) throws JsonParseException { LOG.debug("Parsing xctool event: %s", element); if (!element.isJsonObject()) { LOG.warn("Couldn't parse JSON object from xctool event: %s", element); return;/*from www .j av a 2s . c o m*/ } JsonObject object = element.getAsJsonObject(); if (!object.has("event")) { LOG.warn("Couldn't parse JSON event from xctool event: %s", element); return; } JsonElement event = object.get("event"); if (event == null || !event.isJsonPrimitive()) { LOG.warn("Couldn't parse event field from xctool event: %s", element); return; } switch (event.getAsString()) { case "begin-ocunit": eventCallback.handleBeginOcunitEvent(gson.fromJson(element, BeginOcunitEvent.class)); break; case "end-ocunit": eventCallback.handleEndOcunitEvent(gson.fromJson(element, EndOcunitEvent.class)); break; case "begin-test-suite": eventCallback.handleBeginTestSuiteEvent(gson.fromJson(element, BeginTestSuiteEvent.class)); break; case "end-test-suite": eventCallback.handleEndTestSuiteEvent(gson.fromJson(element, EndTestSuiteEvent.class)); break; case "begin-test": eventCallback.handleBeginTestEvent(gson.fromJson(element, BeginTestEvent.class)); break; case "end-test": eventCallback.handleEndTestEvent(gson.fromJson(element, EndTestEvent.class)); break; } }
From source file:com.facebook.buck.httpserver.TracesHelper.java
License:Apache License
private static Optional<String> tryToFindCommand(JsonObject json) { JsonElement nameEl = json.get("name"); if (nameEl == null || !nameEl.isJsonPrimitive()) { return Optional.absent(); }/*w ww . jav a 2 s . com*/ JsonElement argsEl = json.get("args"); if (argsEl == null || !argsEl.isJsonObject() || argsEl.getAsJsonObject().get("command_args") == null || !argsEl.getAsJsonObject().get("command_args").isJsonPrimitive()) { return Optional.absent(); } String name = nameEl.getAsString(); String commandArgs = argsEl.getAsJsonObject().get("command_args").getAsString(); String command = "buck " + name + " " + commandArgs; return Optional.of(command); }
From source file:com.facebook.buck.json.RawParser.java
License:Apache License
/** * @return One of: String, Boolean, Long, Number, List<Object>, Map<String, Object>. */// w ww . ja v a2 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.facebook.buck.plugin.intellij.commands.SocketClient.java
License:Apache License
private void dispatch(String message) { JsonParser parser = new JsonParser(); JsonElement json = parser.parse(message); if (!json.isJsonObject()) { LOG.error(String.format("Invalid JSON object: %s", json.toString())); return;// ww w . j a va2s. co m } Event event = EventFactory.factory(json.getAsJsonObject()); if (event != null) { listener.onEvent(event); } }
From source file:com.flipkart.android.proteus.parser.custom.HorizontalProgressBarParser.java
License:Apache License
@Override protected void prepareHandlers() { super.prepareHandlers(); addHandler(Attributes.ProgressBar.ProgressTint, new JsonDataProcessor<T>() { @Override//w ww . ja v a 2s . c o m public void handle(String key, JsonElement attributeValue, T view) { if (!attributeValue.isJsonObject() || attributeValue.isJsonNull()) { return; } JsonObject data = attributeValue.getAsJsonObject(); int background = Color.TRANSPARENT; int progress = Color.TRANSPARENT; String value = Utils.getPropertyAsString(data, "background"); if (value != null) { background = ParseHelper.parseColor(value); } value = Utils.getPropertyAsString(data, "progress"); if (value != null) { progress = ParseHelper.parseColor(value); } view.setProgressDrawable(getLayerDrawable(progress, background)); } }); }