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

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

Introduction

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

Prototype

public void endArray() throws IOException 

Source Link

Document

Consumes the next token from the JSON stream and asserts that it is the end of the current array.

Usage

From source file:tools.DrawStatisticsForPubMedData.java

License:Apache License

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

    String journalName;//from ww  w  .  j av a  2 s.c o 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:tools.DrawStatisticsForPubMedData.java

License:Apache License

public int readLabelsArray(JsonReader reader) {

    int count = 0;
    try {//  w  w w.  j av a2  s  .  c om
        reader.beginArray();
        while (reader.hasNext()) {
            String nextString = reader.nextString();
            labelsList.add(nextString);
            count++;
        }
        reader.endArray();
    } catch (IOException ex) {
    }

    return count;
}

From source file:uk.ac.susx.tag.classificationframework.classifiers.NaiveBayesClassifier.java

License:Apache License

protected static IntSet readJsonIntSet(JsonReader reader, FeatureExtractionPipeline pipeline,
        boolean areFeatures) throws IOException {
    IntSet set = new IntOpenHashSet();
    reader.beginArray();// w  w w  . ja v  a 2 s.c om
    while (reader.hasNext()) {
        set.add(areFeatures ? pipeline.featureIndex(reader.nextString())
                : pipeline.labelIndex(reader.nextString()));
    }
    reader.endArray();
    return set;
}

From source file:uk.ac.susx.tag.dependencyparser.datastructures.StringIndexer.java

License:Apache License

public static StringIndexer readJson(JsonReader reader) throws IOException {
    int idStart = 1;
    List<String> strings = new ArrayList<>();
    reader.beginObject();//w ww.  ja  va 2s. c o m
    while (reader.hasNext()) {
        String name = reader.nextName();
        switch (name) {
        case "idStart":
            idStart = reader.nextInt();
            break;
        case "strings":
            reader.beginArray();
            while (reader.hasNext()) {
                strings.add(reader.nextString());
            }
            reader.endArray();
        }
    }
    reader.endObject();
    return new StringIndexer(idStart, strings);
}

From source file:us.blanshard.sudoku.game.GameJson.java

License:Apache License

/**
 * Registers type adapters in the given builder so that history lists and undo
 * stacks can be serialized and deserialized.  Note that undo stacks require a
 * CommandFactory be established before deserialization; see {@link #setFactory}.
 *//*from  ww  w  .j  a  v  a 2  s  .c om*/
public static GsonBuilder register(GsonBuilder builder) {

    builder.registerTypeHierarchyAdapter(Move.class, new TypeAdapter<Move>() {
        @Override
        public void write(JsonWriter out, Move value) throws IOException {
            out.value(value.toJsonValue());
        }

        @Override
        public Move read(JsonReader in) throws IOException {
            return Move.fromJsonValue(in.nextString());
        }
    });

    final TypeAdapter<Command> commandAdapter = new TypeAdapter<Command>() {
        @Override
        public void write(JsonWriter out, Command value) throws IOException {
            out.value(value.toJsonValue());
        }

        @Override
        public Command read(JsonReader in) throws IOException {
            Iterator<String> values = SPLITTER.split(in.nextString()).iterator();
            String type = values.next();
            return factorySlot.get().toCommand(type, values);
        }
    };
    builder.registerTypeHierarchyAdapter(Command.class, commandAdapter);

    builder.registerTypeAdapter(UndoStack.class, new TypeAdapter<UndoStack>() {
        @Override
        public void write(JsonWriter out, UndoStack value) throws IOException {
            out.beginObject();
            out.name("position").value(value.getPosition());
            out.name("commands").beginArray();
            for (Command c : value.commands)
                commandAdapter.write(out, c);
            out.endArray();
            out.endObject();
        }

        @Override
        public UndoStack read(JsonReader in) throws IOException {
            int position = -1;
            List<Command> commands = null;
            in.beginObject();
            while (in.hasNext()) {
                String name = in.nextName();
                if (name.equals("position")) {
                    position = in.nextInt();
                } else if (name.equals("commands")) {
                    commands = Lists.newArrayList();
                    in.beginArray();
                    while (in.hasNext())
                        commands.add(commandAdapter.read(in));
                    in.endArray();
                } else {
                    in.skipValue();
                }
            }
            in.endObject();
            return new UndoStack(commands, position);
        }
    });

    return builder;
}

From source file:util.android.gson.ArrayAdapter.java

License:Apache License

@Override
public List<T> read(JsonReader reader) throws IOException {

    List<T> list = new ArrayList<T>();

    Gson gson = new GsonBuilder().registerTypeAdapterFactory(new ArrayAdapterFactory()).create();

    if (reader.peek() == JsonToken.BEGIN_OBJECT) {
        T inning = gson.fromJson(reader, adapterclass);
        list.add(inning);/*w ww .  ja  v a 2s.  co  m*/

    } else if (reader.peek() == JsonToken.BEGIN_ARRAY) {

        reader.beginArray();
        while (reader.hasNext()) {
            T inning = gson.fromJson(reader, adapterclass);
            list.add(inning);
        }
        reader.endArray();

    }

    return list;
}

From source file:vaeke.restcountries.v0.rest.CountryService.java

License:Mozilla Public License

private void initialize() throws IOException {
    LOG.debug("Loading JSON Database v0");
    InputStream is = this.getClass().getClassLoader().getResourceAsStream("countries.json");
    Gson gson = new Gson();
    JsonReader reader = new JsonReader(new InputStreamReader(is, "UTF-8"));
    countries = new ArrayList<Country>();
    reader.beginArray();/*from  ww w  . j  a  v  a  2  s.  c  om*/
    while (reader.hasNext()) {
        Country country = gson.fromJson(reader, Country.class);
        countries.add(country);
    }
    reader.endArray();
    reader.close();

}

From source file:vaeke.restcountries.v1.rest.CountryService.java

License:Mozilla Public License

private void initialize() {
    LOG.debug("Loading JSON Database v1");
    InputStream is = this.getClass().getClassLoader().getResourceAsStream("countriesV1.json");
    Gson gson = new Gson();
    JsonReader reader;
    try {//from  w  ww.  j a  v a2  s .c  om
        reader = new JsonReader(new InputStreamReader(is, "UTF-8"));
        countries = new ArrayList<Country>();
        reader.beginArray();
        while (reader.hasNext()) {
            Country country = gson.fromJson(reader, Country.class);
            countries.add(country);
        }
        reader.endArray();
        reader.close();
    } catch (Exception e) {
        LOG.error("Could not load JSON Database v1 ");
    }

}

From source file:vaeke.restcountries.v1.service.JsonFileCountryService.java

License:Mozilla Public License

private void initialize() {
    LOG.debug("Loading JSON Database v1");
    InputStream is = this.getClass().getClassLoader().getResourceAsStream("countriesV1.json");
    Gson gson = new Gson();
    JsonReader reader;
    try {/*from w  ww .  j a v a  2  s. co m*/
        reader = new JsonReader(new InputStreamReader(is, "UTF-8"));
        countries = new ArrayList<>();
        reader.beginArray();
        while (reader.hasNext()) {
            Country country = gson.fromJson(reader, Country.class);
            countries.add(country);
        }
        reader.endArray();
        reader.close();
    } catch (Exception e) {
        LOG.error("Could not load JSON Database v1 ");
    }

}

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 {//from w w w  .ja  va 2 s  .  c  om
        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();
        }
    }
}