Example usage for org.json JSONWriter JSONWriter

List of usage examples for org.json JSONWriter JSONWriter

Introduction

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

Prototype

public JSONWriter(Writer w) 

Source Link

Document

Make a fresh JSONWriter.

Usage

From source file:org.everit.osgi.webconsole.configuration.ConfigServlet.java

@Override
protected void doPut(final HttpServletRequest req, final HttpServletResponse resp)
        throws ServletException, IOException {
    String requestBody = requestBody(req);
    String pid = req.getParameter("pid");
    String factoryPid = req.getParameter("factoryPid");
    String configAdminPid = req.getParameter("configAdminPid");
    String location = req.getParameter("location");
    Map<String, List<String>> attributes = extractRawAttributesFromJSON(new JSONObject(requestBody));
    if (pid == null) {
        resp.setContentType("application/json");
        JSONWriter writer = new JSONWriter(resp.getWriter());
        configManager.createConfiguration(configAdminPid, factoryPid, location, attributes).toJSON(writer);

    } else {/*from  w  w  w.j  a v a2  s.c o  m*/
        configManager.updateConfiguration(configAdminPid, pid, factoryPid, attributes);
        printSuccess(resp);
    }
}

From source file:org.everit.osgi.webconsole.configuration.ConfigServlet.java

private void getConfigForm(final HttpServletResponse resp, final String pid, final String factoryPid,
        final String location, final String configAdminPid) {
    try {/*ww w.jav a 2 s .  co m*/
        JSONWriter writer = new JSONWriter(resp.getWriter());
        writer.array();
        configManager.getConfigForm(pid, factoryPid, location, configAdminPid)
                .forEach((attr) -> attr.toJSON(writer));
        writer.endArray();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.everit.osgi.webconsole.configuration.ConfigServlet.java

private void listConfigAdminServices(final Writer writer) {
    JSONWriter jsonWriter = new JSONWriter(writer);
    jsonWriter.array();/* w  ww.j  a  v  a2 s . c  o  m*/
    configManager.configAdminStream().forEach((confAdmin) -> {
        jsonWriter.object();
        jsonWriter.key("pid");
        jsonWriter.value(confAdmin.getProperty("service.pid"));
        jsonWriter.key("description");
        jsonWriter.value(confAdmin.getProperty("service.description"));
        jsonWriter.key("bundleId");
        jsonWriter.value(confAdmin.getBundle().getBundleId());
        jsonWriter.endObject();
    });
    jsonWriter.endArray();
}

From source file:org.everit.osgi.webconsole.configuration.ConfigServlet.java

private void listManagedServices(final String configAdminPid, final Writer writer) {
    JSONWriter jsonWriter = new JSONWriter(writer);
    jsonWriter.array();//w  ww.j a  v  a  2s .  com
    configManager.lookupConfigurations(configAdminPid)
            .forEach((configurable) -> configurable.toJSON(jsonWriter));
    jsonWriter.endArray();
}

From source file:org.everit.osgi.webconsole.configuration.ConfigServlet.java

private void printServiceSuggestions(final HttpServletResponse resp, final String configAdminPid,
        final String pid, final String attributeId, final String ldapQuery) {
    try {/*from  www  .j  av  a 2  s  .  co  m*/
        JSONWriter writer = new JSONWriter(resp.getWriter());
        List<ServiceSuggestion> suggestions;
        try {
            suggestions = configManager.getServiceSuggestions(configAdminPid, pid, attributeId, ldapQuery);
            writer.array();
            for (ServiceSuggestion suggestion : suggestions) {
                suggestion.toJSON(writer);
            }
            writer.endArray();
        } catch (InvalidSyntaxException e) {
            // resp.setStatus(403);
            writer.object();
            writer.key("error");
            writer.value("invalid query: " + e.getMessage());
            writer.endObject();
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.everit.osgi.webconsole.configuration.ConfigServlet.java

private void printSuccess(final HttpServletResponse resp) {
    resp.setContentType("application/json");
    try {//w w w.j  a  v  a 2s  .c om
        JSONWriter writer = new JSONWriter(resp.getWriter());
        writer.object();
        writer.key("status");
        writer.value("success");
        writer.endObject();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.gatein.management.rest.providers.JsonResourceProvider.java

@Override
public void writeTo(Resource resource, Class<?> type, Type genericType, Annotation[] annotations,
        MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
        throws IOException, WebApplicationException {
    PrintWriter printWriter = new PrintWriter(entityStream);
    try {/*from   ww w.  j  a  v  a 2  s  .  co  m*/
        JSONWriter writer = new JSONWriter(printWriter);
        writer.object().key("description").value(resource.getDescription());
        writer.key("children").array();
        for (Child child : resource.getChildren()) {
            writeChild(child, writer);
        }
        writer.endArray();
        if (resource.getOperations() != null) {
            writer.key("operations").array();
            for (Operation operation : resource.getOperations()) {
                writeOperation(operation, writer);
            }
            writer.endArray();
        }
        writer.endObject();

        printWriter.flush();
    } catch (JSONException e) {
        throw new IOException("Exception writing json result.", e);
    } finally {
        printWriter.close();
    }
}

From source file:org.araqne.confdb.file.Exporter.java

public void exportData(OutputStream os) throws IOException {
    if (os == null)
        throw new IllegalArgumentException("export output stream cannot be null");

    logger.debug("araqne confdb: start export data");
    db.lock();//from   w  w w  .j  a  v  a  2  s  .  co  m
    try {
        OutputStreamWriter writer = new OutputStreamWriter(os, Charset.forName("utf-8"));
        JSONWriter jw = new JSONWriter(writer);

        jw.object();

        jw.key("metadata");
        jw.object();
        jw.key("version").value(1);
        jw.key("date").value(sdf.format(new Date()));
        jw.endObject();

        jw.key("collections");
        jw.object();

        for (String name : db.getCollectionNames()) {
            ConfigCollection col = db.getCollection(name);

            // collection name
            jw.key(name);

            // typed doc list
            jw.array();

            jw.value("list");

            // doc list begin
            jw.array();

            ConfigIterator it = col.findAll();
            try {
                while (it.hasNext()) {
                    Object doc = it.next().getDocument();
                    jw.value(insertType(doc));
                }
            } finally {
                it.close();
            }

            // end of doc list
            jw.endArray();

            // end of typed list
            jw.endArray();
        }

        // end of collection
        jw.endObject();

        // end of master doc
        jw.endObject();
        writer.flush();
        logger.debug("araqne confdb: export complete");
    } catch (JSONException e) {
        throw new IOException(e);
    } finally {
        db.unlock();
    }
}

From source file:tap.formatter.JSONFormat.java

@Override
public void writeResult(TableIterator result, OutputStream output, TAPExecutionReport execReport, Thread thread)
        throws TAPException, IOException, InterruptedException {
    try {/*from  w w  w . j  av  a  2s .  c o  m*/

        // Prepare the output stream for JSON:
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(output));
        JSONWriter out = new JSONWriter(writer);

        // {
        out.object();

        // "metadata": [...]
        out.key("metadata");

        // Write metadata part:
        DBColumn[] columns = writeMetadata(result, out, execReport, thread);

        writer.flush();

        if (thread.isInterrupted())
            throw new InterruptedException();

        // "data": [...]
        out.key("data");

        // Write the data part:
        writeData(result, columns, out, execReport, thread);

        // }
        out.endObject();
        writer.flush();

    } catch (JSONException je) {
        throw new TAPException(je.getMessage(), je);
    }
}

From source file:charitypledge.Pledge.java

public void JsonImport() {

    try {//ww  w .  j  a  va 2  s  .  co  m
        InputStream foo = new FileInputStream(JSONFile);
        JSONTokener t = new JSONTokener(foo);
        JSONObject jsonObj = new JSONObject(t);
        foo.close();
        JSONArray jsonList = jsonObj.getJSONArray("contributors");
        for (int i = 0; i < jsonList.length(); i++) {
            // loop array
            JSONObject objects = jsonList.getJSONObject(i);
            String nameField = objects.getString("name");
            String typeField = objects.getString("charity");
            String contributionField = objects.getString("contribution");
            // Add row to jTable
            loadPledgeTable(nameField, typeField, contributionField);
        }
    } catch (FileNotFoundException e) {
        JSONWriter jsonWriter;
        try {
            jsonWriter = new JSONWriter(new FileWriter(JSONFile));
            jsonWriter.object();
            jsonWriter.key("contributors");
            jsonWriter.array();
            jsonWriter.endArray();
            jsonWriter.endObject();
            //jsonWriter.close();
            tableRefresh();
        } catch (IOException f) {
            f.printStackTrace();
        } catch (JSONException g) {
            g.printStackTrace();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }
}