Package org.apache.sling.commons.json

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


            } else {

                // Try multi-value "property"
                final String[] values = resource.adaptTo(String[].class);
                if (values != null) {
                    obj.put(ResourceUtil.getName(resource), new JSONArray(Arrays.asList(values)));
                }

            }

        } else {
View Full Code Here


        Object[] values = null;
        if (value.getClass().isArray()) {
            final int length = Array.getLength(value);
            // write out empty array
            if ( length == 0 ) {
                obj.put(key, new JSONArray());
                return;
            }
            values = new Object[Array.getLength(value)];
            for(int i=0; i<length; i++) {
                values[i] = Array.get(value, i);
            }
        }

        // special handling for binaries: we dump the length and not the data!
        if (value instanceof InputStream
            || (values != null && values[0] instanceof InputStream)) {
            // TODO for now we mark binary properties with an initial colon in
            // their name
            // (colon is not allowed as a JCR property name)
            // in the name, and the value should be the size of the binary data
            if (values == null) {
                obj.put(":" + key, getLength(valueMap, -1, key, (InputStream)value));
            } else {
                final JSONArray result = new JSONArray();
                for (int i = 0; i < values.length; i++) {
                    result.put(getLength(valueMap, i, key, (InputStream)values[i]));
                }
                obj.put(":" + key, result);
            }
            return;
        }

        if (!value.getClass().isArray()) {
            obj.put(key, getValue(value));
        } else {
            final JSONArray result = new JSONArray();
            for (Object v : values) {
                result.put(getValue(v));
            }
            obj.put(key, result);
        }
    }
View Full Code Here

    public String encodeMessage(String body) throws IOException {
        checkActive();
        if (encryptionEnabled) {
            try {
                JSONObject json = new JSONObject();
                json.put("payload", new JSONArray(encrypt(body)));
                return json.toString();
            } catch (InvalidKeyException e) {
                e.printStackTrace();
                throw new IOException("Unable to Encrypt Message " + e.getMessage());
            } catch (IllegalBlockSizeException e) {
View Full Code Here

              + "];");
        }
      }

      if (o1 instanceof JSONArray && o2 instanceof JSONArray) {
        JSONArray e = (JSONArray) o1;
        JSONArray a = (JSONArray) o2;
        assertEquals(header + "arrays first differed at element " + i
            + ";", e, a);
      } else if (o1 instanceof JSONObject && o2 instanceof JSONObject) {
        assertEquals(header + "arrays first differed at element [" + i
            + "];", (JSONObject) o1, (JSONObject) o2);
View Full Code Here

    }
    if (expected == actual /* || expected.equals( actual ) */) {
      return;
    }

    JSONArray expectedNames = expected.names();
    JSONArray actualNames = actual.names();

    if (expectedNames == null && actualNames == null) {
      return;
    }

    if (expectedNames == null) {
        expectedNames = new JSONArray();
    }

    if (actualNames == null) {
        actualNames = new JSONArray();
    }

    assertEquals(header
        + "names sizes differed, expected.names().length()="
        + expectedNames.length() + " actual.names().length()="
        + actualNames.length(), expectedNames.length(), actualNames
        .length());
    for (Iterator<String> keys = expected.keys(); keys.hasNext();) {
      String key = keys.next();
      Object o1 = expected.opt(key);
      Object o2 = actual.opt(key);
View Full Code Here

            announcement.put("backoffInterval", backoffInterval);
        }
        if (resetBackoff) {
            announcement.put("resetBackoff", resetBackoff);
        }
        JSONArray incomingAnnouncements = new JSONArray();
        for (Iterator<Announcement> it = incomings.iterator(); it.hasNext();) {
            Announcement incoming = it.next();
            incomingAnnouncements.put(incoming.asJSONObject(filterTimes));
        }
        announcement.put("topologyAnnouncements", incomingAnnouncements);
        return announcement;
    }
View Full Code Here

            return result;
        }
        final String localClusterViewJSON = announcement
                .getString("localClusterView");
        final ClusterView localClusterView = asClusterView(localClusterViewJSON);
        final JSONArray subAnnouncements = announcement
                .getJSONArray("topologyAnnouncements");

        if (announcement.has("inherited")) {
            final Boolean inherited = announcement.getBoolean("inherited");
            result.inherited = inherited;
        }
        if (announcement.has("serverInfo")) {
            String serverInfo = announcement.getString("serverInfo");
            result.serverInfo = serverInfo;
        }
        result.setLocalCluster(localClusterView);
        for (int i = 0; i < subAnnouncements.length(); i++) {
            String subAnnouncementJSON = subAnnouncements.getString(i);
            result.addIncomingTopologyAnnouncement(fromJSON(subAnnouncementJSON));
        }
        return result;
    }
View Full Code Here

    private static ClusterView asClusterView(final String localClusterViewJSON)
            throws JSONException {
        JSONObject obj = new JSONObject(localClusterViewJSON);
        DefaultClusterViewImpl clusterView = new DefaultClusterViewImpl(
                obj.getString("id"));
        JSONArray instancesObj = obj.getJSONArray("instances");

        for (int i = 0; i < instancesObj.length(); i++) {
            JSONObject anInstance = instancesObj.getJSONObject(i);
            clusterView.addInstanceDescription(asInstance(anInstance));
        }

        return clusterView;
    }
View Full Code Here

    /** 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

        renderer.numberToString(null);
    }
   
    @Test
    public void testEmptyJSONArray() throws JSONException {
        assertEquals("[]", renderer.prettyPrint(new JSONArray(), renderer.options()));
    }
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.