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

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

Introduction

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

Prototype

public JsonReader(Reader in) 

Source Link

Document

Creates a new instance that reads a JSON-encoded stream from in .

Usage

From source file:net.saga.aeroconf.app.data.provider.AbstractAeroConfProvider.java

License:Apache License

private void loadRoomsFromFile() {
    InputStream rooms = getContext().getResources().openRawResource(R.raw.rooms);
    JsonReader reader = new JsonReader(new InputStreamReader(rooms));
    JsonElement root = new JsonParser().parse(reader);
    JsonArray array = root.getAsJsonObject().get("roomList").getAsJsonObject().get("room").getAsJsonArray();

    JsonElement element;//from   w  w w  .  ja  v  a  2  s .c o  m
    Gson gson = GsonUtils.GSON;

    for (int i = 0; i < array.size(); i++) {
        element = array.get(i);
        Room room = gson.fromJson(element, Room.class);
        roomStore.save(room);
    }

}

From source file:net.sf.javaocr.demos.android.recognizer.Recognizer.java

License:Apache License

/**
 * read cluster data  from storage//from   ww  w .ja  va2 s. c o m
 */
private void readClusterData() {
    Log.d(LOG_TAG, "start reading cluster data");
    try {
        InputStream inputStream = getResources().openRawResource(R.raw.freespaces);
        InputStreamReader reader = new InputStreamReader(inputStream);
        JsonReader jreader = new JsonReader(reader);
        jreader.setLenient(true);

        freeSpacesMatcher.setContainers(JSONUnmarshaller.unmarshallArray(jreader, FreeSpacesContainer.class));

        reader.close();

        inputStream = getResources().openRawResource(R.raw.moments);
        reader = new InputStreamReader(inputStream);
        jreader = new JsonReader(reader);
        jreader.setLenient(true);

        metricMatcher
                .setContainers(JSONUnmarshaller.unmarshallArray(jreader, MahalanobisClusterContainer.class));

    } catch (Exception e) {
        e.printStackTrace();
    }
    loadReady = true;
}

From source file:net.visualillusionsent.newu.StationTracker.java

License:Open Source License

private void loadStations() {
    try {//from  w  w  w . ja v  a2  s.  c om
        File stationsJSON = new File(NewU.cfgDir, "stations.json");
        if (!stationsJSON.exists()) {
            stationsJSON.createNewFile();
            return;
        }

        JsonReader reader = new JsonReader(new FileReader(stationsJSON));
        reader.beginObject(); // Begin main object
        while (reader.hasNext()) {
            String name = reader.nextName();
            if (name.equals("Station")) {
                NewUStation temp = null;
                reader.beginObject(); // Begin Station
                String foundName = null;
                while (reader.hasNext()) {
                    name = reader.nextName();
                    if (name.equals("Name")) {
                        foundName = reader.nextString();
                        continue;
                    } else if (name.equals("Location")) {
                        reader.beginObject(); // Begin Location
                        temp = new NewUStation(foundName, reader); // Pass reader into NewUStation object for parsing
                        reader.endObject(); // End Location
                    } else if (name.equals("Discoverers")) {
                        reader.beginArray(); // Begin Discoverers
                        while (reader.hasNext()) {
                            if (temp != null) {
                                temp.addDiscoverer(reader.nextString());
                            }
                        }
                        reader.endArray(); // End Discoverers
                    } else {
                        reader.skipValue(); // UNKNOWN THING
                    }
                }
                if (temp != null) {
                    stations.put(temp.getName(), temp);
                }
                reader.endObject(); //End Station
            }
        }

        reader.endObject(); // End main object
        reader.close();
    } catch (IOException e) {
        logger.log(Level.SEVERE, "Failed to load stations...");
    }
}

From source file:ninja.leaping.configurate.gson.GsonConfigurationLoader.java

License:Apache License

@Override
protected void loadInternal(ConfigurationNode node, BufferedReader reader) throws IOException {
    reader.mark(1);/*from w  w w . ja va  2 s. c  o  m*/
    if (reader.read() == -1) {
        return;
    }
    reader.reset();
    try (JsonReader parser = new JsonReader(reader)) {
        parser.setLenient(lenient);
        parseValue(parser, node);
    }
}

From source file:nl.kpmg.lcm.server.cron.job.processor.DataFetchExecutor.java

License:Apache License

private boolean writeData(InputStream in, MetaDataWrapper metaDataWrapper, String key,
        TransferSettings transferSettings) {
    try {/*from  ww w.  jav a 2  s .co  m*/
        Backend backend = storageService.getBackend(metaDataWrapper);
        backend.setProgressIndicationFactory(
                new ProgressIndicationFactory(taskDescriptionService, taskId, 10000));
        Data data;
        String dataType = metaDataWrapper.getData().getDataType();
        if (StreamingData.getStreamingDataTypes().contains(dataType)) {
            data = new StreamingData(in);
        } else {
            JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
            ContentIterator iterator = new JsonReaderContentIterator(reader);
            data = new IterativeData(iterator);
        }

        backend.store(data, key, transferSettings);
    } catch (Exception ex) {
        LOGGER.error(ex.getMessage());

        return false;
    }
    return true;
}

From source file:nz.ac.otago.psyanlab.common.model.util.ModelUtils.java

License:Open Source License

public static Experiment readDefinition(String paleDefinition) {
    return ModelUtils.getDataReaderWriter().fromJson(new JsonReader(new StringReader(paleDefinition)),
            Experiment.class);
}

From source file:nz.ac.otago.psyanlab.common.model.util.ModelUtils.java

License:Open Source License

public static Experiment readFile(File paleFile) throws FileNotFoundException {
    return ModelUtils.getDataReaderWriter().fromJson(new JsonReader(new FileReader(paleFile)),
            Experiment.class);
}

From source file:nz.ac.otago.psyanlab.common.util.FileUtils.java

License:Open Source License

public static Experiment loadExperimentDefinition(File experimentJsonFile) throws FileNotFoundException {
    try {//from  ww  w  .  j  av a2  s . co  m
        return ModelUtils.getDataReaderWriter().fromJson(
                new JsonReader(new InputStreamReader(new FileInputStream(experimentJsonFile), "UTF-16")),
                Experiment.class);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException("Unexpected internal error.", e);
    }
}

From source file:openwhisk.java.local.CLI.java

License:Apache License

private static JsonObject readParameters(CLI cli) {
    if (cli.parameters == null || cli.parameters.isEmpty()) {
        ExecutorService ex = Executors.newSingleThreadExecutor();
        Future<JsonObject> result = ex.submit(() -> {
            try (JsonReader reader = new JsonReader(new InputStreamReader(System.in))) {
                reader.setLenient(true);
                JsonParser parser = new JsonParser();
                JsonElement element = parser.parse(reader);
                return element.getAsJsonObject();
            }//from  w w w . j a v  a  2 s . c  o m
        });
        try {
            return result.get(1, TimeUnit.SECONDS);
        } catch (InterruptedException | ExecutionException | TimeoutException e) {
            result.cancel(true);
            return new JsonObject();
        }

    } else {
        JsonObject o = new JsonObject();
        cli.parameters.forEach((name, value) -> {
            o.addProperty(name, value);
        });
        return o;
    }
}

From source file:org.apache.accumulo.core.client.impl.BulkSerialize.java

License:Apache License

/**
 * Read Json array of Bulk.Mapping into LoadMappingIterator
 *//* w w w  . ja va  2s .c om*/
public static LoadMappingIterator readLoadMapping(String bulkDir, Table.ID tableId, Input input)
        throws IOException {
    final Path lmFile = new Path(bulkDir, Constants.BULK_LOAD_MAPPING);
    JsonReader reader = new JsonReader(new BufferedReader(new InputStreamReader(input.open(lmFile), UTF_8)));
    reader.beginArray();
    return new LoadMappingIterator(tableId, reader);
}