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:json.tests.reader.GsonPerfTestWithCustomReader.java

License:Open Source License

/**
 * Read last element of a array// www  .  j  av a 2 s.c  om
 */
@Benchmark
public JsonElement printLastElement() throws IOException {
    JSONStreamReader jsonReader = new JSONStreamReader(new StringReader(json), requiredElements);
    JsonParser jp = new JsonParser();
    boolean isInArray = false;
    JsonElement element = null;
    int arrayIndex = 0;

    // 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);
            arrayIndex++;
        } else {
            jsonReader.skipValue();
        }
    }
    return element;
}

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

License:Open Source License

/**
 * Read nth element of a array/*from ww w .  j av  a  2s  .  c  o  m*/
 */
public JsonElement printNthElement() throws IOException {
    JSONStreamReader jsonReader = new JSONStreamReader(new StringReader(json), requiredElements);
    JsonParser jp = new JsonParser();
    boolean isInArray = false;
    JsonElement element = null;
    int arrayIndex = 0;

    // 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)) {
            arrayIndex++;
            if (arrayIndex == n) {
                element = jp.parse(jsonReader);
                break;
            } else {
                jsonReader.skipValue();
            }
        } else {
            jsonReader.skipValue();
        }
    }
    return element;
}

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

License:Open Source License

/**
 * {@inheritDoc}//from w  ww.ja v a2 s  .  c om
 */
@Override
public void beginObject() throws IOException {
    if (this.currentToken == JsonToken.BEGIN_OBJECT || super.peek() == JsonToken.BEGIN_OBJECT) {
        goToNextToken(false, true);
    } else {
        throw new IllegalStateException("Expected a BEGIN_OBJECT token but found " + this.currentToken);
    }
}

From source file:net.hft.dbproject.weatherapp.services.JSONParser.java

/**
 * Parsingmethod for the single JSON- Objects.
 *
 * @return Parsed Location//from  www .j a v  a2s.  co  m
 */
private static Location toLocation() {

    Location result = new Location();
    String cityName = "";
    Temperature cityTemperature = null;
    double tempAvg = 0.0;
    double tempMin = 0.0;
    double tempMax = 0.0;
    String countryCode = "";
    int imageIconID = 0;
    int ident = 0;
    boolean isDayImage = false;
    String weatherDescription = "";

    // GPS- Coordinates
    float lat = 0, lon = 0;

    try {
        int passedCounter = 0;
        while (queryJSONReader.hasNext() && passedCounter < 6) {

            while (passedCounter < 6) {
                String selectedValue = singleObject ? "coord" : queryJSONReader.nextName();
                switch (selectedValue) {
                case "id": {
                    ident = queryJSONReader.nextInt();
                    passedCounter++;
                    break;
                }
                case "name": {
                    cityName = queryJSONReader.nextString();
                    passedCounter++;
                    break;
                }
                case "coord": {
                    queryJSONReader.beginObject();
                    while (queryJSONReader.hasNext()) {
                        String name2 = queryJSONReader.nextName();
                        if (name2.equals("lon")) {
                            lon = (float) queryJSONReader.nextDouble();
                            name2 = queryJSONReader.nextName();
                        }
                        if (name2.equals("lat")) {
                            lat = (float) queryJSONReader.nextDouble();
                        }
                        queryJSONReader.endObject();
                        singleObject = false;
                        passedCounter++;
                        break;
                    }
                    break;
                }
                case "main": {
                    queryJSONReader.beginObject();
                    while (queryJSONReader.hasNext()) {
                        String attribute = queryJSONReader.nextName();
                        if (attribute.equals("temp")) {
                            tempAvg = Utilities.fromKelvinToFahrenheit(queryJSONReader.nextDouble());
                        } else if (attribute.equals("temp_min")) {
                            tempMin = Utilities.fromKelvinToFahrenheit(queryJSONReader.nextDouble());
                        } else if (attribute.equals("temp_max")) {
                            tempMax = Utilities.fromKelvinToFahrenheit(queryJSONReader.nextDouble());
                        } else {
                            queryJSONReader.skipValue();
                        }
                    }
                    queryJSONReader.endObject();
                    cityTemperature = new Temperature(tempAvg, tempMin, tempMax);
                    passedCounter++;
                    break;
                }
                case "sys": {
                    queryJSONReader.beginObject();
                    while (queryJSONReader.hasNext()) {
                        String attribute = queryJSONReader.nextName();
                        if (attribute.equals("country")) {
                            countryCode = queryJSONReader.nextString();
                        } else {
                            queryJSONReader.skipValue();
                        }
                    }
                    queryJSONReader.endObject();
                    passedCounter++;
                    break;
                }
                case "weather": {
                    queryJSONReader.beginArray();
                    queryJSONReader.beginObject();
                    while (queryJSONReader.hasNext()) {
                        String attribute = queryJSONReader.nextName();
                        if (attribute.equals("id")) {
                            imageIconID = queryJSONReader.nextInt();
                        } else if (attribute.equals("description")) {
                            weatherDescription = queryJSONReader.nextString();
                        } else if (attribute.equals("icon")) {
                            String val = queryJSONReader.nextString();
                            if (val.endsWith("d")) {
                                isDayImage = true;
                            }
                        } else {
                            queryJSONReader.skipValue();
                        }
                    }
                    queryJSONReader.endObject();
                    JsonToken t = queryJSONReader.peek();
                    if (t == JsonToken.BEGIN_OBJECT) {
                        queryJSONReader.beginObject();
                        while (t != JsonToken.END_OBJECT) {
                            String v = queryJSONReader.nextName();
                            queryJSONReader.skipValue();
                            t = queryJSONReader.peek();
                        }
                        queryJSONReader.endObject();
                    }
                    queryJSONReader.endArray();

                    passedCounter++;
                    break;
                }
                default: {
                    queryJSONReader.skipValue();
                }
                }

            }

            result = new Location(ident, cityName, countryCode, cityTemperature, lat, lon);
            result.setWeatherDescription(weatherDescription);
            WeatherImage wi = new WeatherImage();
            result.setDayTime(isDayImage);
            wi.setIconId(imageIconID);
            result.setImage(wi);
        }

    } catch (IOException ex) {
        LOGGER.error(ex.toString());
    }
    return result;

}

From source file:org.apache.airavata.workflow.core.parser.JsonWorkflowParser.java

License:Apache License

@Override
public WorkflowInfo parse() throws Exception {
    // TODO parse json string and construct components
    if (jsonReader.peek() != JsonToken.BEGIN_OBJECT) {
        throw new Exception(
                "Invalid Json data expected beginObject but found " + getTokenString(jsonReader.peek()));
    }/*from  ww  w . j av  a  2s. c  om*/
    jsonReader.beginObject();
    while (jsonReader.hasNext()) {
        String name = jsonReader.nextName(); // workflow
        if (name.equals(WORKFLOW)) {
            readWorkflowInfo(jsonReader);
        } else {
            jsonReader.skipValue();
        }
    }
    jsonReader.endObject();

    buildWorkflowGraph();
    workflowInfo.setInputs(inputs);
    workflowInfo.setApplications(applications);
    workflowInfo.setOutputs(outputs);
    return workflowInfo;
}

From source file:org.apache.airavata.workflow.core.parser.JsonWorkflowParser.java

License:Apache License

private void readWorkflowInputs(JsonReader jsonReader) throws ParserException, IOException {
    JsonToken peek = jsonReader.peek();/*from  w w  w  . j  av a 2  s .  c om*/
    InputNode inputNode;
    NodeModel nodeModel;
    ComponentStatus status;
    String name;
    if (peek == JsonToken.NULL) {
        throw new ParserException("Error! workflow inputs can't be null");
    } else if (peek == JsonToken.BEGIN_ARRAY) {
        jsonReader.beginArray();
        while (jsonReader.hasNext()) {
            jsonReader.beginObject();
            nodeModel = new NodeModel();
            status = new ComponentStatus();
            status.setState(ComponentState.CREATED);
            status.setReason("Created");
            nodeModel.setStatus(status);
            inputNode = new InputNodeImpl(nodeModel);
            while (jsonReader.hasNext()) {
                name = jsonReader.nextName();
                if (name.equals(NAME)) {
                    nodeModel.setName(jsonReader.nextString());
                } else if (name.equals(ID)) {
                    nodeModel.setNodeId(jsonReader.nextString());
                } else if (name.equals(DATATYPE)) {
                    inputNode.setDataType(DataType.valueOf(jsonReader.nextString()));
                } else if (name.equals(DESCRIPTION)) {
                    nodeModel.setDescription(jsonReader.nextString());
                } else if (name.equals(POSITION)) {
                    readPosition(jsonReader);
                } else if (name.equals(NODE_ID)) {
                    jsonReader.skipValue();
                    //                        nodeModel.setNodeId(jsonReader.nextString());
                } else if (name.equals(DEFAULT_VALUE)) {
                    inputNode.setValue(jsonReader.nextString());
                } else {
                    jsonReader.skipValue();
                }
            }
            jsonReader.endObject();
            inputs.add(inputNode);
        }
        jsonReader.endArray();
    } else {
        throw new ParserException("Error! Unsupported value for Workflow Inputs, exptected "
                + getTokenString(JsonToken.BEGIN_OBJECT) + " but found" + getTokenString(peek));
    }
}

From source file:org.apache.airavata.workflow.core.parser.JsonWorkflowParser.java

License:Apache License

private void readWorkflowOutputs(JsonReader jsonReader) throws IOException, ParserException {
    JsonToken peek = jsonReader.peek();/*from w  w  w. j  a v  a  2  s  .c om*/
    OutputNode outputNode;
    NodeModel nodeModel;
    ComponentStatus status;
    String name;
    if (peek == JsonToken.NULL) {
        throw new ParserException("Error! workflow outputs can't be null");
    } else if (peek == JsonToken.BEGIN_ARRAY) {
        jsonReader.beginArray();
        while (jsonReader.hasNext()) {
            jsonReader.beginObject();
            nodeModel = new NodeModel();
            status = new ComponentStatus();
            status.setState(ComponentState.CREATED);
            status.setReason("Created");
            nodeModel.setStatus(status);
            outputNode = new OutputNodeImpl(nodeModel);
            while (jsonReader.hasNext()) {
                name = jsonReader.nextName();
                if (name.equals(NAME)) {
                    nodeModel.setName(jsonReader.nextString());
                } else if (name.equals(ID)) {
                    nodeModel.setNodeId(jsonReader.nextString());
                } else if (name.equals(DATATYPE)) {
                    jsonReader.skipValue();
                } else if (name.equals(DESCRIPTION)) {
                    nodeModel.setDescription(jsonReader.nextString());
                } else if (name.equals(POSITION)) {
                    readPosition(jsonReader);
                } else if (name.equals(NODE_ID)) {
                    jsonReader.skipValue();
                    //                        nodeModel.setNodeId(jsonReader.nextString());
                } else if (name.equals(DEFAULT_VALUE)) {
                    jsonReader.skipValue();
                } else {
                    jsonReader.skipValue();
                }

            }
            jsonReader.endObject();
            outputs.add(outputNode);
        }
        jsonReader.endArray();
    } else {
        throw new ParserException("Error! Unsupported value for Workflow Outputs, exptected "
                + getTokenString(JsonToken.BEGIN_OBJECT) + " but found" + getTokenString(peek));
    }
}

From source file:org.apache.airavata.workflow.core.parser.JsonWorkflowParser.java

License:Apache License

private void readPosition(JsonReader jsonReader) throws IOException {
    JsonToken peek = jsonReader.peek();//from   w  ww.  j  a  v a  2  s.c o  m
    if (peek == JsonToken.NULL) {
        jsonReader.nextNull();
    } else if (peek == JsonToken.BEGIN_OBJECT) {
        jsonReader.beginObject();
        while (jsonReader.hasNext()) {
            // skip position data.
            jsonReader.nextName();
            jsonReader.skipValue();
        }
        jsonReader.endObject();
    } else {
        jsonReader.skipValue();
    }
}

From source file:org.apache.airavata.workflow.core.parser.JsonWorkflowParser.java

License:Apache License

private void readProperties(JsonReader jsonReader) throws IOException {
    JsonToken peek = jsonReader.peek();//ww w . ja v a2s. com
    if (peek == JsonToken.NULL) {
        jsonReader.nextNull();
    } else if (peek == JsonToken.BEGIN_OBJECT) {
        jsonReader.beginObject();
        while (jsonReader.hasNext()) {
            // TODO: Read and use proprety values
            String name = jsonReader.nextName();
            jsonReader.skipValue();
        }
        jsonReader.endObject();
    } else {
        jsonReader.skipValue();
    }

}

From source file:org.apache.axis2.json.gson.GsonXMLStreamReader.java

License:Apache License

private void readName() throws IOException, XMLStreamException {
    nextName();//from www .  j a  va2  s.c o m
    tokenType = jsonReader.peek();
    if (tokenType == JsonToken.BEGIN_OBJECT) {
        beginObject();
        state = JsonState.NameName;
    } else if (tokenType == JsonToken.BEGIN_ARRAY) {
        beginArray();
        tokenType = jsonReader.peek();
        if (tokenType == JsonToken.BEGIN_OBJECT) {
            beginObject();
            state = JsonState.NameName;
        } else {
            state = JsonState.NameValue;
        }
    } else if (tokenType == JsonToken.STRING || tokenType == JsonToken.NUMBER || tokenType == JsonToken.BOOLEAN
            || tokenType == JsonToken.NULL) {
        state = JsonState.NameValue;
    }
}