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

License:Open Source License

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

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

License:Open Source License

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

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

License:Open Source License

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

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

License:Open Source License

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

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

License:Open Source License

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

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

License:Open Source License

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

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

License:Open Source License

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

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

License:Open Source License

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

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

License:Open Source License

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

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

License:Open Source License

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

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