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:ch.cyberduck.core.importer.ExpandriveBookmarkCollection.java

License:Open Source License

@Override
protected void parse(final Local file) throws AccessDeniedException {
    try {/*w w  w .j  a v  a2  s  .c om*/
        final JsonReader reader = new JsonReader(new InputStreamReader(file.getInputStream(), "UTF-8"));
        reader.beginArray();
        while (reader.hasNext()) {
            reader.beginObject();
            final Host current = new Host(new FTPProtocol(),
                    PreferencesFactory.get().getProperty("connection.hostname.default"));
            while (reader.hasNext()) {
                final String name = reader.nextName();
                switch (name) {
                case "server":
                    current.setHostname(reader.nextString());
                    break;
                case "username":
                    current.getCredentials().setUsername(reader.nextString());
                    break;
                case "private_key_file":
                    current.getCredentials().setIdentity(LocalFactory.get(reader.nextString()));
                    break;
                case "remotePath":
                    current.setDefaultPath(reader.nextString());
                    break;
                case "type":
                    final Protocol type = ProtocolFactory.forName(reader.nextString());
                    if (null != type) {
                        current.setProtocol(type);
                    }
                    break;
                case "protocol":
                    final Protocol protocol = ProtocolFactory.forName(reader.nextString());
                    if (null != protocol) {
                        current.setProtocol(protocol);
                        // Reset port to default
                        current.setPort(-1);
                    }
                    break;
                case "name":
                    current.setNickname(reader.nextString());
                    break;
                case "region":
                    current.setRegion(reader.nextString());
                    break;
                default:
                    log.warn(String.format("Ignore property %s", name));
                    reader.skipValue();
                    break;
                }
            }
            reader.endObject();
            this.add(current);
        }
        reader.endArray();
    } catch (IllegalStateException | IOException e) {
        throw new LocalAccessDeniedException(e.getMessage(), e);
    }
}

From source file:ch.cyberduck.core.importer.JsonBookmarkCollection.java

License:Open Source License

protected String readNext(final String name, final JsonReader reader) throws IOException {
    if (reader.peek() != JsonToken.NULL) {
        return reader.nextString();
    } else {/* ww w . j av  a  2  s  .  c o  m*/
        reader.skipValue();
        log.warn(String.format("No value for key %s", name));
        return null;
    }
}

From source file:ch.cyberduck.core.importer.NetDrive2BookmarkCollection.java

License:Open Source License

@Override
protected void parse(final ProtocolFactory protocols, final Local file) throws AccessDeniedException {
    try {//from w  w  w .  j av  a  2 s . c  om
        final JsonReader reader = new JsonReader(new InputStreamReader(file.getInputStream(), "UTF-8"));
        reader.beginArray();
        String url;
        String user;
        boolean ssl;
        Protocol protocol;
        while (reader.hasNext()) {
            reader.beginObject();
            boolean skip = false;
            url = null;
            ssl = false;
            protocol = null;
            user = null;
            while (reader.hasNext()) {
                final String name = reader.nextName();
                switch (name) {
                case "url":
                    url = this.readNext(name, reader);
                    if (StringUtils.isBlank(url)) {
                        skip = true;
                    }
                    break;
                case "ssl":
                    ssl = reader.nextBoolean();
                    break;
                case "user":
                    user = this.readNext(name, reader);
                    break;
                case "type":
                    final String type = this.readNext(name, reader);
                    switch (type) {
                    case "google_cloud_storage":
                        protocol = protocols.forType(Protocol.Type.googlestorage);
                        break;
                    case "gdrive":
                        protocol = protocols.forType(Protocol.Type.googledrive);
                        break;
                    default:
                        protocol = protocols.forName(type);
                    }
                    break;

                default:
                    log.warn(String.format("Ignore property %s", name));
                    reader.skipValue();
                    break;
                }
            }
            reader.endObject();
            if (!skip && protocol != null && StringUtils.isNotBlank(user)) {
                if (ssl) {
                    switch (protocol.getType()) {
                    case ftp:
                        protocol = protocols.forScheme(Scheme.ftps);
                        break;
                    case dav:
                        protocol = protocols.forScheme(Scheme.davs);
                        break;
                    }
                }
                this.add(HostParser.parse(protocols, protocol, url));
            }
        }
        reader.endArray();
    } catch (IllegalStateException | IOException e) {
        throw new LocalAccessDeniedException(e.getMessage(), e);
    }
}

From source file:classifiers.DummyClassifier.java

License:Apache License

public void parseStreamAndClassify(String jsonFile, String resultsFile) throws IOException {

    String journalName;/*  w  w w .  jav a2 s. c o m*/
    int count = 0;
    int abstract_count = 0;

    try {
        JsonReader reader = new JsonReader(new InputStreamReader(new FileInputStream(jsonFile)));
        JsonWriter writer = new JsonWriter(new OutputStreamWriter(new FileOutputStream(resultsFile), "UTF-8"));
        writer.setIndent("    ");

        //reader.setLenient(true);
        reader.beginArray();
        writer.beginArray();
        while (reader.hasNext()) {

            reader.beginObject();
            writer.beginObject();
            while (reader.hasNext()) {
                String name = reader.nextName();

                if (name.equals("abstract")) {
                    abstract_count++;
                    reader.skipValue();

                } else if (name.equals("pmid")) {
                    String pmid = reader.nextString();
                    writer.name("labels");
                    writeLabels(writer);
                    writer.name("pmid").value(pmid);
                } else if (name.equals("title")) {
                    reader.skipValue();
                } else {
                    System.out.println(name);
                    reader.skipValue();
                }
            }
            reader.endObject();
            writer.endObject();
        }
        reader.endArray();
        writer.endArray();

        System.out.println("Abstracts: " + abstract_count);

        writer.close();
    } catch (FileNotFoundException ex) {

    }
}

From source file:co.cask.cdap.common.stream.StreamEventTypeAdapter.java

License:Apache License

@Override
public StreamEvent read(JsonReader in) throws IOException {
    long timestamp = -1;
    Map<String, String> headers = null;
    ByteBuffer body = null;/*from  w ww  .j a  v a2  s .  c  o m*/

    in.beginObject();
    while (in.peek() == JsonToken.NAME) {
        String key = in.nextName();
        if ("timestamp".equals(key)) {
            timestamp = in.nextLong();
        } else if ("headers".equals(key)) {
            headers = mapTypeAdapter.read(in);
        } else if ("body".equals(key)) {
            body = ByteBuffer.wrap(Bytes.toBytesBinary(in.nextString()));
        } else {
            in.skipValue();
        }
    }

    if (timestamp >= 0 && headers != null && body != null) {
        in.endObject();
        return new StreamEvent(headers, body, timestamp);
    }
    throw new IOException(String.format("Failed to read StreamEvent. Timestamp: %d, headers: %s, body: %s",
            timestamp, headers, body));
}

From source file:co.cask.cdap.format.StructuredRecordStringConverter.java

License:Apache License

private static StructuredRecord readRecord(JsonReader reader, Schema schema) throws IOException {
    StructuredRecord.Builder builder = StructuredRecord.builder(schema);

    reader.beginObject();/*  w  w  w. ja  va 2  s. c om*/
    while (reader.peek() != JsonToken.END_OBJECT) {
        Schema.Field field = schema.getField(reader.nextName());
        if (field == null) {
            // Ignore unrecognized fields
            reader.skipValue();
            continue;
        }

        builder.set(field.getName(), readJson(reader, field.getSchema()));
    }
    reader.endObject();

    return builder.build();
}

From source file:com.aliyun.openservices.odps.console.common.JobDetailInfo.java

License:Apache License

private List<FuxiJob> loadJobsFromStream(InputStream in) throws ODPSConsoleException {
    boolean debug = true;
    ArrayList<FuxiJob> jobs = new ArrayList<FuxiJob>();
    if (debug) {/*from w  ww .j  ava2s  . c o  m*/
        try {
            JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
            reader.beginObject();
            while (reader.hasNext()) {
                String name = reader.nextName();
                if (name.equals("mapReduce")) {
                    reader.beginObject();
                    while (reader.hasNext()) {
                        String nameInMapReduce = reader.nextName();
                        if (nameInMapReduce.equals("jobs")) {
                            reader.beginArray();
                            while (reader.hasNext()) {
                                jobs.add(getFuxiJobFromJson(reader));
                            }
                            reader.endArray();
                        } else if (nameInMapReduce.equals("jsonSummary")) {
                            getInfoFromJsonSummary(jobs, reader.nextString());
                        } else {
                            reader.skipValue();
                        }
                    }
                    reader.endObject();
                } else {
                    reader.skipValue();
                }
            }
            reader.endObject();
        } catch (IOException e) {
            e.printStackTrace();
            throw new ODPSConsoleException("Bad json format");
        }
    }

    return jobs;
}

From source file:com.aliyun.openservices.odps.console.common.JobDetailInfo.java

License:Apache License

private FuxiJob getFuxiJobFromJson(JsonReader reader) throws IOException {
    FuxiJob job = new FuxiJob();

    reader.beginObject();/*from  w w w  .  ja va2  s .c  o m*/
    while (reader.hasNext()) {
        String nameInJob = reader.nextName();
        if (nameInJob.equals("name")) {
            job.name = reader.nextString();
        } else if (nameInJob.equals("tasks")) {
            reader.beginArray();
            job.tasks = new ArrayList<FuxiTask>();
            while (reader.hasNext()) {
                job.tasks.add(getFuxiTaskFromJson(reader));
            }
            reader.endArray();
        } else {
            reader.skipValue();
        }
    }
    reader.endObject();
    return job;
}

From source file:com.aliyun.openservices.odps.console.common.JobDetailInfo.java

License:Apache License

private FuxiTask getFuxiTaskFromJson(JsonReader reader) throws IOException {
    FuxiTask task = new FuxiTask();

    reader.beginObject();//from w  w w.j  a va 2  s . co m
    while (reader.hasNext()) {
        String nameInTask = reader.nextName();
        if (nameInTask.equals("name")) {
            task.name = reader.nextString();
        } else if (nameInTask.equals("instances")) {
            task.instances = new ArrayList<FuxiInstance>();
            task.upTasks = new ArrayList<FuxiTask>();
            task.downTasks = new ArrayList<FuxiTask>();
            reader.beginArray();
            while (reader.hasNext()) {
                task.instances.add(getFuxiInstanceFromJson(reader));
            }
            reader.endArray();
        } else {
            reader.skipValue();
        }
    }
    reader.endObject();
    return task;
}

From source file:com.aliyun.openservices.odps.console.common.JobDetailInfo.java

License:Apache License

private FuxiInstance getFuxiInstanceFromJson(JsonReader reader) throws IOException {
    FuxiInstance inst = new FuxiInstance();

    reader.beginObject();//from   w w  w . j a v a2  s .c  o  m
    long endTime = 0;
    while (reader.hasNext()) {
        String nameInInstance = reader.nextName();
        if (nameInInstance.equals("id")) {
            inst.id = reader.nextString();
        } else if (nameInInstance.equals("logId")) {
            inst.logid = reader.nextString();
            inst.IpAndPath = this.decodeLogId(inst.logid);
        } else if (nameInInstance.equals("startTime")) {
            inst.startTime = reader.nextLong();
        } else if (nameInInstance.equals("endTime")) {
            endTime = reader.nextLong();
        } else if (nameInInstance.equals("status")) {
            inst.status = reader.nextString();
        } else {
            reader.skipValue();
        }
    }
    inst.duration = (int) (endTime - inst.startTime);
    reader.endObject();
    return inst;
}