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

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

Introduction

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

Prototype

public void beginArray() throws IOException 

Source Link

Document

Consumes the next token from the JSON stream and asserts that it is the beginning of a new array.

Usage

From source file:data.Task1bData.java

License:Apache License

private ArrayList<Snippet> readSnippets(JsonReader reader) {
    ArrayList<Snippet> snippets = new ArrayList<Snippet>();

    try {// w  w  w  .j a  v  a2 s.c om
        reader.beginArray();
        while (reader.hasNext()) {

            reader.beginObject();
            String document = "", fnameBegin = "", fnameEnd = "", text = "";
            int beginIndex = 0;
            int endIndex = 0;

            while (reader.hasNext()) {
                String name = reader.nextName();
                if (name.equals("offsetInBeginSection")) {
                    beginIndex = reader.nextInt();
                } else if (name.equals("offsetInEndSection")) {
                    endIndex = reader.nextInt();
                } else if (name.equals("document")) {
                    document = reader.nextString();
                } else if (name.equals("beginSection")) {
                    fnameBegin = reader.nextString();
                    fnameBegin = fnameBegin.substring(fnameBegin.indexOf('.') + 1);
                } else if (name.equals("endSection")) {
                    fnameEnd = reader.nextString();
                    fnameEnd = fnameEnd.substring(fnameEnd.indexOf('.') + 1);
                } else if (name.equals("text")) {
                    text = reader.nextString();
                } else {
                    //System.out.println("Unknown field "+name +" in snippet");

                }
            }
            Snippet sn = new Snippet(document, text, fnameBegin, fnameEnd, beginIndex, endIndex);
            reader.endObject();
            snippets.add(sn);
        }
        reader.endArray();
    } catch (IOException ex) {
    }

    return snippets;
}

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 {/*w  w w.  j a  va 2s .  c o  m*/
        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  .ja  v  a 2 s  . c  o  m*/
        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

/**
 * /*w  w w.  j a v  a  2 s.  c  om*/
 * Return a json reader and opens the array
 * 
 */
public static JsonReader streamParser(String jsonFile) throws IOException {

    int count = 0;
    int abstract_count = 0;
    int duplicates = 0;
    JsonReader reader = null;
    try {
        reader = new JsonReader(new InputStreamReader(new FileInputStream(jsonFile)));
        reader.setLenient(true);
        reader.beginObject();
        String nam = reader.nextName();
        System.out.println(nam);
        reader.beginArray();

    } catch (Exception ex) {
        System.out.println("File not found");
        System.out.println(ex.toString());
    }
    return reader;
}

From source file:data.TaskADataParser.java

License:Apache License

public static String[] readLabelsArray(JsonReader reader) {

    String labels[];//  ww  w  . jav  a 2s  . co m
    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;
    }//ww  w  .j  a v  a2  s .c om

    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 {/*from  www. 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

/**
 * read array into list/*from  w  w w  . j a  v a  2  s  .  com*/
 *
 * @param reader
 * @param beanToBeCreatedClass
 * @return
 */
public static <T> List<T> unmarshallArray(JsonReader reader, java.lang.Class<T> beanToBeCreatedClass)
        throws IOException, InvocationTargetException, NoSuchMethodException, InstantiationException,
        IllegalAccessException {
    ArrayList<T> retval = new ArrayList();
    reader.beginArray();
    // read objects after each other
    while (reader.peek() == JsonToken.BEGIN_OBJECT) {
        retval.add(unmarshall(reader, beanToBeCreatedClass));
    }
    reader.endArray();

    return retval;
}

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  ww  . j  a v 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();
        while (jReader.hasNext()) {
            T object = mGson.fromJson(jReader, clazz);
            collection.add(object);//w  ww.  j a v  a2  s  .  c om
        }
        jReader.endArray();
        jReader.close();
        return true;
    } else
        return false;
}