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:logic.DiskIOManager.java

License:Open Source License

public static MangaLibrary loadLibrary(String configDirectory) {

    File configLibrary = new File(configDirectory + File.separator + "library.json");
    File configAvailable = new File(configDirectory + File.separator + "available.json");

    if (!Files.exists(Paths.get(configLibrary.toURI())) || !Files.exists(Paths.get(configAvailable.toURI()))) {
        MangaLibrary library = new MangaLibrary(configDirectory);
        library.save();/*  ww w .j  av a 2 s . c o  m*/
        library.saveAvailable();
        return library;
    }

    try {
        JsonReader reader;
        Gson gson = new Gson();

        reader = new JsonReader(new FileReader(configLibrary));
        MangaLibrary library = gson.fromJson(reader, MangaLibrary.class);

        reader = new JsonReader(new FileReader(configAvailable));
        Map<String, Map<String, String>> availableParsed = gson.fromJson(reader, LinkedHashMap.class);
        Map<MangaSource, Map<String, String>> available = new LinkedHashMap<>();
        for (String source : availableParsed.keySet())
            available.put(MangaSource.valueOf(source), availableParsed.get(source));

        library.setConfigDirectory(configDirectory);
        library.setAvailable(available);

        assert library != null;
        return library;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        M.print(e.getMessage());
        return null;
    }
}

From source file:logic.DiskIOManager.java

License:Open Source License

public static Options loadOptions(String configDirectory) {

    File optionsFile = new File(configDirectory + File.separator + "options.json");

    if (!Files.exists(Paths.get(optionsFile.toURI()))) {
        //M.print("new Options");
        Options options = new Options();
        options.init();/*from w w w .j a  va  2  s  . co m*/
        saveOptions(options, configDirectory);
        return options;
    }
    try {
        //M.print("loading Options");
        JsonReader reader = new JsonReader(new FileReader(optionsFile));
        Gson gson = new Gson();

        Options options = gson.fromJson(reader, Options.class);
        assert options != null;
        return options;
    } catch (FileNotFoundException e) {
        M.exception(e);
        return null;
    }
}

From source file:logic.LibraryManager.java

License:Open Source License

public static MangaLibrary loadLibrary(String configDirectory) {

    File configDir = new File(configDirectory);
    if (!Files.exists(Paths.get(configDir.toURI())))
        configDir.mkdirs();/*from  ww  w.  ja v a2  s . co m*/

    File configLibrary = new File(configDirectory + File.separator + "library.json");
    File configAvailable = new File(configDirectory + File.separator + "available.json");

    if (!Files.exists(Paths.get(configLibrary.toURI())) || !Files.exists(Paths.get(configAvailable.toURI()))) {
        MangaLibrary library = new MangaLibrary(configDirectory);
        library.save();
        library.saveAvailable();
        return library;
    }

    try {
        JsonReader reader;
        Gson gson = new Gson();

        reader = new JsonReader(new FileReader(configLibrary));
        MangaLibrary library = gson.fromJson(reader, MangaLibrary.class);

        reader = new JsonReader(new FileReader(configAvailable));
        Map<String, Map<String, String>> availableParsed = gson.fromJson(reader, LinkedHashMap.class);
        Map<MangaSource, Map<String, String>> available = new LinkedHashMap<>();
        for (String source : availableParsed.keySet())
            available.put(MangaSource.valueOf(source), availableParsed.get(source));

        library.setConfigDirectory(configDirectory);
        library.setAvailable(available);

        assert library != null;
        return library;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        M.print(e.getMessage());
        return null;
    }
}

From source file:logic.MyMQ.java

public void recibir(int ciclos, String DB_server) {

    String recieve;//from  w  ww  .j a v  a  2s. c  o m

    long currentTimeMillis = System.currentTimeMillis();

    for (int i = 0; i < ciclos; i++) {
        try {
            recieve = communication.recieve(host, port, "1");

            Gson gson = new Gson();
            DefaultMensaje def_msg;
            String ced = "";

            //recieve = recieve.replace('\0','0');
            System.out.println(" string recibido :" + recieve);
            JsonReader reader = new JsonReader(new StringReader(recieve));
            reader.setLenient(true);
            def_msg = gson.fromJson(reader, DefaultMensaje.class);
            ced = def_msg.Cedula;
            insert(def_msg, DB_server);

        } catch (IOException ex) {
            Logger.getLogger(MyMQ.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
    long currentTimeMillisFin = System.currentTimeMillis();

    System.out.println(" Tiempo Final: " + (currentTimeMillisFin - currentTimeMillis) + " milisegundos");

}

From source file:lolxmpp.api.RiotAPI.java

License:Open Source License

public JsonObject makeRequest(String apiPath) throws APIException {
    String apiHost = region.apiHost();
    String tryUrl = "https://" + apiHost + apiPath;
    try {/* ww  w  .  j ava  2 s .c  o  m*/
        URL url = new URL(tryUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestProperty("X-Riot-Token", key);

        if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
            throw new APIException("[RAPI] Request Failed. HTTP Error Code: " + connection.getResponseCode()
                    + ", URL: " + tryUrl + ", KEY:" + key);
        }

        JsonElement element = new JsonParser()
                .parse(new JsonReader(new InputStreamReader(connection.getInputStream())));
        if (!element.isJsonNull()) {
            return element.getAsJsonObject();
        }
    } catch (MalformedURLException e) {
        System.err.println("[RAPI] Something's wrong!");
        System.err.println("[RAPI] Malformed URL: " + tryUrl);
        e.printStackTrace();
    } catch (IOException e) {
        System.err.println("[RAPI] Something's wrong!");
        System.err.println("[RAPI] IOException");
        e.printStackTrace();
    }

    return new JsonObject();
}

From source file:me.bramhaag.discordselfbot.Bot.java

License:Apache License

/**
 * Load config/*from  ww  w . j  av  a  2s. c om*/
 * @throws FileNotFoundException when file is not found
 */
public void loadConfig() throws FileNotFoundException {
    config = new Gson().fromJson(new JsonReader(new FileReader("config.json")), Config.class);
}

From source file:me.dags.creativeblock.blockpack.FileBlockPack.java

License:Open Source License

private void populate(List<BlockDefinition> definitions) throws IOException {
    ZipFile zipFile = new ZipFile(getSource());
    Enumeration<? extends ZipEntry> entries = zipFile.entries();

    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        if (entry.getName().startsWith(definitionsDir) && entry.getName().endsWith(".json")) {
            JsonReader reader = new JsonReader(new InputStreamReader(zipFile.getInputStream(entry)));
            DefinitionSerializable definition = JsonUtil.deserialize(reader, DefinitionSerializable.class);
            definition.tabId = getTabName(entry.getName());
            definitions.add(BlockDefinition.from(definition));
        }//from  w  w  w . j  a  v  a  2 s  . c  om
    }
    zipFile.close();
}

From source file:me.dags.creativeblock.util.JsonUtil.java

License:Open Source License

public static <T> Optional<T> deserialize(File inFile, Class<T> type) {
    try {/* w ww. j av a 2 s  . c o  m*/
        JsonReader reader = new JsonReader(new FileReader(inFile));
        return Optional.of(deserialize(reader, type));
    } catch (FileNotFoundException e) {
        return Optional.empty();
    } catch (IOException e) {
        return Optional.empty();
    }
}

From source file:me.gnat008.perworldinventory.data.FileWriter.java

License:Open Source License

@Override
public void getFromDatabase(Group group, GameMode gamemode, Player player) {
    File file = getFile(gamemode, group, player.getUniqueId());

    PwiLogger.debug("Getting data for player '" + player.getName() + "' from file '" + file.getPath() + "'");

    try (JsonReader reader = new JsonReader(new FileReader(file))) {
        JsonParser parser = new JsonParser();
        JsonObject data = parser.parse(reader).getAsJsonObject();
        playerSerializer.deserialize(data, player);
    } catch (FileNotFoundException ex) {
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdir();
        }//from w w w  .j  a v a  2s. c  o m

        PwiLogger.debug("File not found for player '" + player.getName() + "' for group '" + group.getName()
                + "'. Getting data from default sources");

        getFromDefaults(group, player);
    } catch (IOException exIO) {
        PwiLogger.severe("Unable to read data for '" + player.getName() + "' for group '" + group.getName()
                + "' in gamemode '" + gamemode.toString() + "' for reason:", exIO);
    }
}

From source file:me.gnat008.perworldinventory.data.FileWriter.java

License:Open Source License

@Override
public Location getLogoutData(Player player) {
    File file = new File(getUserFolder(player.getUniqueId()), "last-logout.json");

    Location location;/* w ww .  ja  va2  s.  c  om*/
    try (JsonReader reader = new JsonReader(new FileReader(file))) {
        JsonParser parser = new JsonParser();
        JsonObject data = parser.parse(reader).getAsJsonObject();
        location = LocationSerializer.deserialize(data);
    } catch (FileNotFoundException ex) {
        // Player probably logged in for the first time, not really an error
        location = null;
    } catch (IOException ioEx) {
        // Something went wrong
        PwiLogger.warning("Unable to get logout location data for '" + player.getName() + "':", ioEx);
        location = null;
    }

    return location;
}