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

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

Introduction

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

Prototype

JsonToken BEGIN_ARRAY

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

Click Source Link

Document

The opening of a JSON array.

Usage

From source file:com.ibm.g11n.pipeline.resfilter.impl.JsonResource.java

License:Open Source License

static void addToken(List<KeyPiece> result, String s, boolean inSubscript) {
    if (s.startsWith("'")) {
        // Turn any "\u0027" in the key back into '
        String modifiedKeyPiece = s.substring(1, s.length() - 1).replaceAll("\\\\u0027", "'");
        result.add(new KeyPiece(modifiedKeyPiece, JsonToken.BEGIN_OBJECT));
    } else if (inSubscript) {
        // [0] produces an array
        result.add(new KeyPiece(s, JsonToken.BEGIN_ARRAY));
    } else {/*from   www  .  j av  a2  s.  c  o  m*/
        for (String s2 : s.split("\\.")) {
            if (!s2.isEmpty()) {
                result.add(new KeyPiece(s2, JsonToken.BEGIN_OBJECT));
            }
        }
    }
}

From source file:com.ibm.watson.developer_cloud.tradeoff_analytics.v1.util.ColumnTypeAdapter.java

License:Open Source License

@Override
public Column read(JsonReader reader) throws IOException {
    if (reader.peek() == JsonToken.NULL) {
        reader.nextNull();//  w  ww.  ja v a2 s.  c om
        return null;
    }

    ColumnType type = ColumnType.TEXT;
    Goal goal = null;
    Boolean objective = null;

    String key = null, format = null, description = null, fullName = null, low = null, high = null;
    Double significantGain = null, significantLoss = null, insignificantLoss = null;

    List<String> categoricalRange = null, categoricalPreference = null;

    reader.beginObject();

    while (reader.hasNext()) {
        String name = reader.nextName();

        if (name.equals(TYPE2)) {
            type = ColumnType.fromString(reader.nextString());
        } else if (name.equals(KEY)) {
            key = reader.nextString();
        } else if (name.equals(GOAL)) {
            goal = Goal.fromString(reader.nextString());
        } else if (name.equals(IS_OBJECTIVE)) {
            objective = reader.nextBoolean();
        } else if (name.equals(FORMAT)) {
            format = reader.nextString();
        } else if (name.equals(DESCRIPTION)) {
            description = reader.nextString();
        } else if (name.equals(FULL_NAME)) {
            fullName = reader.nextString();
        } else if (name.equals(SIGNIFICANT_GAIN)) {
            significantGain = reader.nextDouble();
        } else if (name.equals(SIGNIFICANT_LOSS)) {
            significantLoss = reader.nextDouble();
        } else if (name.equals(INSIGNIFICANT_LOSS)) {
            insignificantLoss = reader.nextDouble();
        } else if (name.equals("preference")) {
            reader.beginArray();
            categoricalPreference = new ArrayList<String>();
            while (reader.hasNext()) {
                categoricalPreference.add(reader.nextString());
            }
            reader.endArray();
        } else if (name.equals(RANGE)) {
            if (reader.peek().equals(JsonToken.BEGIN_ARRAY)) {
                reader.beginArray();
                categoricalRange = new ArrayList<String>();
                while (reader.hasNext()) {
                    categoricalRange.add(reader.nextString());
                }
                reader.endArray();
            } else {
                reader.beginObject();
                while (reader.hasNext()) {
                    name = reader.nextName();
                    if (name.equals(LOW)) {
                        low = reader.nextString();
                    } else if (name.equals(HIGH)) {
                        high = reader.nextString();
                    } else {
                        reader.skipValue();
                    }
                }
                reader.endObject();
            }
        } else {
            reader.skipValue();
        }
    }
    reader.endObject();

    Column column;
    if (type == ColumnType.CATEGORICAL) {
        column = new CategoricalColumn();
        if (categoricalRange != null) {
            ((CategoricalColumn) column).setRange(categoricalRange);
        }
        if (categoricalPreference != null) {
            ((CategoricalColumn) column).setRange(categoricalPreference);
        }
    } else if (type == ColumnType.DATETIME) {
        column = new DateColumn();
        if (low != null) {
            try {
                ((DateColumn) column).withRange(DATE_FORMATTER.parse(low), DATE_FORMATTER.parse(high));
            } catch (final ParseException e) {
                LOG.log(Level.SEVERE, "Error parsing the date", e);
            }
        }
    } else if (type == ColumnType.NUMERIC) {
        column = new NumericColumn();
        if (low != null) {
            ((NumericColumn) column).range(Double.valueOf(low), Double.valueOf(high));
        }
    } else {
        column = new TextColumn();
    }

    column.setKey(key);

    if (description != null) {
        column.setDescription(description);
    }

    if (format != null) {
        column.setFormat(format);
    }

    if (objective != null) {
        column.setObjective(objective);
    }

    if (fullName != null) {
        column.setFullName(fullName);
    }

    if (goal != null) {
        column.setGoal(goal);
    }

    if (key != null) {
        column.setKey(key);
    }

    if (significantGain != null) {
        column.setSignificantGain(significantGain);
    }

    if (significantLoss != null) {
        column.setSignificantLoss(insignificantLoss);
    }

    if (insignificantLoss != null) {
        column.setInsignificantLoss(insignificantLoss);
    }

    return column;
}

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

License:Apache License

public BasicDBObject parseDocument() throws IOException {

    // Different cases:
    // {} //from www .  j  a va 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.ikanow.infinit.e.data_model.custom.InfiniteFileInputJsonParser.java

License:Apache License

private BasicDBObject getDocumentFromJson(boolean bRecursing) throws IOException {
    if (!_inSecondaryObject) {
        reader.beginObject();/*  w  ww  . j a v  a  2  s  . c  om*/
        _inSecondaryObject = true;
    }

    while (reader.hasNext()) {
        String name = reader.nextName();

        boolean bMatch = false;
        if (bRecursing) {
            bMatch = recursiveObjectIdentifiers.contains(name.toLowerCase());
        } else {
            bMatch = objectIdentifiers.contains(name.toLowerCase());
        } //TESTED

        if (bMatch) {
            JsonElement meta = parser.parse(reader);

            if (meta.isJsonObject()) {

                BasicDBObject currObj = convertJsonToDocument(meta);
                if (null != currObj) {
                    return currObj;
                }
            } //TESTED
            else if (meta.isJsonArray()) {
                _secondaryArray = meta.getAsJsonArray();
                _posInSecondaryArray = 0;
                for (JsonElement meta2 : _secondaryArray) {
                    _posInSecondaryArray++;
                    BasicDBObject currObj = convertJsonToDocument(meta2);
                    if (null != currObj) {
                        return currObj;
                    }
                }
                _secondaryArray = null;
            } //TESTED

        } //TESTED
        else {
            if (bRecurse) { //TODO (INF-2469): Not currently supported, it gets a bit tricky? (need to convert to a stack)

                JsonToken tok = reader.peek();
                if (JsonToken.BEGIN_OBJECT == tok) {
                    BasicDBObject currObj = getDocumentFromJson(true);
                    if (null != currObj) {
                        return currObj;
                    }
                } //TESTED
                else if (JsonToken.BEGIN_ARRAY == tok) {
                    reader.beginArray();
                    while (reader.hasNext()) {
                        JsonToken tok2 = reader.peek();

                        if (JsonToken.BEGIN_OBJECT == tok2) {
                            BasicDBObject currObj = getDocumentFromJson(true);
                            if (null != currObj) {
                                return currObj;
                            }
                        } else {
                            reader.skipValue();
                        } //TESTED

                    } //TESTED      
                    reader.endArray();
                } else {
                    reader.skipValue();
                } //TESTED
            } else {
                reader.skipValue();
            } //TESTED
        }

    } //(end loop over reader)

    reader.endObject();
    _inSecondaryObject = false;

    return null;

}

From source file:com.magnet.android.mms.request.GenericResponseParser.java

License:Open Source License

@SuppressWarnings("rawtypes")
@Override/*from w  w w .  j  av  a  2 s .c  o m*/
public T parseDecodedResponse(InputStream responseInputStream) throws MarshallingException {
    if (void.class.equals(responseType) || Void.class.equals(responseType)) {
        return (T) null;
    }
    if (responseInputStream == null || responseType == null) {
        return (T) null;
    }
    try {
        GsonStreamReader gr = new GsonStreamReader(responseInputStream);
        T parsed = null;

        if (gr.getReader().peek() == JsonToken.BEGIN_ARRAY) {
            parsed = (T) fromJsonToPojoCollection(gr.getReader(), actualResponseType);
        } else {
            parsed = genericGson.fromJson(gr.getReader(), actualResponseType);
        }
        return parsed;
    } catch (IOException e) {
        throw new MarshallingException(e);
    } catch (Exception e) {
        throw new MarshallingException(e);
    }
}

From source file:com.magnet.android.mms.request.JsonUtils.java

License:Open Source License

private static boolean isJson(Reader reader) {
    boolean result = false;
    try {//from  w  w  w  . jav  a  2 s .co  m
        JsonReader jr = new JsonReader(reader);
        jr.setLenient(true);
        JsonToken token = jr.peek();
        result = token.equals(JsonToken.BEGIN_OBJECT) || token.equals(JsonToken.BEGIN_ARRAY);
    } catch (Exception e) {
        Log.w(LOG_TAG, "JsonReader exception:" + e.getMessage());
    }
    return result;
}

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;/* ww  w.  j a v a  2s .c  o  m*/
    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.ds.solr.SolrConfigJSON.java

License:Open Source License

private boolean isNextTokenAnArray(JsonReader aReader) throws IOException {
    JsonToken jsonToken = aReader.peek();

    return (jsonToken == JsonToken.BEGIN_ARRAY);
}

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;//w ww  .ja  v a  2  s  . 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);
    }
}

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;/*w  w w.j  av  a2 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);
    }
}