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

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

Introduction

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

Prototype

public void endObject() throws IOException 

Source Link

Document

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

Usage

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

License:Open Source License

/**
 * Parses an JSON stream and loads it into a document and returns
 * an instance to it./*from w  w  w  .  j av a2s  .com*/
 *
 * @param aReader Json reader stream instance.
 *
 * @return Document instance containing the parsed data.
 *
 * @throws java.io.IOException I/O related exception.
 */
public Document loadDocument(JsonReader aReader) throws IOException {
    String jsonName, jsonValue;

    Document document = new Document(IO.XML_DOCUMENT_NODE_NAME);

    aReader.beginObject();
    while (aReader.hasNext()) {
        jsonName = aReader.nextName();
        if (StringUtils.equals(jsonName, IO.JSON_NAME_MEMBER_NAME))
            document.setName(aReader.nextString());
        else if (StringUtils.equals(jsonName, IO.JSON_TYPE_MEMBER_NAME))
            document.setType(aReader.nextString());
        else if (StringUtils.equals(jsonName, IO.JSON_VERSION_MEMBER_NAME))
            document.setSchemaVersion(Integer.parseInt(aReader.nextString()));
        else if (StringUtils.equals(jsonName, IO.JSON_TITLE_MEMBER_NAME))
            document.setTitle(aReader.nextString());
        else if (StringUtils.equals(jsonName, IO.JSON_FEATURES_ARRAY_NAME)) {
            aReader.beginObject();
            while (aReader.hasNext()) {
                jsonName = aReader.nextName();
                jsonValue = aReader.nextString();
                document.addFeature(jsonName, jsonValue);
            }
            aReader.endObject();
        } else if (StringUtils.equals(jsonName, IO.JSON_TABLE_OBJECT_NAME)) {
            DataTableJSON dataTableJSON = new DataTableJSON();
            dataTableJSON.load(aReader);
            document.setTable(dataTableJSON.getTable());
        } else if (StringUtils.equals(jsonName, IO.JSON_RELATED_ARRAY_NAME))
            loadRelated(aReader, document);
        else if (StringUtils.equals(jsonName, IO.JSON_ACL_ARRAY_NAME))
            loadACL(aReader, document);
        else
            aReader.skipValue();
    }
    aReader.endObject();

    return document;
}

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   ww w.j a v  a 2 s . co  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  w  w  w . java 2s .  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();
}

From source file:com.nridge.core.io.gson.DSCriteriaJSON.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  .  c  o  m
public void load(JsonReader aReader) throws IOException {
    DataField dataField;
    String jsonName, jsonValue, logicalOperator;

    mDSCriteria.reset();

    aReader.beginObject();
    while (aReader.hasNext()) {
        jsonName = aReader.nextName();
        if (StringUtils.equals(jsonName, IO.JSON_NAME_MEMBER_NAME))
            mDSCriteria.setName(aReader.nextString());
        else if (StringUtils.equals(jsonName, IO.JSON_FEATURES_ARRAY_NAME)) {
            aReader.beginObject();
            while (aReader.hasNext()) {
                jsonName = aReader.nextName();
                jsonValue = aReader.nextString();
                mDSCriteria.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) {
                    logicalOperator = dataField.getFeature(IO.JSON_OPERATOR_MEMBER_NAME);
                    if (StringUtils.isEmpty(logicalOperator))
                        logicalOperator = Field.operatorToString(Field.Operator.EQUAL);
                    mDSCriteria.add(dataField, Field.stringToOperator(logicalOperator));
                }
            }
            aReader.endArray();
        } else
            aReader.skipValue();
    }
    aReader.endObject();
}

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

License:Open Source License

/**
 * Parses an JSON stream and loads it into an internally managed
 * document instance.//  www.  jav a 2s.co m
 *
 * @param aReader Json reader stream instance.
 *
 * @throws IOException I/O related exception.
 */
private void loadDocument(JsonReader aReader, Document aDocument) throws IOException {
    DataBag dataBag;
    JsonToken jsonToken;
    DataField dataField;
    String jsonName, jsonValue, jsonTitle;

    aReader.beginObject();

    jsonToken = aReader.peek();
    while (jsonToken == JsonToken.NAME) {
        jsonName = aReader.nextName();
        jsonTitle = Field.nameToTitle(jsonName);

        jsonToken = aReader.peek();
        switch (jsonToken) {
        case BOOLEAN:
            dataBag = aDocument.getBag();
            dataField = new DataField(jsonName, jsonTitle, aReader.nextBoolean());
            dataBag.add(dataField);
            break;
        case NUMBER:
            dataBag = aDocument.getBag();
            jsonValue = aReader.nextString();
            if (StringUtils.contains(jsonValue, StrUtl.CHAR_DOT))
                dataField = new DataField(jsonName, jsonTitle, Double.valueOf(jsonValue));
            else
                dataField = new DataField(jsonName, jsonTitle, Long.valueOf(jsonValue));
            dataBag.add(dataField);
            break;
        case STRING:
            dataBag = aDocument.getBag();
            jsonValue = aReader.nextString();
            Date dateValue = DatUtl.detectCreateDate(jsonValue);
            if (dateValue != null)
                dataField = new DataField(jsonName, jsonTitle, dateValue);
            else
                dataField = new DataField(Field.Type.Text, jsonName, jsonTitle, jsonValue);
            dataBag.add(dataField);
            break;
        case NULL:
            dataBag = aDocument.getBag();
            aReader.nextNull();
            dataField = new DataField(Field.Type.Text, jsonName, jsonTitle);
            dataBag.add(dataField);
            break;
        case BEGIN_ARRAY:
            aReader.beginArray();
            if (isNextTokenAnObject(aReader)) {
                Document childDocument = new Document(jsonTitle);
                childDocument.setName(jsonName);
                aDocument.addRelationship(jsonTitle, childDocument);
                loadDocumentArray(aReader, childDocument);
            } else {
                dataBag = aDocument.getBag();
                dataField = new DataField(Field.Type.Text, jsonName, jsonTitle);
                jsonToken = aReader.peek();
                while (jsonToken != JsonToken.END_ARRAY) {
                    jsonValue = aReader.nextString();
                    dataField.addValue(jsonValue);
                    jsonToken = aReader.peek();
                }
                dataBag.add(dataField);
            }
            aReader.endArray();
            break;
        case BEGIN_OBJECT:
            Document childDocument = new Document(jsonTitle);
            childDocument.setName(jsonName);
            aDocument.addRelationship(jsonTitle, childDocument);
            loadDocument(aReader, childDocument);
            break;
        default:
            aReader.skipValue();
            break;
        }
        jsonToken = aReader.peek();
    }

    aReader.endObject();
}

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

License:Open Source License

/**
 * Parses an JSON stream and loads it into a field range.
 *
 * @param aReader Json reader stream instance.
 *
 * @throws java.io.IOException I/O related exception.
 *///from   w  ww . j av a 2 s  .  com
public FieldRange load(JsonReader aReader) throws IOException {
    String jsonName;

    boolean isFirst = true;
    Date firstDate = new Date();
    long firstLong = Long.MIN_VALUE;
    int firstInt = Integer.MIN_VALUE;
    double firstDouble = Double.MIN_VALUE;

    Field.Type rangeType = Field.Type.Text;
    FieldRange fieldRange = new FieldRange();

    aReader.beginObject();
    while (aReader.hasNext()) {
        jsonName = aReader.nextName();
        if (StringUtils.equals(jsonName, IO.JSON_TYPE_MEMBER_NAME))
            rangeType = Field.stringToType(aReader.nextString());
        else if (StringUtils.equals(jsonName, IO.JSON_DELIMITER_MEMBER_NAME))
            fieldRange.setDelimiterChar(aReader.nextString());
        else if (StringUtils.equals(jsonName, IO.JSON_VALUE_MEMBER_NAME))
            fieldRange.setItems(StrUtl.expandToList(aReader.nextString(), fieldRange.getDelimiterChar()));
        else if (StringUtils.equals(jsonName, "min")) {
            switch (rangeType) {
            case Long:
                if (isFirst) {
                    isFirst = false;
                    firstLong = aReader.nextLong();
                } else
                    fieldRange = new FieldRange(aReader.nextLong(), firstLong);
                break;
            case Integer:
                if (isFirst) {
                    isFirst = false;
                    firstInt = aReader.nextInt();
                } else
                    fieldRange = new FieldRange(aReader.nextInt(), firstInt);
                break;
            case Double:
                if (isFirst) {
                    isFirst = false;
                    firstDouble = aReader.nextDouble();
                } else
                    fieldRange = new FieldRange(aReader.nextDouble(), firstDouble);
                break;
            case DateTime:
                if (isFirst) {
                    isFirst = false;
                    firstDate = Field.createDate(aReader.nextString());
                } else
                    fieldRange = new FieldRange(Field.createDate(aReader.nextString()), firstDate);
                break;
            default:
                aReader.skipValue();
                break;
            }
        } else if (StringUtils.equals(jsonName, "max")) {
            switch (rangeType) {
            case Long:
                if (isFirst) {
                    isFirst = false;
                    firstLong = aReader.nextLong();
                } else
                    fieldRange = new FieldRange(firstLong, aReader.nextLong());
                break;
            case Integer:
                if (isFirst) {
                    isFirst = false;
                    firstInt = aReader.nextInt();
                } else
                    fieldRange = new FieldRange(firstInt, aReader.nextInt());
                break;
            case Double:
                if (isFirst) {
                    isFirst = false;
                    firstDouble = aReader.nextDouble();
                } else
                    fieldRange = new FieldRange(firstDouble, aReader.nextDouble());
                break;
            case DateTime:
                if (isFirst) {
                    isFirst = false;
                    firstDate = Field.createDate(aReader.nextString());
                } else
                    fieldRange = new FieldRange(firstDate, Field.createDate(aReader.nextString()));
                break;
            default:
                aReader.skipValue();
                break;
            }
        } else
            aReader.skipValue();
    }

    aReader.endObject();

    return fieldRange;
}

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

License:Open Source License

/**
 * Parses an JSON stream and loads it into a relationship and returns
 * an instance to it.//w  w w. ja v  a  2  s.  c om
 *
 * @param aReader Json reader stream instance.
 *
 * @throws java.io.IOException I/O related exception.
 */
public Relationship loadRelationship(JsonReader aReader) throws IOException {
    String jsonName, jsonValue;

    Relationship relationship = new Relationship();

    aReader.beginObject();
    while (aReader.hasNext()) {
        jsonName = aReader.nextName();
        if (StringUtils.equals(jsonName, IO.JSON_TYPE_MEMBER_NAME))
            relationship.setType(aReader.nextString());
        else if (StringUtils.equals(jsonName, IO.JSON_FEATURES_ARRAY_NAME)) {
            aReader.beginObject();
            while (aReader.hasNext()) {
                jsonName = aReader.nextName();
                jsonValue = aReader.nextString();
                relationship.addFeature(jsonName, jsonValue);
            }
            aReader.endObject();
        } else if (StringUtils.equals(jsonName, IO.JSON_DOCUMENTS_ARRAY_NAME))
            loadDocuments(aReader, relationship);
        else
            aReader.skipValue();
    }
    aReader.endObject();

    return relationship;
}

From source file:com.nridge.ds.solr.SolrCollection.java

License:Open Source License

private ArrayList<String> load(InputStream anIS) throws IOException {
    String jsonName, jsonValue;/*from w ww  .jav a  2 s  .c  o m*/

    ArrayList<String> configSetList = new ArrayList<>();
    String configSetString = IOUtils.toString(anIS, StrUtl.CHARSET_UTF_8);
    StringReader stringReader = new StringReader(configSetString);
    JsonReader jsonReader = new JsonReader(stringReader);
    jsonReader.beginObject();
    while (jsonReader.hasNext()) {
        jsonName = jsonReader.nextName();
        if (StringUtils.equals(jsonName, "collections")) {
            jsonReader.beginArray();
            while (jsonReader.hasNext()) {
                jsonValue = jsonReader.nextString();
                configSetList.add(jsonValue);
            }
            jsonReader.endArray();
        }
    }
    jsonReader.endObject();

    return configSetList;
}

From source file:com.nridge.ds.solr.SolrCollection.java

License:Open Source License

private void parseReply(String aMessage) throws DSException, IOException {
    String jsonName;//  w w  w  .ja v  a2  s  . c  o  m

    Logger appLogger = mAppMgr.getLogger(this, "parseReply");

    appLogger.trace(mAppMgr.LOGMSG_TRACE_ENTER);

    int msgCode = 0;
    String msgString = StringUtils.EMPTY;
    StringReader stringReader = new StringReader(aMessage);
    JsonReader jsonReader = new JsonReader(stringReader);
    jsonReader.beginObject();
    while (jsonReader.hasNext()) {
        jsonName = jsonReader.nextName();
        if (StringUtils.equals(jsonName, "exception")) {
            jsonReader.beginObject();
            while (jsonReader.hasNext()) {
                jsonName = jsonReader.nextName();
                if (StringUtils.equals(jsonName, "msg"))
                    msgString = jsonReader.nextString();
                else if (StringUtils.equals(jsonName, "rspCode"))
                    msgCode = jsonReader.nextInt();
                else
                    jsonReader.skipValue();
            }
            jsonReader.endObject();
        } else
            jsonReader.skipValue();
    }
    jsonReader.endObject();

    if (StringUtils.isNotEmpty(msgString))
        throw new DSException(String.format("Solr Exception [%d]: %s", msgCode, msgString));

    appLogger.trace(mAppMgr.LOGMSG_TRACE_DEPART);
}

From source file:com.nridge.ds.solr.SolrConfigJSON.java

License:Open Source License

private void populateCfgUpdateHandler(JsonReader aReader, Document aCfgDocument, String aDocType)
        throws IOException {
    DataBag dataBag;//w  w  w  . j  a v a2  s  .  com
    String jsonName, jsonValue;
    Document iwDocument, cwDocument, acDocument, ascDocument;

    Document uhDocument = new Document(aDocType);
    DataBag uhBag = uhDocument.getBag();
    aReader.beginObject();
    while (aReader.hasNext()) {
        jsonName = aReader.nextName();
        if (StringUtils.equals(jsonName, OBJECT_RESPONSE_CONFIG_UH_IW)) {
            iwDocument = new Document(Solr.RESPONSE_CONFIG_UH_INDEX_WRITER);
            iwDocument.setName(jsonName);
            dataBag = iwDocument.getBag();
            aReader.beginObject();
            while (aReader.hasNext()) {
                jsonName = aReader.nextName();
                jsonValue = nextValueAsString(aReader);
                addBagTextField(dataBag, jsonName, jsonValue);
            }
            aReader.endObject();
            uhDocument.addRelationship(Solr.RESPONSE_CONFIG_UH_INDEX_WRITER, iwDocument);
        } else if (StringUtils.equals(jsonName, OBJECT_RESPONSE_CONFIG_UH_CW)) {
            cwDocument = new Document(Solr.RESPONSE_CONFIG_UH_COMMIT_WITHIN);
            cwDocument.setName(jsonName);
            dataBag = cwDocument.getBag();
            aReader.beginObject();
            while (aReader.hasNext()) {
                jsonName = aReader.nextName();
                jsonValue = nextValueAsString(aReader);
                addBagTextField(dataBag, jsonName, jsonValue);
            }
            aReader.endObject();
            uhDocument.addRelationship(Solr.RESPONSE_CONFIG_UH_COMMIT_WITHIN, cwDocument);
        } else if (StringUtils.equals(jsonName, OBJECT_RESPONSE_CONFIG_UH_AC)) {
            acDocument = new Document(Solr.RESPONSE_CONFIG_UH_AUTO_COMMIT);
            acDocument.setName(jsonName);
            dataBag = acDocument.getBag();
            aReader.beginObject();
            while (aReader.hasNext()) {
                jsonName = aReader.nextName();
                jsonValue = nextValueAsString(aReader);
                addBagTextField(dataBag, jsonName, jsonValue);
            }
            aReader.endObject();
            uhDocument.addRelationship(Solr.RESPONSE_CONFIG_UH_AUTO_COMMIT, acDocument);
        } else if (StringUtils.equals(jsonName, OBJECT_RESPONSE_CONFIG_UH_ASC)) {
            ascDocument = new Document(Solr.RESPONSE_CONFIG_UH_AUTO_SOFT_COMMIT);
            ascDocument.setName(jsonName);
            dataBag = ascDocument.getBag();
            aReader.beginObject();
            while (aReader.hasNext()) {
                jsonName = aReader.nextName();
                jsonValue = nextValueAsString(aReader);
                addBagTextField(dataBag, jsonName, jsonValue);
            }
            aReader.endObject();
            uhDocument.addRelationship(Solr.RESPONSE_CONFIG_UH_AUTO_SOFT_COMMIT, ascDocument);
        } else {
            jsonValue = nextValueAsString(aReader);
            addBagTextField(uhBag, jsonName, jsonValue);
        }
    }
    aReader.endObject();
    aCfgDocument.addRelationship(Solr.RESPONSE_CONFIG_UPDATE_HANDLER, uhDocument);
}