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

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

Introduction

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

Prototype

JsonToken BEGIN_OBJECT

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

Click Source Link

Document

The opening of a JSON object.

Usage

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

License:Open Source License

protected BundleFile parseFile(JsonReaderExt jsonReader) throws IOException {
    jsonReader.consumeExpected(JsonToken.BEGIN_OBJECT);
    String path = null;/*  w  w  w  .  j  a  v a2  s  .c om*/
    boolean importOnly = false;

    while (jsonReader.hasNext()) {
        String propName = jsonReader.consumeExpectedPropName();

        switch (propName) {
        case "path":
            path = jsonReader.consumeStringValue();
            break;
        case "type":
            //TODO

        default:
            jsonReader.skipValue();
        }
    }
    jsonReader.consumeExpected(JsonToken.END_OBJECT);
    if (path == null) {
        path = "<missing_path>";
        putError("missing path property for files entry.");
    }
    return new DubBundle.BundleFile(path, importOnly);
}

From source file:ddt.dtool.genie.GenieServer.java

License:Open Source License

protected void processJsonMessage(JsonReaderExt jsonParser, JsonWriterExt jsonWriter) throws IOException {
    jsonParser.consumeExpected(JsonToken.BEGIN_OBJECT);
    try {//from   w ww  .ja  v a  2  s  .co  m

        String commandName = jsonParser.consumeExpectedPropName();

        for (JsonCommandHandler commandHandler : getCommandHandlers()) {
            if (commandHandler.canHandle(commandName)) {
                commandHandler.processCommand(jsonParser, jsonWriter);
                return;
            }
        }

        new JsonCommandHandler(commandName, this) {
            @Override
            protected void writeResponseJsonContents() throws IOException {
                String msg = "Unknown command: " + commandName;
                logProtocolMessageError(validationError(msg));
                jsonWriter.writeProperty("error", msg);
            };
        }.processCommand(jsonParser, jsonWriter);

    } finally {
        jsonParser.consumeExpected(JsonToken.END_OBJECT);
    }

}

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();/*w  ww.  j  a va  2  s.  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 static HashMap<String, Object> readJsonObject(JsonReaderExt jsonParser) throws IOException {
    jsonParser.consumeExpected(JsonToken.BEGIN_OBJECT);

    HashMap<String, Object> jsonObject = new HashMap<>();

    while (jsonParser.tryConsume(JsonToken.END_OBJECT) == false) {
        String propName = jsonParser.consumeExpectedPropName();
        Object propvalue = readJsonValue(jsonParser);
        jsonObject.put(propName, propvalue);
    }//from w  w w  .ja va2s  .c  o m

    return jsonObject;
}

From source file:de.pribluda.android.jsonmarshaller.JSONUnmarshaller.java

License:Apache License

/**
 * read array into list/* www.  j a v  a  2  s. c o m*/
 *
 * @param reader
 * @param beanToBeCreatedClass
 * @return
 */
public static <T> List<T> unmarshallArray(JsonReader reader, java.lang.Class<T> beanToBeCreatedClass)
        throws IOException, InvocationTargetException, NoSuchMethodException, InstantiationException,
        IllegalAccessException {
    ArrayList<T> retval = new ArrayList();
    reader.beginArray();
    // read objects after each other
    while (reader.peek() == JsonToken.BEGIN_OBJECT) {
        retval.add(unmarshall(reader, beanToBeCreatedClass));
    }
    reader.endArray();

    return retval;
}

From source file:dtool.dub.DubDescribeParser.java

License:Open Source License

@Override
protected void readData(JsonReaderExt jsonParser) throws IOException, DubBundleException {

    jsonParser.consumeExpected(JsonToken.BEGIN_OBJECT);

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

        if (tokenType == JsonToken.NAME) {
            String propertyName = jsonParser.nextName();

            if (propertyName.equals("mainPackage")) {
                bundleName = jsonParser.consumeStringValue();
            } else if (propertyName.equals("packages")) {
                bundles = readDescribedBundles(jsonParser);
            } else {
                jsonParser.skipValue();//from   w ww. j  a  va 2  s  . c  om
            }
        } else {
            jsonParser.errorUnexpected(tokenType);
        }
    }

    jsonParser.consumeExpected(JsonToken.END_OBJECT);
}

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  w  w . j a  v  a  2 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 DubManifestParser readBundle(JsonReaderExt jsonReader) throws IOException, DubBundleException {
    jsonReader.consumeExpected(JsonToken.BEGIN_OBJECT);

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

        if (tokenType == JsonToken.NAME) {
            String propertyName = jsonReader.nextName();

            if (propertyName.equals("name")) {
                bundleName = jsonReader.consumeStringValue();
            } else if (propertyName.equals("version")) {
                version = jsonReader.consumeStringValue();
            } else if (propertyName.equals("path")) {
                pathString = jsonReader.consumeStringValue();
            } else if (propertyName.equals("importPaths")) {
                sourceFolders = parseSourcePaths(jsonReader);
            } else if (propertyName.equals("dependencies")) {
                dependencies = parseDependencies(jsonReader);
            } else if (propertyName.equals("files")) {
                bundleFiles = parseFiles(jsonReader);
            } else if (propertyName.equals("targetName")) {
                targetName = jsonReader.consumeStringValue();
            } else if (propertyName.equals("targetPath")) {
                targetPath = jsonReader.consumeStringValue();
            } else if (propertyName.equals("configurations")) {
                configurations = parseConfigurations(jsonReader);
            } else {
                jsonReader.skipValue();//from   w ww  .j av a2 s.  com
            }
        } else {
            jsonReader.errorUnexpected(tokenType);
        }
    }

    jsonReader.consumeExpected(JsonToken.END_OBJECT);
    return this;
}

From source file:dtool.dub.DubManifestParser.java

License:Open Source License

protected DubConfiguration parseConfiguration(JsonReaderExt jsonReader) throws IOException, DubBundleException {
    jsonReader.consumeExpected(JsonToken.BEGIN_OBJECT);

    String name = null;//from  w  w w .  j  a va2  s  .  c o  m
    String targetType = null;
    String targetName = null;
    String targetPath = null;

    while (jsonReader.hasNext()) {
        String propName = jsonReader.consumeExpectedPropName();

        switch (propName) {
        case "name":
            name = jsonReader.consumeStringValue();
            break;
        case "targetType":
            targetType = jsonReader.consumeStringValue();
            break;
        case "targetName":
            targetName = jsonReader.consumeStringValue();
            break;
        case "targetPath":
            targetPath = jsonReader.consumeStringValue();
            break;

        default:
            jsonReader.skipValue();
        }
    }
    jsonReader.consumeExpected(JsonToken.END_OBJECT);

    if (name == null) {
        throw new DubBundleException("Build configuration has no name attribute");
    }

    return new DubConfiguration(name, targetType, targetName, targetPath);
}

From source file:json.tests.reader.GsonPerfTestWithCustomReader.java

License:Open Source License

/**
 * Read the first element of a array//  w ww . ja  va  2s .com
 */
public JsonElement printFirstElement() throws IOException {
    JSONStreamReader jsonReader = new JSONStreamReader(new StringReader(json), requiredElements);
    JsonParser jp = new JsonParser();
    boolean isInArray = false;
    JsonElement element = null;

    // consume the first token
    jsonReader.beginObject();

    while (jsonReader.hasNext()) {
        JsonToken token = jsonReader.peek();
        if (token.equals(JsonToken.BEGIN_ARRAY)) {
            jsonReader.beginArray();
            isInArray = true;
        } else if (isInArray && token.equals(JsonToken.END_ARRAY)) {
            jsonReader.endArray();
        } else if (isInArray && token.equals(JsonToken.BEGIN_OBJECT)) {
            element = jp.parse(jsonReader);
            break;
        } else {
            jsonReader.skipValue();
        }
    }
    return element;
}