List of usage examples for com.google.gson.stream JsonToken STRING
JsonToken STRING
To view the source code for com.google.gson.stream JsonToken STRING.
Click Source Link
From source file:com.ibm.watson.developer_cloud.speech_to_text.v1.util.SpeechWordConfidenceTypeAdapter.java
License:Open Source License
@Override public SpeechWordConfidence read(JsonReader reader) throws IOException { if (reader.peek() == JsonToken.NULL) { reader.nextNull();/*from w w w . j a v a 2 s .co m*/ return null; } final SpeechWordConfidence speechWordConfidence = new SpeechWordConfidence(); reader.beginArray(); if (reader.peek() == JsonToken.STRING) { speechWordConfidence.setWord(reader.nextString()); } if (reader.peek() == JsonToken.NUMBER) { speechWordConfidence.setConfidence(reader.nextDouble()); } reader.endArray(); return speechWordConfidence; }
From source file:com.sap.core.odata.core.ep.consumer.JsonFeedConsumer.java
License:Apache License
private void handleName(final String nextName) throws IOException, EdmException, EntityProviderException { if (FormatJson.RESULTS.equals(nextName)) { resultsArrayPresent = true;//from www .ja va2 s . c o m readArrayContent(); } else if (FormatJson.COUNT.equals(nextName)) { readInlineCount(reader, feedMetadata); } else if (FormatJson.NEXT.equals(nextName)) { if (reader.peek() == JsonToken.STRING && feedMetadata.getNextLink() == null) { String nextLink = reader.nextString(); feedMetadata.setNextLink(nextLink); } else { throw new EntityProviderException( EntityProviderException.INVALID_CONTENT.addContent(nextName).addContent("JsonFeed")); } } else if (FormatJson.DELTA.equals(nextName)) { if (reader.peek() == JsonToken.STRING && feedMetadata.getDeltaLink() == null) { String deltaLink = reader.nextString(); feedMetadata.setDeltaLink(deltaLink); } else { throw new EntityProviderException( EntityProviderException.INVALID_CONTENT.addContent(nextName).addContent("JsonFeed")); } } else { throw new EntityProviderException( EntityProviderException.INVALID_CONTENT.addContent(nextName).addContent("JsonFeed")); } }
From source file:com.sap.core.odata.core.ep.consumer.JsonFeedConsumer.java
License:Apache License
protected static void readInlineCount(final JsonReader reader, final FeedMetadataImpl feedMetadata) throws IOException, EntityProviderException { if (reader.peek() == JsonToken.STRING && feedMetadata.getInlineCount() == null) { int inlineCount; try {/*ww w . j av a 2 s. c om*/ inlineCount = reader.nextInt(); } catch (final NumberFormatException e) { throw new EntityProviderException(EntityProviderException.INLINECOUNT_INVALID.addContent(""), e); } if (inlineCount >= 0) { feedMetadata.setInlineCount(inlineCount); } else { throw new EntityProviderException( EntityProviderException.INLINECOUNT_INVALID.addContent(inlineCount)); } } else { throw new EntityProviderException( EntityProviderException.INLINECOUNT_INVALID.addContent(reader.peek())); } }
From source file:com.sap.core.odata.core.ep.consumer.JsonLinkConsumer.java
License:Apache License
/** * Reads single link with format <code>{"d":{"uri":"http://somelink"}}</code> * or <code>{"uri":"http://somelink"}</code>. * @param reader/*from w w w. j a v a 2 s .c o m*/ * @param entitySet * @return link as string object * @throws EntityProviderException */ public String readLink(final JsonReader reader, final EdmEntitySet entitySet) throws EntityProviderException { try { String result; reader.beginObject(); String nextName = reader.nextName(); final boolean wrapped = FormatJson.D.equals(nextName); if (wrapped) { reader.beginObject(); nextName = reader.nextName(); } if (FormatJson.URI.equals(nextName) && reader.peek() == JsonToken.STRING) { result = reader.nextString(); } else { throw new EntityProviderException(EntityProviderException.INVALID_CONTENT .addContent(FormatJson.D + " or " + FormatJson.URI).addContent(nextName)); } reader.endObject(); if (wrapped) { reader.endObject(); } reader.peek(); // to assert end of structure or document return result; } catch (final IOException e) { throw new EntityProviderException( EntityProviderException.EXCEPTION_OCCURRED.addContent(e.getClass().getSimpleName()), e); } catch (final IllegalStateException e) { throw new EntityProviderException( EntityProviderException.EXCEPTION_OCCURRED.addContent(e.getClass().getSimpleName()), e); } }
From source file:com.sap.core.odata.core.ep.consumer.JsonLinkConsumer.java
License:Apache License
private List<String> readLinksArray(final JsonReader reader) throws IOException, EntityProviderException { List<String> links = new ArrayList<String>(); reader.beginArray();/* w w w .j a va2s .c om*/ while (reader.hasNext()) { reader.beginObject(); String nextName = reader.nextName(); if (FormatJson.URI.equals(nextName) && reader.peek() == JsonToken.STRING) { links.add(reader.nextString()); } else { throw new EntityProviderException( EntityProviderException.INVALID_CONTENT.addContent(FormatJson.URI).addContent(nextName)); } reader.endObject(); } reader.endArray(); return links; }
From source file:com.sap.core.odata.core.ep.consumer.JsonPropertyConsumer.java
License:Apache License
private Object readSimpleProperty(final JsonReader reader, final EntityPropertyInfo entityPropertyInfo, final Object typeMapping) throws EdmException, EntityProviderException, IOException { final EdmSimpleType type = (EdmSimpleType) entityPropertyInfo.getType(); Object value = null;/* www . ja va2 s .c om*/ final JsonToken tokenType = reader.peek(); if (tokenType == JsonToken.NULL) { reader.nextNull(); } else { switch (EdmSimpleTypeKind.valueOf(type.getName())) { case Boolean: if (tokenType == JsonToken.BOOLEAN) { value = reader.nextBoolean(); value = value.toString(); } else { throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE .addContent(entityPropertyInfo.getName())); } break; case Byte: case SByte: case Int16: case Int32: if (tokenType == JsonToken.NUMBER) { value = reader.nextInt(); value = value.toString(); } else { throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE .addContent(entityPropertyInfo.getName())); } break; default: if (tokenType == JsonToken.STRING) { value = reader.nextString(); } else { throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE .addContent(entityPropertyInfo.getName())); } break; } } final Class<?> typeMappingClass = typeMapping == null ? type.getDefaultType() : (Class<?>) typeMapping; return type.valueOfString((String) value, EdmLiteralKind.JSON, entityPropertyInfo.getFacets(), typeMappingClass); }
From source file:com.splunk.ResultsReaderJson.java
License:Apache License
/** * Skip the next value, whether it is atomic or compound, in the JSON * stream.//from www . j a va 2s. c om */ private void skipEntity() throws IOException { if (jsonReader.peek() == JsonToken.STRING) { jsonReader.nextString(); } else if (jsonReader.peek() == JsonToken.BOOLEAN) { jsonReader.nextBoolean(); } else if (jsonReader.peek() == JsonToken.NUMBER) { jsonReader.nextDouble(); } else if (jsonReader.peek() == JsonToken.NULL) { jsonReader.nextNull(); } else if (jsonReader.peek() == JsonToken.NAME) { jsonReader.nextName(); } else if (jsonReader.peek() == JsonToken.BEGIN_ARRAY) { jsonReader.beginArray(); while (jsonReader.peek() != JsonToken.END_ARRAY) { skipEntity(); } jsonReader.endArray(); } else if (jsonReader.peek() == JsonToken.BEGIN_OBJECT) { jsonReader.beginObject(); while (jsonReader.peek() != JsonToken.END_OBJECT) { skipEntity(); } jsonReader.endObject(); } }
From source file:com.splunk.ResultsReaderJson.java
License:Apache License
private Event readEvent() throws IOException { Event returnData = null;/*from w ww . j a v a2 s. com*/ String name = null; List<String> values = new ArrayList<String>(); if (jsonReader == null) return null; // Events are almost flat, so no need for a true general parser // solution. But the Gson parser is a little unintuitive here. Nested // objects, have their own relative notion of hasNext. This // means that for every object or array start, hasNext() returns false // and one must consume the closing (END) object to get back to the // previous object. while (jsonReader.hasNext()) { if (returnData == null) { returnData = new Event(); } if (jsonReader.peek() == JsonToken.BEGIN_OBJECT) { jsonReader.beginObject(); } if (jsonReader.peek() == JsonToken.BEGIN_ARRAY) { jsonReader.beginArray(); // The Gson parser is a little unintuitive here. Nested objects, // have their own relative notion of hasNext; when hasNext() // is done, it is only for this array. while (jsonReader.hasNext()) { JsonToken jsonToken2 = jsonReader.peek(); if (jsonToken2 == JsonToken.STRING) { values.add(jsonReader.nextString()); } } jsonReader.endArray(); String[] valuesArray = values.toArray(new String[values.size()]); returnData.putArray(name, valuesArray); values.clear(); } if (jsonReader.peek() == JsonToken.NAME) { name = jsonReader.nextName(); } if (jsonReader.peek() == JsonToken.STRING) { String delimitedValues = jsonReader.nextString(); returnData.putSingleOrDelimited(name, delimitedValues); } if (jsonReader.peek() == JsonToken.END_OBJECT) { jsonReader.endObject(); break; } if (jsonReader.peek() == JsonToken.END_ARRAY) { jsonReader.endArray(); } } return returnData; }
From source file:com.splunk.sdk.ResultsReaderJson.java
License:Apache License
/** {@inheritDoc} */ @Override/*from w w w . ja v a 2 s.c om*/ public Event getNextEvent() throws IOException { Event returnData = null; String name = null; List<String> values = new ArrayList<String>(); if (jsonReader == null) return null; // Events are almost flat, so no need for a true general parser // solution. But the Gson parser is a little unintuitive here. Nested // objects, have their own relative notion of hasNext. This // means that for every object or array start, hasNext() returns false // and one must consume the closing (END) object to get back to the // previous object. while (jsonReader.hasNext()) { if (returnData == null) { returnData = new Event(); } if (jsonReader.peek() == JsonToken.BEGIN_OBJECT) { jsonReader.beginObject(); } if (jsonReader.peek() == JsonToken.BEGIN_ARRAY) { jsonReader.beginArray(); // The Gson parser is a little unintuitive here. Nested objects, // have their own relative notion of hasNext; when hasNext() // is done, it is only for this array. while (jsonReader.hasNext()) { JsonToken jsonToken2 = jsonReader.peek(); if (jsonToken2 == JsonToken.STRING) { values.add(jsonReader.nextString()); } } jsonReader.endArray(); String[] valuesArray = values.toArray(new String[values.size()]); returnData.putArray(name, valuesArray); values.clear(); } if (jsonReader.peek() == JsonToken.NAME) { name = jsonReader.nextName(); } if (jsonReader.peek() == JsonToken.STRING) { String delimitedValues = jsonReader.nextString(); returnData.putSingleOrDelimited(name, delimitedValues); } if (jsonReader.peek() == JsonToken.END_OBJECT) { jsonReader.endObject(); break; } if (jsonReader.peek() == JsonToken.END_ARRAY) { jsonReader.endArray(); } } return returnData; }
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 {/*from ww w. ja va2 s . com*/ 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); } }