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:com.ichi2.anki.AnkiDroidProxy.java

License:Open Source License

/**
 * Parses a JSON InputStream to a list of SharedDecks efficiently 
 * @param is InputStream to parse and convert. It only works with the reply from
 *        getSharedDecks request//w  w  w.jav a2 s  . c o m
 * @return List of SharedDecks that were parsed
 * @throws Exception 
 */
public static List<SharedDeck> parseGetSharedDecksResponce(InputStream is) throws Exception {
    BufferedReader rd = new BufferedReader(new InputStreamReader(is), 409600);
    JsonReader reader = new JsonReader(rd);
    List<SharedDeck> sharedDecks = new ArrayList<SharedDeck>();
    try {
        reader.beginArray();
        int count = 0;
        while (reader.hasNext()) {
            SharedDeck sd = new SharedDeck();
            reader.beginArray();
            sd.setId(reader.nextInt()); // SD_ID
            reader.skipValue(); // SD_USERNAME
            sd.setTitle(reader.nextString()); // SD_TITLE
            reader.skipValue(); // SD_DESCRIPTION
            reader.skipValue(); // SD_TAGS
            reader.skipValue(); // SD_VERSION
            sd.setFacts(reader.nextInt()); // SD_FACTS
            sd.setSize(reader.nextInt()); // SD_SIZE
            reader.skipValue(); // SD_COUNT
            reader.skipValue(); // SD_MODIFIED
            reader.skipValue(); // SD_FNAME
            reader.endArray();
            sharedDecks.add(sd);
            count++;
        }
        reader.endArray();
        reader.close();
        Log.d(AnkiDroidApp.TAG, "parseGetSharedDecksResponce: found " + count + " shared decks");
    } catch (Exception e) {
        Log.e(AnkiDroidApp.TAG, Log.getStackTraceString(e));
        sharedDecks.clear();
        throw e;
    }

    return sharedDecks;
}

From source file:com.javacreed.examples.gson.part3.BookTypeAdapter.java

License:Apache License

@Override
public Book read(final JsonReader in) throws IOException {
    final Book book = new Book();

    in.beginArray();
    book.setIsbn(in.nextString());//from  ww w .j  a  v a  2 s .  com
    book.setTitle(in.nextString());
    final List<Author> authors = new ArrayList<>();
    while (in.hasNext()) {
        final int id = in.nextInt();
        final String name = in.nextString();
        authors.add(new Author(id, name));
    }
    book.setAuthors(authors.toArray(new Author[authors.size()]));
    in.endArray();

    return book;
}

From source file:com.javacreed.examples.gson.part4.BookTypeAdapter.java

License:Apache License

@Override
public Book read(final JsonReader in) throws IOException {
    final Book book = new Book();

    in.beginObject();/*from   w  w w  .  ja v a 2  s.c  o m*/
    while (in.hasNext()) {
        switch (in.nextName()) {
        case "isbn":
            book.setIsbn(in.nextString());
            break;
        case "title":
            book.setTitle(in.nextString());
            break;
        case "authors":
            in.beginArray();
            final List<Author> authors = new ArrayList<>();
            while (in.hasNext()) {
                in.beginObject();
                final Author author = new Author();
                while (in.hasNext()) {
                    switch (in.nextName()) {
                    case "id":
                        author.setId(in.nextInt());
                        break;
                    case "name":
                        author.setName(in.nextString());
                        break;
                    }
                }
                authors.add(author);
                in.endObject();
            }
            book.setAuthors(authors.toArray(new Author[authors.size()]));
            in.endArray();
            break;
        }
    }
    in.endObject();

    return book;
}

From source file:com.magnet.android.mms.request.GenericResponseParser.java

License:Open Source License

private Collection fromJsonToPojoCollection(JsonReader jr, Class<?> bclass) throws IOException {
    List result = new ArrayList();
    // process each element in the array
    jr.beginArray();
    try {//from  w w  w . j av  a2s .c om
        while (jr.hasNext()) {
            Object obj = genericGson.fromJson(jr, bclass);
            ((ArrayList) result).add(obj);
        }
    } finally {
        jr.endArray();
    }
    return result;
}

From source file:com.miki.webapp.webservicerestful.MikiWsJsonTools.java

public List<Map<String, String>> lectureJson(List<String> listeAttribut, final JsonReader reader)
        throws IOException {
    List<Map<String, String>> resultat2 = new ArrayList<>();
    reader.setLenient(true);//from   ww w  .  j a v a  2 s.  c o  m
    reader.beginArray();
    while (reader.hasNext()) {
        resultat2.add(lecture(listeAttribut, reader));
    }
    reader.endArray();
    return resultat2;
}

From source file:com.netease.flume.taildirSource.ReliableTaildirEventReader.java

License:Apache License

/**
 * Load a position file which has the last read position of each file. If the position file exists, update tailFiles
 * mapping.//from  w  w  w .j  ava  2  s  . c  om
 */
public void loadPositionFile(String filePath) {
    Long inode, pos;
    String path;
    FileReader fr = null;
    JsonReader jr = null;
    try {
        fr = new FileReader(filePath);
        jr = new JsonReader(fr);
        jr.beginArray();
        while (jr.hasNext()) {
            inode = null;
            pos = null;
            path = null;
            jr.beginObject();
            while (jr.hasNext()) {
                switch (jr.nextName()) {
                case "inode":
                    inode = jr.nextLong();
                    break;
                case "pos":
                    pos = jr.nextLong();
                    break;
                case "file":
                    path = jr.nextString();
                    break;
                }
            }
            jr.endObject();

            for (Object v : Arrays.asList(inode, pos, path)) {
                Preconditions.checkNotNull(v, "Detected missing value in position file. " + "inode: " + inode
                        + ", pos: " + pos + ", path: " + path);
            }
            TailFile tf = tailFiles.get(inode);
            if (tf != null && tf.updatePos(path, inode, pos)) {
                tailFiles.put(inode, tf);
            } else {
                logger.info("Missing file: " + path + ", inode: " + inode + ", pos: " + pos);
            }
        }
        jr.endArray();
    } catch (FileNotFoundException e) {
        logger.info("File not found: " + filePath + ", not updating position");
    } catch (IOException e) {
        logger.error("Failed loading positionFile: " + filePath, e);
    } finally {
        try {
            if (fr != null)
                fr.close();
            if (jr != null)
                jr.close();
        } catch (IOException e) {
            logger.error("Error: " + e.getMessage(), e);
        }
    }
}

From source file:com.nridge.core.io.gson.DataBagJSON.java

License:Open Source License

/**
 * Parses an JSON stream and loads it into a bag/table.
 *
 * @param aReader Json reader stream instance.
 *
 * @throws java.io.IOException I/O related exception.
 *//*from   w w  w  .  j  a  va 2 s. co  m*/
public void load(JsonReader aReader) throws IOException {
    DataField dataField;
    String jsonName, jsonValue;

    aReader.beginObject();
    while (aReader.hasNext()) {
        jsonName = aReader.nextName();
        if (StringUtils.equals(jsonName, IO.JSON_NAME_MEMBER_NAME))
            mBag.setName(aReader.nextString());
        else if (StringUtils.equals(jsonName, IO.JSON_TITLE_MEMBER_NAME))
            mBag.setTitle(aReader.nextString());
        else if (StringUtils.equals(jsonName, IO.JSON_FEATURES_ARRAY_NAME)) {
            aReader.beginObject();
            while (aReader.hasNext()) {
                jsonName = aReader.nextName();
                jsonValue = aReader.nextString();
                mBag.addFeature(jsonName, jsonValue);
            }
            aReader.endObject();
        } else if (StringUtils.equals(jsonName, IO.JSON_FIELDS_ARRAY_NAME)) {
            aReader.beginArray();
            while (aReader.hasNext()) {
                dataField = mDataFieldJSON.load(aReader);
                if (dataField != null)
                    mBag.add(dataField);
            }
            aReader.endArray();
        } else
            aReader.skipValue();
    }

    aReader.endObject();
}

From source file:com.nridge.core.io.gson.DataTableJSON.java

License:Open Source License

/**
 * Parses an JSON stream and loads it into a bag/table.
 *
 * @param aReader Json reader stream instance.
 *
 * @throws java.io.IOException I/O related exception.
 *//* www . jav a2s.co  m*/
public void load(JsonReader aReader) throws IOException {
    int columnOffset;
    FieldRow fieldRow;
    DataField dataField;
    String jsonName, jsonValue, mvDelimiter;

    resetContext();
    aReader.beginObject();
    while (aReader.hasNext()) {
        jsonName = aReader.nextName();
        if (StringUtils.equals(jsonName, IO.JSON_NAME_MEMBER_NAME))
            mDataTable.setName(aReader.nextString());
        else if (StringUtils.equals(jsonName, IO.JSON_CONTEXT_OBJECT_NAME)) {
            aReader.beginObject();
            while (aReader.hasNext()) {
                jsonName = aReader.nextName();
                if (StringUtils.equals(jsonName, IO.JSON_START_MEMBER_NAME))
                    mContextStart = aReader.nextInt();
                else if (StringUtils.equals(jsonName, IO.JSON_LIMIT_MEMBER_NAME))
                    mContextLimit = aReader.nextInt();
                else if (StringUtils.equals(jsonName, IO.JSON_TOTAL_MEMBER_NAME))
                    mContextTotal = aReader.nextInt();
                else
                    aReader.skipValue();
            }
            aReader.endObject();
        } else if (StringUtils.equals(jsonName, IO.JSON_BAG_OBJECT_NAME)) {
            DataBagJSON dataBagJSON = new DataBagJSON();
            dataBagJSON.load(aReader);
            mDataTable.setColumns(dataBagJSON.getBag());
        } else if (StringUtils.equals(jsonName, IO.JSON_ROWS_ARRAY_NAME)) {
            aReader.beginArray();
            while (aReader.hasNext()) {
                columnOffset = 0;
                fieldRow = mDataTable.newRow();

                aReader.beginObject();
                while (aReader.hasNext()) {
                    jsonName = aReader.nextName();
                    if (StringUtils.equals(jsonName, IO.JSON_CELL_MEMBER_NAME)) {
                        jsonValue = aReader.nextString();
                        dataField = mDataTable.getColumn(columnOffset);
                        if (dataField != null) {
                            if (dataField.isMultiValue()) {
                                mvDelimiter = dataField.getFeature(Field.FEATURE_MV_DELIMITER);
                                if (StringUtils.isNotEmpty(mvDelimiter))
                                    fieldRow.setValues(columnOffset,
                                            StrUtl.expandToList(jsonValue, mvDelimiter.charAt(0)));
                                else
                                    fieldRow.setValues(columnOffset,
                                            StrUtl.expandToList(jsonValue, StrUtl.CHAR_PIPE));
                            } else
                                fieldRow.setValue(columnOffset, jsonValue);
                            columnOffset++;
                        }
                    } else
                        aReader.skipValue();
                }
                aReader.endObject();

                mDataTable.addRow(fieldRow);

            }
            aReader.endArray();
        } else
            aReader.skipValue();
    }
    aReader.endObject();
}

From source file:com.nridge.core.io.gson.DocumentJSON.java

License:Open Source License

private void loadACL(JsonReader aReader, Document aDocument) throws IOException {
    String jsonName, aceName, aceValue;

    HashMap<String, String> docACL = aDocument.getACL();
    aReader.beginArray();
    while (aReader.hasNext()) {
        aceName = StringUtils.EMPTY;/*from w  w w . j av a 2 s.  c o m*/

        aReader.beginObject();
        while (aReader.hasNext()) {
            jsonName = aReader.nextName();
            if (StringUtils.equals(jsonName, IO.JSON_NAME_MEMBER_NAME))
                aceName = aReader.nextString();
            else if (StringUtils.equals(jsonName, IO.JSON_VALUE_MEMBER_NAME)) {
                aceValue = aReader.nextString();
                if (StringUtils.isNotEmpty(aceName))
                    docACL.put(aceName, aceValue);
            } else
                aReader.skipValue();
        }
        aReader.endObject();
    }
    aReader.endArray();
}

From source file:com.nridge.core.io.gson.DocumentJSON.java

License:Open Source License

private void loadRelated(JsonReader aReader, Document aDocument) throws IOException {
    Relationship relationship;/*from  ww w  . j a  v a  2  s  .c om*/

    RelationshipJSON relationshipJSON = new RelationshipJSON();
    ArrayList<Relationship> relationshipList = aDocument.getRelationships();
    aReader.beginArray();
    while (aReader.hasNext()) {
        relationship = relationshipJSON.loadRelationship(aReader);
        relationshipList.add(relationship);
    }
    aReader.endArray();
}