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();
}
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);
}
}
}
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);
}
}
}
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;
}
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;
}
Aggregations