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:io.dockstore.common.FileProvisionUtil.java

License:Apache License

/**
 * Downloads all plugins//from w  w w.jav  a  2 s  .  c  o  m
 *
 * @param configFile The parsed config file
 */
public static void downloadPlugins(INIConfiguration configFile) {
    String filePluginLocation = FileProvisionUtil.getFilePluginLocation(configFile);
    String pluginJSONPath = FileProvisionUtil.getPluginJSONLocation(configFile);
    File f = new File(pluginJSONPath);
    if (!f.exists()) {
        if (f.isDirectory()) {
            LOG.error(PLUGINS_JSON_FILENAME + " is actually a directory.");
            System.exit(1);
        } else {
            createPluginJSONFile(pluginJSONPath);
        }
    }
    Gson gson = new Gson();
    try {
        JsonReader reader = new JsonReader(
                new InputStreamReader(new FileInputStream(pluginJSONPath), Charset.forName("UTF-8")));
        PluginJSON[] arrayJSON = gson.fromJson(reader, PluginJSON[].class);
        List<PluginJSON> listJSON = Arrays.asList(arrayJSON);
        listJSON.forEach(t -> downloadPlugin(filePluginLocation, t));
    } catch (FileNotFoundException e) {
        LOG.error(PLUGINS_JSON_FILENAME + " not found");
    }
}

From source file:io.ecarf.core.term.dictionary.TermDictionary.java

License:Apache License

/**
 * De-serialize a TermDictionary instance from a json file.
 * @param jsonFile - a file path/*  w w w  . j ava 2  s  .  co m*/
 * @return a TermDictionary instance
 * @throws FileNotFoundException if the file is not found
 * @throws IOException if the json conversion fails
 */
public static TermDictionary fromJsonFile(String jsonFile, boolean compressed)
        throws FileNotFoundException, IOException {
    Stopwatch stopwatch = Stopwatch.createStarted();

    String filename = jsonFile;
    if (compressed) {
        filename = Utils.unCompressFile(jsonFile);
    }

    try (FileReader reader = new FileReader(filename)) {
        TermDictionary dictionary = GSON.fromJson(new JsonReader(reader), TermDictionary.class);

        log.debug("TIMER# deserialized dictionary from JSON file: " + jsonFile + ", in: " + stopwatch);

        return dictionary;
    }
}

From source file:io.ecarf.core.triple.TriplesFileStats.java

License:Apache License

/**
 * De-serialize a TriplesFileStats instance from a json file.
 * @param jsonFile - a file path//from w  w  w  .ja  v a2 s . co  m
 * @return a TriplesFileStats instance
 * @throws FileNotFoundException if the file is not found
 * @throws IOException if the json conversion fails
 */
public static List<TriplesFileStats> fromJsonFile(String jsonFile, boolean compressed)
        throws FileNotFoundException, IOException {

    String filename = jsonFile;
    if (compressed) {
        filename = Utils.unCompressFile(jsonFile);
    }

    Type token = new TypeToken<List<TriplesFileStats>>() {
    }.getType();

    try (FileReader reader = new FileReader(filename)) {
        List<TriplesFileStats> stats = ObjectUtils.GSON.fromJson(new JsonReader(reader), token);

        return stats;
    }
}

From source file:io.ecarf.core.utils.Utils.java

License:Apache License

public static void main(String[] args) throws JsonIOException, JsonSyntaxException, FileNotFoundException {
    Set<String> schemaTerms = Utils.GSON.fromJson(
            new JsonReader(new FileReader(Utils.TEMP_FOLDER + "test.json")), new TypeToken<Set<String>>() {
            }.getType());/*from ww w.ja  va 2s  . c  om*/
    System.out.println(schemaTerms);
}

From source file:io.fabric8.etcd.reader.gson.GsonResponseReader.java

License:Apache License

@Override
public Response read(InputStream is) throws IOException {
    Response response = null;/*from   ww w  . j  a  v  a  2s .c  o m*/
    try (InputStreamReader isr = new InputStreamReader(is); JsonReader reader = new JsonReader(isr)) {
        response = GSON.fromJson(reader, ImmutableResponse.class);
    }
    return response;
}

From source file:io.github.weebobot.weebobot.external.TwitchUtilities.java

License:Open Source License

public static String getTitle(String channelNoHash) {
    try {/*from w  w w. j  a va2 s. c  o  m*/
        return new JsonParser()
                .parse(new JsonReader(
                        new InputStreamReader(new URL(BASE_URL + "channels/" + channelNoHash).openStream())))
                .getAsJsonObject().getAsJsonPrimitive("status").getAsString();
    } catch (IOException e) {
        return "There was an issue getting the title for this channel. Please try again later!";
    }
}

From source file:io.github.weebobot.weebobot.external.TwitchUtilities.java

License:Open Source License

public static String getGame(String channelNoHash) {
    try {//from  w w w .  j  av a  2 s  . c om
        return new JsonParser()
                .parse(new JsonReader(
                        new InputStreamReader(new URL(BASE_URL + "channels/" + channelNoHash).openStream())))
                .getAsJsonObject().getAsJsonPrimitive("game").getAsString();
    } catch (IOException e) {
        return "There was an issue getting the game for this channel. Please try again later!";
    }
}

From source file:io.github.weebobot.weebobot.external.TwitchUtilities.java

License:Open Source License

/**
 * Checks if the sender is a follower of channel
 * //from  ww  w . j a  v  a  2s  .c o m
 * @param sender - the user who sent the message
 * @param channelNoHash - the channel the message was sent in
 * @return - true if sender is following channel
 */
public static boolean isFollower(String channelNoHash, String sender) {
    try {
        String nextUrl = "https://api.twitch.tv/kraken/channels/" + channelNoHash + "/follows";
        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 = followerCount(channelNoHash);
            int pages = count / 25;
            if (count % 25 != 0) {
                pages++;
            }
            for (int i = 0; i < pages; i++) {
                for (int j = 0; j < 25; j++) {
                    try {
                        if (sender.equalsIgnoreCase(obj.getAsJsonArray("follows").get(j).getAsJsonObject()
                                .get("user").getAsJsonObject().getAsJsonPrimitive("name").getAsString())) {
                            return true;
                        }
                    } catch (IndexOutOfBoundsException e) {
                        return false;
                    }
                }
                nextUrl = obj.getAsJsonObject("_links").getAsJsonPrimitive("next").getAsString();
                obj = new JsonParser()
                        .parse(new JsonReader(new InputStreamReader(new URL(nextUrl).openStream())))
                        .getAsJsonObject();
            }
            return false;
        }
    } catch (FileNotFoundException e) {
        WLogger.log(channelNoHash + "Does not have any followers.");
    } catch (JsonIOException | JsonSyntaxException | IOException e) {
        logger.log(Level.SEVERE, "An error occurred checking if " + sender + " is following " + channelNoHash,
                e);
        WLogger.logError(e);
    }
    return false;
}

From source file:io.github.weebobot.weebobot.external.TwitchUtilities.java

License:Open Source License

/**
 * Checks if the sender is subscribed to channel
 * /*from w  ww  . jav  a  2  s  .c  om*/
 * @param sender - user who sent the message
 * @param channelNoHash - channel the message was sent in
 * @return - true if sender is subscribed to channel
 */
public static boolean isSubscriber(String sender, String channelNoHash) {
    try {
        String userOAuth = Database.getUserOAuth(channelNoHash);
        String nextUrl = "https://api.twitch.tv/kraken/channels/" + channelNoHash
                + "/subscriptions/?oauth_token=" + userOAuth;
        if (userOAuth == null) {
            return false;
        }
        JsonObject obj = null;
        try {
            obj = new JsonParser().parse(new JsonReader(new InputStreamReader(new URL(nextUrl).openStream())))
                    .getAsJsonObject();
        } catch (IOException e) {
            if (e.getLocalizedMessage().equalsIgnoreCase(
                    "Server returned HTTP response code: 422 for URL: https://api.twitch.tv/kraken/channels/"
                            + channelNoHash + "/subscriptions/?oauth_token=" + userOAuth)) {
                return false;
            }
            logger.log(Level.SEVERE, String.format("There was an issue checking is %s is subscribed to %s",
                    sender, channelNoHash), e);
            WLogger.logError(e);
        }
        if (obj.get("error") != null) { // ie it finds it
            return false;
        } else { // it does not find it
            int count = subscriberCount(channelNoHash, 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 = obj.getAsJsonObject("_links").getAsJsonPrimitive().getAsString() + "?oauth_token="
                        + userOAuth;
                obj = new JsonParser()
                        .parse(new JsonReader(new InputStreamReader(new URL(nextUrl).openStream())))
                        .getAsJsonObject();
            }
            return false;
        }
    } catch (FileNotFoundException e) {
        WLogger.log(channelNoHash + "Does not have any subscribers or is not partnered.");
    } catch (JsonIOException | JsonSyntaxException | IOException e) {
        logger.log(Level.SEVERE, "An error occurred checking if " + sender + " is following " + channelNoHash,
                e);
        WLogger.logError(e);
    }
    return false;
}

From source file:io.github.weebobot.weebobot.external.TwitchUtilities.java

License:Open Source License

/**
 * Gets the amount of people following the specified channel
 * /*from  w w  w . ja va 2 s .com*/
 * @param channelNoHash
 * @return number of followers for channel, 0 if an error occurs
 */
public static int followerCount(String channelNoHash) {
    try (InputStream stream = new URL(BASE_URL + "channels/" + channelNoHash + "/follows").openStream()) {
        return new JsonParser().parse(new JsonReader(new InputStreamReader(stream))).getAsJsonObject()
                .getAsJsonPrimitive("_total").getAsInt();
    } catch (JsonIOException | JsonSyntaxException | IOException | NullPointerException e) {
        logger.log(Level.SEVERE, "An error occurred getting the follower count for " + channelNoHash, e);
        WLogger.logError(e);
    }
    return 0;
}