Example usage for org.json JSONArray write

List of usage examples for org.json JSONArray write

Introduction

In this page you can find the example usage for org.json JSONArray write.

Prototype

public Writer write(Writer writer) throws JSONException 

Source Link

Document

Write the contents of the JSONArray as JSON text to a writer.

Usage

From source file:com.ahanda.techops.noty.clientTest.ClientHandler.java

public void pubEvent(Channel ch, JSONObject e, FullHttpResponse resp) {
    // Prepare the HTTP request.
    JSONArray es = new JSONArray();
    es.put(e);//from   w ww  . ja  va 2s .  c  om

    StringWriter estr = new StringWriter();
    es.write(estr);
    String contentStr = estr.toString();
    FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/events",
            ch.alloc().buffer().writeBytes(contentStr.getBytes()));

    request.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
    request.headers().set(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP);
    request.headers().set(HttpHeaders.Names.CONTENT_TYPE, "application/json");

    request.headers().set(HttpHeaders.Names.CONTENT_LENGTH, request.content().readableBytes());
    // Set some example cookies.
    request.headers().set(HttpHeaders.Names.COOKIE, ClientCookieEncoder.encode(sessCookies));

    l.info("PUB Event request {}", request);

    // Send the HTTP request.
    ch.writeAndFlush(request);
}

From source file:com.controlj.addon.colorreport.servlets.TreeDataServlet.java

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.setContentType("application/json");

    final String id = req.getParameter("id");
    String treeString = req.getParameter("type");
    final SystemTree tree;
    if (treeString == null || treeString.equals("geo")) {
        tree = SystemTree.Geographic;/*from   www  . j a  v a  2  s  .  co m*/
    } else if (treeString.equals("net")) {
        tree = SystemTree.Network;
    } else {
        tree = SystemTree.Geographic;
    }

    final PrintWriter writer = resp.getWriter();

    try {
        SystemConnection connection = DirectAccess.getDirectAccess().getUserSystemConnection(req);

        connection.runReadAction(new ReadAction() {
            @Override
            public void execute(@NotNull SystemAccess access) throws Exception {
                JSONArray arrayData = new JSONArray();
                Collection<Location> children;
                if (id == null) {
                    children = new ArrayList<Location>(1);
                    children.add(access.getTree(tree).getRoot());
                } else {
                    children = access.getTree(tree).resolve(id).getChildren(LocationSort.PRESENTATION);
                }

                for (Location child : children) {
                    if (child.getType() == LocationType.Driver) {
                        continue;
                    }
                    JSONObject next = new JSONObject();
                    if (!child.hasParent()) { // if this is the root, make it selected
                        next.put("activate", true);
                    }
                    next.put("title", child.getDisplayName());
                    next.put("key", child.getTransientLookupString());
                    next.put("path", child.getDisplayPath());
                    next.put("hideCheckbox", false);

                    if (!child.getChildren().isEmpty()) {
                        if ((child.getType() != LocationType.Device) || (child.getChildren().size() != 1)) {
                            next.put("isLazy", true);
                        }
                    }

                    next.put("icon", getIconForType(child.getType()));
                    arrayData.put(next);
                }
                arrayData.write(writer);
            }
        });
    } catch (InvalidConnectionRequestException e) {
        // None of these should occur without programmer error - need to log when logging facility is available
        // This is as good as printing to System.err
        e.printStackTrace();
    } catch (SystemException e) {
        e.printStackTrace();
    } catch (ActionExecutionException e) {
        e.printStackTrace();
    }
}

From source file:com.alcshare.alarmcleanup.servlets.TreeDataServlet.java

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.setContentType("application/json");

    final String id = req.getParameter("id");
    String treeString = req.getParameter("type");
    final SystemTree tree;
    if (treeString == null || treeString.equals("geo")) {
        tree = SystemTree.Geographic;/*  w ww .  j  ava2 s . c o  m*/
    } else if (treeString.equals("net")) {
        tree = SystemTree.Network;
    } else {
        tree = SystemTree.Geographic;
    }

    final PrintWriter writer = resp.getWriter();

    try {
        SystemConnection connection = DirectAccess.getDirectAccess().getUserSystemConnection(req);

        connection.runReadAction(new ReadAction() {
            @Override
            public void execute(@NotNull SystemAccess access) throws Exception {
                JSONArray arrayData = new JSONArray();
                Collection<Location> children;
                if (id == null) {
                    children = new ArrayList<Location>(1);
                    children.add(access.getTree(tree).getRoot());
                } else {
                    children = access.getTree(tree).resolve(id).getChildren(LocationSort.PRESENTATION);
                }

                for (Location child : children) {
                    if (child.getType() == LocationType.Microblock) {
                        continue;
                    }
                    JSONObject next = new JSONObject();
                    if (!child.hasParent()) { // if this is the root, make it selected
                        next.put("activate", true);
                    }
                    next.put("title", child.getDisplayName());
                    next.put("key", child.getTransientLookupString());
                    next.put("path", child.getDisplayPath());
                    next.put("hideCheckbox", false);
                    next.put("isLazy", false);

                    if (!child.getChildren().isEmpty()) {
                        if (child.getType() != LocationType.Equipment) {
                            next.put("isLazy", true);
                        }
                    }

                    next.put("icon", getIconForType(child.getType()));
                    arrayData.put(next);
                }
                arrayData.write(writer);
            }
        });
    } catch (InvalidConnectionRequestException e) {
        // None of these should occur without programmer error - need to log when logging facility is available
        // This is as good as printing to System.err
        e.printStackTrace();
    } catch (SystemException e) {
        e.printStackTrace();
    } catch (ActionExecutionException e) {
        e.printStackTrace();
    }
}