Example usage for com.google.gson.stream JsonToken END_ARRAY

List of usage examples for com.google.gson.stream JsonToken END_ARRAY

Introduction

In this page you can find the example usage for com.google.gson.stream JsonToken END_ARRAY.

Prototype

JsonToken END_ARRAY

To view the source code for com.google.gson.stream JsonToken END_ARRAY.

Click Source Link

Document

The closing of a JSON array.

Usage

From source file:com.ikanow.infinit.e.data_model.custom.InfiniteFileInputJsonParser.java

License:Apache License

public BasicDBObject parseDocument() throws IOException {

    // Different cases:
    // {} //from w  w w .java  2 s . c  o m
    // ^^ many of these
    // [ {}, {}, {} ]
    // For each of these 2/3 cases, you might either want to grab the entire object, or a field
    // within the object

    try {
        while (true) { // (use exceptions to get outta here) 

            try {
                tok = reader.peek();
            } catch (Exception e) {
                // EOF or end of object, keep going and find out...
                tok = reader.peek();
            }
            //TESTED

            if (JsonToken.BEGIN_ARRAY == tok) {
                if (!_inTopLevelArray) {
                    reader.beginArray();
                    _inTopLevelArray = true;
                }
                if (objectIdentifiers.isEmpty()) {
                    while (reader.hasNext()) {
                        JsonElement meta = parser.parse(reader);
                        BasicDBObject currObj = convertJsonToDocument(meta);

                        if (null != currObj) {
                            return currObj;
                        } //(else carry on...)
                    }
                } //TESTED
                else {
                    while (reader.hasNext()) {
                        BasicDBObject currObj = getDocumentFromJson(false);
                        if (null != currObj) {
                            return currObj;
                        } //(else carry on...)
                    }
                } //TESTED
            } else if (JsonToken.BEGIN_OBJECT == tok) {
                if (objectIdentifiers.isEmpty()) {
                    JsonElement meta = parser.parse(reader);
                    BasicDBObject currObj = convertJsonToDocument(meta);

                    if (null != currObj) {
                        return currObj;
                    } //(else carry on...)

                } //TESTED (single and multiple doc case)
                else {
                    BasicDBObject currObj = getDocumentFromJson(false);
                    if (null != currObj) {
                        return currObj;
                    } //(else carry on...)

                } //TESTED (single and multiple doc case)   
            } else if ((JsonToken.END_DOCUMENT == tok) || (JsonToken.END_ARRAY == tok)
                    || (JsonToken.END_OBJECT == tok)) {
                return null;
            } else { // Must be recursing through the next level(s)
                BasicDBObject currObj = getDocumentFromJson(false);
                if (null != currObj) {
                    return currObj;
                } //(else carry on...)               
            }
        } // (end loop forever - exception out)
    } catch (Exception e) {
    } // This is our EOF

    return null;
}

From source file:com.magnet.android.mms.request.marshall.GsonStreamReader.java

License:Open Source License

@Override
public int getTokenType() throws MarshallingException {
    int result = TOKEN_TYPE_FIELD_VALUE;
    JsonToken tokenType;/*from  ww w . j  a v  a  2s .c om*/
    try {
        tokenType = jr.peek();
    } catch (Exception e) {
        throw new MarshallingException(e);
    }
    if (tokenType == JsonToken.BEGIN_OBJECT) {
        result = TOKEN_TYPE_START_OBJECT;
    } else if (tokenType == JsonToken.BEGIN_ARRAY) {
        result = TOKEN_TYPE_START_ARRAY;
    } else if (tokenType == JsonToken.END_OBJECT) {
        result = TOKEN_TYPE_END_OBJECT;
    } else if (tokenType == JsonToken.END_ARRAY) {
        result = TOKEN_TYPE_END_ARRAY;
    } else if (tokenType == JsonToken.NAME) {
        result = TOKEN_TYPE_FIELD_NAME;
    } else if (tokenType == JsonToken.NULL) {
        result = TOKEN_TYPE_NULL;
    } else if (tokenType == JsonToken.END_DOCUMENT) {
        result = TOKEN_TYPE_END_DOCUMENT;
    }
    return result;
}

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  v  a  2s . com
 *
 * @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.ds.solr.SolrConfigJSON.java

License:Open Source License

private void addFieldToDocument(JsonReader aReader, Document aDocument, String aName) throws IOException {
    DataBag childBag;//from   w  ww.j ava 2  s  . com
    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);
    }
}

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

License:Open Source License

private void addObjectToDocument(JsonReader aReader, Document aParentDocument, String aType, String aName)
        throws IOException {
    String jsonValue;//from w  w w .  ja va  2  s . co  m
    JsonToken jsonToken;
    boolean isArrayArray;
    Document childDocument;

    DataBag dataBag = aParentDocument.getBag();
    if (isNextTokenAnArray(aReader)) {
        aReader.beginArray();
        jsonToken = aReader.peek();
        if (jsonToken == JsonToken.BEGIN_ARRAY) {
            isArrayArray = true;
            aReader.beginArray();
            jsonToken = aReader.peek();
        } else
            isArrayArray = false;
        while (jsonToken != JsonToken.END_ARRAY) {
            childDocument = new Document(aType);
            childDocument.setName(aName);
            aReader.beginObject();
            while (aReader.hasNext())
                addFieldToDocument(aReader, childDocument);
            aReader.endObject();
            aParentDocument.addRelationship(aType, childDocument);
            jsonToken = aReader.peek();
        }
        aReader.endArray();
        if (isArrayArray)
            aReader.endArray();
    } else if (isNextTokenAnObject(aReader)) {
        childDocument = new Document(aType);
        childDocument.setName(aName);
        aReader.beginObject();
        while (aReader.hasNext())
            addFieldToDocument(aReader, childDocument);
        aReader.endObject();
        aParentDocument.addRelationship(aType, childDocument);
    } else {
        jsonValue = nextValueAsString(aReader);
        addBagTextField(dataBag, aName, jsonValue);
    }
}

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

License:Open Source License

private void populateSearchComponent(JsonReader aReader, Document aCfgDocument, String aDocType)
        throws IOException {
    DataBag scBag;/*from   w  w w.  j a va2s . c  om*/
    Document scDocument;
    JsonToken jsonToken;
    boolean isArrayArray;
    Document childDocument;
    String jsonName, jsonValue;

    Document searchComponentDocument = new Document(aDocType);
    aReader.beginObject();
    while (aReader.hasNext()) {
        jsonName = aReader.nextName();
        scDocument = new Document(Solr.RESPONSE_CONFIG_SEARCH_COMPONENT);
        scDocument.setName(jsonName);
        scBag = scDocument.getBag();
        DataTextField operationField = new DataTextField(Solr.CONFIG_OPERATION_FIELD_NAME,
                Field.nameToTitle(Solr.CONFIG_OPERATION_FIELD_NAME));
        operationField.addFeature(Field.FEATURE_IS_STORED, StrUtl.STRING_FALSE);
        scBag.add(operationField);

        aReader.beginObject();
        while (aReader.hasNext()) {
            jsonName = aReader.nextName();
            if (isNextTokenAnArray(aReader)) {
                aReader.beginArray();
                jsonToken = aReader.peek();
                if (jsonToken == JsonToken.BEGIN_ARRAY) {
                    isArrayArray = true;
                    aReader.beginArray();
                    jsonToken = aReader.peek();
                } else
                    isArrayArray = false;
                while (jsonToken != JsonToken.END_ARRAY) {
                    childDocument = new Document(Field.nameToTitle(jsonName));
                    childDocument.setName(jsonName);
                    aReader.beginObject();
                    while (aReader.hasNext())
                        addFieldToDocument(aReader, childDocument);
                    aReader.endObject();
                    scDocument.addRelationship(Field.nameToTitle(jsonName), childDocument);
                    jsonToken = aReader.peek();
                }
                aReader.endArray();
                if (isArrayArray)
                    aReader.endArray();
            } else if (isNextTokenAnObject(aReader)) {
                childDocument = new Document(Field.nameToTitle(jsonName));
                childDocument.setName(jsonName);
                aReader.beginObject();
                while (aReader.hasNext())
                    addFieldToDocument(aReader, childDocument);
                aReader.endObject();
                scDocument.addRelationship(Field.nameToTitle(jsonName), childDocument);
            } else {
                jsonValue = nextValueAsString(aReader);
                addBagTextField(scBag, jsonName, jsonValue);
            }
        }
        aReader.endObject();
        assignSearchComponentType(scDocument);
        searchComponentDocument.addRelationship(scDocument.getType(), scDocument);
    }

    aCfgDocument.addRelationship(aDocType, searchComponentDocument);
}

From source file:com.splunk.ResultsReaderJson.java

License:Apache License

/**
 * Skip the next value, whether it is atomic or compound, in the JSON
 * stream./*from www . ja  v a  2  s .  c om*/
 */
private void skipEntity() throws IOException {
    if (jsonReader.peek() == JsonToken.STRING) {
        jsonReader.nextString();
    } else if (jsonReader.peek() == JsonToken.BOOLEAN) {
        jsonReader.nextBoolean();
    } else if (jsonReader.peek() == JsonToken.NUMBER) {
        jsonReader.nextDouble();
    } else if (jsonReader.peek() == JsonToken.NULL) {
        jsonReader.nextNull();
    } else if (jsonReader.peek() == JsonToken.NAME) {
        jsonReader.nextName();
    } else if (jsonReader.peek() == JsonToken.BEGIN_ARRAY) {
        jsonReader.beginArray();
        while (jsonReader.peek() != JsonToken.END_ARRAY) {
            skipEntity();
        }
        jsonReader.endArray();
    } else if (jsonReader.peek() == JsonToken.BEGIN_OBJECT) {
        jsonReader.beginObject();
        while (jsonReader.peek() != JsonToken.END_OBJECT) {
            skipEntity();
        }
        jsonReader.endObject();
    }
}

From source file:com.splunk.ResultsReaderJson.java

License:Apache License

private Event readEvent() throws IOException {
    Event returnData = null;//  ww  w . j  a va2s .  c o m
    String name = null;
    List<String> values = new ArrayList<String>();

    if (jsonReader == null)
        return null;

    // Events are almost flat, so no need for a true general parser
    // solution. But the Gson parser is a little unintuitive here. Nested
    // objects, have their own relative notion of hasNext. This
    // means that for every object or array start, hasNext() returns false
    // and one must consume the closing (END) object to get back to the
    // previous object.
    while (jsonReader.hasNext()) {
        if (returnData == null) {
            returnData = new Event();
        }
        if (jsonReader.peek() == JsonToken.BEGIN_OBJECT) {
            jsonReader.beginObject();
        }
        if (jsonReader.peek() == JsonToken.BEGIN_ARRAY) {
            jsonReader.beginArray();
            // The Gson parser is a little unintuitive here. Nested objects,
            // have their own relative notion of hasNext; when hasNext()
            // is done, it is only for this array.
            while (jsonReader.hasNext()) {
                JsonToken jsonToken2 = jsonReader.peek();
                if (jsonToken2 == JsonToken.STRING) {
                    values.add(jsonReader.nextString());
                }
            }
            jsonReader.endArray();

            String[] valuesArray = values.toArray(new String[values.size()]);
            returnData.putArray(name, valuesArray);

            values.clear();
        }
        if (jsonReader.peek() == JsonToken.NAME) {
            name = jsonReader.nextName();
        }
        if (jsonReader.peek() == JsonToken.STRING) {
            String delimitedValues = jsonReader.nextString();
            returnData.putSingleOrDelimited(name, delimitedValues);
        }
        if (jsonReader.peek() == JsonToken.END_OBJECT) {
            jsonReader.endObject();
            break;
        }
        if (jsonReader.peek() == JsonToken.END_ARRAY) {
            jsonReader.endArray();
        }
    }
    return returnData;
}

From source file:com.splunk.sdk.ResultsReaderJson.java

License:Apache License

/** {@inheritDoc} */
@Override/*  w  w w. j a v a 2s. c om*/
public Event getNextEvent() throws IOException {
    Event returnData = null;
    String name = null;
    List<String> values = new ArrayList<String>();

    if (jsonReader == null)
        return null;

    // Events are almost flat, so no need for a true general parser
    // solution. But the Gson parser is a little unintuitive here. Nested
    // objects, have their own relative notion of hasNext. This
    // means that for every object or array start, hasNext() returns false
    // and one must consume the closing (END) object to get back to the
    // previous object.
    while (jsonReader.hasNext()) {
        if (returnData == null) {
            returnData = new Event();
        }
        if (jsonReader.peek() == JsonToken.BEGIN_OBJECT) {
            jsonReader.beginObject();
        }
        if (jsonReader.peek() == JsonToken.BEGIN_ARRAY) {
            jsonReader.beginArray();
            // The Gson parser is a little unintuitive here. Nested objects,
            // have their own relative notion of hasNext; when hasNext()
            // is done, it is only for this array.
            while (jsonReader.hasNext()) {
                JsonToken jsonToken2 = jsonReader.peek();
                if (jsonToken2 == JsonToken.STRING) {
                    values.add(jsonReader.nextString());
                }
            }
            jsonReader.endArray();

            String[] valuesArray = values.toArray(new String[values.size()]);
            returnData.putArray(name, valuesArray);

            values.clear();
        }
        if (jsonReader.peek() == JsonToken.NAME) {
            name = jsonReader.nextName();
        }
        if (jsonReader.peek() == JsonToken.STRING) {
            String delimitedValues = jsonReader.nextString();
            returnData.putSingleOrDelimited(name, delimitedValues);
        }
        if (jsonReader.peek() == JsonToken.END_OBJECT) {
            jsonReader.endObject();
            break;
        }
        if (jsonReader.peek() == JsonToken.END_ARRAY) {
            jsonReader.endArray();
        }
    }
    return returnData;
}

From source file:com.squareup.wire.MessageTypeAdapter.java

License:Apache License

private void parseUnknownField(JsonReader in, Message.Builder<M> builder, int tag) throws IOException {
    in.beginArray();//w  ww . ja va  2s .c  o m
    UnknownFieldType type = UnknownFieldType.of(in.nextString());
    while (in.peek() != JsonToken.END_ARRAY) {
        switch (type) {
        case VARINT:
            builder.addVarint(tag, in.nextInt());
            break;
        case FIXED32:
            builder.addFixed32(tag, in.nextInt());
            break;
        case FIXED64:
            builder.addFixed64(tag, in.nextInt());
            break;
        case LENGTH_DELIMITED:
            builder.addLengthDelimited(tag, ByteString.decodeBase64(in.nextString()));
            break;
        default:
            throw new AssertionError("Unknown field type " + type);
        }
    }
    in.endArray();
}