Example usage for org.json.simple JSONArray toJSONString

List of usage examples for org.json.simple JSONArray toJSONString

Introduction

In this page you can find the example usage for org.json.simple JSONArray toJSONString.

Prototype

public static String toJSONString(List list) 

Source Link

Usage

From source file:com.simpsonwil.strongquests.util.UUIDFetcher.java

public Map<String, UUID> call() throws Exception {
    Map<String, UUID> uuidMap = new HashMap<String, UUID>();
    int requests = (int) Math.ceil(names.size() / PROFILES_PER_REQUEST);

    for (int i = 0; i < requests; i++) {
        HttpURLConnection connection = createConnection();
        String body = JSONArray.toJSONString(names.subList(i * 100, Math.min((i + 1) * 100, names.size())));
        writeBody(connection, body);//from ww w.j  a  v  a 2 s .  c  om

        JSONArray array = (JSONArray) jsonParser.parse(new InputStreamReader(connection.getInputStream()));
        for (Object profile : array) {
            JSONObject jsonProfile = (JSONObject) profile;

            String id = (String) jsonProfile.get("id");
            String name = (String) jsonProfile.get("name");
            UUID uuid = UUIDFetcher.getUUID(id);

            uuidMap.put(name, uuid);
        }

        if (rateLimiting && i != requests - 1) {
            Thread.sleep(100L);
        }
    }

    return uuidMap;
}

From source file:me.prokopyl.commandtools.migration.UUIDFetcher.java

private static void writeBody(HttpURLConnection connection, List<String> names) throws IOException {
    OutputStream stream = connection.getOutputStream();
    String body = JSONArray.toJSONString(names);
    stream.write(body.getBytes());/* w  w  w. ja  va 2s .c  o  m*/
    stream.flush();
    stream.close();
}

From source file:net.amigocraft.mglib.UUIDFetcher.java

public Map<String, UUID> call() throws Exception {
    Map<String, UUID> uuidMap = new HashMap<String, UUID>();
    int requests = (int) Math.ceil(names.size() / PROFILES_PER_REQUEST);
    for (int i = 0; i < requests; i++) {
        HttpURLConnection connection = createConnection();
        String body = JSONArray.toJSONString(names.subList(i * 100, Math.min((i + 1) * 100, names.size())));
        writeBody(connection, body);/*  w ww  .jav a 2  s.co  m*/
        JSONArray array = (JSONArray) jsonParser.parse(new InputStreamReader(connection.getInputStream()));
        for (Object profile : array) {
            JSONObject jsonProfile = (JSONObject) profile;
            String id = (String) jsonProfile.get("id");
            String name = (String) jsonProfile.get("name");
            UUID uuid = UUIDFetcher.getUUID(id);
            uuidMap.put(name, uuid);
        }
        if (rateLimiting && i != requests - 1) {
            Thread.sleep(100L);
        }
    }
    uuids.putAll(uuidMap);
    return uuidMap;
}

From source file:io.github.apfelcreme.Guilds.UUIDFetcher.java

public Map<String, UUID> call() throws Exception {
    Map<String, UUID> uuidMap = new HashMap<String, UUID>();
    int requests = (int) Math.ceil(names.size() / PROFILES_PER_REQUEST);
    for (int i = 0; i < requests; i++) {
        HttpURLConnection connection = createConnection();
        String body = JSONArray.toJSONString(names.subList(i * 100, Math.min((i + 1) * 100, names.size())));
        writeBody(connection, body);//w  w  w  . java2 s .co m
        JSONArray array = (JSONArray) jsonParser.parse(new InputStreamReader(connection.getInputStream()));
        for (Object profile : array) {
            JSONObject jsonProfile = (JSONObject) profile;
            String id = (String) jsonProfile.get("id");
            String name = (String) jsonProfile.get("name");
            UUID uuid = UUIDFetcher.getUUID(id);
            uuidMap.put(name, uuid);
        }
        if (rateLimiting && i != requests - 1) {
            Thread.sleep(10L);
        }
    }
    return uuidMap;
}

From source file:edu.anu.spice.TupleSet.java

@Override
public String toJSONString() {
    return JSONArray.toJSONString(this.tuples);
}

From source file:me.realized.tm.utilities.profile.UUIDFetcher.java

/**
 * Makes a request to mojang's servers of a sublist of at most 100 player's
 * names. Additionally can provide progress outputs
 *
 * @param output   Whether or not to print output
 * @param log      The {@link Logger} to print to
 * @param doOutput A {@link Predicate} representing when to output a number
 * @return A {@link Map} of player names to their {@link UUID}s
 * @throws IOException          If there's a problem sending or receiving the request
 * @throws ParseException       If the request response cannot be read
 * @throws InterruptedException If the thread is interrupted while sleeping
 * @version 0.1.0/* www.  j  a va 2  s.c om*/
 * @since 0.0.1
 */
public Map<String, UUID> callWithProgressOutput(boolean output, Logger log, Predicate<? super Integer> doOutput)
        throws IOException, ParseException, InterruptedException {
    Map<String, UUID> uuidMap = new HashMap<>();
    int totalNames = this.names.size();
    int completed = 0;
    int failed = 0;
    int requests = (int) Math.ceil(this.names.size() / UUIDFetcher.PROFILES_PER_REQUEST);
    for (int i = 0; i < requests; i++) {
        List<String> request = names.subList(i * 100, Math.min((i + 1) * 100, this.names.size()));
        String body = JSONArray.toJSONString(request);
        HttpURLConnection connection = UUIDFetcher.createConnection();
        UUIDFetcher.writeBody(connection, body);
        if (connection.getResponseCode() == 429 && this.rateLimiting) {
            String out = "[UUIDFetcher] Rate limit hit! Waiting 10 minutes until continuing conversion...";
            if (log != null) {
                log.warning(out);
            } else {
                Bukkit.getLogger().warning(out);
            }
            Thread.sleep(TimeUnit.MINUTES.toMillis(10));
            connection = UUIDFetcher.createConnection();
            UUIDFetcher.writeBody(connection, body);
        }

        JSONArray array = (JSONArray) this.jsonParser.parse(new InputStreamReader(connection.getInputStream()));
        completed += array.size();
        failed += request.size() - array.size();

        for (Object profile : array) {
            JSONObject jsonProfile = (JSONObject) profile;
            UUID uuid = UUIDFetcher.getUUID((String) jsonProfile.get("id"));
            uuidMap.put((String) jsonProfile.get("name"), uuid);
        }

        if (output) {
            int processed = completed + failed;
            if (doOutput.apply(processed) || processed == totalNames) {
                if (log != null) {
                    log.info(String.format("[UUIDFetcher] Progress: %d/%d, %.2f%%, Failed names: %d", processed,
                            totalNames, ((double) processed / totalNames) * 100D, failed));
                }
            }
        }
    }
    return uuidMap;
}

From source file:com.codelanx.codelanxlib.util.auth.UUIDFetcher.java

/**
 * Makes a request to mojang's servers of a sublist of at most 100 player's
 * names. Additionally can provide progress outputs
 * //from   w  w  w  .j a  v a 2  s  .  c o  m
 * @since 0.0.1
 * @version 0.1.0
 * 
 * @param output Whether or not to print output
 * @param log The {@link Logger} to print to
 * @param doOutput A {@link Predicate} representing when to output a number
 * @return A {@link Map} of player names to their {@link UUID}s
 * @throws IOException If there's a problem sending or receiving the request
 * @throws ParseException If the request response cannot be read
 * @throws InterruptedException If the thread is interrupted while sleeping
 */
public Map<String, UUID> callWithProgessOutput(boolean output, Logger log, Predicate<? super Integer> doOutput)
        throws IOException, ParseException, InterruptedException {
    //Method start
    Map<String, UUID> uuidMap = new HashMap<>();
    int totalNames = this.names.size();
    int completed = 0;
    int failed = 0;
    int requests = (int) Math.ceil(this.names.size() / UUIDFetcher.PROFILES_PER_REQUEST);
    for (int i = 0; i < requests; i++) {
        List<String> request = names.subList(i * 100, Math.min((i + 1) * 100, this.names.size()));
        String body = JSONArray.toJSONString(request);
        HttpURLConnection connection = UUIDFetcher.createConnection();
        UUIDFetcher.writeBody(connection, body);
        if (connection.getResponseCode() == 429 && this.rateLimiting) {
            log.warning("[UUIDFetcher] Rate limit hit! Waiting 10 minutes until continuing conversion...");
            Thread.sleep(TimeUnit.MINUTES.toMillis(10));
            connection = UUIDFetcher.createConnection();
            UUIDFetcher.writeBody(connection, body);
        }
        JSONArray array = (JSONArray) this.jsonParser.parse(new InputStreamReader(connection.getInputStream()));
        completed += array.size();
        failed += request.size() - array.size();
        for (Object profile : array) {
            JSONObject jsonProfile = (JSONObject) profile;
            UUID uuid = UUIDFetcher.getUUID((String) jsonProfile.get("id"));
            uuidMap.put((String) jsonProfile.get("name"), uuid);
        }
        if (output) {
            int processed = completed + failed;
            if (doOutput.test(processed) || processed == totalNames) {
                log.info(String.format("[UUIDFetcher] Progress: %d/%d, %.2f%%, Failed names: %d", processed,
                        totalNames, ((double) processed / totalNames) * 100D, failed));
            }
        }
    }
    return uuidMap;
}

From source file:com.intellectualcrafters.plot.uuid.UUIDFetcher.java

@Override
public Map<String, UUID> call() throws Exception {
    final Map<String, UUID> uuidMap = new HashMap<>();
    final int requests = (int) Math.ceil(this.names.size() / PROFILES_PER_REQUEST);
    for (int i = 0; i < requests; i++) {
        final HttpURLConnection connection = createConnection();
        final String body = JSONArray
                .toJSONString(this.names.subList(i * 100, Math.min((i + 1) * 100, this.names.size())));
        writeBody(connection, body);//from   ww  w .jav a2  s.  c  o  m
        final JSONArray array = (JSONArray) this.jsonParser
                .parse(new InputStreamReader(connection.getInputStream()));
        for (final Object profile : array) {
            final JSONObject jsonProfile = (JSONObject) profile;
            final String id = (String) jsonProfile.get("id");
            final String name = (String) jsonProfile.get("name");
            final UUID uuid = UUIDFetcher.getUUID(id);
            uuidMap.put(name, uuid);
        }
        if (this.rateLimiting && (i != (requests - 1))) {
            Thread.sleep(100L);
        }
    }
    return uuidMap;
}

From source file:de.matzefratze123.heavyspleef.core.uuid.UUIDManager.java

private List<GameProfile> fetchGameProfiles(String[] names) throws IOException, ParseException {
    List<String> namesList = Arrays.asList(names);
    URL url = new URL(NAME_BASE_URL);
    List<GameProfile> profiles = Lists.newArrayList();
    int requests = (int) Math.ceil((double) names.length / PROFILES_PER_REQUEST);

    for (int i = 0; i < requests; i++) {
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);// w  ww  . ja v  a  2s  .  c  o m
        connection.setDoOutput(true);
        connection.setUseCaches(false);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "application/json");

        String body = JSONArray
                .toJSONString(namesList.subList(i * 100, Math.min((i + 1) * 100, namesList.size())));

        OutputStream out = connection.getOutputStream();
        out.write(body.getBytes());
        out.flush();
        out.close();

        InputStream in = connection.getInputStream();
        Reader reader = new InputStreamReader(in);

        JSONArray resultArray = (JSONArray) parser.parse(reader);

        for (Object object : resultArray) {
            JSONObject result = (JSONObject) object;

            String name = (String) result.get("name");
            UUID uuid = getUUID((String) result.get("id"));
            GameProfile profile = new GameProfile(uuid, name);

            profiles.add(profile);
        }
    }

    return profiles;
}

From source file:fr.zcraft.zlib.tools.mojang.UUIDFetcher.java

/**
 * Writes a JSON body in this connection from the given list.
 *
 * @param connection The connection.//from w w w .  j  a  v a2 s.  com
 * @param names      The list to write as a JSON object.
 *
 * @throws IOException If an exception occurred while contacting the server.
 */
private static void writeBody(HttpURLConnection connection, List<String> names) throws IOException {
    OutputStream stream = connection.getOutputStream();
    String body = JSONArray.toJSONString(names);
    stream.write(body.getBytes());
    stream.flush();
    stream.close();
}