Package org.apache.sling.commons.json.io

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


    @Override
    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("application/json");
        final JSONWriter writer = new JSONWriter(response.getWriter());
        try {
            writer.array();
            String type = request.getRequestPathInfo().getSelectorString();
            if (type != null) {
                try {
                    Set<String> categories = new TreeSet<String>();
                    LibraryType libraryType = LibraryType.valueOf(type.toUpperCase());
                    Map<String, ClientLibrary> libraries = libraryManager.getLibraries();
                    for (ClientLibrary library : libraries.values()) {
                        if (library.getTypes() != null && library.getTypes().contains(libraryType)) {
                            String[] libraryCats = library.getCategories();
                            if (libraryCats != null) {
                                for (String cat : libraryCats) {
                                    categories.add(cat);
                                }
                            }
                        }
                    }

                    for (String cat : categories) {
                        writer.object();
                        writer.key("value");
                        writer.value(cat);
                        writer.key("text");
                        writer.value(cat);
                        writer.endObject();
                    }

                } catch (IllegalArgumentException e) {
                    // no matching type. no need to log, just return empty array.
                }
            }
            writer.endArray();
        } catch (JSONException e) {
            response.reset();
            throw new ServletException("Unable to produce JSON", e);
        }
    }
View Full Code Here

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

     * Dump given node in JSON, optionally recursing into its child nodes
     * @param tidy if <code>true</code> the json dump is nicely formatted
     */
    public void dump(Node node, Writer w, int maxRecursionLevels, boolean tidy)
            throws RepositoryException, JSONException {
        JSONWriter jw = new JSONWriter(w);
        jw.setTidy(tidy);
        dump(node, jw, 0, maxRecursionLevels);
    }
View Full Code Here

    }

    /** 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

            }

            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

     * Dump given resource in JSON, optionally recursing into its objects
     * @param tidy if <code>true</code> the json dump is nicely formatted
     */
    public void dump(Resource resource, Writer w, int maxRecursionLevels, boolean tidy)
            throws JSONException {
        JSONWriter jw = new JSONWriter(w);
        jw.setTidy(tidy);
        dump(resource, jw, 0, maxRecursionLevels);
    }
View Full Code Here

        // 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

            throws ServletException, IOException {

        response.setContentType("application/json");
        response.setCharacterEncoding("utf-8");
        try {
            JSONWriter w = new JSONWriter(response.getWriter());
            w.setTidy(true);
            w.object();
            w.key("tasks").array();
            for (RcpTask task: taskMgr.getTasks().values()) {
                task.write(w);
            }
            w.endArray();
            w.endObject();
        } catch (JSONException e) {
            throw new IOException(e.toString());
        }

    }
View Full Code Here

                throw new IllegalArgumentException("Invalid command.");
            }
            // default response
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            JSONWriter w = new JSONWriter(response.getWriter());
            w.setTidy(true);
            w.object();
            w.key("status").value("ok");
            if (task != null) {
                w.key("id").value(task.getId());
            }
            w.endObject();

        } catch (Exception e) {
            log.error("Error while executing command {}", cmd, e);
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            response.setStatus(500);
            JSONWriter w = new JSONWriter(response.getWriter());
            w.setTidy(true);
            try {
                w.object();
                w.key("status").value("error");
                w.key("message").value("Error while executing '" + cmd + "': " + e.getMessage());
                w.endObject();
            } catch (JSONException e1) {
                // ignore
            }
        }
    }
View Full Code Here

TOP

Related Classes of org.apache.sling.commons.json.io.JSONWriter

Copyright © 2018 www.massapicom. 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.