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