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:pl.raszkowski.sporttrackersconnector.garminconnect.Authorizer.java

License:Apache License

private Optional<String> getUsername(String responseContent) {
    JsonReader jsonReader = new JsonReader(new StringReader(responseContent));
    jsonReader.setLenient(true);

    JsonParser jsonParser = new JsonParser();

    JsonElement json = jsonParser.parse(jsonReader);

    LOG.debug("Json account: {}.", json);

    if (json.isJsonObject()) {
        JsonObject jsonRoot = json.getAsJsonObject();

        if (jsonRoot.getAsJsonObject().has(JSON_ACCOUNT_KEY)) {
            JsonObject jsonAccount = jsonRoot.get(JSON_ACCOUNT_KEY).getAsJsonObject();

            if (jsonAccount.has(JSON_USERNAME_KEY)) {
                return Optional.ofNullable(jsonAccount.get(JSON_USERNAME_KEY).getAsString());
            }//  w w w .  ja v a2 s.c om
        }
    }

    return Optional.empty();
}

From source file:resources.Resources.java

License:Open Source License

private void load() {
    try (Reader in = new InputStreamReader(this.getClass().getResourceAsStream("/resources/resources"))) {
        JsonReader reader = new JsonReader(in);
        reader.setLenient(true);
        resources = new JsonParser().parse(reader).getAsJsonObject();
    } catch (IOException ex) {
        log.fatal("Missing resources file", ex);
        throw new MissingResourceException("Missing resources/resources", "Resources.java", "resources");
    }/*  w w w. j  ava 2  s.  co m*/
}

From source file:se.oskardevelopment.reqa.simple.utility.OutputHelper.java

License:Open Source License

/**
 * getSavedList gets a list of objects located in the file.
 * @param klazz Class of the objects contained in the list read from the file.
 * @return List of all objects read from file.
 * @throws IOException thrown if reading the file failed.
 *//*w  w  w .j  a va2 s  . c o  m*/
protected <T> List<T> getSavedList(Class<T> klazz) throws IOException {
    ArrayList<T> outcome = null;
    JsonReader reader = null;
    try {
        reader = new JsonReader(getReader());
        reader.setLenient(true);
        Type type = new TypeToken<ArrayList<T>>() {
        }.getType();
        outcome = (ArrayList<T>) gson.fromJson(reader, type);
    } catch (IOException exception) {
        LOGGER.warn("Exception {} when reading file: {}", exception, FILE_NAME);
    } finally {
        if (reader != null) {
            reader.close();
        }
    }
    return outcome;
}

From source file:tk.breezy64.pantex.core.ConfigManager.java

public static void load(File file) throws IOException {
    JsonReader r = new JsonReader(new StringReader(Util.readStream(new FileInputStream(file))));
    r.setLenient(true);
    JsonObject json = new JsonParser().parse(r).getAsJsonObject();
    List<ConfigSection> x = Static.pluginManager.getExtensions(ConfigSection.class);

    Map<Integer, List<ConfigSection>> pr = new HashMap<>();
    Map<ConfigSection, Map<String, Object>> csMap = new HashMap<>();

    for (ConfigSection cs : x) {
        if (json.has(cs.getTitle())) {
            Map<String, Object> map = jsonObjectToMap(json.getAsJsonObject(cs.getTitle()));
            int priority = map.containsKey("priority") ? (Integer) map.get("priority") : 0;

            List<ConfigSection> l = pr.get(priority);
            if (l == null) {
                l = new ArrayList<>();
                pr.put(priority, l);/*from   w ww. j  a v  a  2s .c o  m*/
            }
            l.add(cs);
            csMap.put(cs, map);
        }
    }

    for (List<ConfigSection> e : pr.entrySet().stream().sorted((a, b) -> a.getKey().compareTo(b.getKey()))
            .map(z -> z.getValue()).collect(Collectors.toList())) {
        for (ConfigSection c : e) {
            c.load(csMap.get(c));
        }
    }
}

From source file:tools.DrawStatisticsForPubMedData.java

License:Apache License

public void parseStream(String jsonFile, String listOfJournals) throws IOException {

    String journalName;// ww w  . j a va  2s.  co  m
    int count = 0;
    int abstract_count = 0;
    int duplicates = 0;

    try {
        JsonReader reader = new JsonReader(new InputStreamReader(new FileInputStream(jsonFile)));
        reader.setLenient(true);

        reader.beginObject();
        reader.skipValue();

        //System.out.println(nam);
        reader.beginArray();
        while (reader.hasNext()) {

            reader.beginObject();
            this.numeOfArticles++;
            while (reader.hasNext()) {
                String name = reader.nextName();

                if (name.equals("abstractText")) {
                    abstract_count++;
                    reader.skipValue();

                } else if (name.equals("journal")) {
                    journalName = reader.nextString();
                    journalList.add(journalName);
                } else if (name.equals("meshMajor")) {
                    int num_labels = readLabelsArray(reader);
                    count += num_labels;
                    labelDensity += (double) num_labels / 26563.0;
                } else if (name.equals("pmid")) {
                    int pmid = reader.nextInt();
                    if (!pmids.contains(pmid))
                        pmids.add(pmid);
                    else
                        duplicates++;
                } else if (name.equals("title")) {
                    reader.skipValue();
                } else if (name.equals("year")) {
                    reader.skipValue();
                } else {
                    System.out.println(name);
                    reader.skipValue();
                }
            }
            reader.endObject();
        }
        reader.endArray();

        System.out.println("Abstracts: " + abstract_count);
        System.out.println("Duplicates: " + duplicates);

        labelsPerArticle = (double) count / (double) numeOfArticles;
        labelDensity = labelDensity / (double) numeOfArticles;
        exportListOfJournals(listOfJournals);
        printStatistics();

    } catch (Exception ex) {
        System.out.println("Abstracts: " + abstract_count);
        System.out.println("Duplicates: " + duplicates);

        labelsPerArticle = (double) count / (double) numeOfArticles;
        labelDensity = labelDensity / (double) numeOfArticles;
        exportListOfJournals(listOfJournals);
        printStatistics();
        Logger.getLogger(DrawStatisticsForPubMedData.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:tv.loilo.promise.http.ResponseJsonConverter.java

License:Apache License

@NonNull
public static JsonElement parse(@NonNull final JsonReader json) throws JsonIOException, JsonSyntaxException {
    boolean lenient = json.isLenient();
    json.setLenient(true);
    try {/*from   w  w  w. j  a  v  a2 s  .c  o  m*/
        return Streams.parse(json);
    } catch (StackOverflowError | OutOfMemoryError e) {
        throw new JsonIOException("Failed parsing JSON source: " + json + " to Json", e);
    } finally {
        json.setLenient(lenient);
    }
}

From source file:vogar.ExpectationStore.java

License:Apache License

public void parse(File expectationsFile, ModeId mode, Variant variant) throws IOException {
    log.verbose("loading expectations file " + expectationsFile);

    int count = 0;
    JsonReader reader = null;
    try {//  w w  w.  j  a va2s.c  o m
        reader = new JsonReader(new FileReader(expectationsFile));
        reader.setLenient(true);
        reader.beginArray();
        while (reader.hasNext()) {
            readExpectation(reader, mode, variant);
            count++;
        }
        reader.endArray();

        log.verbose("loaded " + count + " expectations from " + expectationsFile);
    } finally {
        if (reader != null) {
            reader.close();
        }
    }
}