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

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

Introduction

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

Prototype

public boolean hasNext() throws IOException 

Source Link

Document

Returns true if the current array or object has another element.

Usage

From source file:cc.recommenders.utils.gson.UsageTypeAdapter.java

License:Open Source License

private DefinitionSite readDefinition(JsonReader in) throws IOException {
    DefinitionSite def = DefinitionSites.createUnknownDefinitionSite();
    def.setKind(null);// w  ww .  ja v  a 2 s  .  co  m

    in.beginObject();
    while (in.hasNext()) {
        String name = in.nextName();
        if (DEF_KIND.equals(name)) {
            def.setKind(DefinitionSiteKind.valueOf(in.nextString()));
        } else if (DEF_ARG.equals(name)) {
            def.setArgIndex(in.nextInt());
        } else if (DEF_FIELD.equals(name)) {
            def.setField(VmFieldName.get(in.nextString()));
        } else if (DEF_METHOD.equals(name)) {
            def.setMethod(VmMethodName.get(in.nextString()));
        }
    }
    in.endObject();

    return def;
}

From source file:cc.recommenders.utils.gson.UsageTypeAdapter.java

License:Open Source License

private CallSite readCallSite(JsonReader in) throws IOException {
    CallSite site = CallSites.createReceiverCallSite("LT.m()V");
    site.setKind(null);//  w ww. j  a  va2 s.  c  o  m
    site.setMethod(null);

    in.beginObject();
    while (in.hasNext()) {
        String name = in.nextName();
        if (CS_ARG.equals(name)) {
            site.setArgIndex(in.nextInt());
        } else if (CS_CALL.equals(name)) {
            site.setMethod(VmMethodName.get(in.nextString()));
        } else if (CS_KIND.equals(name)) {
            site.setKind(CallSiteKind.valueOf(in.nextString()));
        }
    }
    in.endObject();
    return site;
}

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

License:Open Source License

@Override
protected void parse(final Local file) throws AccessDeniedException {
    try {/*from w ww  . j ava2  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.NetDrive2BookmarkCollection.java

License:Open Source License

@Override
protected void parse(final ProtocolFactory protocols, final Local file) throws AccessDeniedException {
    try {/*  www. j a v  a2 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:ch.cyberduck.core.s3.S3SessionCredentialsRetriever.java

License:Open Source License

protected AWSCredentials parse(final InputStream in) throws BackgroundException {
    try {/*from  w  w  w . ja  va2 s . c  om*/
        final JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
        reader.beginObject();
        String key = null;
        String secret = null;
        String token = null;
        while (reader.hasNext()) {
            final String name = reader.nextName();
            final String value = reader.nextString();
            switch (name) {
            case "AccessKeyId":
                key = value;
                break;
            case "SecretAccessKey":
                secret = value;
                break;
            case "Token":
                token = value;
                break;
            }
        }
        reader.endObject();
        return new AWSSessionCredentials(key, secret, token);
    } catch (UnsupportedEncodingException e) {
        throw new BackgroundException(e);
    } catch (MalformedJsonException e) {
        throw new InteroperabilityException("Invalid JSON response", e);
    } catch (IOException e) {
        throw new DefaultIOExceptionMappingService().map(e);
    }
}

From source file:classifiers.DummyClassifier.java

License:Apache License

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

    String journalName;//from  w  w w  .j  av  a  2s .  co  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.forsaken.projectindigo.utils.mojangtokens.FileTypeAdapter.java

License:Open Source License

@Override
public File read(JsonReader json) throws IOException {
    if (json.hasNext()) {
        String value = json.nextString();
        return value == null ? null : new File(value);
    }//www  . ja v a2s.  c  o  m
    return null;
}

From source file:com.aelitis.azureus.util.ObjectTypeAdapterLong.java

License:Apache License

@Override
public Object read(JsonReader in) throws IOException {
    JsonToken token = in.peek();//www.j a v a2s .  c  om
    switch (token) {
    case BEGIN_ARRAY:
        List<Object> list = new ArrayList<Object>();
        in.beginArray();
        while (in.hasNext()) {
            list.add(read(in));
        }
        in.endArray();
        return list;

    case BEGIN_OBJECT:
        Map<String, Object> map = new LinkedTreeMap<String, Object>();
        in.beginObject();
        while (in.hasNext()) {
            map.put(in.nextName(), read(in));
        }
        in.endObject();
        return map;

    case STRING:
        return in.nextString();

    case NUMBER: {
        String value = in.nextString();
        if (value.indexOf('.') >= 0) {
            return Double.parseDouble(value);
        } else {
            return Long.parseLong(value);
        }
    }

    case BOOLEAN:
        return in.nextBoolean();

    case NULL:
        in.nextNull();
        return null;

    default:
        throw new IllegalStateException();
    }
}

From source file:com.aliakseipilko.flightdutytracker.utils.AirportCodeConverter.java

License:Open Source License

private void generateAirports() {
    JsonReader jsonReader = new JsonReader(
            new InputStreamReader(ctx.getResources().openRawResource(R.raw.airports)));
    try {//from w  w w.j  a v  a 2  s .  co m
        jsonReader.beginArray();
        while (jsonReader.hasNext()) {
            Airport airport = gson.fromJson(jsonReader, Airport.class);
            if (airport.getIATA() == null) {
                airport.setIATA("");
            }
            if (airport.getICAO() == null) {
                airport.setICAO("");
            }
            airports.add(airport);
        }
        jsonReader.endArray();
        jsonReader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

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  w w.  jav a  2s . co 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;
}