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

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

Introduction

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

Prototype

public void endArray() throws IOException 

Source Link

Document

Consumes the next token from the JSON stream and asserts that it is the end of the current array.

Usage

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();// w w  w  .  j  a  va  2s .  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();//from  w ww.jav  a  2s. c  o  m
    try {
        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  a2  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./*w  ww  .  j  a  v  a 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.
 *//*w w  w.j a v  a 2s. c  o 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.
 */// ww w  . java  2s.  c  o  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();/*w ww  .  java 2 s  .co  m*/
    while (aReader.hasNext()) {
        aceName = StringUtils.EMPTY;

        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   w  w 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();
}

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

License:Open Source License

/**
 * Parses an JSON stream and loads it into an internally managed
 * document operation instance.//from  w w  w .jav a 2 s . c o m
 *
 * @param aReader Json reader stream instance.
 *
 * @throws java.io.IOException I/O related exception.
 */
public void load(JsonReader aReader) throws IOException {
    String jsonName;

    mCriteria = null;
    mDocumentList.clear();
    mField.clearFeatures();

    aReader.beginObject();
    while (aReader.hasNext()) {
        jsonName = aReader.nextName();
        if (StringUtils.equals(jsonName, IO.JSON_FIELD_OBJECT_NAME)) {
            DataFieldJSON dataFieldJSON = new DataFieldJSON();
            mField = dataFieldJSON.load(aReader);
        } else if (StringUtils.equals(jsonName, IO.JSON_DOCUMENTS_ARRAY_NAME)) {
            DocumentJSON documentJSON;

            aReader.beginArray();
            while (aReader.hasNext()) {
                documentJSON = new DocumentJSON();
                documentJSON.load(aReader);
                mDocumentList.add(documentJSON.getDocument());
            }
            aReader.endArray();
        } else if (StringUtils.equals(jsonName, IO.JSON_CRITERIA_OBJECT_NAME)) {
            DSCriteriaJSON dsCriteriaJSON = new DSCriteriaJSON();
            dsCriteriaJSON.load(aReader);
            mCriteria = dsCriteriaJSON.getCriteria();
        } else
            aReader.skipValue();
    }

    aReader.endObject();
}

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

License:Open Source License

/**
 * Parses an JSON stream and loads it into an internally managed
 * document instance./*from ww w. ja va 2  s  .c om*/
 *
 * @param aReader Json reader stream instance.
 *
 * @throws java.io.IOException I/O related exception.
 */
public void load(JsonReader aReader) throws IOException {
    String jsonName;

    mDocumentList.clear();
    mField.clearFeatures();

    aReader.beginObject();
    while (aReader.hasNext()) {
        jsonName = aReader.nextName();
        if (StringUtils.equals(jsonName, IO.JSON_FIELD_OBJECT_NAME)) {
            DataFieldJSON dataFieldJSON = new DataFieldJSON();
            mField = dataFieldJSON.load(aReader);
        } else if (StringUtils.equals(jsonName, IO.JSON_DOCUMENTS_ARRAY_NAME)) {
            DocumentJSON documentJSON;

            aReader.beginArray();
            while (aReader.hasNext()) {
                documentJSON = new DocumentJSON();
                documentJSON.load(aReader);
                mDocumentList.add(documentJSON.getDocument());
            }
            aReader.endArray();
        } else
            aReader.skipValue();
    }

    aReader.endObject();
}