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

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

Introduction

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

Prototype

public void skipValue() throws IOException 

Source Link

Document

Skips the next value recursively.

Usage

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

License:Apache License

private ApplicationNode readApplication(JsonReader jsonReader) throws IOException, ParserException {
    jsonReader.beginObject();//  w  w  w. ja v  a  2 s  .c om
    NodeModel nodeModel = new NodeModel();
    ComponentStatus status = new ComponentStatus();
    status.setState(ComponentState.CREATED);
    status.setReason("Created");
    nodeModel.setStatus(status);
    ApplicationNode applicationNode = new ApplicationNodeImpl(nodeModel);
    String name;
    while (jsonReader.hasNext()) {
        name = jsonReader.nextName();
        if (name.equals(APPLICATION_ID)) {
            nodeModel.setApplicationId(jsonReader.nextString());
        } else if (name.equals(NAME)) {
            nodeModel.setName(jsonReader.nextString());
        } else if (name.equals(DESCRIPTION)) {
            nodeModel.setDescription(jsonReader.nextString());
        } else if (name.equals(APPTYPE)) {
            jsonReader.skipValue();
        } else if (name.equals(INPUTS)) {
            applicationNode.addInputPorts(readApplicationInputs(jsonReader));
        } else if (name.equals(OUTPUTS)) {
            applicationNode.addOutPorts(readApplicationOutputs(jsonReader));
        } else if (name.equals(POSITION)) {
            readPosition(jsonReader);
        } else if (name.equals(NODE_ID)) {
            nodeModel.setNodeId(jsonReader.nextString());
        } else if (name.equals(PARALLEL_EXECUTION)) {
            jsonReader.skipValue();
        } else if (name.equals(PROPERTIES)) {
            readProperties(jsonReader);
        }
    }
    jsonReader.endObject();
    return applicationNode;
}

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   ww  w . ja v  a2 s.co  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;/*from  w  w  w . j  av  a 2  s.co  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.airavata.workflow.core.parser.JsonWorkflowParser.java

License:Apache License

private void readPosition(JsonReader jsonReader) throws IOException {
    JsonToken peek = jsonReader.peek();/*  w ww  . ja  va2 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();/*from   www.  j  av a2s . c  o m*/
    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.hadoop.dynamodb.importformat.ImportInputFormat.java

License:Open Source License

/**
 * An example manifest file looks like/*  w ww.  java  2 s  .co m*/
 *
 * {"name":"DynamoDB-export","version":3, "entries":[
 * {"url":"s3://path/to/object/92dd1414-a049-4c68-88fb-a23acd44907e","mandatory":true},
 * {"url":"s3://path/to/object/ba3f3535-7aa1-4f97-a530-e72938bf4b76","mandatory":true} ]}
 */
// @formatter:on
private List<InputSplit> parseManifest(FileSystem fs, Path manifestPath, JobConf job) throws IOException {
    List<InputSplit> splits = null;

    FSDataInputStream fp = fs.open(manifestPath);
    JsonReader reader = new JsonReader(new InputStreamReader(fp, Charsets.UTF_8));

    reader.beginObject();
    while (reader.hasNext()) {
        String name = reader.nextName();
        switch (name) {
        case VERSION_JSON_KEY:
            job.set(DynamoDBConstants.EXPORT_FORMAT_VERSION, String.valueOf(reader.nextInt()));
            break;
        case ENTRIES_JSON_KEY:
            splits = readEntries(reader, job);
            break;
        default:
            log.info("Skipping a JSON key in the manifest file: " + name);
            reader.skipValue();
            break;
        }
    }
    reader.endObject();

    if (splits == null) {
        return Collections.emptyList();
    }
    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 .  ja va2  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();//w  ww  . jav a2  s  . c  o m
        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

/**
 * Read the string value from the JsonReader or 'null' if no value is available.
 * // w  ww.java 2s . c o m
 * @param reader to read from
 * @return the string value or 'null'
 * @throws IOException if an exception occurs
 */
private String getValue(final JsonReader reader) throws IOException {
    JsonToken token = reader.peek();
    if (JsonToken.NULL == token) {
        reader.skipValue();
        return null;
    }
    return reader.nextString();
}

From source file:org.arl.fjage.remote.ArrayAdapterFactory.java

License:BSD License

@SuppressWarnings("unchecked")
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
    final Class<T> rawType = (Class<T>) type.getRawType();
    if (!rawType.isArray())
        return null;
    final Class<?> compType = rawType.getComponentType();
    if (compType == null)
        return null;
    if (!compType.isPrimitive())
        return null;
    if (!compType.equals(byte.class) && !compType.equals(int.class) && !compType.equals(short.class)
            && !compType.equals(long.class) && !compType.equals(float.class) && !compType.equals(double.class))
        return null;
    final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);
    return new TypeAdapter<T>() {

        @Override/*from   w  w w.j  a v  a  2 s.c o  m*/
        public void write(JsonWriter out, T value) throws IOException {
            if (value == null)
                out.nullValue();
            else {
                byte[] data;
                if (compType.equals(byte.class))
                    data = (byte[]) value;
                else {
                    ByteBuffer buf = null;
                    if (compType.equals(int.class)) {
                        buf = ByteBuffer.allocate(Integer.SIZE / Byte.SIZE * ((int[]) value).length)
                                .order(ByteOrder.LITTLE_ENDIAN);
                        buf.asIntBuffer().put((int[]) value);
                    } else if (compType.equals(short.class)) {
                        buf = ByteBuffer.allocate(Short.SIZE / Byte.SIZE * ((short[]) value).length)
                                .order(ByteOrder.LITTLE_ENDIAN);
                        buf.asShortBuffer().put((short[]) value);
                    } else if (compType.equals(long.class)) {
                        buf = ByteBuffer.allocate(Long.SIZE / Byte.SIZE * ((long[]) value).length)
                                .order(ByteOrder.LITTLE_ENDIAN);
                        buf.asLongBuffer().put((long[]) value);
                    } else if (compType.equals(float.class)) {
                        buf = ByteBuffer.allocate(Float.SIZE / Byte.SIZE * ((float[]) value).length)
                                .order(ByteOrder.LITTLE_ENDIAN);
                        buf.asFloatBuffer().put((float[]) value);
                    } else if (compType.equals(double.class)) {
                        buf = ByteBuffer.allocate(Double.SIZE / Byte.SIZE * ((double[]) value).length)
                                .order(ByteOrder.LITTLE_ENDIAN);
                        buf.asDoubleBuffer().put((double[]) value);
                    }
                    data = buf.array();
                }
                if (bare)
                    out.value(Base64.getEncoder().encodeToString(data));
                else {
                    out.beginObject();
                    out.name("clazz").value(rawType.getName());
                    out.name("data").value(Base64.getEncoder().encodeToString(data));
                    out.endObject();
                }
            }
        }

        @Override
        public T read(JsonReader in) throws IOException {
            JsonToken tok = in.peek();
            if (tok == JsonToken.NULL) {
                in.nextNull();
                return null;
            }
            if (tok == JsonToken.STRING)
                return decodeString(in.nextString());
            if (tok == JsonToken.BEGIN_ARRAY)
                return delegate.read(in);
            if (tok != JsonToken.BEGIN_OBJECT)
                return null;
            T rv = null;
            in.beginObject();
            while (in.hasNext()) {
                String name = in.nextName();
                if (name.equals("data"))
                    rv = decodeString(in.nextString());
                else
                    in.skipValue();
            }
            in.endObject();
            return rv;
        }

        private T decodeString(String s) {
            byte[] data = Base64.getDecoder().decode(s);
            if (compType.equals(byte.class))
                return (T) data;
            ByteBuffer buf = ByteBuffer.wrap(data);
            if (compType.equals(int.class)) {
                IntBuffer buf2 = ByteBuffer.wrap(Base64.getDecoder().decode(s)).order(ByteOrder.LITTLE_ENDIAN)
                        .asIntBuffer();
                int[] array = new int[buf2.limit()];
                buf2.get(array);
                return (T) array;
            }
            if (compType.equals(short.class)) {
                ShortBuffer buf2 = ByteBuffer.wrap(Base64.getDecoder().decode(s)).order(ByteOrder.LITTLE_ENDIAN)
                        .asShortBuffer();
                short[] array = new short[buf2.limit()];
                buf2.get(array);
                return (T) array;
            }
            if (compType.equals(long.class)) {
                LongBuffer buf2 = ByteBuffer.wrap(Base64.getDecoder().decode(s)).order(ByteOrder.LITTLE_ENDIAN)
                        .asLongBuffer();
                long[] array = new long[buf2.limit()];
                buf2.get(array);
                return (T) array;
            }
            if (compType.equals(float.class)) {
                FloatBuffer buf2 = ByteBuffer.wrap(Base64.getDecoder().decode(s)).order(ByteOrder.LITTLE_ENDIAN)
                        .asFloatBuffer();
                float[] array = new float[buf2.limit()];
                buf2.get(array);
                return (T) array;
            }
            if (compType.equals(double.class)) {
                DoubleBuffer buf2 = ByteBuffer.wrap(Base64.getDecoder().decode(s))
                        .order(ByteOrder.LITTLE_ENDIAN).asDoubleBuffer();
                double[] array = new double[buf2.limit()];
                buf2.get(array);
                return (T) array;
            }
            return null;
        }

    };
}