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

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

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

JSONObject.toString介绍

[英]Make a JSON text of this JSONObject using JSONRenderer#toString
[中]使用JSONRenderer#toString生成此JSONObject的JSON文本

代码示例

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

/**
 * Make a prettyprinted JSON text of this JSONObject.
 * <p>
 * Warning: This method assumes that the data structure is acyclical.
 * @param indentFactor The number of spaces to add to each level of
 *  indentation.
 * @return a printable, displayable, portable, transmittable
 *  representation of the object, beginning
 *  with <code>{</code>&nbsp;<small>(left brace)</small> and ending
 *  with <code>}</code>&nbsp;<small>(right brace)</small>.
 * @throws JSONException If the object contains an invalid number.
 */
public String toString(int indentFactor) throws JSONException {
  return toString(indentFactor, 0);
}

代码示例来源:origin: com.citytechinc.aem.apps.ionic/ionic-aem-apps-core

private void addListingFile(JSONObject fileListing, String cachePath, Session adminSession)
    throws RepositoryException, JSONException
{
  JcrUtil.createPath(cachePath, "sling:Folder", "nt:file", adminSession, false);
  Node cacheContentNode = JcrUtil.createPath(cachePath + "/jcr:content", "nt:resource", adminSession);
  Calendar calTS = Calendar.getInstance();
  cacheContentNode.setProperty("jcr:data", adminSession.getValueFactory().createBinary(new ByteArrayInputStream(fileListing.toString(3).getBytes())));
  cacheContentNode.setProperty("jcr:lastModified", calTS);
  adminSession.save();
}

代码示例来源:origin: Adobe-Marketing-Cloud/aem-guides

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

代码示例来源:origin: Adobe-Consulting-Services/acs-aem-tools

private void writeJsonResponse(final List<String> categories,
                final SlingHttpServletResponse response) throws JSONException, IOException {
  final JSONObject jsonObject = new JSONObject();
  jsonObject.put("categories", new JSONArray(categories));
  response.getWriter().print(jsonObject.toString());
}

代码示例来源:origin: Adobe-Consulting-Services/acs-aem-tools

public void triggerReload(String path) throws JSONException {
  if (group != null) {
    JSONObject reload = createReloadObject(path);
    group.flushAndWrite(new TextWebSocketFrame(reload.toString()), matcher);
  }
}

代码示例来源:origin: com.adobe.acs/acs-aem-tools-bundle-livereload

public void triggerReload(String path) throws JSONException {
  if (group != null) {
    JSONObject reload = createReloadObject(path);
    group.flushAndWrite(new TextWebSocketFrame(reload.toString()), matcher);
  }
}

代码示例来源:origin: com.adobe.acs/acs-aem-commons-bundle

/**
 * {@inheritDoc}
 */
public String getErrorJSON(final String msg) {
  final JSONObject json = new JSONObject();
  try {
    json.put("status", "error");
    json.put("msg", msg);
    return json.toString();
  } catch (JSONException e) {
    log.error("Error creating JSON Error response message: {}", e.getMessage());
    return JSON_EXCEPTION_MSG;
  }
}

代码示例来源:origin: com.adobe.acs/acs-aem-commons-bundle

@Override
  protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
      throws ServletException, IOException {
    final Command cmd = new Command(request);

    response.setHeader("Content-Type", " application/json; charset=UTF-8");

    try {
      response.getWriter().append(quicklyEngine.execute(request, response, cmd).toString());
    } catch (JSONException e) {
      response.sendError(SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR);
      response.getWriter().print("{\"status:\": \"error\"}");
    }
  }
}

代码示例来源:origin: Adobe-Consulting-Services/acs-aem-tools

private void sendJSONError(SlingHttpServletResponse response, String title, String message) throws IOException {
  final JSONObject json = new JSONObject();
  response.setStatus(SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR);
  try {
    json.put("title", title);
    json.put("message", message);
    response.getWriter().write(json.toString());
  } catch (JSONException e) {
    String fallbackJSON = "{ \"title\": \"Error creating error response. "
        + "Please review AEM error logs.\" }";
    response.getWriter().write(fallbackJSON);
  }
}

代码示例来源:origin: com.adobe.acs/acs-aem-commons-bundle

@Override
public String startTranscriptionJob(InputStream stream, String mimeType) {
  Request request = httpClientFactory.post("/speech-to-text/api/v1/recognitions?continuous=true&timestamps=true").
      addHeader("Content-Type", mimeType).
      bodyStream(stream);
  try {
    JSONObject json = httpClientFactory.getExecutor().execute(request).handleResponse(HANDLER);
    log.trace("content: {}", json.toString(2));
    return json.getString("id");
  } catch (Exception e) {
    log.error("error submitting job", e);
    return null;
  }
}

代码示例来源:origin: com.adobe.acs/acs-aem-commons-bundle

@Override
  public final void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
      throws ServletException, IOException {

    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");

    try {
      if (workflowInstanceRemover.getStatus() != null) {
        response.getWriter().write(workflowInstanceRemover.getStatus().getJSON().toString());
      } else {
        WorkflowRemovalStatus workflowStatus = new WorkflowRemovalStatus(request.getResourceResolver());
        workflowStatus.setRunning(false);

        response.getWriter().write(workflowStatus.getJSON().toString());
      }
    } catch (Exception e) {
      log.error("Unable to create Workflow Removal status", e);
      response.setStatus(SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR);
      response.getWriter().write(e.getMessage());
    }
  }
}

代码示例来源:origin: com.adobe.acs/acs-aem-commons-bundle

@Override
public final void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
    throws ServletException, IOException {
  response.setContentType("application/json");
  response.setCharacterEncoding("UTF-8");
  final JSONObject json = new JSONObject();
  try {
    // Only populate the form if removal is not running.
    json.put("form", this.getFormJSONObject(request.getResourceResolver()));
    response.getWriter().write(json.toString());
  } catch (Exception e) {
    response.setStatus(SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    response.getWriter().write(e.getMessage());
  }
}

代码示例来源:origin: com.adobe.acs/acs-aem-commons-bundle

public static void sendJSONError(SlingHttpServletResponse response,
                   int statusCode,
                   String title,
                   String message) throws IOException {

    response.setStatus(statusCode);
    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");

    JSONObject json = new JSONObject();
    try {
      json.put("title", title);
      json.put("message", message);
      response.getWriter().write(json.toString());
    } catch (JSONException e) {
      String jsonString = "{title: \"Error constructing error message\"}";
      response.getWriter().write(jsonString);
    }
  }
}

代码示例来源:origin: sinnerschrader/aem-react

/**
 * get the current resource
 *
 * @param depth
 * @return
 */
public String currentResource(int depth) {
 SlingHttpServletRequest request = (SlingHttpServletRequest) context.getBindings(ScriptContext.ENGINE_SCOPE).get(SlingBindings.REQUEST);
 try {
  return JsonObjectCreator.create(request.getResource(), depth).toString();
 } catch (JSONException e) {
  throw new TechnicalException("could not get current resource", e);
 }
}

代码示例来源:origin: com.adobe.acs/acs-aem-commons-bundle

@Override
  protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
      throws ServletException, IOException {

    response.setHeader("Content-Type", " application/json; charset=UTF-8");

    final JSONObject json = new JSONObject();

    try {
      json.put("user", request.getResourceResolver().getUserID());
      json.put("throttle", 200);
      response.getWriter().write(json.toString());
    } catch (JSONException e) {
      response.sendError(SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR);
      response.getWriter().print("{\"status:\": \"error\"}");
    }
  }
}

代码示例来源:origin: com.adobe.acs/acs-aem-commons-bundle

/**
 * Serializes an OSGi {@link org.osgi.service.event.Event} into a JSON object string
 *
 * @param event the event to be serialized as
 * @return a serialized JSON object
 * @throws org.apache.sling.commons.json.JSONException
 */
protected static String constructMessage(Event event) throws JSONException {
  JSONObject obj = new JSONObject();
  for (String prop : event.getPropertyNames()) {
    Object val = event.getProperty(prop);
    Object converted = convertValue(val);
    obj.put(prop, converted == null ? val : converted);
  }
  obj.put(PROP_TIMESTAMP, ISO8601.format(Calendar.getInstance()));
  return obj.toString();
}

代码示例来源: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: com.adobe.acs/acs-aem-commons-bundle

/**
 * {@inheritDoc}
 */
public String getPathFilterSetPreviewJSON(final Collection<PathFilterSet> pathFilterSets) throws JSONException {
  final JSONObject json = new JSONObject();
  json.put("status", "preview");
  json.put("path", "Not applicable (Preview)");
  json.put("filterSets", new JSONArray());
  for (final PathFilterSet pathFilterSet : pathFilterSets) {
    final JSONObject tmp = new JSONObject();
    tmp.put("importMode", "Not applicable (Preview)");
    tmp.put("rootPath", pathFilterSet.getRoot());
    json.accumulate("filterSets", tmp);
  }
  return json.toString();
}

代码示例来源:origin: com.adobe.acs/acs-aem-commons-bundle

/**
 * {@inheritDoc}
 */
public String getSuccessJSON(final JcrPackage jcrPackage) throws JSONException, RepositoryException {
  final JSONObject json = new JSONObject();
  json.put("status", "success");
  json.put("path", jcrPackage.getNode().getPath());
  json.put("filterSets", new JSONArray());
  final List<PathFilterSet> filterSets = jcrPackage.getDefinition().getMetaInf().getFilter().getFilterSets();
  for (final PathFilterSet filterSet : filterSets) {
    final JSONObject jsonFilterSet = new JSONObject();
    jsonFilterSet.put("importMode", filterSet.getImportMode().name());
    jsonFilterSet.put("rootPath", filterSet.getRoot());
    json.accumulate("filterSets", jsonFilterSet);
  }
  return json.toString();
}

代码示例来源:origin: com.adobe.acs/acs-aem-commons-bundle

protected final String getQueryParameterValue(Form form) throws JSONException {
    boolean hasData = false;
    final JSONObject jsonData = new JSONObject();

    form = this.clean(form);

    jsonData.put(KEY_FORM_NAME, form.getName());

    if (form.hasData()) {
      final JSONObject jsonForm = new JSONObject(form.getData());
      jsonData.put(KEY_FORM, jsonForm);
      hasData = true;
    }

    if (form.hasErrors()) {
      final JSONObject jsonError = new JSONObject(form.getErrors());
      jsonData.put(KEY_ERRORS, jsonError);
      hasData = true;
    }

    return hasData ? this.encode(jsonData.toString()) : "";

  }
}

相关文章