org.apache.sling.commons.json.JSONObject.putOpt()方法的使用及代码示例

x33g5p2x  于2022-01-22 转载在 其他  
字(5.6k)|赞(0)|评价(0)|浏览(207)

本文整理了Java中org.apache.sling.commons.json.JSONObject.putOpt()方法的一些代码示例,展示了JSONObject.putOpt()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JSONObject.putOpt()方法的具体详情如下:
包路径:org.apache.sling.commons.json.JSONObject
类名称:JSONObject
方法名:putOpt

JSONObject.putOpt介绍

[英]Put a key/value pair in the JSONObject, but only if the key and the value are both non-null.
[中]在JSONObject中放置一个键/值对,但前提是键和值都非null。

代码示例

代码示例来源:origin: io.wcm/io.wcm.caconfig.editor

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;
 }
}

代码示例来源:origin: org.apache.sling/org.apache.sling.commons.json

/**
 * 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]));
  }
}

代码示例来源:origin: io.wcm/io.wcm.caconfig.editor

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;
}

代码示例来源:origin: io.wcm/io.wcm.caconfig.editor

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);

代码示例来源:origin: io.wcm/io.wcm.caconfig.editor

@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);
 }
}

代码示例来源:origin: io.wcm/io.wcm.caconfig.editor

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;
}

代码示例来源:origin: io.wcm/io.wcm.dam.asset-service

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;
}

相关文章