Example usage for com.google.gson JsonParser JsonParser

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

Introduction

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

Prototype

@Deprecated
public JsonParser() 

Source Link

Usage

From source file:com.driver733.vkuploader.wallpost.attachment.support.QueryResultsBasic.java

License:Open Source License

@Override
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
public List<JsonElement> results() throws IOException {
    final List<AbstractQueryBuilder> constructed = this.queries.queries();
    final List<JsonElement> results = new ArrayList<>(constructed.size());
    for (final AbstractQueryBuilder query : constructed) {
        if (query.isCached()) {
            final String response;
            try {
                response = query.executeAsString();
            } catch (final ClientException ex) {
                throw new IOException("Failed to execute the query", ex);
            }/*from  ww w . jav  a 2s  .  com*/
            results.add(new JsonParser().parse(new JsonReader(new StringReader(response))));
        }
    }
    return new StickyList<>(results);
}

From source file:com.dvdprime.mobile.android.util.GsonUtil.java

License:Apache License

/**
 * Json Array //ww  w .  j  a v a  2 s . c  o m
 * 
 * @param json
 *            json string
 * @return
 */
public static JsonArray getAsJsonArray(String json) {
    if (json == null) {
        return null;
    }

    return new JsonParser().parse(json).getAsJsonArray();
}

From source file:com.dydabo.blackbox.common.utils.DyDaBoUtils.java

License:Apache License

/**
 * @param jsonString//from  w ww .ja  v a  2  s  .  co m
 * @return
 */
public static JsonElement parseJsonString(String jsonString) {
    try {
        return new JsonParser().parse(jsonString);
    } catch (JsonSyntaxException | NullPointerException ex) {
        // ignore for now
    }

    return null;
}

From source file:com.ecollege.gson.GsonExt.java

License:Apache License

public static JsonElement parseJson(String jsonString) throws JsonParseException {
    if (jsonString == null || jsonString.trim().length() == 0) {
        return null;
    }/* w w w. j  a v a2s  . c  o  m*/

    String json = jsonString.trim();
    return new JsonParser().parse(json);
}

From source file:com.ecwid.mailchimp.MailChimpClient.java

License:Apache License

private JsonElement execute(String url, JsonElement request) throws IOException {
    return new JsonParser().parse(execute(url, request.toString()));
}

From source file:com.edgebox.eacds.net.CDPostResponse.java

License:Open Source License

public static CDPostResponse build(String json) {
    CDPostResponse rt = new CDPostResponse();
    JsonElement e = new JsonParser().parse(json);
    rt.json = e.getAsJsonObject();/* ww  w .j av  a  2s .  co  m*/
    rt.success = rt.json.get("success").getAsBoolean();
    rt.message = rt.json.get("message").getAsString();
    rt.errorTrace = rt.json.get("errorTrace").getAsString();

    try {
        rt.data = rt.json.getAsJsonObject("data").getAsJsonObject();
    } catch (java.lang.ClassCastException ex) {
        rt.data = rt.json.get("data").getAsString();
    }

    JsonElement dt = rt.json.get("data");
    if (dt.isJsonNull()) {
        rt.data = null;
    } else if (dt.isJsonObject()) {
        rt.data = dt.getAsJsonObject();
    } else {
        rt.data = dt.getAsJsonPrimitive();
    }
    return rt;
}

From source file:com.edgytech.umongo.DocView.java

License:Apache License

String getTextForNode(Object obj, boolean indent) throws IOException {
    DBObject doc = null;/*from  ww  w.  ja v a 2  s .  co  m*/
    if (obj instanceof DBObject) {
        doc = (DBObject) obj;
    } else if (obj instanceof TreeNodeDocumentField) {
        doc = (DBObject) ((TreeNodeDocumentField) obj).getValue();
    } else if (obj instanceof TreeNodeDocument) {
        doc = ((TreeNodeDocument) obj).getDBObject();
    }
    if (doc == null) {
        return "";
    }

    final DocumentSerializer ds = new DocumentSerializer(DocumentSerializer.Format.JSON, null);
    ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
    ds.setOutputStream(baos);
    try {
        ds.writeObject(doc);
    } finally {
        ds.close();
    }

    String txt = new String(baos.toByteArray());

    if (indent) {
        Gson gson = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();
        JsonParser jp = new JsonParser();
        JsonElement je = jp.parse(txt);
        txt = gson.toJson(je);
        // add newline in case there are following docs
        txt += "\n";
    }
    return txt;
}

From source file:com.edgytech.umongo.MongoUtils.java

License:Apache License

public static JsonParser getJsonParser() {
    if (jsonParser == null) {
        jsonParser = new JsonParser();
    }
    return jsonParser;
}

From source file:com.elevenpaths.latch.LatchAuth.java

License:Open Source License

/**
 * Makes an HTTP request/*from  w  w  w . j av a2 s  . c om*/
 * @param URL The request URL.
 * @param method The request method.
 * @param headers Headers to add to the HTTP request.
 * @param data Parameters to add to the HTTP request body.
 * @return The server's JSON response or null if something has gone wrong.
 */
private JsonElement HTTP(String URL, String method, Map<String, String> headers, Map<String, String> data) {

    JsonElement rv = null;
    InputStream is = null;
    OutputStream os = null;
    InputStreamReader isr = null;

    try {

        URL theURL = new URL(URL);
        HttpURLConnection theConnection = (HttpURLConnection) theURL.openConnection();

        theConnection.setRequestMethod(method);

        if (headers != null && !headers.isEmpty()) {
            Iterator<String> iterator = headers.keySet().iterator();
            while (iterator.hasNext()) {
                String headerName = iterator.next();
                theConnection.setRequestProperty(headerName, headers.get(headerName));
            }
        }

        if (!(HTTP_METHOD_GET.equals(method) || HTTP_METHOD_DELETE.equals(method))) {
            StringBuilder sb = new StringBuilder();
            if (data != null && !data.isEmpty()) {
                String[] paramNames = new String[data.size()];
                data.keySet().toArray(paramNames);
                for (int i = 0; i < paramNames.length; i++) {
                    sb.append(URLEncoder.encode(paramNames[i], CHARSET_UTF_8));
                    sb.append(LatchAuth.PARAM_VALUE_SEPARATOR);
                    sb.append(URLEncoder.encode(data.get(paramNames[i]), CHARSET_UTF_8));
                    if (i < paramNames.length - 1) {
                        sb.append(PARAM_SEPARATOR);
                    }
                }
            }
            byte[] body = sb.toString().getBytes(CHARSET_ASCII);
            theConnection.setRequestProperty(HTTP_HEADER_CONTENT_TYPE,
                    HTTP_HEADER_CONTENT_TYPE_FORM_URLENCODED);
            theConnection.setRequestProperty(HTTP_HEADER_CONTENT_LENGTH, String.valueOf(body.length));
            theConnection.setDoOutput(true);
            os = theConnection.getOutputStream();
            os.write(body);
            os.flush();
        }

        JsonParser parser = new JsonParser();
        is = theConnection.getInputStream();
        isr = new InputStreamReader(is, CHARSET_UTF_8);
        rv = parser.parse(isr);

    } catch (MalformedURLException e) {
        System.err.println("The URL is malformed (" + URL + ")");
        e.printStackTrace();
    } catch (IOException e) {
        System.err.println("An exception has been thrown when communicating with Latch backend");
        e.printStackTrace();
    } finally {

        if (os != null) {
            try {
                os.close();
            } catch (IOException e) {
                System.err.println("An exception has been thrown when trying to close the output stream");
                e.printStackTrace();
            }
        }

        if (isr != null) {
            try {
                isr.close();
            } catch (IOException e) {
                System.err.println("An exception has been thrown when trying to close the input stream reader");
                e.printStackTrace();
            }
        }

        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                System.err.println("An exception has been thrown when trying to close the input stream");
                e.printStackTrace();
            }
        }

    }

    return rv;

}

From source file:com.elevenpaths.latch.LatchResponse.java

License:Open Source License

/**
 *
 * @param json a json string received from one of the methods of the Latch API
 * @throws JsonParseException/* w ww  .j  a va2  s . c om*/
 * @throws JsonSyntaxException
 */
public LatchResponse(String json) {
    this(new JsonParser().parse(json));
}