List of usage examples for com.google.gson.stream JsonReader hasNext
public boolean hasNext() throws IOException
From source file:data.Task1bData.java
License:Apache License
public ArrayList<String> readConcepts(JsonReader reader) { ArrayList<String> conc = new ArrayList<String>(); int count = 0; try {//from w w w . j a va 2s . c om reader.beginArray(); while (reader.hasNext()) { String nextString = reader.nextString(); if (!conc.contains(nextString)) conc.add(nextString); } reader.endArray(); } catch (IOException ex) { } return conc; }
From source file:data.Task1bData.java
License:Apache License
public ArrayList<String> readDocuments(JsonReader reader) { ArrayList<String> docs = new ArrayList<String>(); int count = 0; try {/*from w w w .j a v a 2 s .c om*/ reader.beginArray(); while (reader.hasNext()) { String nextString = reader.nextString(); if (!docs.contains(nextString)) docs.add(nextString); } reader.endArray(); } catch (IOException ex) { } return docs; }
From source file:data.TaskADataParser.java
License:Apache License
public static PubMedDocument getNextDocument(JsonReader reader) { String text = null;/*from www . j a va 2 s . c o m*/ String title = null; String pmid = null; String journal = null; String[] meshMajor = null; try { if (reader.hasNext()) { reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); if (name.equals("abstractText")) { text = reader.nextString(); } else if (name.equals("journal")) { journal = reader.nextString(); } else if (name.equals("meshMajor")) { meshMajor = readLabelsArray(reader); } else if (name.equals("pmid")) { pmid = reader.nextString(); } else if (name.equals("title")) { title = reader.nextString(); } else if (name.equals("year")) { reader.skipValue(); } else { System.out.println(name); reader.skipValue(); } } reader.endObject(); } } catch (Exception ex) { } return new PubMedDocument(text, title, pmid, journal, meshMajor); }
From source file:data.TaskADataParser.java
License:Apache License
public static String[] readLabelsArray(JsonReader reader) { String labels[];//from ww w .j a va 2 s . c om ArrayList<String> lab = new ArrayList<String>(); try { reader.beginArray(); while (reader.hasNext()) { String nextString = reader.nextString(); lab.add(nextString); } reader.endArray(); } catch (IOException ex) { } labels = new String[lab.size()]; labels = lab.toArray(labels); return labels; }
From source file:de.dentrassi.eclipse.provider.eclipse.ValueAdapter.java
License:Open Source License
@Override public String read(final JsonReader in) throws IOException { if (in.peek() == JsonToken.NULL) { return null; }/* w w w .j a v a2 s .c o m*/ String result = null; in.beginArray(); while (in.hasNext()) { in.beginObject(); while (in.hasNext()) { final String name = in.nextName(); if ("value".equals(name)) { result = in.nextString(); } else { in.skipValue(); } } in.endObject(); } in.endArray(); return result; }
From source file:de.loercher.geomodule.cloudant.CloudantGeoSearchStream.java
private void extractEntities(BufferedReader bufferedReader) throws JSONParseException { JsonReader reader = new JsonReader(bufferedReader); try {/* w ww. j a v a 2 s .c o m*/ reader.beginObject(); String arrayName = null; while (reader.hasNext() && !("features".equals(arrayName))) { arrayName = reader.nextName(); if ("bookmark".equals(arrayName)) { bookmark = reader.nextString(); } else if (!("features".equals(arrayName))) { reader.skipValue(); } } if ("features".equals(arrayName)) { reader.beginArray(); while (reader.hasNext()) { Gson gson = new Gson(); CloudantArticleEntity entity = gson.fromJson(reader, CloudantArticleEntity.class); // Duplicates should not be returned if (!(alreadyAvailableIds.contains(entity.getId()))) { bufferedEntities.add(entity); } alreadyAvailableIds.add(entity.getId()); } reader.endArray(); reader.endObject(); reader.close(); } else { JSONParseException e = new JSONParseException( "Parsing of cloudant response failed. Tag 'features' not found. "); log.error(e.getLoggingString()); throw e; } } catch (IOException ex) { JSONParseException e = new JSONParseException("Parsing of cloudant response failed.", ex); log.error(e.getLoggingString()); throw e; } }
From source file:de.pribluda.android.jsonmarshaller.JSONUnmarshaller.java
License:Apache License
/** * unmarshall single JSON object//from ww w . ja v a2 s. co m * * @param beanToBeCreatedClass * @param <T> * @return * @throws IllegalAccessException * @throws InstantiationException * @throws NoSuchMethodException * @throws InvocationTargetException */ public static <T> T unmarshall(JsonReader reader, java.lang.Class<T> beanToBeCreatedClass) throws IOException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException { // nothing there - bail out reader.beginObject(); if (reader.peek() == null) { return null; } T value = beanToBeCreatedClass.getConstructor().newInstance(); while (reader.hasNext()) { String key = reader.nextName(); // capitalise to standard setter pattern StringBuilder sb = new StringBuilder(); sb.append(SETTER_PREFIX).append(Character.toUpperCase(key.charAt(0))).append(key.substring(1)); String methodName = sb.toString(); Method method = getCandidateMethod(beanToBeCreatedClass, methodName); // must be kind of setter method if (method != null) { Class clazz = method.getParameterTypes()[0]; // as we have setter, we can process value Object v = unmarshalValue(reader, clazz); // can we use setter method directly? if (clazz.isAssignableFrom(v.getClass())) { method.invoke(value, v); continue; } Object obj = convertToObject(clazz, v); if (obj != null) method.invoke(value, obj); } else { // no suitable method was found - skip this value altogether reader.skipValue(); } } reader.endObject(); return value; }
From source file:de.pribluda.android.jsonmarshaller.JSONUnmarshaller.java
License:Apache License
/** * recursively populate array out of hierarchy of JSON Objects * * @param arrayClass original array class * @param reader reader to be processed * @return//w w w .j av a 2 s.com */ private static Object populateRecusrsive(Class arrayClass, JsonReader reader) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException, IOException { ArrayList value = new ArrayList(); Object retval = null; reader.beginArray(); if (arrayClass.isArray()) { // create list, as we do not know size yet final Class componentType = arrayClass.getComponentType(); // iterate over reader while (reader.hasNext()) { Object component; if (componentType.isArray()) { // component is array - dive down component = populateRecusrsive(componentType, reader); if (component != null) { value.add(component); } } else { // component is leaf, Object leaf = unmarshalValue(reader, componentType); Object obj = convertToObject(componentType, leaf); if (obj != null) { value.add(obj); } } } // copy everything to array, retval = Array.newInstance(componentType, value.size()); for (int i = 0; i < value.size(); i++) { Array.set(retval, i, value.get(i)); } } else { return null; } reader.endArray(); return retval; }
From source file:de.sabian.objectstore.ObjectStoreRaw.java
License:Apache License
public <T> boolean fillCollection(String identifier, Class<T> clazz, Collection<T> collection) throws IOException { if (contains(identifier)) { JsonReader jReader = new JsonReader(new StringReader(mJsonStorage.getJson(identifier))); jReader.beginArray();/*from w ww . j a v a2 s . c om*/ while (jReader.hasNext()) { T object = mGson.fromJson(jReader, clazz); collection.add(object); } jReader.endArray(); jReader.close(); return true; } else return false; }
From source file:de.sabian.objectstore.ObjectStoreRaw.java
License:Apache License
public <T, X> boolean fillMap(String identifier, Class<T> classOfKeys, Class<X> classOfValues, Map<T, X> map) throws IOException { if (contains(identifier)) { JsonReader jReader = new JsonReader(new StringReader(mJsonStorage.getJson(identifier))); jReader.beginArray();//from w w w .ja va 2 s . c o m while (jReader.hasNext()) { jReader.beginObject(); String name = jReader.nextName(); if (!name.equals("key")) throw new IOException("expected name key but found: " + name); T key = mGson.fromJson(jReader, classOfKeys); name = jReader.nextName(); if (!name.equals("value")) throw new IOException("expected name value but found: " + name); X value = mGson.fromJson(jReader, classOfValues); jReader.endObject(); map.put(key, value); } jReader.endArray(); jReader.close(); return true; } else return false; }