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:me.gnat008.perworldinventory.data.FileWriter.java

License:Open Source License

private void getFromDefaults(Group group, Player player) {
    File file = new File(FILE_PATH, "defaults" + File.separator + group.getName() + ".json");

    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) {
        file = new File(FILE_PATH, "defaults" + File.separator + "__default.json");

        try (JsonReader reader = new JsonReader(new FileReader(file))) {
            JsonParser parser = new JsonParser();
            JsonObject data = parser.parse(reader).getAsJsonObject();
            playerSerializer.deserialize(data, player);
        } catch (FileNotFoundException ex2) {
            player.sendMessage(ChatColor.RED + " " + ChatColor.GRAY
                    + "Something went horribly wrong when loading your inventory! "
                    + "Please notify a server administrator!");
            PwiLogger.severe("Unable to find inventory data for player '" + player.getName() + "' for group '"
                    + group.getName() + "':", ex2);
        } catch (IOException exIO) {
            PwiLogger.severe("Unable to read data for '" + player.getName() + "' for group '" + group.getName()
                    + "' for reason:", exIO);
        }//from w  ww .  j  a  v a2s  . c o  m
    } catch (IOException exIO) {
        PwiLogger.severe("Unable to read data for '" + player.getName() + "' for group '" + group.getName()
                + "' for reason:", exIO);
    }
}

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

License:Open Source License

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

    ConsoleLogger//from ww  w  .j  a  v  a 2s.  c o m
            .debug("Getting data for player '" + player.getName() + "' from file '" + file.getPath() + "'");

    bukkitService.runTaskAsync(() -> {
        try (JsonReader reader = new JsonReader(new FileReader(file))) {
            JsonParser parser = new JsonParser();
            JsonObject data = parser.parse(reader).getAsJsonObject();

            bukkitService.runTask(() -> playerSerializer.deserialize(data, player, cause));
        } catch (FileNotFoundException ex) {
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdir();
            }

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

            getFromDefaults(group, player, cause);
        } catch (IOException exIO) {
            ConsoleLogger.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.FlatFile.java

License:Open Source License

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

    Location location;//from  w  w w . j  a  v  a  2  s  . c  o m
    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
        ConsoleLogger.warning("Unable to get logout location data for '" + player.getName() + "':", ioEx);
        location = null;
    }

    return location;
}

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

License:Open Source License

private void getFromDefaults(Group group, Player player, DeserializeCause cause) {
    File file = new File(FILE_PATH + File.separator + "defaults", group.getName() + ".json");

    try (JsonReader reader = new JsonReader(new FileReader(file))) {
        JsonParser parser = new JsonParser();
        JsonObject data = parser.parse(reader).getAsJsonObject();

        bukkitService.runTask(() -> playerSerializer.deserialize(data, player, cause));
    } catch (FileNotFoundException ex) {
        file = new File(FILE_PATH + File.separator + "defaults", "__default.json");

        try (JsonReader reader = new JsonReader(new FileReader(file))) {
            JsonParser parser = new JsonParser();
            JsonObject data = parser.parse(reader).getAsJsonObject();
            bukkitService.runTask(() -> playerSerializer.deserialize(data, player, cause));
        } catch (FileNotFoundException ex2) {
            player.sendMessage(ChatColor.RED + " " + ChatColor.GRAY
                    + "Something went horribly wrong when loading your inventory! "
                    + "Please notify a server administrator!");
            ConsoleLogger.severe("Unable to find inventory data for player '" + player.getName()
                    + "' for group '" + group.getName() + "':", ex2);
        } catch (IOException exIO) {
            ConsoleLogger.severe("Unable to read data for '" + player.getName() + "' for group '"
                    + group.getName() + "' for reason:", exIO);
        }/*from   w w  w .  ja  v  a 2s. c  o m*/
    } catch (IOException exIO) {
        ConsoleLogger.severe("Unable to read data for '" + player.getName() + "' for group '" + group.getName()
                + "' for reason:", exIO);
    }
}

From source file:me.j360.trace.core.internal.JsonCodec.java

License:Apache License

private static JsonReader jsonReader(byte[] bytes) {
    return new JsonReader(new InputStreamReader(new ByteArrayInputStream(bytes)));
}

From source file:me.jewsofhazard.pcmrbot.twitch.TwitchUtilities.java

License:Open Source License

/**
 * Checks if the sender is a follower of channel
 * /*from w w w. j av a  2 s. c o  m*/
 * @param sender
 * @param channel
 * @return - true if sender is following channel
 */
public static boolean isFollower(String channel, String sender) {
    try {
        String nextUrl = "https://api.twitch.tv/kraken/users/" + sender + "/follows/channels/" + channel;
        System.out.println(nextUrl);
        JsonObject following = new JsonParser()
                .parse(new JsonReader(new InputStreamReader(new URL(nextUrl).openStream()))).getAsJsonObject();
        if (following.get("error") != null) {
            return false;
        } else {
            return true;
        }
    } catch (JsonIOException | JsonSyntaxException | IOException e) {
        logger.log(Level.SEVERE,
                "An error occurred checking if " + sender + " is following " + channel.substring(1), e);
    }
    return false;
}

From source file:me.jewsofhazard.pcmrbot.twitch.TwitchUtilities.java

License:Open Source License

/**
 * Checks if the sender is subscribed to channel
 * /*from  w  w  w .j a  va  2s  .  c  o m*/
 * @param sender
 * @param channel
 * @return - true if sender is subscribed to channel
 */
public static boolean isSubscriber(String sender, String channel) {
    try {
        String userOAuth = Database.getUserOAuth(channel.substring(1));
        String nextUrl = "https://api.twitch.tv/kraken/channels/" + channel.substring(0)
                + "/subscriptions/?oauth_token=" + userOAuth;
        JsonObject obj = new JsonParser()
                .parse(new JsonReader(new InputStreamReader(new URL(nextUrl).openStream()))).getAsJsonObject();
        if (obj.get("error") != null) { // ie it finds it
            return false;
        } else { // it does not find it
            int count = subscriberCount(channel, userOAuth);
            int pages = count / 25;
            if (count % 25 != 0) {
                pages++;
            }
            for (int i = 0; i < pages; i++) {
                for (int j = 0; j < 25; j++) {
                    if (sender.equalsIgnoreCase(obj.getAsJsonArray("subscriptions").get(j).getAsJsonObject()
                            .getAsJsonPrimitive("display_name").getAsString())) {
                        return true;
                    }
                }
                nextUrl = URLEncoder
                        .encode(obj.getAsJsonArray("_links").get(1).getAsJsonPrimitive().getAsString()
                                + "?oauth_token=" + userOAuth, CHARSET);
                obj = new JsonParser()
                        .parse(new JsonReader(new InputStreamReader(new URL(nextUrl).openStream())))
                        .getAsJsonObject();
            }
            return false;
        }
    } catch (JsonIOException | JsonSyntaxException | IOException e) {
        logger.log(Level.SEVERE,
                "An error occurred checking if " + sender + " is following " + channel.substring(1), e);
    }
    return false;
}

From source file:me.jewsofhazard.pcmrbot.twitch.TwitchUtilities.java

License:Open Source License

/**
 * Gets the amount of people following the specified channel
 * /*from   www  .  j  a  va 2  s .c  o m*/
 * @param channel
 * @return number of followers for channel, 0 if an error occurs
 */
public static int followerCount(String channel) {
    try {
        return new JsonParser()
                .parse(new JsonReader(new InputStreamReader(
                        new URL(BASE_URL + "channels/" + channel.substring(1)).openStream())))
                .getAsJsonObject().getAsJsonPrimitive("followers").getAsInt();
    } catch (JsonIOException | JsonSyntaxException | IOException e) {
        logger.log(Level.SEVERE, "An error occurred getting the follower count for " + channel.substring(1), e);
    }
    return 0;
}

From source file:me.jewsofhazard.pcmrbot.twitch.TwitchUtilities.java

License:Open Source License

/**
 * Gets the amount of people subscribed to the specified channel
 * //from  w  w w. j a  v a  2 s .  c om
 * @param channel
 * @param oAuth
 * @return number of subscribers for the channel
 */
public static int subscriberCount(String channel, String oAuth) {
    try {
        return new JsonParser().parse(new JsonReader(new InputStreamReader(
                new URL(BASE_URL + "channels/" + channel.substring(1) + "/subscriptions/?oauth_token=" + oAuth)
                        .openStream())))
                .getAsJsonObject().getAsJsonPrimitive("_total").getAsInt();
    } catch (JsonIOException | JsonSyntaxException | IOException e) {
        logger.log(Level.SEVERE, "An error occurred getting the follower count for " + channel.substring(1), e);
    }
    return 0;
}

From source file:me.jewsofhazard.pcmrbot.twitch.TwitchUtilities.java

License:Open Source License

/**
 * @param channelNoHash/*from ww w  . j  a  v a 2 s.  c o  m*/
 *            - channel to run the commercial in without the leading #
 * @return true if the channel is live, false otherwise
 */
public static boolean isLive(String channelNoHash) {
    try {
        new JsonParser()
                .parse(new JsonReader(
                        new InputStreamReader(new URL(BASE_URL + "streams/" + channelNoHash).openStream())))
                .getAsJsonObject().getAsJsonObject("stream").getAsJsonNull();
        return false;
    } catch (IllegalStateException | ClassCastException e) {
        return true;
    } catch (JsonSyntaxException | IOException e) {
        logger.log(Level.SEVERE, "An error occurred checking if the streamer is live!", e);
    }
    return false;
}