List of usage examples for com.google.gson JsonElement toString
@Override
public String toString()
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 w w .j a 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++) { 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 ww w. j a 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++) { 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.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;// w ww.j a va 2s .co m } Event event = EventFactory.factory(json.getAsJsonObject()); if (event != null) { listener.onEvent(event); } }
From source file:com.flipkart.android.proteus.builder.LayoutBuilderFactory.java
License:Apache License
protected void registerFormatter(DataParsingLayoutBuilder layoutBuilder) { Formatter NumberFormatter = new Formatter() { private DecimalFormat formatter; @Override/*from ww w .j a v a 2s. com*/ public String format(JsonElement elementValue) { double valueAsNumber; try { valueAsNumber = Double.parseDouble(elementValue.getAsString()); } catch (NumberFormatException e) { return elementValue.toString(); } formatter = new DecimalFormat("#,###"); if (Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD) { formatter.setRoundingMode(RoundingMode.FLOOR); } formatter.setMinimumFractionDigits(0); formatter.setMaximumFractionDigits(2); return formatter.format(valueAsNumber); } @Override public String getName() { return "number"; } }; Formatter DateFormatter = new Formatter() { @SuppressLint("SimpleDateFormat") private SimpleDateFormat from = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @SuppressLint("SimpleDateFormat") private SimpleDateFormat to = new SimpleDateFormat("d MMM, E"); @Override public String format(JsonElement elementValue) { try { // 2015-06-18 12:01:37 Date date = from.parse(elementValue.getAsString()); return to.format(date); } catch (Exception e) { return elementValue.toString(); } } @Override public String getName() { return "date"; } }; Formatter IndexFormatter = new Formatter() { @Override public String format(JsonElement elementValue) { int valueAsNumber; try { valueAsNumber = Integer.parseInt(elementValue.getAsString()); } catch (NumberFormatException e) { return elementValue.toString(); } return String.valueOf(valueAsNumber + 1); } @Override public String getName() { return "index"; } }; Formatter joinFormatter = new Formatter() { @Override public String format(JsonElement elementValue) { if (elementValue.isJsonArray()) { return Utils.getStringFromArray(elementValue.getAsJsonArray(), ","); } else { return elementValue.toString(); } } @Override public String getName() { return "join"; } }; layoutBuilder.registerFormatter(NumberFormatter); layoutBuilder.registerFormatter(DateFormatter); layoutBuilder.registerFormatter(IndexFormatter); layoutBuilder.registerFormatter(joinFormatter); }
From source file:com.flipkart.android.proteus.builder.SimpleLayoutBuilder.java
License:Apache License
public boolean handleAttribute(LayoutHandler handler, ProteusView view, String attribute, JsonElement value) { if (ProteusConstants.isLoggingEnabled()) { Log.d(TAG, "Handle '" + attribute + "' : " + value.toString() + " for view with " + Utils.getLayoutIdentifier(view.getViewManager().getLayout())); }//www . j ava2 s. c o m //noinspection unchecked return handler.handleAttribute((View) view, attribute, value); }
From source file:com.flipkart.android.proteus.processor.DrawableResourceProcessor.java
License:Apache License
@Override public void handle(String key, JsonElement value, V view) { if (value.isJsonPrimitive()) { handleString(key, value.getAsString(), view); } else if (value.isJsonObject()) { handleElement(key, value, view); } else {//from ww w . ja v a2 s .c om if (ProteusConstants.isLoggingEnabled()) { Log.e(TAG, "Resource for key: " + key + " must be a primitive or an object. value -> " + value.toString()); } } }
From source file:com.flipkart.android.proteus.processor.StringAttributeProcessor.java
License:Apache License
/** * @param view View//from w w w.ja v a 2 s. c o m */ @Override public void handle(String key, JsonElement value, V view) { if (value.isJsonPrimitive()) { handle(key, getStringFromAttribute(view, value.getAsString()), view); } else { handle(key, value.toString(), view); } }
From source file:com.flipkart.android.proteus.processor.TweenAnimationResourceProcessor.java
License:Apache License
@Override public void handle(String key, JsonElement value, V view) { Animation animation = AnimationUtils.loadAnimation(view.getContext(), value); if (null != animation) { setAnimation(view, animation);//from w w w . j a v a 2 s .c om } else { if (ProteusConstants.isLoggingEnabled()) { Log.e(TAG, "Resource for key: " + key + " must be a primitive or an object. value -> " + value.toString()); } } }
From source file:com.flipkart.android.proteus.toolbox.AnimationUtils.java
License:Apache License
/** * Loads an {@link Animation} object from a resource * * @param context Application context used to access resources * @param value JSON representation of the Animation * @return The animation object reference by the specified id * @throws android.content.res.Resources.NotFoundException when the animation cannot be loaded *///from ww w.j ava 2 s . c o m public static Animation loadAnimation(Context context, JsonElement value) throws Resources.NotFoundException { Animation anim = null; if (value.isJsonPrimitive()) { anim = handleString(context, value.getAsString()); } else if (value.isJsonObject()) { anim = handleElement(context, value.getAsJsonObject()); } else { if (ProteusConstants.isLoggingEnabled()) { Log.e(TAG, "Could not load animation for : " + value.toString()); } } return anim; }
From source file:com.flipkart.android.proteus.toolbox.AnimationUtils.java
License:Apache License
/** * Loads an {@link Interpolator} object from a resource * * @param context Application context used to access resources * @param value Json representation of the Interpolator * @return The animation object reference by the specified id * @throws android.content.res.Resources.NotFoundException *///from w w w .ja v a2s. c o m public static Interpolator loadInterpolator(Context context, JsonElement value) throws Resources.NotFoundException { Interpolator interpolator = null; if (value.isJsonPrimitive()) { interpolator = handleStringInterpolator(context, value.getAsString()); } else if (value.isJsonObject()) { interpolator = handleElementInterpolator(context, value.getAsJsonObject()); } else { if (ProteusConstants.isLoggingEnabled()) { Log.e(TAG, "Could not load interpolator for : " + value.toString()); } } return interpolator; }