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:ddt.dtool.dub.DubDescribeParser.java

License:Open Source License

protected static ArrayList<DubBundle> readDescribedBundles(JsonReaderExt jsonParser) throws IOException {
    jsonParser.consumeExpected(JsonToken.BEGIN_ARRAY);

    ArrayList<DubBundle> bundles = new ArrayList<>();

    while (jsonParser.hasNext()) {
        JsonToken tokenType = jsonParser.peek();

        if (tokenType == JsonToken.BEGIN_OBJECT) {
            DubBundle bundle = new DubManifestParser().readBundle(jsonParser).createBundle(null, false);
            bundles.add(bundle);//from   w w w  . j a  v a 2  s.com
        } else {
            jsonParser.errorUnexpected(tokenType);
        }
    }

    jsonParser.consumeExpected(JsonToken.END_ARRAY);
    return bundles;
}

From source file:ddt.dtool.dub.DubManifestParser.java

License:Open Source License

protected ArrayList<BundleFile> parseFiles(JsonReaderExt jsonReader) throws IOException {
    jsonReader.consumeExpected(JsonToken.BEGIN_ARRAY);

    ArrayList<BundleFile> bundleFiles = new ArrayList<>();

    while (jsonReader.hasNext()) {
        BundleFile bundleFile = parseFile(jsonReader);
        bundleFiles.add(bundleFile);//  w ww.  j  a v  a2  s  .c o  m
    }

    jsonReader.consumeExpected(JsonToken.END_ARRAY);
    return bundleFiles;
}

From source file:ddt.dtool.util.JsonReaderExt.java

License:Open Source License

public void consumeExpected(JsonToken expectedToken) throws IOException {
    JsonToken tokenType = validateExpectedToken(expectedToken);
    if (tokenType == JsonToken.BEGIN_OBJECT) {
        jsonReader.beginObject();//from  w  w w  . j a  va2s. c  om
    } else if (tokenType == JsonToken.END_OBJECT) {
        jsonReader.endObject();
    } else if (tokenType == JsonToken.BEGIN_ARRAY) {
        jsonReader.beginArray();
    } else if (tokenType == JsonToken.END_ARRAY) {
        jsonReader.endArray();
    } else if (tokenType == JsonToken.END_DOCUMENT) {
    } else {
        assertFail();
    }
}

From source file:ddt.dtool.util.JsonReaderExt.java

License:Open Source License

public ArrayList<String> consumeStringArray(boolean ignoreNulls) throws IOException {
    jsonReader.consumeExpected(JsonToken.BEGIN_ARRAY);

    ArrayList<String> strings = new ArrayList<>();

    while (jsonReader.hasNext()) {
        JsonToken tokenType = jsonReader.peek();

        if (ignoreNulls && tokenType == JsonToken.NULL) {
            jsonReader.nextNull();//from  w  ww  .jav  a2  s. com
            continue;
        }

        if (tokenType != JsonToken.STRING) {
            sourceError("Expected String value, instead got: " + tokenType);
        }

        String entry = jsonReader.nextString();
        strings.add(entry);
    }
    jsonReader.consumeExpected(JsonToken.END_ARRAY);
    return strings;
}

From source file:ddt.dtool.util.JsonReaderExt.java

License:Open Source License

public ArrayList<Object> readJsonArray() throws IOException {
    jsonReader.consumeExpected(JsonToken.BEGIN_ARRAY);

    ArrayList<Object> array = new ArrayList<>();
    while (jsonReader.hasNext()) {
        array.add(readJsonValue());//from  w  w w  .  j  a  va2 s . c om
    }
    jsonReader.consumeExpected(JsonToken.END_ARRAY);
    return array;
}

From source file:dtool.dub.DubDescribeParser.java

License:Open Source License

protected static ArrayList<DubBundle> readDescribedBundles(JsonReaderExt jsonParser)
        throws IOException, DubBundleException {
    jsonParser.consumeExpected(JsonToken.BEGIN_ARRAY);

    ArrayList<DubBundle> bundles = new ArrayList<>();

    while (jsonParser.hasNext()) {
        JsonToken tokenType = jsonParser.peek();

        if (tokenType == JsonToken.BEGIN_OBJECT) {
            DubBundle bundle = new DubManifestParser().readBundle(jsonParser).createBundle(null, false);
            bundles.add(bundle);//w ww.  j  a va2  s. c  o m
        } else {
            jsonParser.errorUnexpected(tokenType);
        }
    }

    jsonParser.consumeExpected(JsonToken.END_ARRAY);
    return bundles;
}

From source file:dtool.dub.DubManifestParser.java

License:Open Source License

protected ArrayList2<BundleFile> parseFiles(JsonReaderExt jsonReader) throws IOException {
    jsonReader.consumeExpected(JsonToken.BEGIN_ARRAY);

    ArrayList2<BundleFile> bundleFiles = new ArrayList2<>();

    while (jsonReader.hasNext()) {
        BundleFile bundleFile = parseFile(jsonReader);
        bundleFiles.add(bundleFile);/*from  w  ww  .  ja v a2 s .com*/
    }

    jsonReader.consumeExpected(JsonToken.END_ARRAY);
    return bundleFiles;
}

From source file:dtool.dub.DubManifestParser.java

License:Open Source License

protected ArrayList2<DubConfiguration> parseConfigurations(JsonReaderExt jsonReader)
        throws IOException, DubBundleException {
    jsonReader.consumeExpected(JsonToken.BEGIN_ARRAY);

    ArrayList2<DubConfiguration> bundleFiles = new ArrayList2<>();

    while (jsonReader.hasNext()) {
        DubConfiguration element = parseConfiguration(jsonReader);
        bundleFiles.add(element);//from  ww  w.jav  a  2 s . co m
    }

    jsonReader.consumeExpected(JsonToken.END_ARRAY);
    return bundleFiles;
}

From source file:eu.jtzipi.project0.common.json.impl.GsonReadWrite.java

License:Apache License

static Collection<Object> parseArray(final JsonReader jsonReader) throws IOException {
    assert null != jsonReader : "DEBUG: The json Reader is null";

    Collection<Object> l = new ArrayList<>();
    JsonToken tok;//  www.ja  va2s  .c  o m

    //
    while (jsonReader.hasNext()) {

        tok = jsonReader.peek();
        // we reach the end of this array
        if (JsonToken.END_ARRAY == tok) {
            jsonReader.endArray();
            // return
            break;
        }
        // what token
        switch (tok) {
        // if array/map - parse and append
        case BEGIN_ARRAY:
            l.add(parseArray(jsonReader));
            break;
        case BEGIN_OBJECT:
            l.add(parseObject(jsonReader));
            break;
        // if raw type
        case STRING:
            l.add(jsonReader.nextString());
            break;
        case BOOLEAN:
            l.add(jsonReader.nextBoolean());
            break;
        case NUMBER:
            l.add(jsonReader.nextDouble());
            break;
        // if null , add null and consume
        case NULL:
            l.add(null);
            jsonReader.nextNull();
            break;

        // all other cases are errors
        default:
            throw new IllegalStateException("Wrong Token '" + tok + "' , while parsing an array");
        }
    }

    return l;
}

From source file:eu.jtzipi.project0.common.json.impl.GsonReadWrite.java

License:Apache License

static Map<String, Object> parseObject(final JsonReader jsonReader) throws IOException {

    Map<String, Object> map = new HashMap<>();
    String tempKey;//  w ww. ja v  a 2s .  com
    JsonToken tok;
    //
    while (jsonReader.hasNext()) {

        // since we want to restore a map we assue a key/value pair
        tempKey = jsonReader.nextName();
        //
        tok = jsonReader.peek();
        // we reach the end of this array
        if (JsonToken.END_ARRAY == tok) {
            jsonReader.endArray();
            // return
            break;
        }
        // what token
        switch (tok) {
        // if array/map - parse and append
        case BEGIN_ARRAY:
            map.put(tempKey, parseArray(jsonReader));
            break;
        case BEGIN_OBJECT:
            map.put(tempKey, parseObject(jsonReader));
            break;
        // if raw type
        case STRING:
            map.put(tempKey, jsonReader.nextString());
            break;
        case BOOLEAN:
            map.put(tempKey, jsonReader.nextBoolean());
            break;
        case NUMBER:
            map.put(tempKey, jsonReader.nextDouble());
            break;
        // if null , add null and consume
        case NULL:
            map.put(tempKey, null);
            jsonReader.nextNull();
            break;

        // all other cases are errors
        default:
            throw new IllegalStateException("Wrong Token '" + tok + "' , while parsing an array");
        }
    }

    return map;
}