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

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

Introduction

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

Prototype

public void beginObject() throws IOException 

Source Link

Document

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

Usage

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

License:Open Source License

private void assignFieldNameValue(DataField aField, String aName, JsonReader aReader) throws IOException {
    if (StringUtils.equals(aName, IO.JSON_TITLE_MEMBER_NAME))
        aField.setTitle(aReader.nextString());
    else if (StringUtils.equals(aName, "displaySize"))
        aField.setDisplaySize(aReader.nextInt());
    else if (StringUtils.equals(aName, "defaultValue"))
        aField.setDefaultValue(aReader.nextString());
    else if (StringUtils.equals(aName, "sortOrder"))
        aField.setSortOrder(Field.Order.valueOf(aReader.nextString()));
    else if (StringUtils.equals(aName, "isMultiValue"))
        aField.setMultiValueFlag(aReader.nextBoolean());
    else if (StringUtils.equals(aName, IO.JSON_RANGE_OBJECT_NAME))
        aField.setRange(mRangeJSON.load(aReader));
    else if (StringUtils.equals(aName, IO.JSON_FEATURES_ARRAY_NAME)) {
        String jsonName, jsonValue;

        aReader.beginObject();
        while (aReader.hasNext()) {
            jsonName = aReader.nextName();
            jsonValue = aReader.nextString();
            aField.addFeature(jsonName, jsonValue);
        }/*from w w  w  . j  av  a2s.c  o m*/
        aReader.endObject();
    } else if (StringUtils.equals(aName, IO.JSON_VALUE_MEMBER_NAME)) {
        String jsonValue = aReader.nextString();
        if (aField.isMultiValue()) {
            String mvDelimiter = aField.getFeature(Field.FEATURE_MV_DELIMITER);
            if (StringUtils.isNotEmpty(mvDelimiter))
                aField.expand(jsonValue, mvDelimiter.charAt(0));
            else
                aField.expand(jsonValue);
        } else
            aField.setValue(jsonValue);
    }
}

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

License:Open Source License

/**
 * Parses aJSON formatted input reader stream and loads it into a field.
 *
 * @param aReader Input reader stream instance.
 *
 * @return DataField instance.// ww  w  .  j a va  2s  . c  o m
 *
 * @throws java.io.IOException I/O related exception.
 */
public DataField load(JsonReader aReader) throws IOException {
    String jsonName;

    DataField dataField = null;
    String fieldName = StringUtils.EMPTY;
    String fieldType = Field.typeToString(Field.Type.Text);

    aReader.beginObject();
    while (aReader.hasNext()) {
        jsonName = aReader.nextName();
        if (StringUtils.equals(jsonName, IO.JSON_NAME_MEMBER_NAME))
            fieldName = aReader.nextString();
        else if (StringUtils.equals(jsonName, IO.JSON_TYPE_MEMBER_NAME))
            fieldType = aReader.nextString();
        else {
            if (dataField == null) {
                if ((StringUtils.isNotEmpty(fieldName)) && (StringUtils.isNotEmpty(fieldType)))
                    dataField = new DataField(Field.stringToType(fieldType), fieldName);
                else
                    throw new IOException("JSON Parser: Data field is missing name/type field.");
            }
            assignFieldNameValue(dataField, jsonName, aReader);
        }
    }
    aReader.endObject();

    // OK - if we get here and the data field is null, then the JSON lacked a value field.

    if (dataField == null) {
        if ((StringUtils.isNotEmpty(fieldName)) && (StringUtils.isNotEmpty(fieldType)))
            dataField = new DataField(Field.stringToType(fieldType), fieldName);
        else
            throw new IOException("JSON Parser: Data field is missing name/type field.");
    }

    return dataField;
}

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.
 *//* w w  w . ja  va2 s  . 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();//from w  ww  . j  a  v  a 2  s .  c o 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

/**
 * Parses an JSON stream and loads it into a document and returns
 * an instance to it./*from w ww  . j av a2s  .co  m*/
 *
 * @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./* ww w. j  a va2s . 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 w ww. jav  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   w  w w.ja v  a  2 s.  co  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.//from w w w.j  a va2 s  .  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.
 *//*w  w w .ja v  a2  s  .c  o  m*/
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;
}