Example usage for com.google.gson JsonObject entrySet

List of usage examples for com.google.gson JsonObject entrySet

Introduction

In this page you can find the example usage for com.google.gson JsonObject entrySet.

Prototype

public Set<Map.Entry<String, JsonElement>> entrySet() 

Source Link

Document

Returns a set of members of this object.

Usage

From source file:com.facebook.ads.sdk.TransactionCurrencyAmount.java

License:Open Source License

public static APINodeList<TransactionCurrencyAmount> parseResponse(String json, APIContext context,
        APIRequest request) throws MalformedResponseException {
    APINodeList<TransactionCurrencyAmount> transactionCurrencyAmounts = new APINodeList<TransactionCurrencyAmount>(
            request, json);/*from  w w w .  java  2s . 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++) {
                transactionCurrencyAmounts.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context));
            }
            ;
            return transactionCurrencyAmounts;
        } 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;
                    transactionCurrencyAmounts.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++) {
                        transactionCurrencyAmounts
                                .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()) {
                                transactionCurrencyAmounts.add(loadJSON(entry.getValue().toString(), context));
                            }
                            break;
                        }
                    }
                    if (!isRedownload) {
                        transactionCurrencyAmounts.add(loadJSON(obj.toString(), context));
                    }
                }
                return transactionCurrencyAmounts;
            } 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()) {
                    transactionCurrencyAmounts.add(loadJSON(entry.getValue().toString(), context));
                }
                return transactionCurrencyAmounts;
            } 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)) {
                        transactionCurrencyAmounts.add(loadJSON(value.toString(), context));
                    } else {
                        isIdIndexedArray = false;
                        break;
                    }
                }
                if (isIdIndexedArray) {
                    return transactionCurrencyAmounts;
                }

                // Sixth, check if it's pure JsonObject
                transactionCurrencyAmounts.clear();
                transactionCurrencyAmounts.add(loadJSON(json, context));
                return transactionCurrencyAmounts;
            }
        }
    } catch (Exception e) {
        exception = e;
    }
    throw new MalformedResponseException("Invalid response string: " + json, exception);
}

From source file:com.facebook.ads.sdk.User.java

License:Open Source License

public static APINodeList<User> parseResponse(String json, APIContext context, APIRequest request)
        throws MalformedResponseException {
    APINodeList<User> users = new APINodeList<User>(request, json);
    JsonArray arr;//  w w  w  . jav  a  2 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++) {
                users.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context));
            }
            ;
            return users;
        } 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;
                    users.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++) {
                        users.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()) {
                                users.add(loadJSON(entry.getValue().toString(), context));
                            }
                            break;
                        }
                    }
                    if (!isRedownload) {
                        users.add(loadJSON(obj.toString(), context));
                    }
                }
                return users;
            } 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()) {
                    users.add(loadJSON(entry.getValue().toString(), context));
                }
                return users;
            } 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)) {
                        users.add(loadJSON(value.toString(), context));
                    } else {
                        isIdIndexedArray = false;
                        break;
                    }
                }
                if (isIdIndexedArray) {
                    return users;
                }

                // Sixth, check if it's pure JsonObject
                users.clear();
                users.add(loadJSON(json, context));
                return users;
            }
        }
    } catch (Exception e) {
        exception = e;
    }
    throw new MalformedResponseException("Invalid response string: " + json, exception);
}

From source file:com.facebook.ads.sdk.UserLeadGenDisclaimerResponse.java

License:Open Source License

public static APINodeList<UserLeadGenDisclaimerResponse> parseResponse(String json, APIContext context,
        APIRequest request) throws MalformedResponseException {
    APINodeList<UserLeadGenDisclaimerResponse> userLeadGenDisclaimerResponses = new APINodeList<UserLeadGenDisclaimerResponse>(
            request, json);/* w  ww.j  a va2  s.  com*/
    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++) {
                userLeadGenDisclaimerResponses.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context));
            }
            ;
            return userLeadGenDisclaimerResponses;
        } 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;
                    userLeadGenDisclaimerResponses.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++) {
                        userLeadGenDisclaimerResponses
                                .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()) {
                                userLeadGenDisclaimerResponses
                                        .add(loadJSON(entry.getValue().toString(), context));
                            }
                            break;
                        }
                    }
                    if (!isRedownload) {
                        userLeadGenDisclaimerResponses.add(loadJSON(obj.toString(), context));
                    }
                }
                return userLeadGenDisclaimerResponses;
            } 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()) {
                    userLeadGenDisclaimerResponses.add(loadJSON(entry.getValue().toString(), context));
                }
                return userLeadGenDisclaimerResponses;
            } 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)) {
                        userLeadGenDisclaimerResponses.add(loadJSON(value.toString(), context));
                    } else {
                        isIdIndexedArray = false;
                        break;
                    }
                }
                if (isIdIndexedArray) {
                    return userLeadGenDisclaimerResponses;
                }

                // Sixth, check if it's pure JsonObject
                userLeadGenDisclaimerResponses.clear();
                userLeadGenDisclaimerResponses.add(loadJSON(json, context));
                return userLeadGenDisclaimerResponses;
            }
        }
    } catch (Exception e) {
        exception = e;
    }
    throw new MalformedResponseException("Invalid response string: " + json, exception);
}

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   www  . ja  v  a 2s  .  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;/*from   w w w.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++) {
                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;/*from ww w  . jav 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 w w  .j  a v a 2 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;// w w  w  .  j av  a2s.co 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.flipkart.android.proteus.builder.SimpleLayoutBuilder.java

License:Apache License

@Override
@Nullable/*w  ww  .  ja  v a 2 s  .c  o m*/
public ProteusView build(ViewGroup parent, JsonObject layout, JsonObject data, int index, Styles styles) {
    String type = Utils.getPropertyAsString(layout, ProteusConstants.TYPE);

    if (type == null) {
        throw new IllegalArgumentException("'type' missing in layout: " + layout.toString());
    }

    LayoutHandler handler = layoutHandlers.get(type);
    if (handler == null) {
        return onUnknownViewEncountered(type, parent, layout, data, index, styles);
    }

    /**
     * View creation.
     */
    final ProteusView view;

    onBeforeCreateView(handler, parent, layout, data, index, styles);
    view = createView(handler, parent, layout, data, index, styles);
    onAfterCreateView(handler, view, parent, layout, data, index, styles);

    ProteusViewManager viewManager = createViewManager(handler, parent, layout, data, index, styles);
    viewManager.setView((View) view);
    view.setViewManager(viewManager);

    /**
     * Parsing each attribute and setting it on the view.
     */
    JsonElement value;
    String attribute;

    for (Map.Entry<String, JsonElement> entry : layout.entrySet()) {
        if (ProteusConstants.TYPE.equals(entry.getKey())) {
            continue;
        }

        value = entry.getValue();
        attribute = entry.getKey();

        boolean handled = handleAttribute(handler, view, attribute, value);

        if (!handled) {
            onUnknownAttributeEncountered(attribute, value, view);
        }
    }

    return view;
}

From source file:com.flipkart.android.proteus.DataContext.java

License:Apache License

public static DataContext updateDataContext(DataContext dataContext, JsonObject data, JsonObject scope,
        int dataIndex) {
    JsonObject reverseScope = new JsonObject();
    JsonObject newData = new JsonObject();

    dataContext.setIndex(dataIndex);//ww  w .  j  a v a  2 s .  c om

    if (data == null) {
        data = new JsonObject();
    }

    for (Map.Entry<String, JsonElement> entry : scope.entrySet()) {
        String key = entry.getKey();
        String value = entry.getValue().getAsString();
        Result result = Utils.readJson(value, data, dataContext.getIndex());
        JsonElement element = result.isSuccess() ? result.element : new JsonObject();
        newData.add(key, element);
        String unAliasedValue = value.replace(ProteusConstants.INDEX, String.valueOf(dataContext.getIndex()));
        reverseScope.add(unAliasedValue, new JsonPrimitive(key));
    }

    Utils.addElements(newData, data, false);

    if (dataContext.getData() == null) {
        dataContext.setData(new JsonObject());
    } else {
        dataContext.setData(newData);
    }
    dataContext.setScope(scope);
    dataContext.setReverseScope(reverseScope);
    return dataContext;
}