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

License:Open Source License

public static APINodeList<AdAccountTargetingSuggestions> parseResponse(String json, APIContext context,
        APIRequest request) {/*from  www  .  j  a  v a  2  s .  c  o m*/
    APINodeList<AdAccountTargetingSuggestions> adAccountTargetingSuggestionss = new APINodeList<AdAccountTargetingSuggestions>(
            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++) {
                adAccountTargetingSuggestionss.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context));
            }
            ;
            return adAccountTargetingSuggestionss;
        } else if (result.isJsonObject()) {
            obj = result.getAsJsonObject();
            if (obj.has("data")) {
                try {
                    JsonObject paging = obj.get("paging").getAsJsonObject().get("cursors").getAsJsonObject();
                    adAccountTargetingSuggestionss.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++) {
                        adAccountTargetingSuggestionss
                                .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();
                    adAccountTargetingSuggestionss.add(loadJSON(obj.toString(), context));
                }
                return adAccountTargetingSuggestionss;
            } 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()) {
                    adAccountTargetingSuggestionss.add(loadJSON(entry.getValue().toString(), context));
                }
                return adAccountTargetingSuggestionss;
            } else {
                // Fifth, check if it's pure JsonObject
                adAccountTargetingSuggestionss.add(loadJSON(json, context));
                return adAccountTargetingSuggestionss;
            }
        }
    } catch (Exception e) {
    }
    return null;
}

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

License:Open Source License

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

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

License:Open Source License

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

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

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

License:Open Source License

public static AdAccountTargetingValidation loadJSON(String json, APIContext context) {
    AdAccountTargetingValidation adAccountTargetingValidation = getGson().fromJson(json,
            AdAccountTargetingValidation.class);
    if (context.isDebug()) {
        JsonParser parser = new JsonParser();
        JsonElement o1 = parser.parse(json);
        JsonElement o2 = parser.parse(adAccountTargetingValidation.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 . ja v a2 s .co  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);
        }
        ;
    }
    adAccountTargetingValidation.mContext = context;
    adAccountTargetingValidation.rawValue = json;
    return adAccountTargetingValidation;
}

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

License:Open Source License

public static APINodeList<AdAccountTargetingValidation> parseResponse(String json, APIContext context,
        APIRequest request) {/*from  w  w w.  j a  v  a2 s  .  c o m*/
    APINodeList<AdAccountTargetingValidation> adAccountTargetingValidations = new APINodeList<AdAccountTargetingValidation>(
            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++) {
                adAccountTargetingValidations.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context));
            }
            ;
            return adAccountTargetingValidations;
        } else if (result.isJsonObject()) {
            obj = result.getAsJsonObject();
            if (obj.has("data")) {
                try {
                    JsonObject paging = obj.get("paging").getAsJsonObject().get("cursors").getAsJsonObject();
                    adAccountTargetingValidations.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++) {
                        adAccountTargetingValidations
                                .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();
                    adAccountTargetingValidations.add(loadJSON(obj.toString(), context));
                }
                return adAccountTargetingValidations;
            } 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()) {
                    adAccountTargetingValidations.add(loadJSON(entry.getValue().toString(), context));
                }
                return adAccountTargetingValidations;
            } else {
                // Fifth, check if it's pure JsonObject
                adAccountTargetingValidations.add(loadJSON(json, context));
                return adAccountTargetingValidations;
            }
        }
    } catch (Exception e) {
    }
    return null;
}

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

License:Open Source License

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

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

License:Open Source License

public static APINodeList<AdAccountUser> parseResponse(String json, APIContext context, APIRequest request)
        throws MalformedResponseException {
    APINodeList<AdAccountUser> adAccountUsers = new APINodeList<AdAccountUser>(request, json);
    JsonArray arr;/*from  www. j a  v  a 2s  . c  o  m*/
    JsonObject obj;
    JsonParser parser = new JsonParser();
    Exception exception = null;
    try {
        JsonElement result = parser.parse(json);
        if (result.isJsonArray()) {
            // First, check if it's a pure JSON Array
            arr = result.getAsJsonArray();
            for (int i = 0; i < arr.size(); i++) {
                adAccountUsers.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context));
            }
            ;
            return adAccountUsers;
        } 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;
                    adAccountUsers.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++) {
                        adAccountUsers.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()) {
                                adAccountUsers.add(loadJSON(entry.getValue().toString(), context));
                            }
                            break;
                        }
                    }
                    if (!isRedownload) {
                        adAccountUsers.add(loadJSON(obj.toString(), context));
                    }
                }
                return adAccountUsers;
            } 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()) {
                    adAccountUsers.add(loadJSON(entry.getValue().toString(), context));
                }
                return adAccountUsers;
            } 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)) {
                        adAccountUsers.add(loadJSON(value.toString(), context));
                    } else {
                        isIdIndexedArray = false;
                        break;
                    }
                }
                if (isIdIndexedArray) {
                    return adAccountUsers;
                }

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

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

License:Open Source License

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

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

License:Open Source License

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

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

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

License:Open Source License

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