Example usage for com.google.gson JsonParser parse

List of usage examples for com.google.gson JsonParser parse

Introduction

In this page you can find the example usage for com.google.gson JsonParser parse.

Prototype

@Deprecated
public JsonElement parse(JsonReader json) throws JsonIOException, JsonSyntaxException 

Source Link

Usage

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

License:Open Source License

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

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

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

License:Open Source License

public static AdAccountRoas loadJSON(String json, APIContext context) {
    AdAccountRoas adAccountRoas = getGson().fromJson(json, AdAccountRoas.class);
    if (context.isDebug()) {
        JsonParser parser = new JsonParser();
        JsonElement o1 = parser.parse(json);
        JsonElement o2 = parser.parse(adAccountRoas.toString());
        if (o1.getAsJsonObject().get("__fb_trace_id__") != null) {
            o2.getAsJsonObject().add("__fb_trace_id__", o1.getAsJsonObject().get("__fb_trace_id__"));
        }// ww w.  j  a v a  2  s  .  c o m
        if (!o1.equals(o2)) {
            context.log("[Warning] When parsing response, object is not consistent with JSON:");
            context.log("[JSON]" + o1);
            context.log("[Object]" + o2);
        }
        ;
    }
    adAccountRoas.context = context;
    adAccountRoas.rawValue = json;
    return adAccountRoas;
}

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

License:Open Source License

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

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

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

License:Open Source License

public static AdAccountRoasCohorts loadJSON(String json, APIContext context) {
    AdAccountRoasCohorts adAccountRoasCohorts = getGson().fromJson(json, AdAccountRoasCohorts.class);
    if (context.isDebug()) {
        JsonParser parser = new JsonParser();
        JsonElement o1 = parser.parse(json);
        JsonElement o2 = parser.parse(adAccountRoasCohorts.toString());
        if (o1.getAsJsonObject().get("__fb_trace_id__") != null) {
            o2.getAsJsonObject().add("__fb_trace_id__", o1.getAsJsonObject().get("__fb_trace_id__"));
        }/*  ww w .  ja v  a  2 s .c o m*/
        if (!o1.equals(o2)) {
            context.log("[Warning] When parsing response, object is not consistent with JSON:");
            context.log("[JSON]" + o1);
            context.log("[Object]" + o2);
        }
        ;
    }
    adAccountRoasCohorts.mContext = context;
    adAccountRoasCohorts.rawValue = json;
    return adAccountRoasCohorts;
}

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

License:Open Source License

public static APINodeList<AdAccountRoasCohorts> parseResponse(String json, APIContext context,
        APIRequest request) {//from   www. j  a  va2 s  .  co  m
    APINodeList<AdAccountRoasCohorts> adAccountRoasCohortss = new APINodeList<AdAccountRoasCohorts>(request,
            json);
    JsonArray arr;
    JsonObject obj;
    JsonParser parser = new JsonParser();
    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++) {
                adAccountRoasCohortss.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context));
            }
            ;
            return adAccountRoasCohortss;
        } else if (result.isJsonObject()) {
            obj = result.getAsJsonObject();
            if (obj.has("data")) {
                try {
                    JsonObject paging = obj.get("paging").getAsJsonObject().get("cursors").getAsJsonObject();
                    adAccountRoasCohortss.setPaging(paging.get("before").getAsString(),
                            paging.get("after").getAsString());
                } catch (Exception ignored) {
                }
                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++) {
                        adAccountRoasCohortss.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();
                    adAccountRoasCohortss.add(loadJSON(obj.toString(), context));
                }
                return adAccountRoasCohortss;
            } 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()) {
                    adAccountRoasCohortss.add(loadJSON(entry.getValue().toString(), context));
                }
                return adAccountRoasCohortss;
            } else {
                // Fifth, check if it's pure JsonObject
                adAccountRoasCohortss.add(loadJSON(json, context));
                return adAccountRoasCohortss;
            }
        }
    } catch (Exception e) {
    }
    return null;
}

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

License:Open Source License

public static AdAccountTargetingBrowse loadJSON(String json, APIContext context) {
    AdAccountTargetingBrowse adAccountTargetingBrowse = getGson().fromJson(json,
            AdAccountTargetingBrowse.class);
    if (context.isDebug()) {
        JsonParser parser = new JsonParser();
        JsonElement o1 = parser.parse(json);
        JsonElement o2 = parser.parse(adAccountTargetingBrowse.toString());
        if (o1.getAsJsonObject().get("__fb_trace_id__") != null) {
            o2.getAsJsonObject().add("__fb_trace_id__", o1.getAsJsonObject().get("__fb_trace_id__"));
        }// w  w w  .j  a va2s.  c  o  m
        if (!o1.equals(o2)) {
            context.log("[Warning] When parsing response, object is not consistent with JSON:");
            context.log("[JSON]" + o1);
            context.log("[Object]" + o2);
        }
        ;
    }
    adAccountTargetingBrowse.mContext = context;
    adAccountTargetingBrowse.rawValue = json;
    return adAccountTargetingBrowse;
}

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

License:Open Source License

public static APINodeList<AdAccountTargetingBrowse> parseResponse(String json, APIContext context,
        APIRequest request) {// w w  w.ja v  a  2  s. c  o  m
    APINodeList<AdAccountTargetingBrowse> adAccountTargetingBrowses = new APINodeList<AdAccountTargetingBrowse>(
            request, json);
    JsonArray arr;
    JsonObject obj;
    JsonParser parser = new JsonParser();
    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++) {
                adAccountTargetingBrowses.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context));
            }
            ;
            return adAccountTargetingBrowses;
        } else if (result.isJsonObject()) {
            obj = result.getAsJsonObject();
            if (obj.has("data")) {
                try {
                    JsonObject paging = obj.get("paging").getAsJsonObject().get("cursors").getAsJsonObject();
                    adAccountTargetingBrowses.setPaging(paging.get("before").getAsString(),
                            paging.get("after").getAsString());
                } catch (Exception ignored) {
                }
                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++) {
                        adAccountTargetingBrowses
                                .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();
                    adAccountTargetingBrowses.add(loadJSON(obj.toString(), context));
                }
                return adAccountTargetingBrowses;
            } 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()) {
                    adAccountTargetingBrowses.add(loadJSON(entry.getValue().toString(), context));
                }
                return adAccountTargetingBrowses;
            } else {
                // Fifth, check if it's pure JsonObject
                adAccountTargetingBrowses.add(loadJSON(json, context));
                return adAccountTargetingBrowses;
            }
        }
    } catch (Exception e) {
    }
    return null;
}

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

License:Open Source License

public static AdAccountTargetingSearch loadJSON(String json, APIContext context) {
    AdAccountTargetingSearch adAccountTargetingSearch = getGson().fromJson(json,
            AdAccountTargetingSearch.class);
    if (context.isDebug()) {
        JsonParser parser = new JsonParser();
        JsonElement o1 = parser.parse(json);
        JsonElement o2 = parser.parse(adAccountTargetingSearch.toString());
        if (o1.getAsJsonObject().get("__fb_trace_id__") != null) {
            o2.getAsJsonObject().add("__fb_trace_id__", o1.getAsJsonObject().get("__fb_trace_id__"));
        }// www. ja  v  a2  s  .com
        if (!o1.equals(o2)) {
            context.log("[Warning] When parsing response, object is not consistent with JSON:");
            context.log("[JSON]" + o1);
            context.log("[Object]" + o2);
        }
        ;
    }
    adAccountTargetingSearch.mContext = context;
    adAccountTargetingSearch.rawValue = json;
    return adAccountTargetingSearch;
}

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

License:Open Source License

public static APINodeList<AdAccountTargetingSearch> parseResponse(String json, APIContext context,
        APIRequest request) {/*  w w  w.j a  va  2s .  c om*/
    APINodeList<AdAccountTargetingSearch> adAccountTargetingSearchs = new APINodeList<AdAccountTargetingSearch>(
            request, json);
    JsonArray arr;
    JsonObject obj;
    JsonParser parser = new JsonParser();
    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++) {
                adAccountTargetingSearchs.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context));
            }
            ;
            return adAccountTargetingSearchs;
        } else if (result.isJsonObject()) {
            obj = result.getAsJsonObject();
            if (obj.has("data")) {
                try {
                    JsonObject paging = obj.get("paging").getAsJsonObject().get("cursors").getAsJsonObject();
                    adAccountTargetingSearchs.setPaging(paging.get("before").getAsString(),
                            paging.get("after").getAsString());
                } catch (Exception ignored) {
                }
                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++) {
                        adAccountTargetingSearchs
                                .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();
                    adAccountTargetingSearchs.add(loadJSON(obj.toString(), context));
                }
                return adAccountTargetingSearchs;
            } 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()) {
                    adAccountTargetingSearchs.add(loadJSON(entry.getValue().toString(), context));
                }
                return adAccountTargetingSearchs;
            } else {
                // Fifth, check if it's pure JsonObject
                adAccountTargetingSearchs.add(loadJSON(json, context));
                return adAccountTargetingSearchs;
            }
        }
    } catch (Exception e) {
    }
    return null;
}

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

License:Open Source License

public static AdAccountTargetingSuggestions loadJSON(String json, APIContext context) {
    AdAccountTargetingSuggestions adAccountTargetingSuggestions = getGson().fromJson(json,
            AdAccountTargetingSuggestions.class);
    if (context.isDebug()) {
        JsonParser parser = new JsonParser();
        JsonElement o1 = parser.parse(json);
        JsonElement o2 = parser.parse(adAccountTargetingSuggestions.toString());
        if (o1.getAsJsonObject().get("__fb_trace_id__") != null) {
            o2.getAsJsonObject().add("__fb_trace_id__", o1.getAsJsonObject().get("__fb_trace_id__"));
        }/*from  w w w.  j  a va 2  s.  c om*/
        if (!o1.equals(o2)) {
            context.log("[Warning] When parsing response, object is not consistent with JSON:");
            context.log("[JSON]" + o1);
            context.log("[Object]" + o2);
        }
        ;
    }
    adAccountTargetingSuggestions.mContext = context;
    adAccountTargetingSuggestions.rawValue = json;
    return adAccountTargetingSuggestions;
}