Search in sources :

Example 56 with JSONArray

use of org.json.simple.JSONArray in project eiger by wlloyd.

the class SSTableExportTest method testEscapingDoubleQuotes.

@Test
public void testEscapingDoubleQuotes() throws IOException {
    File tempSS = tempSSTableFile("Keyspace1", "ValuesWithQuotes");
    ColumnFamily cfamily = ColumnFamily.create("Keyspace1", "ValuesWithQuotes");
    SSTableWriter writer = new SSTableWriter(tempSS.getPath(), 2);
    // Add rowA
    cfamily.addColumn(null, new Column(ByteBufferUtil.bytes("data"), UTF8Type.instance.fromString("{\"foo\":\"bar\"}")));
    writer.append(Util.dk("rowA"), cfamily);
    cfamily.clear();
    SSTableReader reader = writer.closeAndOpenReader();
    // Export to JSON and verify
    File tempJson = File.createTempFile("ValuesWithQuotes", ".json");
    SSTableExport.export(reader, new PrintStream(tempJson.getPath()), new String[0]);
    JSONObject json = (JSONObject) JSONValue.parse(new FileReader(tempJson));
    JSONArray rowA = (JSONArray) json.get(asHex("rowA"));
    JSONArray data = (JSONArray) rowA.get(0);
    assert hexToBytes((String) data.get(0)).equals(ByteBufferUtil.bytes("data"));
    assert data.get(1).equals("{\"foo\":\"bar\"}");
}
Also used : PrintStream(java.io.PrintStream) SSTableReader(org.apache.cassandra.io.sstable.SSTableReader) JSONObject(org.json.simple.JSONObject) Column(org.apache.cassandra.db.Column) ExpiringColumn(org.apache.cassandra.db.ExpiringColumn) CounterColumn(org.apache.cassandra.db.CounterColumn) SSTableWriter(org.apache.cassandra.io.sstable.SSTableWriter) JSONArray(org.json.simple.JSONArray) FileReader(java.io.FileReader) File(java.io.File) SSTableUtils.tempSSTableFile(org.apache.cassandra.io.sstable.SSTableUtils.tempSSTableFile) ColumnFamily(org.apache.cassandra.db.ColumnFamily) Test(org.junit.Test)

Example 57 with JSONArray

use of org.json.simple.JSONArray in project eiger by wlloyd.

the class SSTableExportTest method testExportCounterCf.

@Test
public void testExportCounterCf() throws IOException {
    File tempSS = tempSSTableFile("Keyspace1", "Counter1");
    ColumnFamily cfamily = ColumnFamily.create("Keyspace1", "Counter1");
    SSTableWriter writer = new SSTableWriter(tempSS.getPath(), 2);
    // Add rowA
    cfamily.addColumn(null, new CounterColumn(ByteBufferUtil.bytes("colA"), 42, System.currentTimeMillis()));
    writer.append(Util.dk("rowA"), cfamily);
    cfamily.clear();
    SSTableReader reader = writer.closeAndOpenReader();
    // Export to JSON and verify
    File tempJson = File.createTempFile("Counter1", ".json");
    SSTableExport.export(reader, new PrintStream(tempJson.getPath()), new String[0]);
    JSONObject json = (JSONObject) JSONValue.parse(new FileReader(tempJson));
    JSONArray rowA = (JSONArray) json.get(asHex("rowA"));
    JSONArray colA = (JSONArray) rowA.get(0);
    assert hexToBytes((String) colA.get(0)).equals(ByteBufferUtil.bytes("colA"));
    assert ((String) colA.get(3)).equals("c");
    assert (Long) colA.get(4) == Long.MIN_VALUE;
}
Also used : PrintStream(java.io.PrintStream) SSTableReader(org.apache.cassandra.io.sstable.SSTableReader) JSONObject(org.json.simple.JSONObject) SSTableWriter(org.apache.cassandra.io.sstable.SSTableWriter) JSONArray(org.json.simple.JSONArray) CounterColumn(org.apache.cassandra.db.CounterColumn) FileReader(java.io.FileReader) File(java.io.File) SSTableUtils.tempSSTableFile(org.apache.cassandra.io.sstable.SSTableUtils.tempSSTableFile) ColumnFamily(org.apache.cassandra.db.ColumnFamily) Test(org.junit.Test)

Example 58 with JSONArray

use of org.json.simple.JSONArray in project Glowstone by GlowstoneMC.

the class JsonListFile method save.

/**
     * Saves to the file.
     */
@SuppressWarnings("unchecked")
protected void save() {
    JSONArray array = new JSONArray();
    for (BaseEntry entry : entries) {
        JSONObject obj = new JSONObject();
        for (Entry<String, String> mapEntry : entry.write().entrySet()) {
            obj.put(mapEntry.getKey(), mapEntry.getValue());
        }
        array.add(obj);
    }
    try (Writer writer = new FileWriter(file)) {
        array.writeJSONString(writer);
    } catch (Exception ex) {
        GlowServer.logger.log(Level.SEVERE, "Error writing to: " + file, ex);
    }
}
Also used : JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray)

Example 59 with JSONArray

use of org.json.simple.JSONArray in project Glowstone by GlowstoneMC.

the class Metrics method getPluginData.

/**
     * Gets the plugin specific data.
     * This method is called using Reflection.
     *
     * @return The plugin specific data.
     */
public JSONObject getPluginData() {
    JSONObject data = new JSONObject();
    String pluginName = server.getName();
    String pluginVersion = server.getVersion();
    // Append the name of the plugin
    data.put("pluginName", pluginName);
    // Append the version of the plugin
    data.put("pluginVersion", pluginVersion);
    JSONArray customCharts = new JSONArray();
    for (CustomChart customChart : charts) {
        // Add the data of the custom charts
        JSONObject chart = customChart.getRequestJsonObject();
        if (chart == null) {
            // If the chart is null, we skip it
            continue;
        }
        customCharts.add(chart);
    }
    data.put("customCharts", customCharts);
    return data;
}
Also used : JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray)

Example 60 with JSONArray

use of org.json.simple.JSONArray in project Glowstone by GlowstoneMC.

the class PlayerDataFetcher method getUUID.

/**
     * Look up the UUID for a given username.
     *
     * @param playerName The name to look up.
     * @return The UUID, or null on failure.
     */
public static UUID getUUID(String playerName) {
    HttpsURLConnection conn;
    try {
        URL url = new URL(UUID_URL);
        conn = (HttpsURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/json");
    } catch (IOException e) {
        GlowServer.logger.log(Level.WARNING, "Failed to look up UUID", e);
        return null;
    }
    List<String> playerList = new ArrayList<>();
    playerList.add(playerName);
    JSONArray json;
    try {
        try (DataOutputStream os = new DataOutputStream(conn.getOutputStream())) {
            os.writeBytes(JSONValue.toJSONString(playerList));
        }
        json = (JSONArray) JSONValue.parse(new InputStreamReader(conn.getInputStream()));
    } catch (IOException e) {
        GlowServer.logger.warning("Couldn't get UUID due to IO error: " + e);
        return null;
    }
    if (!json.isEmpty()) {
        String uuid = (String) ((JSONObject) json.get(0)).get("id");
        return UuidUtils.fromFlatString(uuid);
    }
    return null;
}
Also used : ArrayList(java.util.ArrayList) JSONArray(org.json.simple.JSONArray) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) URL(java.net.URL)

Aggregations

JSONArray (org.json.simple.JSONArray)267 JSONObject (org.json.simple.JSONObject)238 JSONParser (org.json.simple.parser.JSONParser)73 Test (org.junit.Test)40 ParseException (org.json.simple.parser.ParseException)23 ArrayList (java.util.ArrayList)21 HttpClient (org.apache.commons.httpclient.HttpClient)21 IOException (java.io.IOException)17 HashMap (java.util.HashMap)17 GetMethod (org.apache.commons.httpclient.methods.GetMethod)17 Transaction (org.xel.Transaction)12 File (java.io.File)11 List (java.util.List)10 HttpResponse (org.apache.http.HttpResponse)10 BlockchainTest (org.xel.BlockchainTest)10 APICall (org.xel.http.APICall)10 Block (org.xel.Block)9 MapLayer (au.org.emii.portal.menu.MapLayer)8 Map (java.util.Map)8 FileReader (java.io.FileReader)6