Example usage for org.json.simple JSONObject size

List of usage examples for org.json.simple JSONObject size

Introduction

In this page you can find the example usage for org.json.simple JSONObject size.

Prototype

int size();

Source Link

Document

Returns the number of key-value mappings in this map.

Usage

From source file:com.cloudera.lib.service.instrumentation.TestInstrumentationService.java

@Test
public void variableHolder() throws Exception {
    InstrumentationService.VariableHolder<String> variableHolder = new InstrumentationService.VariableHolder<String>();

    variableHolder.var = new Instrumentation.Variable<String>() {
        @Override/*from  w  w w  .  j  a  va 2s. c  o m*/
        public String getValue() {
            return "foo";
        }
    };

    JSONObject json = (JSONObject) new JSONParser().parse(variableHolder.toJSONString());
    Assert.assertEquals(json.size(), 1);
    Assert.assertEquals(json.get("value"), "foo");

    StringWriter writer = new StringWriter();
    variableHolder.writeJSONString(writer);
    writer.close();
    json = (JSONObject) new JSONParser().parse(writer.toString());
    Assert.assertEquals(json.size(), 1);
    Assert.assertEquals(json.get("value"), "foo");
}

From source file:com.cloudera.lib.service.instrumentation.TestInstrumentationService.java

@Test
public void sampler() throws Exception {
    final long value[] = new long[1];
    Instrumentation.Variable<Long> var = new Instrumentation.Variable<Long>() {
        @Override/*w ww  .  j a  v a  2s  . c o m*/
        public Long getValue() {
            return value[0];
        }
    };

    InstrumentationService.Sampler sampler = new InstrumentationService.Sampler();
    sampler.init(4, var);
    Assert.assertEquals(sampler.getRate(), 0f, 0.0001);
    sampler.sample();
    Assert.assertEquals(sampler.getRate(), 0f, 0.0001);
    value[0] = 1;
    sampler.sample();
    Assert.assertEquals(sampler.getRate(), (0d + 1) / 2, 0.0001);
    value[0] = 2;
    sampler.sample();
    Assert.assertEquals(sampler.getRate(), (0d + 1 + 2) / 3, 0.0001);
    value[0] = 3;
    sampler.sample();
    Assert.assertEquals(sampler.getRate(), (0d + 1 + 2 + 3) / 4, 0.0001);
    value[0] = 4;
    sampler.sample();
    Assert.assertEquals(sampler.getRate(), (4d + 1 + 2 + 3) / 4, 0.0001);

    JSONObject json = (JSONObject) new JSONParser().parse(sampler.toJSONString());
    Assert.assertEquals(json.size(), 2);
    Assert.assertEquals(json.get("sampler"), sampler.getRate());
    Assert.assertEquals(json.get("size"), 4L);

    StringWriter writer = new StringWriter();
    sampler.writeJSONString(writer);
    writer.close();
    json = (JSONObject) new JSONParser().parse(writer.toString());
    Assert.assertEquals(json.size(), 2);
    Assert.assertEquals(json.get("sampler"), sampler.getRate());
    Assert.assertEquals(json.get("size"), 4L);
}

From source file:com.opensoc.json.serialization.JSONKafkaSerializer.java

@SuppressWarnings({ "unchecked", "rawtypes" })
public byte[] toBytes(JSONObject input) {

    ByteArrayOutputStream outputBuffer = new ByteArrayOutputStream();
    DataOutputStream data = new DataOutputStream(outputBuffer);

    Iterator it = input.entrySet().iterator();
    try {//from   www.  jav a 2s.  c o  m

        // write num of entries into output. 
        //each KV pair is counted as an entry
        data.writeInt(input.size());

        // Write every single entry in hashmap
        //Assuming key to be String.
        while (it.hasNext()) {
            Map.Entry<String, Object> entry = (Entry<String, Object>) it.next();
            putObject(data, entry.getKey());
            putObject(data, entry.getValue());
        }
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }

    return outputBuffer.toByteArray();
}

From source file:com.cloudera.lib.service.instrumentation.TestInstrumentationService.java

@Test
public void timer() throws Exception {
    InstrumentationService.Timer timer = new InstrumentationService.Timer(2);
    InstrumentationService.Cron cron = new InstrumentationService.Cron();

    long ownStart;
    long ownEnd;/*from  w  w  w . j  av  a  2  s.com*/
    long totalStart;
    long totalEnd;
    long ownDelta;
    long totalDelta;
    long avgTotal;
    long avgOwn;

    cron.start();
    ownStart = System.currentTimeMillis();
    totalStart = ownStart;
    ownDelta = 0;
    sleep(100);

    cron.stop();
    ownEnd = System.currentTimeMillis();
    ownDelta += ownEnd - ownStart;
    sleep(100);

    cron.start();
    ownStart = System.currentTimeMillis();
    sleep(100);

    cron.stop();
    ownEnd = System.currentTimeMillis();
    ownDelta += ownEnd - ownStart;
    totalEnd = ownEnd;
    totalDelta = totalEnd - totalStart;

    avgTotal = totalDelta;
    avgOwn = ownDelta;

    timer.addCron(cron);
    long[] values = timer.getValues();
    Assert.assertEquals(values[InstrumentationService.Timer.LAST_TOTAL], totalDelta, 20);
    Assert.assertEquals(values[InstrumentationService.Timer.LAST_OWN], ownDelta, 20);
    Assert.assertEquals(values[InstrumentationService.Timer.AVG_TOTAL], avgTotal, 20);
    Assert.assertEquals(values[InstrumentationService.Timer.AVG_OWN], avgOwn, 20);

    cron = new InstrumentationService.Cron();

    cron.start();
    ownStart = System.currentTimeMillis();
    totalStart = ownStart;
    ownDelta = 0;
    sleep(200);

    cron.stop();
    ownEnd = System.currentTimeMillis();
    ownDelta += ownEnd - ownStart;
    sleep(200);

    cron.start();
    ownStart = System.currentTimeMillis();
    sleep(200);

    cron.stop();
    ownEnd = System.currentTimeMillis();
    ownDelta += ownEnd - ownStart;
    totalEnd = ownEnd;
    totalDelta = totalEnd - totalStart;

    avgTotal = (avgTotal * 1 + totalDelta) / 2;
    avgOwn = (avgOwn * 1 + ownDelta) / 2;

    timer.addCron(cron);
    values = timer.getValues();
    Assert.assertEquals(values[InstrumentationService.Timer.LAST_TOTAL], totalDelta, 20);
    Assert.assertEquals(values[InstrumentationService.Timer.LAST_OWN], ownDelta, 20);
    Assert.assertEquals(values[InstrumentationService.Timer.AVG_TOTAL], avgTotal, 20);
    Assert.assertEquals(values[InstrumentationService.Timer.AVG_OWN], avgOwn, 20);

    avgTotal = totalDelta;
    avgOwn = ownDelta;

    cron = new InstrumentationService.Cron();

    cron.start();
    ownStart = System.currentTimeMillis();
    totalStart = ownStart;
    ownDelta = 0;
    sleep(300);

    cron.stop();
    ownEnd = System.currentTimeMillis();
    ownDelta += ownEnd - ownStart;
    sleep(300);

    cron.start();
    ownStart = System.currentTimeMillis();
    sleep(300);

    cron.stop();
    ownEnd = System.currentTimeMillis();
    ownDelta += ownEnd - ownStart;
    totalEnd = ownEnd;
    totalDelta = totalEnd - totalStart;

    avgTotal = (avgTotal * 1 + totalDelta) / 2;
    avgOwn = (avgOwn * 1 + ownDelta) / 2;

    cron.stop();
    timer.addCron(cron);
    values = timer.getValues();
    Assert.assertEquals(values[InstrumentationService.Timer.LAST_TOTAL], totalDelta, 20);
    Assert.assertEquals(values[InstrumentationService.Timer.LAST_OWN], ownDelta, 20);
    Assert.assertEquals(values[InstrumentationService.Timer.AVG_TOTAL], avgTotal, 20);
    Assert.assertEquals(values[InstrumentationService.Timer.AVG_OWN], avgOwn, 20);

    JSONObject json = (JSONObject) new JSONParser().parse(timer.toJSONString());
    Assert.assertEquals(json.size(), 4);
    Assert.assertEquals(json.get("lastTotal"), values[InstrumentationService.Timer.LAST_TOTAL]);
    Assert.assertEquals(json.get("lastOwn"), values[InstrumentationService.Timer.LAST_OWN]);
    Assert.assertEquals(json.get("avgTotal"), values[InstrumentationService.Timer.AVG_TOTAL]);
    Assert.assertEquals(json.get("avgOwn"), values[InstrumentationService.Timer.AVG_OWN]);

    StringWriter writer = new StringWriter();
    timer.writeJSONString(writer);
    writer.close();
    json = (JSONObject) new JSONParser().parse(writer.toString());
    Assert.assertEquals(json.size(), 4);
    Assert.assertEquals(json.get("lastTotal"), values[InstrumentationService.Timer.LAST_TOTAL]);
    Assert.assertEquals(json.get("lastOwn"), values[InstrumentationService.Timer.LAST_OWN]);
    Assert.assertEquals(json.get("avgTotal"), values[InstrumentationService.Timer.AVG_TOTAL]);
    Assert.assertEquals(json.get("avgOwn"), values[InstrumentationService.Timer.AVG_OWN]);
}

From source file:org.opencastproject.adminui.endpoint.UsersSettingsEndpointTest.java

private void compareIds(String key, JSONObject expected, JSONObject actual) {
    JSONArray expectedArray = (JSONArray) expected.get(key);
    JSONArray actualArray = (JSONArray) actual.get(key);

    Assert.assertEquals(expectedArray.size(), actualArray.size());
    JSONObject exObject;//from   w  w w  . j av a 2s.  c om
    JSONObject acObject;
    int actualId;
    for (int i = 0; i < actualArray.size(); i++) {
        acObject = (JSONObject) actualArray.get(i);
        actualId = Integer.parseInt(acObject.get("id").toString()) - 1;
        exObject = (JSONObject) expectedArray.get(actualId);
        Set<String> exEntrySet = exObject.keySet();
        Assert.assertEquals(exEntrySet.size(), acObject.size());
        Iterator<String> exIter = exEntrySet.iterator();

        while (exIter.hasNext()) {
            String item = exIter.next();
            Object exValue = exObject.get(item);
            Object acValue = acObject.get(item);
            Assert.assertEquals(exValue, acValue);
        }

    }
}

From source file:blog.cobablog.java

/**
 * Web service operation/*from   w  w  w.jav  a  2 s . c  o m*/
 * @return 
 */
@WebMethod(operationName = "listUser")
public List<Pengguna> listUser() {
    //TODO write your implementation code here:
    ArrayList<Pengguna> out = new ArrayList<>();
    try {
        //TODO write your implementation code here:
        String linkString = LINK_FIREBASE + "/users.json";
        URL link = new URL(linkString);
        BufferedReader reader = new BufferedReader(new InputStreamReader(link.openStream()));

        String s = "";
        String tmp;
        while ((tmp = reader.readLine()) != null) {
            s += tmp;
        }

        JSONParser parser = new JSONParser();
        JSONObject o = (JSONObject) parser.parse(s);

        int i;
        for (i = 0; i < o.size(); i++) {
            Pengguna p = new Pengguna();
            p.setUsername(o.keySet().toArray()[i].toString());
            JSONObject postEntry = (JSONObject) parser.parse(o.get(p.getUsername()).toString());
            p.setEmail((String) postEntry.get("email"));
            p.setNama((String) postEntry.get("nama"));
            p.setPassword((String) postEntry.get("password"));
            p.setRole((String) postEntry.get("role"));
            out.add(p);
        }

        return out;
        //System.out.println(array.get(0));
    } catch (MalformedURLException ex) {
        Logger.getLogger(cobablog.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(cobablog.class.getName()).log(Level.SEVERE, null, ex);
    } catch (ParseException ex) {
        Logger.getLogger(cobablog.class.getName()).log(Level.SEVERE, null, ex);
    }
    return out;
}

From source file:blog.cobablog.java

/**
 * Web service operation//  w w  w  .ja v a 2 s  . c  om
 * @param idPost
 * @return 
 */
@WebMethod(operationName = "listComment")
public List<Komentar> listComment(@WebParam(name = "idPost") String idPost) {
    ArrayList<Komentar> out = new ArrayList<>();
    try {
        //TODO write your implementation code here:
        String linkString = LINK_FIREBASE + "posts/" + idPost + "/komentar.json";
        URL link = new URL(linkString);
        BufferedReader reader = new BufferedReader(new InputStreamReader(link.openStream()));

        String s = "";
        String tmp;
        while ((tmp = reader.readLine()) != null) {
            s += tmp;
        }

        JSONParser parser = new JSONParser();
        JSONObject o = (JSONObject) parser.parse(s);

        int i;
        for (i = 0; i < o.size(); i++) {
            Komentar k = new Komentar();
            k.setId(o.keySet().toArray()[i].toString());
            JSONObject postEntry = (JSONObject) parser.parse(o.get(k.getId()).toString());
            k.setEmail((String) postEntry.get("email"));
            k.setKonten((String) postEntry.get("konten"));
            k.setNama((String) postEntry.get("nama"));
            k.setTanggal((String) postEntry.get("tanggal"));

            out.add(k);

        }

        return out;
        //System.out.println(array.get(0));
    } catch (MalformedURLException ex) {
        Logger.getLogger(cobablog.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(cobablog.class.getName()).log(Level.SEVERE, null, ex);
    } catch (ParseException ex) {
        Logger.getLogger(cobablog.class.getName()).log(Level.SEVERE, null, ex);
    }
    return out;
}

From source file:blog.cobablog.java

@WebMethod(operationName = "listUnpublishedPost")
public List<Post> listUnpublishedPost() {
    List<Post> out = new ArrayList<Post>();
    try {//from w w w.  j a  v a2s  .c  om
        //TODO write your implementation code here:
        String linkString = LINK_FIREBASE + "posts.json";
        URL link = new URL(linkString);
        BufferedReader reader = new BufferedReader(new InputStreamReader(link.openStream()));

        String s = "";
        String tmp;
        while ((tmp = reader.readLine()) != null) {
            s += tmp;
        }

        JSONParser parser = new JSONParser();
        JSONObject o = (JSONObject) parser.parse(s);

        int i;
        for (i = 0; i < o.size(); i++) {
            Post p = new Post();
            p.setId(o.keySet().toArray()[i].toString());
            JSONObject postEntry = (JSONObject) parser.parse(o.get(p.getId()).toString());
            p.setAuthor((String) postEntry.get("author"));
            p.setJudul((String) postEntry.get("judul"));
            p.setKonten((String) postEntry.get("konten"));
            p.setTanggal((String) postEntry.get("tanggal"));
            p.setDeleted((String) postEntry.get("deleted"));
            p.setPublished((String) postEntry.get("published"));

            if ((!p.isDeleted()) && (!p.isPublished())) {
                out.add(p);
            }
        }

        return out;
        //System.out.println(array.get(0));
    } catch (MalformedURLException ex) {
        Logger.getLogger(cobablog.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(cobablog.class.getName()).log(Level.SEVERE, null, ex);
    } catch (ParseException ex) {
        Logger.getLogger(cobablog.class.getName()).log(Level.SEVERE, null, ex);
    }
    return out;
}

From source file:blog.cobablog.java

@WebMethod(operationName = "listDeletedPost")
public List<Post> listDeletedPost() {
    List<Post> out = new ArrayList<Post>();
    try {/*w ww . java 2 s  . c  o m*/
        //TODO write your implementation code here:
        String linkString = LINK_FIREBASE + "posts.json";
        URL link = new URL(linkString);
        BufferedReader reader = new BufferedReader(new InputStreamReader(link.openStream()));

        String s = "";
        String tmp;
        while ((tmp = reader.readLine()) != null) {
            s += tmp;
        }

        JSONParser parser = new JSONParser();
        JSONObject o = (JSONObject) parser.parse(s);

        int i;
        for (i = 0; i < o.size(); i++) {
            Post p = new Post();
            p.setId(o.keySet().toArray()[i].toString());
            JSONObject postEntry = (JSONObject) parser.parse(o.get(p.getId()).toString());
            p.setAuthor((String) postEntry.get("author"));
            p.setJudul((String) postEntry.get("judul"));
            p.setKonten((String) postEntry.get("konten"));
            p.setTanggal((String) postEntry.get("tanggal"));
            p.setDeleted((String) postEntry.get("deleted"));
            p.setPublished((String) postEntry.get("published"));

            if ((p.isDeleted())) {
                out.add(p);
            }
        }

        return out;
        //System.out.println(array.get(0));
    } catch (MalformedURLException ex) {
        Logger.getLogger(cobablog.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(cobablog.class.getName()).log(Level.SEVERE, null, ex);
    } catch (ParseException ex) {
        Logger.getLogger(cobablog.class.getName()).log(Level.SEVERE, null, ex);
    }
    return out;
}

From source file:blog.cobablog.java

/**
 * Web service operation/* ww w.j  a  v a 2  s.  c om*/
 * @return 
 */
@WebMethod(operationName = "listPost")
public List<Post> listPost() {
    List<Post> out = new ArrayList<Post>();
    try {
        //TODO write your implementation code here:
        String linkString = LINK_FIREBASE + "posts.json";
        URL link = new URL(linkString);
        BufferedReader reader = new BufferedReader(new InputStreamReader(link.openStream()));

        String s = "";
        String tmp;
        while ((tmp = reader.readLine()) != null) {
            s += tmp;
        }

        JSONParser parser = new JSONParser();
        JSONObject o = (JSONObject) parser.parse(s);

        int i;
        for (i = 0; i < o.size(); i++) {
            Post p = new Post();
            p.setId(o.keySet().toArray()[i].toString());
            JSONObject postEntry = (JSONObject) parser.parse(o.get(p.getId()).toString());
            p.setAuthor((String) postEntry.get("author"));
            p.setJudul((String) postEntry.get("judul"));
            p.setKonten((String) postEntry.get("konten"));
            p.setTanggal((String) postEntry.get("tanggal"));
            p.setDeleted((String) postEntry.get("deleted"));
            p.setPublished((String) postEntry.get("published"));

            if ((!p.isDeleted()) && p.isPublished()) {
                out.add(p);
            }
        }

        return out;
        //System.out.println(array.get(0));
    } catch (MalformedURLException ex) {
        Logger.getLogger(cobablog.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(cobablog.class.getName()).log(Level.SEVERE, null, ex);
    } catch (ParseException ex) {
        Logger.getLogger(cobablog.class.getName()).log(Level.SEVERE, null, ex);
    }
    return out;
}