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.AdsPixelStatsResult.java

License:Open Source License

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

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

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

License:Open Source License

public static AdStudy loadJSON(String json, APIContext context) {
    AdStudy adStudy = getGson().fromJson(json, AdStudy.class);
    if (context.isDebug()) {
        JsonParser parser = new JsonParser();
        JsonElement o1 = parser.parse(json);
        JsonElement o2 = parser.parse(adStudy.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  v a  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);
        }
        ;
    }
    adStudy.context = context;
    adStudy.rawValue = json;
    return adStudy;
}

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

License:Open Source License

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

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

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

License:Open Source License

public static AdStudyCell loadJSON(String json, APIContext context) {
    AdStudyCell adStudyCell = getGson().fromJson(json, AdStudyCell.class);
    if (context.isDebug()) {
        JsonParser parser = new JsonParser();
        JsonElement o1 = parser.parse(json);
        JsonElement o2 = parser.parse(adStudyCell.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 av a  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);
        }
        ;
    }
    adStudyCell.context = context;
    adStudyCell.rawValue = json;
    return adStudyCell;
}

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

License:Open Source License

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

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

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

License:Open Source License

public static AdStudyObjective loadJSON(String json, APIContext context) {
    AdStudyObjective adStudyObjective = getGson().fromJson(json, AdStudyObjective.class);
    if (context.isDebug()) {
        JsonParser parser = new JsonParser();
        JsonElement o1 = parser.parse(json);
        JsonElement o2 = parser.parse(adStudyObjective.toString());
        if (o1.getAsJsonObject().get("__fb_trace_id__") != null) {
            o2.getAsJsonObject().add("__fb_trace_id__", o1.getAsJsonObject().get("__fb_trace_id__"));
        }/*from ww w .ja  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);
        }
        ;
    }
    adStudyObjective.context = context;
    adStudyObjective.rawValue = json;
    return adStudyObjective;
}

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

License:Open Source License

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

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

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

License:Open Source License

public static AgencyClientDeclaration loadJSON(String json, APIContext context) {
    AgencyClientDeclaration agencyClientDeclaration = getGson().fromJson(json, AgencyClientDeclaration.class);
    if (context.isDebug()) {
        JsonParser parser = new JsonParser();
        JsonElement o1 = parser.parse(json);
        JsonElement o2 = parser.parse(agencyClientDeclaration.toString());
        if (o1.getAsJsonObject().get("__fb_trace_id__") != null) {
            o2.getAsJsonObject().add("__fb_trace_id__", o1.getAsJsonObject().get("__fb_trace_id__"));
        }/*from   ww w.ja  v a2s.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);
        }
        ;
    }
    agencyClientDeclaration.context = context;
    agencyClientDeclaration.rawValue = json;
    return agencyClientDeclaration;
}

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

License:Open Source License

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

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

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

License:Open Source License

public static AndroidAppLink loadJSON(String json, APIContext context) {
    AndroidAppLink androidAppLink = getGson().fromJson(json, AndroidAppLink.class);
    if (context.isDebug()) {
        JsonParser parser = new JsonParser();
        JsonElement o1 = parser.parse(json);
        JsonElement o2 = parser.parse(androidAppLink.toString());
        if (o1.getAsJsonObject().get("__fb_trace_id__") != null) {
            o2.getAsJsonObject().add("__fb_trace_id__", o1.getAsJsonObject().get("__fb_trace_id__"));
        }//  w ww.  j  av  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);
        }
        ;
    }
    androidAppLink.context = context;
    androidAppLink.rawValue = json;
    return androidAppLink;
}