List of usage examples for com.google.gson.stream JsonToken BEGIN_ARRAY
JsonToken BEGIN_ARRAY
To view the source code for com.google.gson.stream JsonToken BEGIN_ARRAY.
Click Source Link
From source file:data.Task1bData.java
License:Apache License
public void readData(String jsonFile) throws IOException { int num_questions = 0; int num_triples = 0; int type_yesno = 0; int type_factoid = 0; int type_list = 0; int type_summary = 0; try {// w w w . j ava 2 s. c om JsonReader reader = new JsonReader(new InputStreamReader(new FileInputStream(jsonFile))); //reader.setLenient(true); // JsonToken peeknext = reader.peek(); // peeknext. reader.beginObject(); while (reader.hasNext()) { String nextfield = reader.nextName(); if (nextfield.equals("questions")) { reader.beginArray(); while (reader.hasNext()) { reader.beginObject(); num_questions++; Question qst = new Question(); while (reader.hasNext()) { String name = reader.nextName(); int k = 0; if (name.equals("body")) { String body = reader.nextString(); qst.setBody(body); } else if (name.equals("triples")) { num_triples++; ArrayList<Triple> triples = readTriplesArray(reader); qst.addTriples(triples); } else if (name.equals("type")) { String type = reader.nextString(); if (type.equals("yesno")) { qst.setType(Question.YESNO); type_yesno++; } else if (type.equals("factoid")) { qst.setType(Question.FACTOID); type_factoid++; } if (type.equals("summary")) { qst.setType(Question.SUMMARY); type_summary++; } if (type.equals("list")) { qst.setType(Question.LIST); type_list++; } } else if (name.equals("id")) { String id = reader.nextString(); qst.setId(id); } else if (name.equals("concepts")) { ArrayList<String> concepts = readConcepts(reader); qst.addConcepts(concepts); } else if (name.equals("documents")) { ArrayList<String> docs = readDocuments(reader); qst.addDocuments(docs); } else if (name.equals("exact_answer")) { ExactAnswer ea = new ExactAnswer(); JsonToken peek = reader.peek(); if (peek == JsonToken.BEGIN_ARRAY) //list or factoid { reader.beginArray(); JsonToken peek1 = reader.peek(); ArrayList<String> listOfAnswers = new ArrayList<String>(); ArrayList<ArrayList<String>> listofarrays = new ArrayList<ArrayList<String>>(); if (peek1 == JsonToken.BEGIN_ARRAY) // list (or factoid-list since BioASQ3) { /* * Warning: changed the following for BioASQ 5 * No synonyms in submissions anymore, only in gold files */ if (VERSION_OF_CHALLENGE == evaluation.EvaluatorTask1b.BIOASQ2 || VERSION_OF_CHALLENGE == evaluation.EvaluatorTask1b.BIOASQ3) { listofarrays = readExactAnswerListOfArraysv2(reader); ea.setLists(listofarrays); } else if (VERSION_OF_CHALLENGE == evaluation.EvaluatorTask1b.BIOASQ5) { if (!this.isGold) { // For submissions use restricted parsing : only first of synonyms taken into account listofarrays = readExactAnswerListOfArraysv3(reader); } else { // For golden read all synonyms normally listofarrays = readExactAnswerListOfArraysv2(reader); } ea.setLists(listofarrays); } else { System.out.println("Wrong challenge version. I will exit."); System.exit(0); } } else if (peek1 == JsonToken.STRING) // factoid (for BioASQ1&2) { /* * Warning: changed the following for BioASQ 3 * we now have list of arrays for factoid */ if (VERSION_OF_CHALLENGE == evaluation.EvaluatorTask1b.BIOASQ2) { listOfAnswers = readExactAnswerArray(reader); ea.setAnswers(listOfAnswers); } //not reached! else if (VERSION_OF_CHALLENGE == evaluation.EvaluatorTask1b.BIOASQ3) { listofarrays = readExactAnswerListOfArraysv2(reader); ea.setLists(listofarrays); } /* * Warning: changed the following for BioASQ 5 * No synonyms are submitted anymore by participants */ //not reached! else if (VERSION_OF_CHALLENGE == evaluation.EvaluatorTask1b.BIOASQ5) { listofarrays = readExactAnswerListOfArraysv3(reader); ea.setLists(listofarrays); } else { System.out.println("Wrong challenge version. I will exit."); System.exit(0); } } //ea.setAnswers(listOfAnswers); qst.setExact_answer(ea); reader.endArray(); } else if (peek == JsonToken.STRING) //yesno { String yesno_answer = reader.nextString(); yesno_answer = yesno_answer.toLowerCase(); if (yesno_answer.contains("yes")) ea.setAnswer("yes"); else if (yesno_answer.contains("no")) ea.setAnswer("no"); else { ea.setAnswer("none"); // System.out.println("Unknown answer in yesno question: "+yesno_answer); } qst.setExact_answer(ea); } } // Edited for BioASQ4 Evaluation (to solve format conflict with Rouge.py) // ideal answers are not evaluated with this code, so no need to read them(Rouge and manual queration is used instead) // else if(name.equals("ideal_answer")) // { // String ideal=""; // try{ideal = reader.nextString();}catch(IllegalStateException ex){System.out.println(ex.toString());System.out.println(jsonFile); // } // qst.setIdeal_answer(ideal); // } else if (name.equals("snippets")) { ArrayList<Snippet> snippets = readSnippets(reader); qst.addSnippets(snippets); } else { reader.skipValue(); } } //reader.skipValue(); reader.endObject(); this.questions.add(qst); } reader.endArray(); } else { reader.skipValue(); } } reader.endObject(); /* System.out.println("Number of questions:"+num_questions); System.out.println("Number of triples:"+num_triples); System.out.println("Number of yesno:"+type_yesno); System.out.println("Number of factoid:"+type_factoid); System.out.println("Number of list:"+type_list); System.out.println("Number of summary:"+type_summary);*/ } catch (FileNotFoundException ex) { System.out.println("Problem in JSONfile : " + jsonFile); } }
From source file:ddt.dtool.dub.DubDescribeParser.java
License:Open Source License
protected static ArrayList<DubBundle> readDescribedBundles(JsonReaderExt jsonParser) throws IOException { jsonParser.consumeExpected(JsonToken.BEGIN_ARRAY); ArrayList<DubBundle> bundles = new ArrayList<>(); while (jsonParser.hasNext()) { JsonToken tokenType = jsonParser.peek(); if (tokenType == JsonToken.BEGIN_OBJECT) { DubBundle bundle = new DubManifestParser().readBundle(jsonParser).createBundle(null, false); bundles.add(bundle);/*from ww w . j a v a 2 s .c om*/ } else { jsonParser.errorUnexpected(tokenType); } } jsonParser.consumeExpected(JsonToken.END_ARRAY); return bundles; }
From source file:ddt.dtool.dub.DubManifestParser.java
License:Open Source License
protected ArrayList<BundleFile> parseFiles(JsonReaderExt jsonReader) throws IOException { jsonReader.consumeExpected(JsonToken.BEGIN_ARRAY); ArrayList<BundleFile> bundleFiles = new ArrayList<>(); while (jsonReader.hasNext()) { BundleFile bundleFile = parseFile(jsonReader); bundleFiles.add(bundleFile);// w w w.j a v a2 s . c o m } jsonReader.consumeExpected(JsonToken.END_ARRAY); return bundleFiles; }
From source file:ddt.dtool.util.JsonReaderExt.java
License:Open Source License
public void consumeExpected(JsonToken expectedToken) throws IOException { JsonToken tokenType = validateExpectedToken(expectedToken); if (tokenType == JsonToken.BEGIN_OBJECT) { jsonReader.beginObject();/*from w w w .j av a2 s .com*/ } else if (tokenType == JsonToken.END_OBJECT) { jsonReader.endObject(); } else if (tokenType == JsonToken.BEGIN_ARRAY) { jsonReader.beginArray(); } else if (tokenType == JsonToken.END_ARRAY) { jsonReader.endArray(); } else if (tokenType == JsonToken.END_DOCUMENT) { } else { assertFail(); } }
From source file:ddt.dtool.util.JsonReaderExt.java
License:Open Source License
public ArrayList<String> consumeStringArray(boolean ignoreNulls) throws IOException { jsonReader.consumeExpected(JsonToken.BEGIN_ARRAY); ArrayList<String> strings = new ArrayList<>(); while (jsonReader.hasNext()) { JsonToken tokenType = jsonReader.peek(); if (ignoreNulls && tokenType == JsonToken.NULL) { jsonReader.nextNull();/*from ww w .j ava 2 s . c om*/ continue; } if (tokenType != JsonToken.STRING) { sourceError("Expected String value, instead got: " + tokenType); } String entry = jsonReader.nextString(); strings.add(entry); } jsonReader.consumeExpected(JsonToken.END_ARRAY); return strings; }
From source file:ddt.dtool.util.JsonReaderExt.java
License:Open Source License
public ArrayList<Object> readJsonArray() throws IOException { jsonReader.consumeExpected(JsonToken.BEGIN_ARRAY); ArrayList<Object> array = new ArrayList<>(); while (jsonReader.hasNext()) { array.add(readJsonValue());//www. j ava 2 s .co m } jsonReader.consumeExpected(JsonToken.END_ARRAY); return array; }
From source file:dtool.dub.DubDescribeParser.java
License:Open Source License
protected static ArrayList<DubBundle> readDescribedBundles(JsonReaderExt jsonParser) throws IOException, DubBundleException { jsonParser.consumeExpected(JsonToken.BEGIN_ARRAY); ArrayList<DubBundle> bundles = new ArrayList<>(); while (jsonParser.hasNext()) { JsonToken tokenType = jsonParser.peek(); if (tokenType == JsonToken.BEGIN_OBJECT) { DubBundle bundle = new DubManifestParser().readBundle(jsonParser).createBundle(null, false); bundles.add(bundle);/*from ww w . j av a 2 s. com*/ } else { jsonParser.errorUnexpected(tokenType); } } jsonParser.consumeExpected(JsonToken.END_ARRAY); return bundles; }
From source file:dtool.dub.DubManifestParser.java
License:Open Source License
protected ArrayList2<BundleFile> parseFiles(JsonReaderExt jsonReader) throws IOException { jsonReader.consumeExpected(JsonToken.BEGIN_ARRAY); ArrayList2<BundleFile> bundleFiles = new ArrayList2<>(); while (jsonReader.hasNext()) { BundleFile bundleFile = parseFile(jsonReader); bundleFiles.add(bundleFile);/*ww w . j a v a 2 s. c om*/ } jsonReader.consumeExpected(JsonToken.END_ARRAY); return bundleFiles; }
From source file:dtool.dub.DubManifestParser.java
License:Open Source License
protected ArrayList2<DubConfiguration> parseConfigurations(JsonReaderExt jsonReader) throws IOException, DubBundleException { jsonReader.consumeExpected(JsonToken.BEGIN_ARRAY); ArrayList2<DubConfiguration> bundleFiles = new ArrayList2<>(); while (jsonReader.hasNext()) { DubConfiguration element = parseConfiguration(jsonReader); bundleFiles.add(element);/*from w w w.j a v a 2 s . c o m*/ } jsonReader.consumeExpected(JsonToken.END_ARRAY); return bundleFiles; }
From source file:json.tests.reader.GsonPerfTestWithCustomReader.java
License:Open Source License
/** * Read the first element of a array/*from ww w . j a v a 2 s.c o m*/ */ public JsonElement printFirstElement() throws IOException { JSONStreamReader jsonReader = new JSONStreamReader(new StringReader(json), requiredElements); JsonParser jp = new JsonParser(); boolean isInArray = false; JsonElement element = null; // consume the first token jsonReader.beginObject(); while (jsonReader.hasNext()) { JsonToken token = jsonReader.peek(); if (token.equals(JsonToken.BEGIN_ARRAY)) { jsonReader.beginArray(); isInArray = true; } else if (isInArray && token.equals(JsonToken.END_ARRAY)) { jsonReader.endArray(); } else if (isInArray && token.equals(JsonToken.BEGIN_OBJECT)) { element = jp.parse(jsonReader); break; } else { jsonReader.skipValue(); } } return element; }