private JSONObject toJson(Map<String, String> properties) throws JSONException { if (properties == null || properties.isEmpty()) { return null; } else { JSONObject metadataProps = new JSONObject(); for (Map.Entry<String, String> entry : properties.entrySet()) { metadataProps.putOpt(entry.getKey(), entry.getValue()); } return metadataProps; } }
/** * Construct a JSONObject from a subset of another JSONObject. * An array of strings is used to identify the keys that should be copied. * Missing keys are ignored. * @param jo A JSONObject. * @param sa An array of strings. * @exception JSONException If a value is a non-finite number. */ public JSONObject(JSONObject jo, String[] sa) throws JSONException { this(); // basic setup for (int i = 0; i < sa.length; i += 1) { putOpt(sa[i], jo.opt(sa[i])); } }
private JSONObject toJson(ConfigurationCollectionData configCollection, ConfigurationData newItem) throws JSONException { JSONObject result = new JSONObject(); result.putOpt("configName", configCollection.getConfigName()); if (!configCollection.getProperties().isEmpty()) { JSONObject properties = new JSONObject(); for (Map.Entry<String, Object> entry : configCollection.getProperties().entrySet()) { properties.putOpt(entry.getKey(), entry.getValue()); } result.put("properties", properties); } JSONArray items = new JSONArray(); for (ConfigurationData configData : configCollection.getItems()) { items.put(toJson(configData, configData.isInherited())); } result.put("items", items); result.put("newItem", toJson(newItem, null)); return result; }
JSONObject result = new JSONObject(); result.putOpt("configName", config.getConfigName()); result.putOpt("collectionItemName", config.getCollectionItemName()); result.putOpt("overridden", config.isOverridden()); if (inherited != null) { result.put("inherited", inherited.booleanValue()); prop.putOpt("name", item.getName()); metadata.putOpt("label", itemMetadata.getLabel()); metadata.putOpt("description", itemMetadata.getDescription()); metadata.putOpt("properties", toJson(itemMetadata.getProperties())); prop.put("metadata", metadata); prop.putOpt("value", toJsonValue(item.getValue())); prop.putOpt("effectiveValue", toJsonValue(item.getEffectiveValue())); prop.putOpt("configSourcePath", item.getConfigSourcePath()); prop.putOpt("default", item.isDefault()); prop.putOpt("inherited", item.isInherited()); prop.putOpt("overridden", item.isOverridden()); metadata.putOpt("defaultValue", toJsonValue(itemMetadata.getDefaultValue())); metadata.putOpt("label", itemMetadata.getLabel()); metadata.putOpt("description", itemMetadata.getDescription()); metadata.putOpt("properties", toJson(itemMetadata.getProperties())); prop.put("metadata", metadata);
@Override protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException { if (!editorConfig.isEnabled()) { response.sendError(HttpServletResponse.SC_FORBIDDEN); return; } Resource contextResource = request.getResource(); try { JSONObject result = new JSONObject(); result.putOpt("contextPath", getContextPath(contextResource)); result.put("configNames", getConfigNames(contextResource)); response.setContentType("application/json;charset=" + CharEncoding.UTF_8); response.getWriter().write(result.toString()); } catch (JSONException ex) { throw new ServletException("Unable to generate JSON.", ex); } }
private JSONArray getConfigNames(Resource contextResource) throws JSONException { JSONArray output = new JSONArray(); SortedSet<String> configNames = configManager.getConfigurationNames(); for (String configName : configNames) { ConfigurationMetadata metadata = configManager.getConfigurationMetadata(configName); if (metadata != null) { JSONObject item = new JSONObject(); item.put("configName", configName); item.putOpt("label", metadata.getLabel()); item.putOpt("description", metadata.getDescription()); item.put("collection", metadata.isCollection()); item.put("exists", hasConfig(contextResource, configName, metadata.isCollection())); output.put(item); } } return output; }
private JSONArray toResultJson(List<Media> mediaList) throws JSONException { JSONArray array = new JSONArray(); for (Media media : mediaList) { Rendition rendition = media.getRendition(); JSONObject mediaObject = new JSONObject(); mediaObject.put("assetPath", media.getAsset().getPath()); mediaObject.put("url", media.getUrl()); if (rendition.getWidth() > 0 && rendition.getHeight() > 0) { mediaObject.put("width", rendition.getWidth()); mediaObject.put("height", rendition.getHeight()); } if (rendition.getFileSize() > 0) { mediaObject.put("fileSize", rendition.getFileSize()); } mediaObject.putOpt("fileExtension", rendition.getFileExtension()); mediaObject.putOpt("mimeType", rendition.getMimeType()); array.put(mediaObject); } return array; }