Package org.json.simple.parser

Examples of org.json.simple.parser.JSONParser


            throw new AssertionError("not a valid JSON array: " + e.getMessage());
        }
    }

    protected JSONObject parseJSONObject(String json) throws AssertionError {
        JSONParser parser = new JSONParser();
        try {
            Object obj = parser.parse(json);
            assertTrue(obj instanceof JSONObject);
            return (JSONObject) obj;
        } catch (Exception e) {
            throw new AssertionError("not a valid JSON object: " + e.getMessage());
        }
View Full Code Here


        }
        throw new AssertionError("failed to resolve JSONObject array entry at pos " + pos + ": " + entry);
    }

    protected JSONArray parseJSONArray(String json) throws AssertionError {
        JSONParser parser = new JSONParser();
        try {
            Object obj = parser.parse(json);
            assertTrue(obj instanceof JSONArray);
            return (JSONArray) obj;
        } catch (Exception e) {
            throw new AssertionError("not a valid JSON array: " + e.getMessage());
        }
View Full Code Here

            throw new AssertionError("not a valid JSON array: " + e.getMessage());
        }
    }

    protected JSONObject parseJSONObject(String json) throws AssertionError {
        JSONParser parser = new JSONParser();
        try {
            Object obj = parser.parse(json);
            assertTrue(obj instanceof JSONObject);
            return (JSONObject) obj;
        } catch (Exception e) {
            throw new AssertionError("not a valid JSON object: " + e.getMessage());
        }
View Full Code Here

                        rev = mk.getHeadRevision();
                    }
                    int sum = 0;
                    for (Map.Entry<Integer, JSONObject> entry : nodes.entrySet()) {
                        String json = mk.getNodes("/node-" + entry.getKey(), rev, 0, 0, 1000, null);
                        JSONParser parser = new JSONParser();
                        JSONObject obj = (JSONObject) parser.parse(json);
                        entry.setValue(obj);
                        sum += (Long) obj.get("value");
                    }
                    if (sum < 60) {
                        // retry with other nodes
                        return false;
                    }
                    StringBuilder jsop = new StringBuilder();
                    boolean withdrawn = false;
                    for (Map.Entry<Integer, JSONObject> entry : nodes.entrySet()) {
                        long value = (Long) entry.getValue().get("value");
                        jsop.append("^\"/node-").append(entry.getKey());
                        jsop.append("/value\":");
                        if (value >= 20 && !withdrawn) {
                            jsop.append(value - 20);
                            withdrawn = true;
                        } else {
                            jsop.append(value + 10);
                        }
                    }
                    String oldRev = rev;
                    rev = mk.commit("", jsop.toString(), rev, null);
                    if (useBranch) {
                        rev = mk.merge(rev, null);
                    }
                    log("Successful transfer @" + oldRev + ": " + jsop.toString() + " (new rev: " + rev + ")");
                    return true;
                }
            }));
        }
        for (Thread t : writers) {
            t.start();
        }
        for (Thread t : writers) {
            t.join();
        }
        // dispose will flush all pending revisions
        for (MongoMK mk : kernels) {
            mk.dispose();
        }
        MongoMK mk = openMongoMK();
        String rev = mk.getHeadRevision();
        long sum = 0;
        for (int i = 0; i < NUM_NODES; i++) {
            String json = mk.getNodes("/node-" + i, rev, 0, 0, 1000, null);
            JSONParser parser = new JSONParser();
            JSONObject obj = (JSONObject) parser.parse(json);
            sum += (Long) obj.get("value");
        }
        log("Conflict rate: " + conflicts.get() +
                "/" + (NUM_WRITERS * NUM_TRANSFERS_PER_THREAD));
        System.out.print(logBuffer);
View Full Code Here

        String rev2 = mk.commit("", "+\"/child2\":{}", null, "");

        String branchRev2 = mk.branch(rev2);
        String json = mk.getNodes("/child1", branchRev2, 0, 0, -1, null);
        JSONParser parser = new JSONParser();
        JSONObject obj = (JSONObject) parser.parse(json);
        assertFalse(obj.containsKey("foo"));
    }
View Full Code Here

        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"));
    }
View Full Code Here

            ResponseHandler<String> responseHandler = new BasicResponseHandler();
           
            String responseBody = httpClient.execute(getRequest, responseHandler);
           
            //Parse it as JSON
            JSONParser parser= new JSONParser();
           
            JSONObject wrappingObject = (JSONObject) parser.parse(responseBody);
           
            JSONObject wrappingObjectData = (JSONObject) wrappingObject.get("data");
           
            JSONArray children = (JSONArray) wrappingObjectData.get("children");
           
            if(children.size() == 0)
              break;
           
            //reverse order so printed order is consistent
            for(int c=children.size()-1; c>=0; c--){
              JSONObject childData = (JSONObject) ((JSONObject) children.get(c)).get("data");
              QUEUE.add(new Post((String) childData.get("title"), SUBREDDIT));
            }
           
            lastItemId = (String) wrappingObjectData.get("after");
           
            //If this is the first page, then it's the point we want to store to ensure that we don't get repeated posts
            if(i == 0){
              latestTimestamp = ((Double) ((JSONObject)((JSONObject) children.get(0)).get("data")).get("created")).longValue();
            }
           
            //Rate limit
            if(i != INITIAL_PAGE_COUNT - 1)
              Utils.sleep(1000);
            count += 100;
          }
          initialPull = false;
        }else{
          //Rate limit for the API (pages are cached for 30 seconds)
          Utils.sleep(10000);
          //Get the page
          HttpGet getRequest = new HttpGet(URL);
          ResponseHandler<String> responseHandler = new BasicResponseHandler();
               
          String responseBody = httpClient.execute(getRequest, responseHandler);
         
          //Parse it
          JSONParser parser= new JSONParser();
         
          JSONObject wrappingObject = (JSONObject) parser.parse(responseBody);
         
          JSONObject wrappingObjectData = (JSONObject) wrappingObject.get("data");
         
          JSONArray children = (JSONArray) wrappingObjectData.get("children");
         
View Full Code Here

        String rev2 = mk.commit("", "+\"/child2\":{}", null, "");

        String branchRev2 = mk.branch(rev2);
        String json = mk.getNodes("/child1", branchRev2, 0, 0, -1, null);
        JSONParser parser = new JSONParser();
        JSONObject obj = (JSONObject) parser.parse(json);
        assertFalse(obj.containsKey("foo"));
    }
View Full Code Here

        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"));
    }
View Full Code Here

        return obj.toJSONString();
    }

    private <T extends Document> T fromString(Collection<T> collection, String data) throws ParseException {
        T doc = collection.newDocument(this);
        Map<String, Object> obj = (Map<String, Object>) new JSONParser().parse(data);
        for (Map.Entry<String, Object> entry : obj.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            if (value == null) {
                // ???
View Full Code Here

TOP

Related Classes of org.json.simple.parser.JSONParser

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.