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

License:Open Source License

/**
 * Read last element of a array//from  ww w  .  j  a  v  a2 s  .c  o m
 */
@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 w  w w  .jav a 2  s  . co 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}// w  w  w .  ja  v a 2 s .  com
 */
@Override
public void beginArray() throws IOException {
    if (this.currentToken == JsonToken.BEGIN_ARRAY || super.peek() == JsonToken.BEGIN_ARRAY) {
        goToNextToken(false, true);
    } else {
        throw new IllegalStateException("Expected a BEGIN_ARRAY token but found " + this.currentToken);
    }
}

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  ww  w .jav a 2  s . com*/
    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();/*  w w w .jav a  2 s  .  c  o  m*/
    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 readWorkflowLinks(JsonReader jsonReader) throws IOException, ParserException {
    JsonToken peek = jsonReader.peek();//from  w ww .  ja v  a 2 s . c om
    if (peek == JsonToken.NULL) {
        throw new ParserException(
                "Error! Workflow should have connecting links, found " + getTokenString(peek));
    } else if (peek == JsonToken.BEGIN_ARRAY) {
        jsonReader.beginArray();
        while (jsonReader.hasNext()) {
            links.add(readLink(jsonReader));
        }
        jsonReader.endArray();
    } else {
        throw new ParserException("Error! Unsupported value for workflow links, expected "
                + getTokenString(JsonToken.BEGIN_ARRAY) + " but found" + getTokenString(peek));

    }
}

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

License:Apache License

private List<InPort> readApplicationInputs(JsonReader jsonReader) throws IOException, ParserException {
    List<InPort> inPorts = new ArrayList<>();
    JsonToken peek = jsonReader.peek();// ww w . j  a va  2 s .  com
    PortModel portModel;
    InPort inPort;
    String name;
    if (peek == JsonToken.NULL) {
        jsonReader.nextNull();
    } else if (peek == JsonToken.BEGIN_ARRAY) {
        jsonReader.beginArray();
        while (jsonReader.hasNext()) {
            portModel = new PortModel();
            inPort = new InputPortIml(portModel);
            jsonReader.beginObject();
            while (jsonReader.hasNext()) {
                name = jsonReader.nextName();
                if (name.equals(NAME)) {
                    portModel.setName(jsonReader.nextString());
                } else if (name.equals(ID)) {
                    portModel.setPortId(jsonReader.nextString());
                } else if (name.equals(DATATYPE)) {
                    jsonReader.skipValue();
                } else if (name.equals(DEFAULT_VALUE)) {
                    inPort.setDefaultValue(jsonReader.nextString());
                } else if (name.equals(DESCRIPTION)) {
                    portModel.setDescription(jsonReader.nextString());
                } else {
                    jsonReader.skipValue();
                }
            }
            jsonReader.endObject();
            inPorts.add(inPort);
        }
        jsonReader.endArray();
    } else {
        throw new ParserException(
                "Error! reading application inputs, expected " + getTokenString(JsonToken.NULL) + " or "
                        + getTokenString(JsonToken.BEGIN_ARRAY) + " but found " + getTokenString(peek));
    }

    return inPorts;
}

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

License:Apache License

private List<OutPort> readApplicationOutputs(JsonReader jsonReader) throws IOException, ParserException {
    List<OutPort> outPorts = new ArrayList<>();
    PortModel portModel;/*from w w w  .  j  a  va 2s.c o m*/
    OutPort outPort;
    String name;
    JsonToken peek = jsonReader.peek();
    if (peek == JsonToken.NULL) {
        jsonReader.nextNull();
    } else if (peek == JsonToken.BEGIN_ARRAY) {
        jsonReader.beginArray();
        while (jsonReader.hasNext()) {
            portModel = new PortModel();
            outPort = new OutPortImpl(portModel);
            jsonReader.beginObject();
            while (jsonReader.hasNext()) {
                name = jsonReader.nextName();
                if (name.equals(NAME)) {
                    portModel.setName(jsonReader.nextString());
                } else if (name.equals(ID)) {
                    portModel.setPortId(jsonReader.nextString());
                } else if (name.equals(DATATYPE)) {
                    jsonReader.skipValue();
                } else if (name.equals(DEFAULT_VALUE)) {
                    jsonReader.skipValue(); // can output has default values?
                } else if (name.equals(DESCRIPTION)) {
                    portModel.setDescription(jsonReader.nextString());
                } else {
                    jsonReader.skipValue();
                }
            }
            jsonReader.endObject();
            outPorts.add(outPort);
        }
        jsonReader.endArray();
    } else {
        throw new ParserException(
                "Error! reading application outputs, expected " + getTokenString(JsonToken.NULL) + " or "
                        + getTokenString(JsonToken.BEGIN_ARRAY) + " but found " + getTokenString(peek));

    }
    return outPorts;
}

From source file:org.apache.ambari.view.hive.resources.uploads.parsers.json.JSONIterator.java

License:Apache License

public JSONIterator(JsonReader reader) throws IOException {
    this.reader = reader;
    // test the start of array
    JsonToken jt = reader.peek();//from w  w  w  .  ja v a  2  s .c  o m
    if (jt != JsonToken.BEGIN_ARRAY) {
        throw new IllegalArgumentException("Expected the whole document to contain a single JsonArray.");
    }

    reader.beginArray(); // read the start of array
    try {
        nextObject = readNextObject(this.reader);
    } catch (EndOfDocumentException e) {
    }
}

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

License:Apache License

private void readName() throws IOException, XMLStreamException {
    nextName();/*from  w ww . jav  a 2 s  .co 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;
    }
}