Package org.json.simple

Examples of org.json.simple.JSONArray


            IOException {
    }

    @SuppressWarnings("unchecked")
    private <T> JSONArray instrElementsToJson(Map<String, Map<String, Instrumentation.Element<T>>> instrElements) {
        JSONArray array = new JSONArray();
        for (Map.Entry<String, Map<String, Instrumentation.Element<T>>> group : instrElements.entrySet()) {
            JSONObject json = new JSONObject();
            String groupName = group.getKey();
            json.put(JsonTags.INSTR_GROUP, groupName);
            JSONArray dataArray = new JSONArray();
            for (Map.Entry<String, Instrumentation.Element<T>> elementEntry : group.getValue().entrySet()) {
                String samplerName = elementEntry.getKey();
                JSONObject dataJson = new JSONObject();
                dataJson.put(JsonTags.INSTR_NAME, samplerName);
                Object value = elementEntry.getValue().getValue();
                if (value instanceof Instrumentation.Timer) {
                    Instrumentation.Timer timer = (Instrumentation.Timer) value;
                    dataJson.put(JsonTags.INSTR_TIMER_TICKS, timer.getTicks());
                    dataJson.put(JsonTags.INSTR_TIMER_OWN_TIME_AVG, timer.getOwnAvg());
                    dataJson.put(JsonTags.INSTR_TIMER_TOTAL_TIME_AVG, timer.getTotalAvg());
                    dataJson.put(JsonTags.INSTR_TIMER_OWN_STD_DEV, timer.getOwnStdDev());
                    dataJson.put(JsonTags.INSTR_TIMER_TOTAL_STD_DEV, timer.getTotalStdDev());
                    dataJson.put(JsonTags.INSTR_TIMER_OWN_MIN_TIME, timer.getOwnMin());
                    dataJson.put(JsonTags.INSTR_TIMER_OWN_MAX_TIME, timer.getOwnMax());
                    dataJson.put(JsonTags.INSTR_TIMER_TOTAL_MIN_TIME, timer.getTotalMin());
                    dataJson.put(JsonTags.INSTR_TIMER_TOTAL_MAX_TIME, timer.getTotalMax());
                }
                else {
                    dataJson.put(JsonTags.INSTR_VARIABLE_VALUE, value);
                }
                dataArray.add(dataJson);
            }
            json.put(JsonTags.INSTR_DATA, dataArray);
            array.add(json);
        }
        return array;
View Full Code Here


     * @param SLAEVent list.
     * @return the corresponding JSON array.
     */
    @SuppressWarnings("unchecked")
    public static JSONArray toJSONArray(List<? extends SLAEventBean> events) {
        JSONArray array = new JSONArray();
        if (events != null) {
            for (JsonSLAEvent node : events) {
                array.add(node.toJSONObject());
            }
        }
        return array;
    }
View Full Code Here

     */
    @SuppressWarnings("unchecked")
    @Override
    protected void getQueueDump(JSONObject json) throws XServletException {
        List<String> queueDumpList = Services.get().get(CallableQueueService.class).getQueueDump();
        JSONArray queueDumpArray = new JSONArray();
        for (String str: queueDumpList) {
            JSONObject jObject = new JSONObject();
            jObject.put(JsonTags.CALLABLE_DUMP, str);
            queueDumpArray.add(jObject);
        }
        json.put(JsonTags.QUEUE_DUMP, queueDumpArray);

        List<String> uniqueDumpList = Services.get().get(CallableQueueService.class).getUniqueDump();
        JSONArray uniqueDumpArray = new JSONArray();
        for (String str: uniqueDumpList) {
            JSONObject jObject = new JSONObject();
            jObject.put(JsonTags.UNIQUE_ENTRY_DUMP, str);
            uniqueDumpArray.add(jObject);
        }
        json.put(JsonTags.UNIQUE_MAP_DUMP, uniqueDumpArray);
    }
View Full Code Here

        }

        long now = System.currentTimeMillis();

        // get history since 'now'
        JSONArray array = parseJSONArray(mk.getRevisionHistory(now, -1, null));
        // history should be empty since there was no commit since 'now'
        assertEquals(0, array.size());

        // get oldest available revision
        array = parseJSONArray(mk.getRevisionHistory(0, 1, null));
        // there should be exactly 1 revision
        assertEquals(1, array.size());

        long ts0 = System.currentTimeMillis();

        final int NUM_COMMITS = 100;

        // perform NUM_COMMITS commits
        for (int i = 0; i < NUM_COMMITS; i++) {
            mk.commit("/test", "+\"child" + i + "\":{}", null, "commit#" + i);
        }

        // get oldest available revision
        array = parseJSONArray(mk.getRevisionHistory(ts0, -1, null));
        // there should be exactly NUM_COMMITS revisions
        assertEquals(NUM_COMMITS, array.size());
        long previousTS = ts0;
        for (int i = 0; i < NUM_COMMITS; i++) {
            JSONObject rev = getObjectArrayEntry(array, i);
            assertPropertyExists(rev, "id", String.class);
            assertPropertyExists(rev, "ts", Long.class);
            // verify commit msg
            assertPropertyValue(rev, "msg", "commit#" + i);
            // verify chronological order
            long ts = (Long) resolveValue(rev, "ts");
            assertTrue(previousTS <= ts);
            previousTS = ts;
        }

        // last revision should be the current head revision
        assertPropertyValue(getObjectArrayEntry(array, array.size() - 1), "id", mk.getHeadRevision());

        String fromRev = (String) resolveValue(getObjectArrayEntry(array, 0), "id");
        String toRev = (String) resolveValue(getObjectArrayEntry(array, array.size() - 1), "id");

        // verify journal
        array = parseJSONArray(mk.getJournal(fromRev, toRev, ""));
        // there should be exactly NUM_COMMITS entries
        assertEquals(NUM_COMMITS, array.size());
        // verify that 1st and last rev match fromRev and toRev
        assertPropertyValue(getObjectArrayEntry(array, 0), "id", fromRev);
        assertPropertyValue(getObjectArrayEntry(array, array.size() - 1), "id", toRev);

        previousTS = ts0;
        for (int i = 0; i < NUM_COMMITS; i++) {
            JSONObject rev = getObjectArrayEntry(array, i);
            assertPropertyExists(rev, "id", String.class);
            assertPropertyExists(rev, "ts", Long.class);
            assertPropertyExists(rev, "changes", String.class);
            // TODO verify json diff
            // verify commit msg
            assertPropertyValue(rev, "msg", "commit#" + i);
            // verify chronological order
            long ts = (Long) resolveValue(rev, "ts");
            assertTrue(previousTS <= ts);
            previousTS = ts;
        }

        // test with 'negative' range (from and to swapped)
        array = parseJSONArray(mk.getJournal(toRev, fromRev, ""));
        // there should be exactly 0 entries
        assertEquals(0, array.size());
    }
View Full Code Here

        String revFoo = mk.commit("/test/foo", "^\"p1\":123", null, "");
        String revBar = mk.commit("/test/bar", "^\"p2\":456", null, "");
        String rev0 = revFoo;

        // get history since ts (no filter)
        JSONArray array = parseJSONArray(mk.getRevisionHistory(ts, -1, null));
        // history should contain 2 commits: revFoo and revBar
        assertEquals(2, array.size());
        assertPropertyValue(getObjectArrayEntry(array, 0), "id", revFoo);
        assertPropertyValue(getObjectArrayEntry(array, 1), "id", revBar);

        // get history since ts (non-matching filter)
        array = parseJSONArray(mk.getRevisionHistory(ts, -1, "/blah"));
        // history should contain 0 commits since filter doesn't match
        assertEquals(0, array.size());

        // get history since ts (filter on /test/bar)
        array = parseJSONArray(mk.getRevisionHistory(ts, -1, "/test/bar"));
        // history should contain 1 commit: revBar
        assertEquals(1, array.size());
        assertPropertyValue(getObjectArrayEntry(array, 0), "id", revBar);

        // get journal (no filter)
        array = parseJSONArray(mk.getJournal(rev0, null, ""));
        // journal should contain 2 commits: revFoo and revBar
        assertEquals(2, array.size());
        assertPropertyValue(getObjectArrayEntry(array, 0), "id", revFoo);
        assertPropertyValue(getObjectArrayEntry(array, 1), "id", revBar);

        // get journal (non-matching filter)
        array = parseJSONArray(mk.getJournal(rev0, null, "/blah"));
        // journal should contain 0 commits since filter doesn't match
        assertEquals(0, array.size());

        // get journal (filter on /test/bar)
        array = parseJSONArray(mk.getJournal(rev0, null, "/test/bar"));
        // journal should contain 1 commit: revBar
        assertEquals(1, array.size());
        assertPropertyValue(getObjectArrayEntry(array, 0), "id", revBar);
        String diff = (String) resolveValue(getObjectArrayEntry(array, 0), "changes");
        assertNotNull(diff);
        assertTrue(diff.contains("456"));
        assertFalse(diff.contains("123"));
View Full Code Here

        // add a node /branch/foo in branchRev
        branchRev = mk.commit("", "+\"/branch/foo\":{}", branchRev, "");

        // make sure branchRev doesn't show up in revision history
        String hist = mk.getRevisionHistory(0, -1, null);
        JSONArray array = parseJSONArray(hist);
        for (Object entry : array) {
            assertTrue(entry instanceof JSONObject);
            JSONObject rev = (JSONObject) entry;
            assertFalse(branchRev.equals(rev.get("id")));
        }

        // add a node /test123 in head
        mk.commit("", "+\"/test123\":{}", null, "");
        // make sure /test123 doesn't exist in branchRev
        assertFalse(mk.nodeExists("/test123", branchRev));

        // merge branchRev with head
        String newHead = mk.merge(branchRev, "");
        // make sure /test123 still exists in head
        assertTrue(mk.nodeExists("/test123", null));
        // make sure /branch/foo does now exist in head
        assertTrue(mk.nodeExists("/branch/foo", null));

        try {
            mk.getJournal(oldHead, branchRev, "/");
        } catch (MicroKernelException e) {
            fail("getJournal should succeed if the range spans from a (older) head to a (newer) private branch revision");
        }
        try {
            mk.getJournal(branchRootRev, null, "/");
            fail("getJournal should throw if the range spans from a (older) private branch to a (newer) head revision");
        } catch (MicroKernelException e) {
            // expected
        }
        try {
            mk.getJournal(branchRootRev, branchRev, "/");
        } catch (MicroKernelException e) {
            fail("getJournal should succeed if the range spans a single private branch");
        }

        String jrnl = mk.getJournal(newHead, newHead, "/");
        array = parseJSONArray(jrnl);
        assertEquals(1, array.size());
        JSONObject rev = getObjectArrayEntry(array, 0);
        assertPropertyValue(rev, "id", newHead);
        String diff = (String) resolveValue(rev, "changes");
        // TODO properly verify json diff format
        // make sure json diff contains +"/branch":{...}
View Full Code Here

        // Super columns
        for (Map.Entry<String, JSONObject> entry : (Set<Map.Entry<String, JSONObject>>)row.entrySet())
        {
            byte[] superName = hexToBytes(entry.getKey());
            long deletedAt = (Long)entry.getValue().get("deletedAt");
            JSONArray subColumns = (JSONArray)entry.getValue().get("subColumns");
           
            // Add sub-columns
            for (Object c : subColumns)
            {
                JsonColumn col = new JsonColumn(c);
View Full Code Here

        private long timestamp;
        private boolean isDeleted;
       
        private JsonColumn(Object obj) throws ClassCastException
        {
            JSONArray colSpec = (JSONArray)obj;
            assert colSpec.size() == 4;
            name = (String)colSpec.get(0);
            value = (String)colSpec.get(1);
            timestamp = (Long)colSpec.get(2);
            isDeleted = (Boolean)colSpec.get(3);
        }
View Full Code Here

            conn.setDoOutput(true);

            final BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            final String response = reader.readLine();

            final JSONArray array = (JSONArray) JSONValue.parse(response);

            if (array.size() == 0) {
                this.plugin.getLogger().warning("The updater could not find any files for the project id " + this.id);
                this.result = UpdateResult.FAIL_BADID;
                return false;
            }

            final JSONObject jsonObject = (JSONObject) array.get(array.size() - 1);
            this.versionFileName = (String) jsonObject.get(Updater.FILE_NAME);
      this.versionName = (String) jsonObject.get(Updater.TITLE_VALUE);
            this.versionLink = (String) jsonObject.get(Updater.LINK_VALUE);
            this.versionType = (String) jsonObject.get(Updater.TYPE_VALUE);
            this.versionGameVersion = (String) jsonObject.get(Updater.VERSION_VALUE);
View Full Code Here

           
            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");
         
          if(children.size() > 0){
            //reverse order so it is an actual stream
            for(int c=children.size()-1; c>=0; c--){
              JSONObject childData = (JSONObject) ((JSONObject) children.get(c)).get("data");
              if(latestTimestamp < ((Double) childData.get("created")).longValue())
                QUEUE.add(new Post((String) childData.get("title"), SUBREDDIT));
            }
            latestTimestamp = ((Double) ((JSONObject)((JSONObject) children.get(0)).get("data")).get("created")).longValue();
          }
        }
      } catch (ClientProtocolException e) {
        e.printStackTrace();
      } catch (IOException e) {
View Full Code Here

TOP

Related Classes of org.json.simple.JSONArray

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.