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:at.univie.sensorium.preferences.Preferences.java

License:Open Source License

private void loadPrefsFromStream(InputStream input) {
    List<BasicNameValuePair> preferencelist = new LinkedList<BasicNameValuePair>();
    try {/*www. j  av a  2  s  .com*/
        InputStreamReader isreader = new InputStreamReader(input);
        JsonReader reader = new JsonReader(isreader);
        //         String jsonVersion = "";

        reader.beginArray(); // do we have an array or just a single object?
        reader.beginObject();
        while (reader.hasNext()) {
            String name = reader.nextName();
            String value = reader.nextString();
            if (name.equalsIgnoreCase(PREFERENCES_VERSION))
                currentPrefVersion = Integer.valueOf(value);
            BasicNameValuePair kv = new BasicNameValuePair(name, value);
            preferencelist.add(kv);
        }
        reader.endObject();
        reader.endArray();
        reader.close();

        if (newerPrefsAvailable()) {
            Log.d(SensorRegistry.TAG, "Newer preferences available in json, overwriting existing.");
            for (BasicNameValuePair kv : preferencelist) {
                putPreference(kv.getName(), kv.getValue());
            }
            // also reset the welcome screen
            putBoolean(WELCOME_SCREEN_SHOWN, false);
        } else {
            Log.d(SensorRegistry.TAG, "Preferences are recent, not overwriting.");
        }

    } catch (FileNotFoundException e) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        Log.d(SensorRegistry.TAG, sw.toString());
    } catch (IOException e) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        Log.d(SensorRegistry.TAG, sw.toString());
    }
}

From source file:ca.mcgill.cs.creco.data.CRDeadlinks.java

License:Apache License

private void tryreadingthejson() throws IOException {
    InputStream in = new FileInputStream(DataPath.get() + "dead_links.json");
    JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
    reader.beginArray();/*w ww.j av  a  2 s.  co  m*/
    while (reader.hasNext()) {

    }

    reader.endArray();
    reader.close();
    in.close();
}

From source file:ca.mcgill.cs.creco.data.CRDeadlinks.java

License:Apache License

private static void readFile(String pFilePath) throws IOException, InterruptedException {
    InputStream in = new FileInputStream(pFilePath);
    JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
    reader.beginArray();//from   w  w w  .  j  ava 2s .c  o m
    while (reader.hasNext()) {

        ProductStub prodStub = new Gson().fromJson(reader, ProductStub.class);
        writeToFile = writeToFile.concat("{\"product_id\":" + prodStub.id + ",");
        String urltext = prodStub.modelOverviewPageUrl;
        // Write 404 error if no URL exists
        if (urltext == null) {
            writeToFile = writeToFile.concat("\"state\":" + "404" + "},");
            continue;
        }

        Thread.sleep(SLEEP);
        URL url = new URL(urltext);
        // Attempt a connection and see the resulting response code it returns
        int responseCode = ((HttpURLConnection) url.openConnection()).getResponseCode();
        writeToFile = writeToFile.concat("\"state\":" + responseCode + "},");
    }
    reader.endArray();
    reader.close();
    in.close();
}

From source file:ca.mcgill.cs.creco.data.json.JsonLoadingService.java

License:Apache License

private static void readFile(String filePath, IDataCollector pCollector) throws IOException {
    InputStream in = new FileInputStream(filePath);
    JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));

    reader.beginArray();/*from  w  w  w  . ja  va  2 s. com*/
    while (reader.hasNext()) {
        ProductStub prodStub = new Gson().fromJson(reader, ProductStub.class);
        pCollector.addProduct(buildProduct(prodStub));

    }
    reader.endArray();
    reader.close();
    in.close();
}

From source file:ca.mcgill.cs.creco.data.json.JsonLoadingService.java

License:Apache License

private void readDeadLinks() throws FileNotFoundException, IOException {
    // Try to read the dead links file
    InputStream in;/*  ww  w.j av a2 s .  c om*/
    try {
        in = new FileInputStream(aPath + aDeadLinksFileName);
    } catch (FileNotFoundException e) {
        return;
    }

    // Flag that we were succesful finding the deadlins file, so deadlinks
    // will be checked while building products
    aDoCheckDeadLinks = true;

    // Make a json reader for the deadlinks file
    JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
    reader.beginArray();

    // Iterate over each entry in the deadlinks file, putting a records in a HashTable
    while (reader.hasNext()) {
        LinkResponseStub responseStub = new Gson().fromJson(reader, LinkResponseStub.class);
        aDeadLinks.put(responseStub.product_id, responseStub.state);
    }

    reader.endArray();
    reader.close();
    in.close();
}

From source file:cc.kave.commons.utils.json.legacy.UsageTypeAdapter.java

License:Open Source License

@Override
public Usage read(JsonReader in) throws IOException {

    if (in.peek() == JsonToken.STRING) {
        String val = in.nextString();
        Asserts.assertEquals("NoUsage", val, "Invalid JSON. Expected 'NoUsage', but found '" + val + "'.");
        return new NoUsage();
    }//from w  ww .  j  av  a2s.  co  m

    Query q = new Query();
    q.setAllCallsites(null);

    in.beginObject();
    while (in.hasNext()) {
        String name = in.nextName();
        if (TYPE.equals(name)) {
            q.setType(CoReTypeName.get(in.nextString()));
        } else if (CLASS_CTX.equals(name)) {
            q.setClassContext(CoReTypeName.get(in.nextString()));
        } else if (METHOD_CTX.equals(name)) {
            q.setMethodContext(CoReMethodName.get(in.nextString()));
        } else if (DEFINITION.equals(name)) {
            q.setDefinition(readDefinition(in));
        } else if (SITES.equals(name)) {
            q.setAllCallsites(readCallSites(in));
        } else {
            // skip value (most likely $type key from .net serialization)
            in.nextString();
        }
    }
    in.endObject();
    return q;
}

From source file:cc.kave.commons.utils.json.legacy.UsageTypeAdapter.java

License:Open Source License

private DefinitionSite readDefinition(JsonReader in) throws IOException {
    DefinitionSite def = DefinitionSites.createUnknownDefinitionSite();
    def.setKind(null);/* ww w  .  j  a v  a2s .c  o 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(CoReFieldName.get(in.nextString()));
        } else if (DEF_METHOD.equals(name)) {
            def.setMethod(CoReMethodName.get(in.nextString()));
        }
    }
    in.endObject();

    return def;
}

From source file:cc.kave.commons.utils.json.legacy.UsageTypeAdapter.java

License:Open Source License

private Set<CallSite> readCallSites(JsonReader in) throws IOException {
    Set<CallSite> sites = Sets.newLinkedHashSet();
    in.beginArray();/*from   w  ww. j ava  2  s .co  m*/
    while (in.hasNext()) {
        sites.add(readCallSite(in));
    }
    in.endArray();
    return sites;
}

From source file:cc.kave.commons.utils.json.legacy.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 av a 2  s .c om
    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(CoReMethodName.get(in.nextString()));
        } else if (CS_KIND.equals(name)) {
            site.setKind(CallSiteKind.valueOf(in.nextString()));
        }
    }
    in.endObject();
    return site;
}

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

License:Open Source License

@Override
public Usage read(JsonReader in) throws IOException {
    Query q = new Query();
    q.setAllCallsites(null);/*from  ww w. j av  a  2s .  c o m*/

    in.beginObject();
    while (in.hasNext()) {
        String name = in.nextName();
        if (TYPE.equals(name)) {
            q.setType(VmTypeName.get(in.nextString()));
        } else if (CLASS_CTX.equals(name)) {
            q.setClassContext(VmTypeName.get(in.nextString()));
        } else if (METHOD_CTX.equals(name)) {
            q.setMethodContext(VmMethodName.get(in.nextString()));
        } else if (DEFINITION.equals(name)) {
            q.setDefinition(readDefinition(in));
        } else if (SITES.equals(name)) {
            q.setAllCallsites(readCallSites(in));
        } else {
            // skip value (most likely $type key from .net serialization)
            in.nextString();
        }
    }
    in.endObject();
    return q;
}