Example usage for org.json JSONWriter key

List of usage examples for org.json JSONWriter key

Introduction

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

Prototype

public JSONWriter key(String string) throws JSONException 

Source Link

Document

Append a key.

Usage

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

private void listConfigAdminServices(final Writer writer) {
    JSONWriter jsonWriter = new JSONWriter(writer);
    jsonWriter.array();//w w w  .  j  a va2 s  . c  om
    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 printServiceSuggestions(final HttpServletResponse resp, final String configAdminPid,
        final String pid, final String attributeId, final String ldapQuery) {
    try {//w  w w .j a va 2 s  .c om
        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 .  ja  va2  s.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:com.vaynberg.wicket.select2.TextChoiceProvider.java

@Override
public final void toJson(T choice, JSONWriter writer) throws JSONException {
    writer.key("id").value(getId(choice)).key("text").value(getDisplayText(choice));
}

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 {//w  w  w  . j  a v  a  2  s  .c om
        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.gatein.management.rest.providers.JsonResourceProvider.java

private void writeOperation(Operation operation, JSONWriter writer) throws IOException, JSONException {
    writer.object().key("operation-name").value(operation.getOperationName());
    writer.key("operation-description").value(operation.getOperationDescription());
    writeLink("link", operation.getOperationLink(), writer);
    writer.endObject();//from   w w w . j  a  v a2s  . co  m
}

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

private void writeChild(Child child, JSONWriter writer) throws IOException, JSONException {
    writer.object().key("name").value(child.getName());
    writer.key("description").value(child.getDescription());
    writeLink("link", child.getLink(), writer);
    writer.endObject();//from  w ww.  j  a v a2  s . c  o  m
}

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

private void writeLink(String name, Link link, JSONWriter writer) throws IOException, JSONException {
    writer.key(name).object();
    if (link.getRel() != null) {
        writer.key("rel").value(link.getRel());
    }/*from  w  ww.  j a  v a2 s .  c o m*/
    writer.key("href").value(link.getHref());

    if (link.getType() != null) {
        writer.key("type").value(link.getType());
    }
    if (link.getMethod() != null) {
        writer.key("method").value(link.getMethod());
    }
    writer.endObject();
}

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();// w  w w. j  a  v a2 s.  c  o 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 {/* w  w  w . ja v  a  2s.c  om*/

        // 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);
    }
}