Example usage for com.google.gson.stream JsonReader setLenient

List of usage examples for com.google.gson.stream JsonReader setLenient

Introduction

In this page you can find the example usage for com.google.gson.stream JsonReader setLenient.

Prototype

public final void setLenient(boolean lenient) 

Source Link

Document

Configure this parser to be liberal in what it accepts.

Usage

From source file:com.imaginea.betterdocs.BetterDocsAction.java

License:Apache License

private ArrayList<Integer> getLineNumbers(Collection<String> imports, String tokens) {
    ArrayList<Integer> lineNumbers = new ArrayList<Integer>();
    JsonReader reader = new JsonReader(new StringReader(tokens));
    reader.setLenient(true);
    JsonArray tokensArray = new JsonParser().parse(reader).getAsJsonArray();

    for (JsonElement token : tokensArray) {
        JsonObject jObject = token.getAsJsonObject();
        String importName = jObject.getAsJsonPrimitive(IMPORT_NAME).getAsString();
        if (imports.contains(importName)) {
            JsonArray lineNumbersArray = jObject.getAsJsonArray(LINE_NUMBERS);
            for (JsonElement lineNumber : lineNumbersArray) {
                lineNumbers.add(lineNumber.getAsInt());
            }/*from  ww  w . java2 s  .com*/
        }
    }
    return lineNumbers;
}

From source file:com.imaginea.betterdocs.BetterDocsAction.java

License:Apache License

private String getContentsForFile(String file) {
    String esFileQueryJson = getJsonForFileContent(file);
    String esFileResultJson = getESResultJson(esFileQueryJson, esURL + SOURCEFILE_SEARCH);

    JsonReader reader = new JsonReader(new StringReader(esFileResultJson));
    reader.setLenient(true);
    JsonElement jsonElement = new JsonParser().parse(reader);
    JsonObject jsonObject = jsonElement.getAsJsonObject();
    JsonObject hitsObject = jsonObject.getAsJsonObject(HITS);
    JsonArray hitsArray = hitsObject.getAsJsonArray(HITS);
    JsonObject hitObject = hitsArray.get(0).getAsJsonObject();
    JsonObject sourceObject = hitObject.getAsJsonObject(SOURCE);
    //Replacing \r as it's treated as bad end of line character
    String fileContent = sourceObject.getAsJsonPrimitive(FILE_CONTENT).getAsString().replaceAll("\r", "");
    return fileContent;
}

From source file:com.imaginea.betterdocs.BetterDocsAction.java

License:Apache License

private static Map<String, String> getFileTokens(String esResultJson) {
    Map<String, String> fileTokenMap = new HashMap<String, String>();
    JsonReader reader = new JsonReader(new StringReader(esResultJson));
    reader.setLenient(true);
    JsonElement jsonElement = new JsonParser().parse(reader);
    JsonObject jsonObject = jsonElement.getAsJsonObject();
    JsonObject hitsObject = jsonObject.getAsJsonObject(HITS);
    JsonArray hitsArray = hitsObject.getAsJsonArray(HITS);

    for (JsonElement hits : hitsArray) {
        JsonObject hitObject = hits.getAsJsonObject();
        JsonObject sourceObject = hitObject.getAsJsonObject(SOURCE);
        String fileName = sourceObject.getAsJsonPrimitive(FILE).getAsString();
        String tokens = sourceObject.get(TOKENS).toString();
        fileTokenMap.put(fileName, tokens);
    }//from   w w  w  . j a va2 s  . c om
    return fileTokenMap;
}

From source file:com.imaginea.kodebeagle.base.util.ESUtils.java

License:Apache License

protected final JsonObject getJsonElements(final String esResultJson) {
    JsonReader reader = new JsonReader(new StringReader(esResultJson));
    reader.setLenient(true);
    JsonElement jsonElement = new JsonParser().parse(reader);
    if (jsonElement.isJsonObject()) {
        return jsonElement.getAsJsonObject().getAsJsonObject(HITS);
    } else {/*from   w ww .  ja  v a2 s. c o  m*/
        return null;
    }
}

From source file:com.imaginea.kodebeagle.base.util.JSONUtils.java

License:Apache License

public final List<Integer> getLineNumbers(final Collection<String> imports, final String tokens) {
    List<Integer> lineNumbers = new ArrayList<Integer>();
    JsonReader reader = new JsonReader(new StringReader(tokens));
    reader.setLenient(true);
    JsonArray tokensArray = new JsonParser().parse(reader).getAsJsonArray();
    for (JsonElement token : tokensArray) {
        JsonObject jObject = token.getAsJsonObject();
        String importName = jObject.getAsJsonPrimitive(IMPORT_EXACT_NAME).getAsString();
        if (imports.contains(importName)) {
            JsonArray lineNumbersArray = jObject.getAsJsonArray(LINE_NUMBERS);
            for (JsonElement lineNumberInfo : lineNumbersArray) {
                lineNumbers.add(lineNumberInfo.getAsJsonObject().getAsJsonPrimitive(LINE_NUMBER).getAsInt());
            }/*w  w w  .ja  v a2  s  . c o m*/
        }
    }
    return lineNumbers;
}

From source file:com.imaginea.kodebeagle.base.util.SearchUtils.java

License:Apache License

protected final JsonObject getJsonElements(final String esResultJson) {
    JsonReader reader = new JsonReader(new StringReader(esResultJson));
    reader.setLenient(true);
    JsonElement jsonElement = new JsonParser().parse(reader);
    if (jsonElement.isJsonObject()) {
        return jsonElement.getAsJsonObject();
    } else {// w  w  w .  j  ava2 s. c  o m
        return null;
    }
}

From source file:com.imaginea.kodebeagle.base.util.SearchUtils.java

License:Apache License

public final List<Integer> getLineNumbers(final Collection<String> imports, final String tokens) {

    List<Integer> lineNumbers = new ArrayList<Integer>();
    JsonReader reader = new JsonReader(new StringReader(tokens));
    reader.setLenient(true);
    JsonArray tokensArray = new JsonParser().parse(reader).getAsJsonArray();
    for (JsonElement token : tokensArray) {
        JsonObject jObject = token.getAsJsonObject();
        String typeName = jObject.getAsJsonPrimitive(TYPE_EXACT_NAME).getAsString();
        if (imports.contains(typeName)) {

            JsonArray propsArray = jObject.getAsJsonArray(PROPS);
            for (JsonElement propsInfo : propsArray) {

                JsonArray lineNumbersArray = propsInfo.getAsJsonObject().getAsJsonArray(LINES);
                for (JsonElement lineNumber : lineNumbersArray) {

                    lineNumbers.add(lineNumber.getAsJsonArray().get(0).getAsInt());

                }/*from   www. j ava 2  s. c o m*/

            }
        }
    }
    return lineNumbers;
}

From source file:com.intellij.openapi.updateSettings.impl.pluginsAdvertisement.PluginsAdvertiser.java

License:Apache License

public static List<PluginId> retrieve(UnknownFeature unknownFeature) {
    final String featureType = unknownFeature.getFeatureType();
    final String implementationName = unknownFeature.getImplementationName();
    final String buildNumber = ApplicationInfo.getInstance().getBuild().asString();
    final String pluginRepositoryUrl = FEATURE_IMPLEMENTATIONS_URL + "featureType=" + featureType
            + "&implementationName=" + implementationName.replaceAll("#", "%23") + "&build=" + buildNumber;
    try {//from   ww w.  java2 s . c o m
        HttpURLConnection connection = HttpConfigurable.getInstance().openHttpConnection(pluginRepositoryUrl);
        connection.connect();
        final InputStreamReader streamReader = new InputStreamReader(connection.getInputStream());
        try {
            final JsonReader jsonReader = new JsonReader(streamReader);
            jsonReader.setLenient(true);
            final JsonElement jsonRootElement = new JsonParser().parse(jsonReader);
            final List<PluginId> result = new ArrayList<PluginId>();
            for (JsonElement jsonElement : jsonRootElement.getAsJsonArray()) {
                final JsonObject jsonObject = jsonElement.getAsJsonObject();
                final JsonElement pluginId = jsonObject.get("pluginId");
                result.add(PluginId.getId(StringUtil.unquoteString(pluginId.toString())));
            }
            return result;
        } finally {
            streamReader.close();
        }
    } catch (IOException e) {
        LOG.info(e);
        return null;
    }
}

From source file:com.intellij.openapi.updateSettings.impl.pluginsAdvertisement.PluginsAdvertiser.java

License:Apache License

private static Map<String, Set<PluginId>> loadSupportedExtensions() {
    final String pluginRepositoryUrl = FEATURE_IMPLEMENTATIONS_URL + "featureType="
            + FileTypeFactory.FILE_TYPE_FACTORY_EP.getName();
    try {/*from   w w  w .  j  av  a 2  s.c o  m*/
        HttpURLConnection connection = HttpConfigurable.getInstance().openHttpConnection(pluginRepositoryUrl);
        connection.connect();
        final InputStreamReader streamReader = new InputStreamReader(connection.getInputStream());
        try {
            final JsonReader jsonReader = new JsonReader(streamReader);
            jsonReader.setLenient(true);
            final JsonElement jsonRootElement = new JsonParser().parse(jsonReader);
            final Map<String, Set<PluginId>> result = new HashMap<String, Set<PluginId>>();
            for (JsonElement jsonElement : jsonRootElement.getAsJsonArray()) {
                final JsonObject jsonObject = jsonElement.getAsJsonObject();
                final JsonElement ext = jsonObject.get("implementationName");
                final String extension = StringUtil.unquoteString(ext.toString());
                Set<PluginId> pluginIds = result.get(extension);
                if (pluginIds == null) {
                    pluginIds = new HashSet<PluginId>();
                    result.put(extension, pluginIds);
                }
                pluginIds.add(PluginId.getId(StringUtil.unquoteString(jsonObject.get("pluginId").toString())));
            }
            saveExtensions(result);
            return result;
        } finally {
            streamReader.close();
        }
    } catch (IOException e) {
        LOG.info(e);
        return null;
    }
}

From source file:com.inverseinnovations.VBulletinAPI.VBulletinAPI.java

License:Apache License

/**
 * Calls a method through the API.//from   w  w w . java2s . c  o  m
 *
 * @param methodname
 *            the name of the method to call
 * @param params
 *            the parameters as a map
 * @param sign
 *            if the request should be signed or not. Generally, you want this to be true
 * @return the array returned by the server
 * @throws IOException
 *             If the URL is wrong, or a connection is unable to be made for
 *             whatever reason.
 */
private LinkedTreeMap<String, Object> callMethod(String methodname, Map<String, String> params, boolean sign) {// throws IOException{
    LinkedTreeMap<String, Object> map = new LinkedTreeMap<String, Object>();

    try {

        StringBuffer queryStringBuffer = new StringBuffer("api_m=" + methodname);
        SortedSet<String> keys = new TreeSet<String>(params.keySet());
        for (String key : keys) {// ' " \ are unsafe
            String value = Functions.querySafeString(params.get(key));
            queryStringBuffer.append("&" + key + "=" + URLEncoder.encode(value, "UTF-8"));
        }
        if (sign) {
            queryStringBuffer.append("&api_sig=" + Functions.MD5((queryStringBuffer.toString()
                    + getAPIAccessToken() + apiClientID + getSecret() + getAPIkey())).toLowerCase());
            if (DEBUG) {
                System.out.println("encoded: " + queryStringBuffer.toString());
            }
        }

        queryStringBuffer.append("&api_c=" + apiClientID);
        queryStringBuffer.append("&api_s=" + getAPIAccessToken());
        String queryString = queryStringBuffer.toString();
        queryString = queryString.replace(" ", "%20");
        URL apiUrl = new URL(apiURL + "?" + queryString);
        HttpURLConnection conn = (HttpURLConnection) apiUrl.openConnection();
        conn.setRequestMethod("POST");

        conn.setConnectTimeout(10000); //set timeout to 10 seconds
        conn.setReadTimeout(10000);//set timeout to 10 seconds
        conn.setDoOutput(true);
        conn.setDoInput(true);
        DataOutputStream out = new DataOutputStream(conn.getOutputStream());
        out.writeBytes(queryString);
        InputStream is = null;
        try {
            is = conn.getInputStream();
        } finally {
            if (is != null) {
                String json = Functions.inputStreamToString(is);

                //need to remove everything before {
                if (json.contains("{")) {
                    json = json.substring(json.indexOf("{"));
                }

                Gson gson = new Gson();
                JsonReader reader = new JsonReader(new StringReader(json));
                reader.setLenient(true);
                try {
                    map = gson.fromJson(reader, new TypeToken<Map<String, Object>>() {
                    }.getType());
                } catch (Exception e) {//TODO need to check what kind of errors...
                    System.out.println(json);
                    e.printStackTrace();
                    map = new LinkedTreeMap<String, Object>();
                    map.put("custom", new String("IllegalStateException"));
                }
            }

        }
        conn.disconnect();
    } catch (java.net.SocketTimeoutException e) {
        map = new LinkedTreeMap<String, Object>();
        map.put("custom", new String("SocketTimeoutException"));
    } catch (IOException e) {
        map = new LinkedTreeMap<String, Object>();
        map.put("custom", new String("IOException"));
    }
    return map;
}