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

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

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes this JSON reader and the underlying java.io.Reader .

Usage

From source file:com.quarterfull.newsAndroid.reader.owncloud.OwnCloudReaderMethods.java

License:Open Source License

/**
 * can read json like {"version":"1.101"}
 * @param in/*from www. j  av  a  2  s  .c o  m*/
 * @param iJoBj
 * @return
 * @throws IOException
 */
private static int readJsonStreamSimple(InputStream in, IHandleJsonObject iJoBj) throws IOException {
    int count = 0;
    JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
    //reader.setLenient(true);

    //JsonToken token = reader.peek();
    //while(token.equals(JsonToken.STRING))
    //   reader.skipValue();

    JSONObject e = getJSONObjectFromReader(reader);
    iJoBj.performAction(e);

    reader.close();

    return count;
}

From source file:com.razza.apps.iosched.sync.ConferenceDataHandler.java

License:Open Source License

/**
 * Processes a conference data body and calls the appropriate data type handlers
 * to process each of the objects represented therein.
 *
 * @param dataBody The body of data to process
 * @throws IOException If there is an error parsing the data.
 *///from w  w w  . j a  va  2  s.  c  o  m
private void processDataBody(String dataBody) throws IOException {
    JsonReader reader = new JsonReader(new StringReader(dataBody));
    JsonParser parser = new JsonParser();
    try {
        reader.setLenient(true); // To err is human

        // the whole file is a single JSON object
        reader.beginObject();

        while (reader.hasNext()) {
            // the key is "rooms", "speakers", "tracks", etc.
            String key = reader.nextName();
            if (mHandlerForKey.containsKey(key)) {
                // pass the value to the corresponding handler
                mHandlerForKey.get(key).process(parser.parse(reader));
            } else {
                LogUtils.LOGW(TAG, "Skipping unknown key in conference data json: " + key);
                reader.skipValue();
            }
        }
        reader.endObject();
    } finally {
        reader.close();
    }
}

From source file:com.registrodevisitas.services.CausasServices.java

public void generarListaDeCausasDeCache() throws FileNotFoundException, IOException {

    logger.info("CausasServices:generarListaDeCausasDeCache: BEGIN ");

    Gson gson = new Gson();

    JsonReader jsonReader = new JsonReader(new FileReader(this.cacheFile));

    final ListaDeCausas listaDecausas = gson.fromJson(jsonReader, ListaDeCausas.class);

    jsonReader.close();

    listaDecausas.sortCauses();/*from   w  w  w .jav  a  2s . c o m*/

    causas.addAll(listaDecausas.getCausas());

    listaDecausas.getCausas().clear();

    logger.info("CausasServices:generarListaDeCausasDeCache: END ");

}

From source file:com.registrodevisitas.services.ConfiguradorGeneralService.java

public void retrieveSettings() {

    logger.log(Level.INFO, "ConfiguradorGeneralService:retrieveSettings: BEGIN ");

    Configuracion configuracion = new Configuracion();

    try {/*from   www  .j a  v  a  2s  .c  om*/

        String fileName = PlatformFactory.getPlatform().getPrivateStorage() + File.separator + VISITAS_SETTINGS
                + ".json";

        File configFile = new File(fileName);

        logger.log(Level.INFO,
                "ConfiguradorGeneralService:retrieveSettings: folder: " + configFile.getAbsolutePath());

        if (configFile.exists()) {

            Gson gson = new Gson();

            JsonReader jsonReader = new JsonReader(new FileReader(fileName));

            final Configuracion respuesta = gson.fromJson(jsonReader, Configuracion.class);

            settings.get().setUrlServicios(respuesta.getUrlServicios());
            settings.get().setUltimoUser(respuesta.getUltimoUser());
            settings.get().setUltimaPassword(respuesta.getUltimaPassword());

            jsonReader.close();

        } else {
            settings.get().setUrlServicios("");
        }

        logger.log(Level.INFO, "ConfiguradorGeneralService:retrieveSettings: Config: " + settings.toString());

    } catch (IOException ex) {
        settings.get().setUrlServicios("");
        logger.log(Level.SEVERE, ex.getMessage(), ex);
    }

    logger.log(Level.INFO, "ConfiguradorGeneralService:retrieveSettings: END ");

}

From source file:com.registrodevisitas.services.MedicosServices.java

public void generarListaDeMedicosDeCache() throws FileNotFoundException, IOException {

    logger.info("MedicosServices:generarListaDeMedicosDeCache: BEGIN ");

    Gson gson = new Gson();

    JsonReader jsonReader = new JsonReader(new FileReader(this.cacheFile));

    final ListaDeMedicos listaDemedicos = gson.fromJson(jsonReader, ListaDeMedicos.class);

    jsonReader.close();

    medicos.addAll(listaDemedicos.getMedicos());

    listaDemedicos.getMedicos().clear();

    logger.info("MedicosServices:generarListaDeMedicosDeCache: END ");

}

From source file:com.registrodevisitas.services.PromocionesServices.java

public void generarListaDePromocionesDeCache() throws FileNotFoundException, IOException {

    logger.info("PromocionesServices:generarListaDePromocionesDeCache: BEGIN ");

    Gson gson = new Gson();

    JsonReader jsonReader = new JsonReader(new FileReader(this.cacheFile));

    final ListaDePromociones listaDepromociones = gson.fromJson(jsonReader, ListaDePromociones.class);

    jsonReader.close();

    listaDepromociones.sortPromociones();

    promociones.addAll(listaDepromociones.getPromociones());

    listaDepromociones.getPromociones().clear();

    logger.info("PromocionesServices:generarListaDePromocionesDeCache: END ");

}

From source file:com.sap.core.odata.core.ep.consumer.JsonEntityConsumer.java

License:Apache License

public ODataEntry readEntry(final EdmEntitySet entitySet, final InputStream content,
        final EntityProviderReadProperties properties) throws EntityProviderException {
    JsonReader reader = null;
    EntityProviderException cachedException = null;

    try {//from   w  w  w . ja v  a2  s.  c  om
        EntityInfoAggregator eia = EntityInfoAggregator.create(entitySet);
        reader = createJsonReader(content);

        return new JsonEntryConsumer(reader, eia, properties).readSingleEntry();
    } catch (UnsupportedEncodingException e) {
        cachedException = new EntityProviderException(
                EntityProviderException.EXCEPTION_OCCURRED.addContent(e.getClass().getSimpleName()), e);
        throw cachedException;
    } finally {// NOPMD (suppress DoNotThrowExceptionInFinally)
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                if (cachedException != null) {
                    throw cachedException;
                } else {
                    throw new EntityProviderException(
                            EntityProviderException.EXCEPTION_OCCURRED.addContent(e.getClass().getSimpleName()),
                            e);
                }
            }
        }
    }
}

From source file:com.sap.core.odata.core.ep.consumer.JsonEntityConsumer.java

License:Apache License

public ODataFeed readFeed(final EdmEntitySet entitySet, final InputStream content,
        final EntityProviderReadProperties readProperties) throws EntityProviderException {
    JsonReader reader = null;
    EntityProviderException cachedException = null;

    try {/*w w  w  . j  av  a 2 s  . com*/
        EntityInfoAggregator eia = EntityInfoAggregator.create(entitySet);
        reader = createJsonReader(content);

        JsonFeedConsumer jfc = new JsonFeedConsumer(reader, eia, readProperties);
        ODataFeed result = jfc.readFeedStandalone();

        return result;
    } catch (UnsupportedEncodingException e) {
        cachedException = new EntityProviderException(
                EntityProviderException.EXCEPTION_OCCURRED.addContent(e.getClass().getSimpleName()), e);
        throw cachedException;
    } finally {// NOPMD (suppress DoNotThrowExceptionInFinally)
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                if (cachedException != null) {
                    throw cachedException;
                } else {
                    throw new EntityProviderException(
                            EntityProviderException.EXCEPTION_OCCURRED.addContent(e.getClass().getSimpleName()),
                            e);
                }
            }
        }
    }
}

From source file:com.sap.core.odata.core.ep.consumer.JsonEntityConsumer.java

License:Apache License

public Map<String, Object> readProperty(final EdmProperty property, final InputStream content,
        final EntityProviderReadProperties readProperties) throws EntityProviderException {
    JsonReader reader = null;
    EntityProviderException cachedException = null;

    try {/*from   www.  j  av a  2  s .com*/
        reader = createJsonReader(content);
        return new JsonPropertyConsumer().readPropertyStandalone(reader, property, readProperties);
    } catch (final UnsupportedEncodingException e) {
        cachedException = new EntityProviderException(
                EntityProviderException.EXCEPTION_OCCURRED.addContent(e.getClass().getSimpleName()), e);
        throw cachedException;
    } finally {// NOPMD (suppress DoNotThrowExceptionInFinally)
        if (reader != null) {
            try {
                reader.close();
            } catch (final IOException e) {
                if (cachedException != null) {
                    throw cachedException;
                } else {
                    throw new EntityProviderException(
                            EntityProviderException.EXCEPTION_OCCURRED.addContent(e.getClass().getSimpleName()),
                            e);
                }
            }
        }
    }
}

From source file:com.sap.core.odata.core.ep.consumer.JsonEntityConsumer.java

License:Apache License

public String readLink(final EdmEntitySet entitySet, final Object content) throws EntityProviderException {
    JsonReader reader = null;
    EntityProviderException cachedException = null;

    try {//w  ww. ja v a  2  s  .  c om
        reader = createJsonReader(content);
        return new JsonLinkConsumer().readLink(reader, entitySet);
    } catch (final UnsupportedEncodingException e) {
        cachedException = new EntityProviderException(
                EntityProviderException.EXCEPTION_OCCURRED.addContent(e.getClass().getSimpleName()), e);
        throw cachedException;
    } finally {// NOPMD (suppress DoNotThrowExceptionInFinally)
        if (reader != null) {
            try {
                reader.close();
            } catch (final IOException e) {
                if (cachedException != null) {
                    throw cachedException;
                } else {
                    throw new EntityProviderException(
                            EntityProviderException.EXCEPTION_OCCURRED.addContent(e.getClass().getSimpleName()),
                            e);
                }
            }
        }
    }
}