Search in sources :

Example 41 with JSONObject

use of org.apache.sling.commons.json.JSONObject in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class ImageImpl method buildJson.

private void buildJson() {
    Map<String, Object> objectMap = new HashMap<>();
    objectMap.put(Image.JSON_SMART_SIZES, new JSONArray(Arrays.asList(ArrayUtils.toObject(smartSizes))));
    objectMap.put(Image.JSON_SMART_IMAGES, new JSONArray(Arrays.asList(smartImages)));
    objectMap.put(Image.JSON_LAZY_ENABLED, !disableLazyLoading);
    json = new JSONObject(objectMap).toString();
}
Also used : JSONObject(org.apache.sling.commons.json.JSONObject) HashMap(java.util.HashMap) JSONArray(org.apache.sling.commons.json.JSONArray) JSONObject(org.apache.sling.commons.json.JSONObject)

Example 42 with JSONObject

use of org.apache.sling.commons.json.JSONObject in project sling by apache.

the class JSONAssert method assertEquals.

/**
	 * Asserts that two JSONObjects are equal.
	 */
public static void assertEquals(String message, JSONObject expected, JSONObject actual) throws JSONException {
    String header = message == null ? "" : message + ": ";
    if (expected == null) {
        fail(header + "expected object was null");
    }
    if (actual == null) {
        fail(header + "actual object was null");
    }
    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);
        if (JSONObject.NULL.equals(o1)) {
            if (JSONObject.NULL.equals(o2)) {
                continue;
            }
            fail(header + "objects differed at key [" + key + "];");
        } else {
            if (JSONObject.NULL.equals(o2)) {
                fail(header + "objects differed at key [" + key + "];");
            }
        }
        if (o1 instanceof JSONObject && o2 instanceof JSONObject) {
            assertEquals(header + "objects differed at key [" + key + "];", (JSONObject) o1, (JSONObject) o2);
        } else if (o1 instanceof JSONArray && o2 instanceof JSONArray) {
            assertEquals(header + "objects differed at key [" + key + "];", (JSONArray) o1, (JSONArray) o2);
        } else if (o1 instanceof String) {
            assertEquals(header + "objects differed at key [" + key + "];", (String) o1, String.valueOf(o2));
        } else if (o2 instanceof String) {
            assertEquals(header + "objects differed at key [" + key + "];", String.valueOf(o1), (String) o2);
        } else {
            assertEquals(header + "objects differed at key [" + key + "];", o1, o2);
        }
    }
}
Also used : JSONObject(org.apache.sling.commons.json.JSONObject) JSONArray(org.apache.sling.commons.json.JSONArray) JSONObject(org.apache.sling.commons.json.JSONObject)

Example 43 with JSONObject

use of org.apache.sling.commons.json.JSONObject in project sling by apache.

the class JSONAssert method assertEquals.

/**
	 * Asserts that two JSONArrays are equal.
	 */
public static void assertEquals(String message, JSONArray expected, JSONArray actual) throws JSONException {
    String header = message == null ? "" : message + ": ";
    if (expected == null) {
        fail(header + "expected array was null");
    }
    if (actual == null) {
        fail(header + "actual array was null");
    }
    if (expected == actual || expected.equals(actual)) {
        return;
    }
    if (actual.length() != expected.length()) {
        fail(header + "arrays sizes differed, expected.length()=" + expected.length() + " actual.length()=" + actual.length());
    }
    int max = expected.length();
    for (int i = 0; i < max; i++) {
        Object o1 = expected.get(i);
        Object o2 = actual.get(i);
        // handle nulls
        if (JSONObject.NULL.equals(o1)) {
            if (JSONObject.NULL.equals(o2)) {
                continue;
            }
            fail(header + "arrays first differed at element [" + i + "];");
        } else {
            if (JSONObject.NULL.equals(o2)) {
                fail(header + "arrays first differed at element [" + i + "];");
            }
        }
        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);
        } else if (o1 instanceof String) {
            assertEquals(header + "arrays first differed at element [" + i + "];", (String) o1, String.valueOf(o2));
        } else if (o2 instanceof String) {
            assertEquals(header + "arrays first differed at element [" + i + "];", String.valueOf(o1), (String) o2);
        } else {
            assertEquals(header + "arrays first differed at element [" + i + "];", o1, o2);
        }
    }
}
Also used : JSONObject(org.apache.sling.commons.json.JSONObject) JSONArray(org.apache.sling.commons.json.JSONArray) JSONObject(org.apache.sling.commons.json.JSONObject)

Example 44 with JSONObject

use of org.apache.sling.commons.json.JSONObject in project acs-aem-commons by Adobe-Consulting-Services.

the class TypeUtil method toMap.

/**
 * Converts a JSONObject to a simple Map. This will only work properly for
 * JSONObjects of depth 1.
 * <p/>
 * Resulting map will be type'd <String, T> where T is the type of the second parameter (klass)
 *
 * @param json
 * @param klass
 * @return
 */
@SuppressWarnings("unchecked")
public static <T> Map<String, T> toMap(JSONObject json, Class<T> klass) throws JSONException {
    final HashMap<String, T> map = new HashMap<String, T>();
    final List<?> keys = IteratorUtils.toList(json.keys());
    for (final Object key : keys) {
        final String strKey = key.toString();
        final Object obj = json.get(strKey);
        if (klass.isInstance(obj)) {
            // Only add objects of this type
            map.put(strKey, (T) obj);
        }
    }
    return map;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) JSONObject(org.apache.sling.commons.json.JSONObject)

Example 45 with JSONObject

use of org.apache.sling.commons.json.JSONObject in project acs-aem-commons by Adobe-Consulting-Services.

the class LastModifiedOperationImpl method getLastModifiedQuery.

private List<Resource> getLastModifiedQuery(final ResourceResolver resourceResolver, final String userId, final String relativeDateRange, final String nodeType, final String dateProperty, final int limit) {
    final List<Resource> resources = new ArrayList<Resource>();
    final Map<String, String> map = new HashMap<String, String>();
    map.put("path", "/content");
    map.put("type", nodeType);
    map.put("1_property", NameConstants.PN_PAGE_LAST_MOD_BY);
    map.put("1_property.value", userId);
    map.put("relativedaterange.property", dateProperty);
    map.put("relativedaterange.lowerBound", relativeDateRange);
    map.put("orderby", dateProperty);
    map.put("orderby.sort", "desc");
    map.put("p.limit", String.valueOf(limit));
    map.put("p.guessTotal", "true");
    try {
        log.debug("Lastmod QueryBuilder Map: {}", new JSONObject(map).toString(2));
    } catch (JSONException e) {
    // no-op
    }
    final Query query = queryBuilder.createQuery(PredicateGroup.create(map), resourceResolver.adaptTo(Session.class));
    final SearchResult result = query.getResult();
    for (final Hit hit : result.getHits()) {
        try {
            resources.add(hit.getResource());
        } catch (RepositoryException e) {
            log.error("Error resolving Hit to Resource [ {} ]. " + "Likely issue with lucene index being out of sync.", hit.toString());
        }
    }
    return resources;
}
Also used : Query(com.day.cq.search.Query) HashMap(java.util.HashMap) Resource(org.apache.sling.api.resource.Resource) ArrayList(java.util.ArrayList) JSONException(org.apache.sling.commons.json.JSONException) SearchResult(com.day.cq.search.result.SearchResult) RepositoryException(javax.jcr.RepositoryException) Hit(com.day.cq.search.result.Hit) JSONObject(org.apache.sling.commons.json.JSONObject) Session(javax.jcr.Session)

Aggregations

JSONObject (org.apache.sling.commons.json.JSONObject)74 JSONArray (org.apache.sling.commons.json.JSONArray)22 Test (org.junit.Test)20 JSONException (org.apache.sling.commons.json.JSONException)16 HashMap (java.util.HashMap)15 ValueMap (org.apache.sling.api.resource.ValueMap)9 Map (java.util.Map)8 ArrayList (java.util.ArrayList)6 ServletException (javax.servlet.ServletException)6 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)6 Resource (org.apache.sling.api.resource.Resource)6 RepositoryException (javax.jcr.RepositoryException)5 Calendar (java.util.Calendar)4 ValueMapDecorator (org.apache.sling.api.wrappers.ValueMapDecorator)4 Config (com.adobe.acs.commons.workflow.bulk.execution.model.Config)3 IOException (java.io.IOException)3 LinkedHashMap (java.util.LinkedHashMap)3 Session (javax.jcr.Session)3 ModifiableValueMapDecorator (org.apache.sling.api.wrappers.ModifiableValueMapDecorator)3 Form (com.adobe.acs.commons.forms.Form)2