Package org.apache.sling.commons.json

Examples of org.apache.sling.commons.json.JSONObject


    }

    /** convert a clusterview into json **/
    private static JSONObject asJSON(final ClusterView clusterView)
            throws JSONException {
        JSONObject obj = new JSONObject();
        obj.put("id", clusterView.getId());
        JSONArray instancesObj = new JSONArray();
        List<InstanceDescription> instances = clusterView.getInstances();
        for (Iterator<InstanceDescription> it = instances.iterator(); it
                .hasNext();) {
            InstanceDescription instanceDescription = it.next();
            instancesObj.put(asJSON(instanceDescription));
        }
        obj.put("instances", instancesObj);
        return obj;
    }
View Full Code Here


    private static DefaultInstanceDescriptionImpl asInstance(
            final JSONObject anInstance) throws JSONException {
        final boolean isLeader = anInstance.getBoolean("isLeader");
        final String slingId = anInstance.getString("slingId");

        final JSONObject propertiesObj = anInstance.getJSONObject("properties");
        Iterator<String> it = propertiesObj.keys();
        Map<String, String> properties = new HashMap<String, String>();
        while (it.hasNext()) {
            String key = it.next();
            properties.put(key, propertiesObj.getString(key));
        }

        IncomingInstanceDescription instance = new IncomingInstanceDescription(
                null, isLeader, slingId, properties);
        return instance;
View Full Code Here

    }

    /** convert an instance description into a json object **/
    private static JSONObject asJSON(final InstanceDescription instanceDescription)
            throws JSONException {
        JSONObject obj = new JSONObject();
        obj.put("slingId", instanceDescription.getSlingId());
        obj.put("isLeader", instanceDescription.isLeader());
        ClusterView cluster = instanceDescription.getClusterView();
        if (cluster != null) {
            obj.put("cluster", cluster.getId());
        }
        JSONObject propertiesObj = new JSONObject();
        Map<String, String> propertiesMap = instanceDescription.getProperties();
        for (Iterator<Entry<String, String>> it = propertiesMap.entrySet()
                .iterator(); it.hasNext();) {
            Entry<String, String> entry = it.next();
            propertiesObj.put(entry.getKey(), entry.getValue());
        }
        obj.put("properties", propertiesObj);
        return obj;
    }
View Full Code Here

     * property - which gets added to the JSON object automatically due
     * to SLING-3389 wire-backwards-compatibility - and backoffInterval
     * introduced as part of SLING-3382
     */
    public boolean correspondsTo(Announcement announcement) throws JSONException {
        final JSONObject myJson = asJSONObject(true);
        final JSONObject otherJson = announcement.asJSONObject(true);
        return myJson.toString().equals(otherJson.toString());
    }
View Full Code Here

        assertEquals("[42]", renderer.prettyPrint(ja, renderer.options()));
    }
   
    @Test
    public void testDefaultArrayOutput() throws JSONException {
        final JSONObject jo = new TestJSONObject();
        final String pp = renderer.prettyPrint(jo.getJSONArray("array"), renderer.options());
        final DespacedRendering r = new DespacedRendering("{array:" + pp + "}");
        r.expect("[true,_hello_,52,212]");
    }
View Full Code Here

        r.expect("[true,_hello_,52,212]");
    }
   
    @Test
    public void testArraysPrettyPrint() throws JSONException {
        final JSONObject jo = new TestJSONObject();
       
        // Verify that we get an array for children, by re-parsing the output
        final String json = renderer.prettyPrint(jo, renderer.options().withArraysForChildren(true));
        final JSONObject copy = new JSONObject(json);
        final JSONArray a = copy.getJSONArray(JSONRenderer.Options.DEFAULT_CHILDREN_KEY);
        final String str = renderer.toString(a);
        final String expected = "[{\"__name__\":\"k0\",\"name\":\"k0\",\"this is\":\"k0\"},{\"__name__\":\"k1\",\"name\":\"k1\",\"this is\":\"k1\"}]";
        assertEquals(expected, str);
    }
View Full Code Here

        assertEquals(expected, str);
    }
   
    @Test
    public void testCustomNamesArraysPrettyPrint() throws JSONException {
        final JSONObject jo = new TestJSONObject();
        final JSONRenderer.Options opt = renderer.options();
        opt.withArraysForChildren(true).withIndent(2).withChildrenKey("KIDS").withChildNameKey("KID.NAME");
        final DespacedRendering r = new DespacedRendering(renderer.prettyPrint(jo, opt));
        r.expect(
                "-nl-_string_:_thisstring_,-nl-_int_:12,-nl-_long_:42,-nl-_boolean_:true,",
View Full Code Here

                );
    }   
   
    @Test
    public void testIndentedArraysPrettyPrint() throws JSONException {
        final JSONObject jo = new TestJSONObject();
        final JSONRenderer.Options opt = renderer.options().withArraysForChildren(true).withIndent(2);
        final DespacedRendering r = new DespacedRendering(renderer.prettyPrint(jo, opt));
        r.expect(
                "-nl-_string_:_thisstring_,-nl-_int_:12,-nl-_long_:42,-nl-_boolean_:true,",
                "_array_:[-nl-true,-nl-_hello_,-nl-52,-nl-212-nl-]-nl-,",
View Full Code Here

           
        };
        put("JSONString", js);
       
        for(int i=0; i<2; i++) {
            final JSONObject k = new JSONObject();
            final String name = "k" + i;
            k.put("name", name);
            k.put("this is", name);
            put(name,  k);
        }
       
        final JSONArray a = new JSONArray();
        a.put(true).put("hello").put(52.0).put(new BigInteger("212"));
View Full Code Here

    public DespacedRendering(String str, String spaceReplacement) throws JSONException {
       
        boolean previousWasSpace = false;
       
        // Verify that str parses
        new JSONObject(str);
       
        // And convert whitespace and quotes to
        // make comparisons easier
        final StringBuilder sb = new StringBuilder();
        for(int i=0; i < str.length(); i++) {
View Full Code Here

TOP

Related Classes of org.apache.sling.commons.json.JSONObject

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.