Package org.apache.sling.commons.json

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


        assertEquals("[]", renderer.prettyPrint(new JSONArray(), renderer.options()));
    }
   
    @Test
    public void testSingleJSONArray() throws JSONException {
        final JSONArray ja = new JSONArray();
        ja.put(42);
        assertEquals("[42]", renderer.prettyPrint(ja, renderer.options()));
    }
View Full Code Here


        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

            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"));
        put("array", a);
    }
View Full Code Here

    private Throwable error;

    public JSONResponse() throws JSONResponseException {
        try {
            json = new JSONObject();
            changes = new JSONArray();
            json.put(PROP_CHANGES, changes);
        } catch (Throwable e) {
            throw new JSONResponseException(e);
        }
    }
View Full Code Here

       
        final JSONObject root = new JSONObject(content);
        if(!root.has(JSON_KEY_DATA)) {
            fail(path + " does not provide '" + JSON_KEY_DATA + "' element, JSON content=" + content);
        }
        final JSONArray data = root.getJSONArray(JSON_KEY_DATA);
        if(data.length() < 1) {
            fail(path + "." + JSON_KEY_DATA + " is empty, JSON content=" + content);
        }
        final JSONObject bundle = data.getJSONObject(0);
        if(!bundle.has(JSON_KEY_STATE)) {
            fail(path + ".data[0].state missing, JSON content=" + content);
        }
        return bundle;
    }
View Full Code Here

    private Resource createResource(Resource parentResource, String childName, JSONObject jsonObject)
            throws IOException, JSONException {

        // collect all properties first
        Map<String, Object> props = new HashMap<String, Object>();
        JSONArray names = jsonObject.names();
        for (int i = 0; names != null && i < names.length(); i++) {
            final String name = names.getString(i);
            if (!IGNORED_NAMES.contains(name)) {
                Object obj = jsonObject.get(name);
                if (!(obj instanceof JSONObject)) {
                    this.setProperty(props, name, obj);
                }
            }
        }

        // validate JCR primary type
        Object primaryTypeObj = jsonObject.opt(JcrConstants.JCR_PRIMARYTYPE);
        String primaryType = null;
        if (primaryTypeObj != null) {
            primaryType = String.valueOf(primaryTypeObj);
        }
        if (primaryType == null) {
            primaryType = JcrConstants.NT_UNSTRUCTURED;
        }
        props.put(JcrConstants.JCR_PRIMARYTYPE, primaryType);

        // create resource
        Resource resource = resourceResolver.create(parentResource, childName, props);

        // add child resources
        for (int i = 0; names != null && i < names.length(); i++) {
            final String name = names.getString(i);
            if (!IGNORED_NAMES.contains(name)) {
                Object obj = jsonObject.get(name);
                if (obj instanceof JSONObject) {
                    createResource(resource, name, (JSONObject) obj);
                }
View Full Code Here

    }

    private void setProperty(Map<String, Object> props, String name, Object value) throws JSONException {
        if (value instanceof JSONArray) {
            // multivalue
            final JSONArray array = (JSONArray) value;
            if (array.length() > 0) {
                final Object[] values = new Object[array.length()];
                for (int i = 0; i < array.length(); i++) {
                    values[i] = array.get(i);
                }

                if (values[0] instanceof Double || values[0] instanceof Float) {
                    Double[] arrayValues = new Double[values.length];
                    for (int i = 0; i < values.length; i++) {
View Full Code Here

                    getLog().debug("Response type from web console is not JSON, but " + contentType);
                    throwWebConsoleTooOldException();
                }
                final String jsonText = get.getResponseBodyAsString();
                try {
                    JSONArray array = new JSONArray(jsonText);
                    for(int i=0; i<array.length(); i++) {
                        final JSONObject obj = array.getJSONObject(i);
                        final String pid = obj.getString("pid");
                        final String path = obj.getJSONObject("provider.file").getString("value");
                        final String roots = obj.getJSONObject("provider.roots").getString("value");
                        if ( path != null && path.startsWith(this.project.getBasedir().getAbsolutePath()) ) {
                            getLog().debug("Found configuration with pid: " + pid + ", path: " + path + ", roots: " + roots);
View Full Code Here

                    return null;
                }
                final String jsonText = gm.getResponseBodyAsString();
                try {
                    final JSONObject obj = new JSONObject(jsonText);
                    final JSONArray props = obj.getJSONArray("props");
                    for(int i=0; i<props.length(); i++) {
                        final JSONObject property = props.getJSONObject(i);
                        if ( "Version".equals(property.get("key")) ) {
                            final String version = property.getString("value");
                            getLog().debug("Found web console version " + version);
                            return version;
                        }
View Full Code Here

    public void testArray() throws IOException, JSONException {
        final String toDelete = uploadTestScript("builder_array.groovy","json.groovy");
        try {
            final String content = getContent(displayUrl + ".json", CONTENT_TYPE_JSON);
            JSONArray jo = new JSONArray(content);
            assertEquals("Content contained wrong number of items", 1, jo.length());
            assertEquals("Content contained wrong data", testText, jo.get(0));
        } finally {
            testClient.delete(toDelete);
        }
    }
View Full Code Here

TOP

Related Classes of org.apache.sling.commons.json.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.