public String getValidJSON()

in src/main/java/org/apache/sling/xss/impl/XSSAPIImpl.java [308:346]


    public String getValidJSON(String json, String defaultJson) {
        if (json == null) {
            return getValidJSON(defaultJson, "");
        }
        json = json.trim();
        if ("".equals(json)) {
            return "";
        }
        int curlyIx = json.indexOf("{");
        int straightIx = json.indexOf("[");
        if (curlyIx >= 0 && (curlyIx < straightIx || straightIx < 0)) {
            try {
                StringWriter output = new StringWriter();
                Json.createGenerator(output)
                        .write(jsonReaderFactory
                                .createReader(new StringReader(json))
                                .readObject())
                        .close();
                return output.getBuffer().toString();
            } catch (Exception e) {
                LOGGER.warn("Unable to get valid JSON from the input.", e);
                LOGGER.debug("JSON input:\n{}", json);
            }
        } else {
            try {
                StringWriter output = new StringWriter();
                Json.createGenerator(output)
                        .write(jsonReaderFactory
                                .createReader(new StringReader(json))
                                .readArray())
                        .close();
                return output.getBuffer().toString();
            } catch (Exception e) {
                LOGGER.warn("Unable to get valid JSON from the input.", e);
                LOGGER.debug("JSON input:\n{}", json);
            }
        }
        return getValidJSON(defaultJson, "");
    }