Examples of JsonWriter


Examples of org.apache.sling.commons.json.io.JSONWriter

    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
    throws ServletException,IOException {
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        try {
            final JSONWriter w = new JSONWriter(response.getWriter());
            w.setTidy(Arrays.asList(request.getRequestPathInfo().getSelectors()).contains("tidy"));
            w.object();
            w.key("descriptors");
            w.object();
            for(String key : repository.getDescriptorKeys()) {
                w.key(key).value(repository.getDescriptor(key));
            }
            w.endObject();
            w.endObject();
        } catch(JSONException je) {
            throw (IOException)new IOException("JSONException in doGet").initCause(je);
        }
        response.getWriter().flush();
    }
View Full Code Here

Examples of org.apache.sling.commons.json.io.JSONWriter

    protected void doGet(SlingHttpServletRequest request,SlingHttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        try {
            final JSONWriter w = new JSONWriter(response.getWriter());
            w.setTidy(true);
            w.object();
            for(Map.Entry<String, AtomicInteger> entry : counters.entrySet()) {
                w.key(entry.getKey()).value(entry.getValue());
            }
            w.endObject();
        } catch(JSONException je) {
            throw (IOException)new IOException("JSONException in doGet").initCause(je);
        }
    }
View Full Code Here

Examples of org.apache.sling.commons.json.io.JSONWriter

        @Override
        public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
            if (type == InputStream.class) {
                StringWriter buffer = new StringWriter();
                try {
                    JSONWriter writer = new JSONWriter(buffer);
                    writer.object();
                    writer.key("options");
                    writer.array();
                    for (Item item : list.getItems()) {
                        writer.object();
                        writer.key("text").value(item.getTitle());
                        writer.key("value").value(item.getValue());
                        writer.endObject();
                    }
                    writer.endArray();
                    writer.endObject();
                    return (AdapterType) new ByteArrayInputStream(buffer.toString().getBytes("UTF-8"));
                } catch (Exception e) {
                    log.warn("Unable to generate JSON object.", e);
                    return null;
                }
View Full Code Here

Examples of org.apache.sling.commons.json.io.JSONWriter

            log.debug("vanity path parameter passed is {}; page path parameter passed is {}", vanityPath, pagePath);

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

            JSONWriter jsonWriter = new JSONWriter(response.getWriter());
            jsonWriter.array();

            if (StringUtils.isNotBlank(vanityPath)) {
                String xpath = "//element(*)[" + NameConstants.PN_SLING_VANITY_PATH + "='" + vanityPath + "']";
                @SuppressWarnings("deprecation")
                Iterator<Resource> resources = resolver.findResources(xpath, Query.XPATH);
                while (resources.hasNext()) {
                    Resource resource = resources.next();
                    String path = resource.getPath();
                    if (path.startsWith("/content") && !path.equals(pagePath)) {
                        jsonWriter.value(path);
                    }
                }

            }
            jsonWriter.endArray();
        } catch (JSONException e) {
            throw new ServletException("Unable to generate JSON result", e);
        }
    }
View Full Code Here

Examples of org.apache.sling.commons.json.io.JSONWriter

            caughtException = true;
        }

        if (request.getRequestPathInfo().getExtension().equals("json")) {
            response.setContentType("application/json");
            JSONWriter writer = new JSONWriter(response.getWriter());
            try {
                writer.object();
                for (final FlushResult result : overallResults) {
                    writer.key(result.agentId);
                    writer.value(result.success);
                }
                writer.endObject();
            } catch (JSONException e) {
                throw new ServletException("Unable to output JSON data", e);
            }
        } else {
            String suffix;
View Full Code Here

Examples of org.apache.sling.commons.json.io.JSONWriter

            }

            resp.setContentType(JsonRendererServlet.responseContentType);
            resp.setCharacterEncoding("UTF-8");

            final JSONWriter w = new JSONWriter(resp.getWriter());
            w.array();

            long count = -1;
            if (req.getParameter(ROWS) != null) {
                count = Long.parseLong(req.getParameter(ROWS));
            }

            List<String> properties = new ArrayList<String>();
            if (req.getParameterValues(PROPERTY) != null) {
                for (String property : req.getParameterValues(PROPERTY)) {
                    properties.add(property);
                }
            }

            String exerptPath = "";
            if (req.getParameter(EXCERPT_PATH) != null) {
                exerptPath = req.getParameter(EXCERPT_PATH);
            }

            // iterate through the result set and build the "json result"
            while (result.hasNext() && count != 0) {
                Map<String, Object> row = result.next();

                w.object();
                String path = row.get("jcr:path").toString();

                w.key("name");
                w.value(ResourceUtil.getName(path));

                // dump columns
                for (String colName : row.keySet()) {
                    w.key(colName);
                    String strValue = "";
                    if (colName.equals(REP_EXCERPT)) {
                        Object ev = row.get("rep:excerpt(" + exerptPath + ")");
                        strValue = (ev == null) ? "" : ev.toString();
                    } else {
                        strValue = formatValue(row.get(colName));
                    }
                    w.value(strValue);
                }

                // load properties and add it to the result set
                if (!properties.isEmpty()) {
                    Resource nodeRes = resolver.getResource(path);
                    dumpProperties(w, nodeRes, properties);
                }

                w.endObject();
                count--;
            }
            w.endArray();
        } catch (JSONException je) {
            throw wrapException(je);
        }
    }
View Full Code Here

Examples of org.apache.sling.commons.json.io.JSONWriter

        // render data in JSON format
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");

        final Writer out = response.getWriter();
        final JSONWriter w = new JSONWriter(out);

        try {
            w.object();
            for (Map.Entry<String, String> e : data.entrySet()) {
                w.key(e.getKey());
                w.value(e.getValue());
            }
            w.endObject();

        } catch (JSONException jse) {
            out.write(jse.toString());

        } finally {
View Full Code Here

Examples of org.apache.sling.commons.json.io.JSONWriter

     *
     * @throws JSONException
     */
    public void dump(NodeIterator it, Writer out) throws RepositoryException,
            JSONException {
        final JSONWriter w = new JSONWriter(out);
        w.array();
        while (it.hasNext()) {
            dumpSingleNode(it.nextNode(), w, 1, 0);
        }
        w.endArray();
    }
View Full Code Here

Examples of org.apache.sling.commons.json.io.JSONWriter

    }

    /** Dump given node in JSON, optionally recursing into its child nodes */
    public void dump(Node node, Writer w, int maxRecursionLevels)
            throws RepositoryException, JSONException {
        dump(node, new JSONWriter(w), 0, maxRecursionLevels);
    }
View Full Code Here

Examples of org.apache.sling.commons.json.io.JSONWriter

    }

    /** Dump given property in JSON */
    public void dump(Property p, Writer w) throws JSONException,
            ValueFormatException, RepositoryException {
        final JSONWriter jw = new JSONWriter(w);
        jw.object();
        writeProperty(jw, p);
        jw.endObject();
    }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.