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

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

Introduction

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

Prototype

public String nextName() throws IOException 

Source Link

Document

Returns the next token, a com.google.gson.stream.JsonToken#NAME property name , and consumes it.

Usage

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./*w ww  . j av a  2s.  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;

    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  ww  w.  j av  a2s  .c  om*/
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.// w ww  .j a  va2s. c om
 *
 * @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  w w .  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.//from  www .j  av a 2 s.  c  o  m
 *
 * @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   www  .  j av  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;//  ww w .j  a va 2  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  ww .jav a 2 s.c  om*/
    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);
}

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

License:Open Source License

private void populateCfgQuery(JsonReader aReader, Document aCfgDocument, String aDocType) throws IOException {
    DataBag dataBag;/*from  ww w  . j  a v a2 s.  c  o m*/
    String jsonName, jsonValue;
    Document fcDocument, rcDocument, dcDocument, fvcDocument;

    Document queryDocument = new Document(aDocType);
    DataBag queryBag = queryDocument.getBag();
    aReader.beginObject();
    while (aReader.hasNext()) {
        jsonName = aReader.nextName();
        if (StringUtils.equals(jsonName, OBJECT_RESPONSE_CONFIG_QUERY_FC)) {
            fcDocument = new Document(Solr.RESPONSE_CONFIG_QUERY_FC);
            fcDocument.setName(jsonName);
            dataBag = fcDocument.getBag();
            aReader.beginObject();
            while (aReader.hasNext()) {
                jsonName = aReader.nextName();
                jsonValue = nextValueAsString(aReader);
                addBagTextField(dataBag, jsonName, jsonValue);
            }
            aReader.endObject();
            queryDocument.addRelationship(Solr.RESPONSE_CONFIG_QUERY_FC, fcDocument);
        } else if (StringUtils.equals(jsonName, OBJECT_RESPONSE_CONFIG_QUERY_RC)) {
            rcDocument = new Document(Solr.RESPONSE_CONFIG_QUERY_RC);
            rcDocument.setName(jsonName);
            dataBag = rcDocument.getBag();
            aReader.beginObject();
            while (aReader.hasNext()) {
                jsonName = aReader.nextName();
                jsonValue = nextValueAsString(aReader);
                addBagTextField(dataBag, jsonName, jsonValue);
            }
            aReader.endObject();
            queryDocument.addRelationship(Solr.RESPONSE_CONFIG_QUERY_RC, rcDocument);
        } else if (StringUtils.equals(jsonName, OBJECT_RESPONSE_CONFIG_QUERY_DC)) {
            dcDocument = new Document(Solr.RESPONSE_CONFIG_QUERY_DC);
            dcDocument.setName(jsonName);
            dataBag = dcDocument.getBag();
            aReader.beginObject();
            while (aReader.hasNext()) {
                jsonName = aReader.nextName();
                jsonValue = nextValueAsString(aReader);
                addBagTextField(dataBag, jsonName, jsonValue);
            }
            aReader.endObject();
            queryDocument.addRelationship(Solr.RESPONSE_CONFIG_QUERY_DC, dcDocument);
        } else if ((StringUtils.isEmpty(jsonName)) || // This handles a Solr 7.5 JSON output bug (empty string)
                (StringUtils.equals(jsonName, OBJECT_RESPONSE_CONFIG_QUERY_FVC))) {
            fvcDocument = new Document(Solr.RESPONSE_CONFIG_QUERY_FVC);
            fvcDocument.setName(jsonName);
            dataBag = fvcDocument.getBag();
            aReader.beginObject();
            while (aReader.hasNext()) {
                jsonName = aReader.nextName();
                jsonValue = nextValueAsString(aReader);
                addBagTextField(dataBag, jsonName, jsonValue);
            }
            aReader.endObject();
            queryDocument.addRelationship(Solr.RESPONSE_CONFIG_QUERY_FVC, fvcDocument);
        } else {
            jsonValue = nextValueAsString(aReader);
            addBagTextField(queryBag, jsonName, jsonValue);
        }
    }
    aReader.endObject();
    aCfgDocument.addRelationship(Solr.RESPONSE_CONFIG_QUERY, queryDocument);
}

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

License:Open Source License

private void addFieldToDocument(JsonReader aReader, Document aDocument, String aName) throws IOException {
    DataBag childBag;/*from ww  w . j  a  v a2s  .c o m*/
    JsonToken jsonToken;
    boolean isArrayArray;
    Document childDocument;
    String jsonName, jsonValue;
    DataTextField dataTextField;

    DataBag dataBag = aDocument.getBag();
    if (isNextTokenAnArray(aReader)) {
        aReader.beginArray();
        dataTextField = new DataTextField(aName, Field.nameToTitle(aName));
        dataTextField.addFeature(Field.FEATURE_IS_STORED, StrUtl.STRING_TRUE);
        jsonToken = aReader.peek();
        if (jsonToken == JsonToken.BEGIN_ARRAY) {
            isArrayArray = true;
            aReader.beginArray();
            jsonToken = aReader.peek();
        } else
            isArrayArray = false;
        while (jsonToken != JsonToken.END_ARRAY) {
            jsonValue = nextValueAsString(aReader);
            dataTextField.addValue(jsonValue);
            jsonToken = aReader.peek();
        }
        aReader.endArray();
        if (isArrayArray)
            aReader.endArray();
        dataBag.add(dataTextField);
    } else if (isNextTokenAnObject(aReader)) {
        childDocument = new Document(aName);
        childDocument.setName(aName);
        childBag = childDocument.getBag();

        aReader.beginObject();
        jsonToken = aReader.peek();
        while (jsonToken != JsonToken.END_OBJECT) {
            jsonName = aReader.nextName();
            jsonValue = nextValueAsString(aReader);
            addBagTextField(childBag, jsonName, jsonValue);
            jsonToken = aReader.peek();
        }
        aReader.endObject();
        aDocument.addRelationship(aName, childDocument);
    } else {
        jsonValue = nextValueAsString(aReader);
        addBagTextField(dataBag, aName, jsonValue);
    }
}