Search in sources :

Example 61 with JSONParser

use of org.json.simple.parser.JSONParser in project jackrabbit-oak by apache.

the class DocumentMKBranchTest method movesInBranch.

@Test
public void movesInBranch() throws Exception {
    String branchRev = mk.branch(null);
    branchRev = mk.commit("/", "+\"a\":{}", branchRev, null);
    branchRev = mk.commit("/a", "^\"foo\":1", branchRev, null);
    branchRev = mk.commit("/", ">\"a\" : \"b\"", branchRev, null);
    branchRev = mk.commit("/", ">\"b\" : \"a\"", branchRev, null);
    mk.merge(branchRev, null);
    String json = mk.getNodes("/a", null, 0, 0, -1, null);
    JSONParser parser = new JSONParser();
    JSONObject obj = (JSONObject) parser.parse(json);
    assertTrue(obj.containsKey("foo"));
}
Also used : JSONObject(org.json.simple.JSONObject) JSONParser(org.json.simple.parser.JSONParser) Test(org.junit.Test)

Example 62 with JSONParser

use of org.json.simple.parser.JSONParser in project Glowstone by GlowstoneMC.

the class LootingManager method register.

private static void register(EntityType type, String location) throws Exception {
    try {
        InputStream in = LootingManager.class.getClassLoader().getResourceAsStream(location);
        if (in == null) {
            GlowServer.logger.warning("Could not find default entity loot table '" + location + "' on classpath");
            return;
        }
        JSONObject json = (JSONObject) new JSONParser().parse(new InputStreamReader(in));
        entities.put(type, new EntityLootTable(json));
    } catch (Exception e) {
        Exception ex = new Exception("Failed to load loot table '" + location + "': " + e.getClass().getName() + " (" + e.getMessage() + ")");
        ex.setStackTrace(e.getStackTrace());
        throw ex;
    }
}
Also used : JSONObject(org.json.simple.JSONObject) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) JSONParser(org.json.simple.parser.JSONParser)

Example 63 with JSONParser

use of org.json.simple.parser.JSONParser in project Glowstone by GlowstoneMC.

the class PlayerStatisticIoService method readStats.

/**
     * Reads the stats of a player from its statistics file and writes the values to the StatisticMap.
     *
     * @param player the player to read the statistics from
     */
public void readStats(GlowPlayer player) {
    File statsFile = getPlayerFile(player.getUniqueId());
    player.getStatisticMap().getValues().clear();
    if (statsFile.exists()) {
        try {
            JSONParser parser = new JSONParser();
            JSONObject json = (JSONObject) parser.parse(new FileReader(statsFile));
            for (Object obj : json.entrySet()) {
                Map.Entry<String, Object> entry = (Map.Entry<String, Object>) obj;
                Long longValue = null;
                if (entry.getValue() instanceof Long) {
                    longValue = (Long) entry.getValue();
                } else if (entry.getValue() instanceof JSONObject) {
                    JSONObject object = (JSONObject) entry.getValue();
                    if (object.containsKey("value")) {
                        longValue = (Long) object.get("value");
                    }
                } else {
                    GlowServer.logger.warning("Unknown statistic type for '" + entry.getKey() + "': " + entry.getValue() + " (" + entry.getValue().getClass().getSimpleName() + ")");
                }
                if (longValue != null) {
                    player.getStatisticMap().getValues().put(entry.getKey(), longValue.intValue());
                }
            }
        } catch (ParseException | IOException e) {
            e.printStackTrace();
        }
    }
}
Also used : IOException(java.io.IOException) JSONObject(org.json.simple.JSONObject) JSONParser(org.json.simple.parser.JSONParser) FileReader(java.io.FileReader) JSONObject(org.json.simple.JSONObject) ParseException(org.json.simple.parser.ParseException) File(java.io.File) StatisticMap(net.glowstone.util.StatisticMap) Map(java.util.Map)

Example 64 with JSONParser

use of org.json.simple.parser.JSONParser in project Gargoyle by callakrsos.

the class JsonTest method jsonTest.

@Test
public void jsonTest() throws ParseException {
    JSONArray jsonArray = new JSONArray();
    {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("svn.url", "http://sample.net");
        jsonObject.put("svn.userId", "userId");
        jsonObject.put("svn.userPass", "userPass");
        jsonArray.add(jsonObject);
    }
    {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("svn.url", "http://sample.ssdsds.net");
        jsonObject.put("svn.userId", "userId");
        jsonObject.put("svn.userPass", "userPass");
        jsonArray.add(jsonObject);
    }
    System.out.println(jsonArray.toJSONString());
    //		JSONArray jsonArray2 = new JSONArray();
    JSONArray parse = (JSONArray) new JSONParser().parse(jsonArray.toJSONString());
    parse.forEach(System.out::println);
}
Also used : JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray) JSONParser(org.json.simple.parser.JSONParser) Test(org.junit.Test)

Example 65 with JSONParser

use of org.json.simple.parser.JSONParser in project cogcomp-nlp by CogComp.

the class QueryMQL method getResponse.

public JSONObject getResponse(String mqlQuery) throws IOException, ParseException {
    HttpTransport httpTransport = new NetHttpTransport();
    HttpRequestFactory requestFactory = httpTransport.createRequestFactory();
    JSONParser parser = new JSONParser();
    GenericUrl url = new GenericUrl("https://www.googleapis.com/freebase/v1/mqlread");
    url.put("query", mqlQuery);
    url.put("key", apikey);
    logger.debug("Querying Freebase QUERY URL: " + url.toString());
    HttpRequest request = requestFactory.buildGetRequest(url);
    HttpResponse httpResponse;
    try {
        httpResponse = request.execute();
    } catch (HttpResponseException e) {
        e.printStackTrace();
        int statusCode = e.getStatusCode();
        logger.error("StatusCode " + statusCode);
        logger.error("Query URL was " + url.toString());
        logger.error("Query was " + mqlQuery);
        if (// max limit reached for a day
        statusCode == 403) {
            System.exit(-1);
        }
        return null;
    } catch (SocketTimeoutException e) {
        e.printStackTrace();
        return null;
    }
    JSONObject response = (JSONObject) parser.parse(httpResponse.parseAsString());
    return response;
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) HttpTransport(com.google.api.client.http.HttpTransport) SocketTimeoutException(java.net.SocketTimeoutException) JSONObject(org.json.simple.JSONObject) HttpRequestFactory(com.google.api.client.http.HttpRequestFactory) NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) HttpResponse(com.google.api.client.http.HttpResponse) JSONParser(org.json.simple.parser.JSONParser) HttpResponseException(com.google.api.client.http.HttpResponseException) GenericUrl(com.google.api.client.http.GenericUrl)

Aggregations

JSONParser (org.json.simple.parser.JSONParser)200 JSONObject (org.json.simple.JSONObject)168 JSONArray (org.json.simple.JSONArray)73 ParseException (org.json.simple.parser.ParseException)49 HttpClient (org.apache.commons.httpclient.HttpClient)44 GetMethod (org.apache.commons.httpclient.methods.GetMethod)34 Test (org.junit.Test)28 HashMap (java.util.HashMap)23 Map (java.util.Map)19 IOException (java.io.IOException)18 File (java.io.File)16 Before (org.junit.Before)15 URL (java.net.URL)14 ArrayList (java.util.ArrayList)13 InputStreamReader (java.io.InputStreamReader)12 PostMethod (org.apache.commons.httpclient.methods.PostMethod)11 MapLayer (au.org.emii.portal.menu.MapLayer)9 Point (com.vividsolutions.jts.geom.Point)9 FileReader (java.io.FileReader)9 InputStream (java.io.InputStream)7