Search in sources :

Example 56 with JSONObject

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

the class TypeUtilTest method testToMap.

@Test
public void testToMap() throws JSONException {
    final JSONObject json = new JSONObject();
    json.put("one", "uno");
    json.put("two", 2);
    json.put("three", new Long(3));
    final Map<String, Object> expResult = new HashMap<String, Object>();
    expResult.put("one", "uno");
    expResult.put("two", 2);
    expResult.put("three", new Long(3));
    final Map<String, Object> actual = TypeUtil.toMap(json);
    assertEquals(expResult, actual);
}
Also used : JSONObject(org.apache.sling.commons.json.JSONObject) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) JSONObject(org.apache.sling.commons.json.JSONObject) Test(org.junit.Test)

Example 57 with JSONObject

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

the class ChildrenAsPropertyResource method serializeToJSON.

/**
 * Converts a list of SyntheticChildAsPropertyResource to their JSON representation, keeping the provided order.
 *
 * @param resourceToSerialize the resource to serialize to JSON.
 * @return the JSONObject representing the resources.
 * @throws JSONException
 */
protected final JSONObject serializeToJSON(final Resource resourceToSerialize) throws JSONException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
    final DateTimeFormatter dtf = ISODateTimeFormat.dateTime();
    final Map<String, Object> serializedData = new HashMap<String, Object>();
    for (Map.Entry<String, Object> entry : resourceToSerialize.getValueMap().entrySet()) {
        if (entry.getValue() instanceof Calendar) {
            final Calendar cal = (Calendar) entry.getValue();
            serializedData.put(entry.getKey(), dtf.print(cal.getTimeInMillis()));
        } else if (entry.getValue() instanceof Date) {
            final Date date = (Date) entry.getValue();
            serializedData.put(entry.getKey(), dtf.print(date.getTime()));
        } else {
            serializedData.put(entry.getKey(), entry.getValue());
        }
    }
    return new JSONObject(serializedData);
}
Also used : JSONObject(org.apache.sling.commons.json.JSONObject) HashMap(java.util.HashMap) Calendar(java.util.Calendar) JSONObject(org.apache.sling.commons.json.JSONObject) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) ValueMap(org.apache.sling.api.resource.ValueMap) HashMap(java.util.HashMap) Map(java.util.Map) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Date(java.util.Date)

Example 58 with JSONObject

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

the class AbstractWidgetConfigurationServlet method underlay.

/**
 * Load the base configuration and "underlay" it under the provided
 * configuration so that the provided configuration overwrites the default
 * configuration.
 *
 * @param config the configuration to underlay
 * @param resource the resource to underlay
 * @return the underlayed configuration
 * @throws JSONException
 * @throws ServletException
 */
protected final JSONObject underlay(JSONObject config, Resource resource) throws JSONException, ServletException {
    JSONObject baseStructure = toJSONObject(resource);
    if (baseStructure != null) {
        Iterator<String> keys = config.keys();
        while (keys.hasNext()) {
            String key = keys.next();
            baseStructure.put(key, config.get(key));
        }
        return baseStructure;
    } else {
        return config;
    }
}
Also used : JSONObject(org.apache.sling.commons.json.JSONObject)

Example 59 with JSONObject

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

the class AbstractWidgetConfigurationServlet method toJSONObject.

protected JSONObject toJSONObject(Resource resource) throws JSONException, ServletException {
    JSONObject config = null;
    Node node = resource.adaptTo(Node.class);
    if (node != null) {
        JsonItemWriter writer = new JsonItemWriter(null);
        StringWriter string = new StringWriter();
        try {
            writer.dump(node, string, -1);
        } catch (RepositoryException e) {
            throw new ServletException(e);
        }
        config = new JSONObject(string.toString());
    }
    return config;
}
Also used : ServletException(javax.servlet.ServletException) JSONObject(org.apache.sling.commons.json.JSONObject) StringWriter(java.io.StringWriter) Node(javax.jcr.Node) RepositoryException(javax.jcr.RepositoryException) JsonItemWriter(org.apache.sling.commons.json.jcr.JsonItemWriter)

Example 60 with JSONObject

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

the class AbstractWidgetConfigurationServlet method writeEmptyWidget.

protected void writeEmptyWidget(String propertyName, SlingHttpServletResponse response) throws IOException, JSONException {
    JSONObject rte = createEmptyWidget(propertyName);
    rte.write(response.getWriter());
}
Also used : JSONObject(org.apache.sling.commons.json.JSONObject)

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