Example usage for com.google.gson.stream JsonReader endArray

List of usage examples for com.google.gson.stream JsonReader endArray

Introduction

In this page you can find the example usage for com.google.gson.stream JsonReader endArray.

Prototype

public void endArray() throws IOException 

Source Link

Document

Consumes the next token from the JSON stream and asserts that it is the end of the current array.

Usage

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.java  2  s . c  o  m*/
    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  ww.j  ava 2 s .  co 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 ww  w  . j  a  v a2  s.  co  m
    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();//from w  w w  . j av  a 2s  .c o m
    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;// www. ja v a 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.axis2.json.gson.rpc.JsonUtils.java

License:Apache License

public static Object invokeServiceClass(JsonReader jsonReader, Object service, Method operation,
        Class[] paramClasses, int paramCount)
        throws InvocationTargetException, IllegalAccessException, IOException {

    Object[] methodParam = new Object[paramCount];
    Gson gson = new Gson();
    String[] argNames = new String[paramCount];

    if (!jsonReader.isLenient()) {
        jsonReader.setLenient(true);//www . ja v a  2  s.  c  o  m
    }
    jsonReader.beginObject();
    jsonReader.nextName(); // get message name from input json stream
    jsonReader.beginArray();

    int i = 0;
    for (Class paramType : paramClasses) {
        jsonReader.beginObject();
        argNames[i] = jsonReader.nextName();
        methodParam[i] = gson.fromJson(jsonReader, paramType); // gson handle all types well and return an object from it
        jsonReader.endObject();
        i++;
    }

    jsonReader.endArray();
    jsonReader.endObject();

    return operation.invoke(service, methodParam);

}

From source file:org.apache.hadoop.dynamodb.importformat.ImportInputFormat.java

License:Open Source License

/**
 * This method retrieves the URLs of all S3 files and generates input splits by combining
 * multiple S3 URLs into one split./* w w  w  .  j  a  v  a2 s .com*/
 *
 * @return a list of input splits. The length of this list may not be exactly the same as
 * <code>numSplits</code>. For example, if numSplits is larger than MAX_NUM_SPLITS or the number
 * of S3 files, then numSplits is ignored. Furthermore, not all input splits contain the same
 * number of S3 files. For example, with five S3 files {s1, s2, s3, s4, s5} and numSplits = 3,
 * this method returns a list of three input splits: {s1, s2}, {s3, s4} and {s5}.
 */
private List<InputSplit> readEntries(JsonReader reader, JobConf job) throws IOException {
    List<Path> paths = new ArrayList<Path>();
    Gson gson = DynamoDBUtil.getGson();

    reader.beginArray();
    while (reader.hasNext()) {
        ExportManifestEntry entry = gson.fromJson(reader, ExportManifestEntry.class);
        paths.add(new Path(entry.url));
    }
    reader.endArray();
    log.info("Number of S3 files: " + paths.size());

    if (paths.size() == 0) {
        return Collections.emptyList();
    }

    int filesPerSplit = (int) Math.ceil((double) (paths.size()) / Math.min(MAX_NUM_SPLITS, paths.size()));
    int numSplits = (int) Math.ceil((double) (paths.size()) / filesPerSplit);

    long[] fileMaxLengths = new long[filesPerSplit];
    Arrays.fill(fileMaxLengths, Long.MAX_VALUE / filesPerSplit);

    long[] fileStarts = new long[filesPerSplit];
    Arrays.fill(fileStarts, 0);

    List<InputSplit> splits = new ArrayList<InputSplit>(numSplits);
    for (int i = 0; i < numSplits; i++) {
        int start = filesPerSplit * i;
        int end = filesPerSplit * (i + 1);
        if (i == (numSplits - 1)) {
            end = paths.size();
        }
        Path[] pathsInOneSplit = paths.subList(start, end).toArray(new Path[end - start]);
        CombineFileSplit combineFileSplit = new CombineFileSplit(job, pathsInOneSplit, fileStarts,
                fileMaxLengths, new String[0]);
        splits.add(combineFileSplit);
    }

    return splits;
}

From source file:org.apache.jclouds.oneandone.rest.util.ServerFirewallPolicyAdapter.java

License:Apache License

@Override
public List<T> read(JsonReader reader) throws IOException {
    List<ServerFirewallPolicy> list = new ArrayList<ServerFirewallPolicy>();
    if (reader.peek() == JsonToken.BEGIN_OBJECT) {
        Type mapType = new TypeToken<Map<String, Object>>() {
        }.getType();/*from w w w.j  a  v  a 2 s .  c o  m*/
        Map<String, String> jsonMap = gson.fromJson(reader, mapType);
        ServerFirewallPolicy inning = ServerFirewallPolicy.create(jsonMap.get("id"), jsonMap.get("name"));
        list.add(inning);

    } else if (reader.peek() == JsonToken.BEGIN_ARRAY) {

        reader.beginArray();
        while (reader.hasNext()) {
            Type mapType = new TypeToken<Map<String, Object>>() {
            }.getType();
            Map<String, String> jsonMap = gson.fromJson(reader, mapType);
            ServerFirewallPolicy inning = ServerFirewallPolicy.create(jsonMap.get("id"), jsonMap.get("name"));
            list.add(inning);
        }
        reader.endArray();

    } else {
        reader.skipValue();
    }
    return (List<T>) list;
}

From source file:org.apache.jclouds.oneandone.rest.util.SnapshotAdapter.java

License:Apache License

@Override
public List<T> read(JsonReader reader) throws IOException {
    List<Snapshot> list = new ArrayList<Snapshot>();
    if (reader.peek() == JsonToken.BEGIN_OBJECT) {
        Type mapType = new TypeToken<Map<String, Object>>() {
        }.getType();//ww w .  j  a  v a  2 s .  c  om
        Map<String, String> jsonMap = gson.fromJson(reader, mapType);
        Snapshot inning = Snapshot.create(jsonMap.get("id"), jsonMap.get("creation_date"),
                jsonMap.get("deletion_date"));
        list.add(inning);
    } else if (reader.peek() == JsonToken.BEGIN_ARRAY) {

        reader.beginArray();
        while (reader.hasNext()) {
            Type mapType = new TypeToken<Map<String, Object>>() {
            }.getType();
            Map<String, String> jsonMap = gson.fromJson(reader, mapType);
            Snapshot inning = Snapshot.create(jsonMap.get("id"), jsonMap.get("creation_date"),
                    jsonMap.get("deletion_date"));
            list.add(inning);
        }
        reader.endArray();
    } else {
        reader.skipValue();
    }
    return (List<T>) list;
}

From source file:org.apache.olingo.odata2.core.ep.consumer.JsonErrorDocumentConsumer.java

License:Apache License

private String readJson(final JsonReader reader) throws IOException {
    StringBuilder sb = new StringBuilder();

    while (reader.hasNext()) {
        JsonToken token = reader.peek();
        if (token == JsonToken.NAME) {
            if (sb.length() > 0) {
                sb.append(",");
            }/*from   ww w. ja  va 2 s. c om*/
            String name = reader.nextName();
            sb.append("\"").append(name).append("\"").append(":");
        } else if (token == JsonToken.BEGIN_OBJECT) {
            reader.beginObject();
            sb.append("{").append(readJson(reader)).append("}");
            reader.endObject();
        } else if (token == JsonToken.BEGIN_ARRAY) {
            reader.beginArray();
            sb.append("[").append(readJson(reader)).append("]");
            reader.endArray();
        } else {
            sb.append("\"").append(reader.nextString()).append("\"");
        }
    }

    return sb.toString();
}