Example usage for org.json JSONWriter object

List of usage examples for org.json JSONWriter object

Introduction

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

Prototype

public JSONWriter object() throws JSONException 

Source Link

Document

Begin appending a new object.

Usage

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

private void listConfigAdminServices(final Writer writer) {
    JSONWriter jsonWriter = new JSONWriter(writer);
    jsonWriter.array();//  ww  w.j a v a 2  s .com
    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 {//from ww w .  j a  v a2s.  c  o 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 {/*from w  w  w .j  a  v  a 2  s.c o  m*/
        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 {/* w ww. j a  va 2s  . 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.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();/*w ww  .j a v  a  2s.c  o 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   ww  w.  j av  a  2 s. c  o  m*/
}

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 ww.j a va2s. 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 {/*  ww  w . jav  a  2s .co  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:tap.formatter.JSONFormat.java

/**
 * Formats in JSON and writes the given {@link TAPColumn} in the given output.
 * // w  w  w.  ja v  a  2 s.  c  om
 * @param tapCol         The column metadata to format/write in JSON.
 * @param out            The stream in which the formatted column metadata must be written.
 * 
 * @throws IOException      If there is an error while writing the field metadata.
 * @throws JSONException   If there is an error while formatting something in JSON format.
 * @throws TAPException      If there is any other error (by default: never happen).
 */
protected void writeFieldMeta(TAPColumn tapCol, JSONWriter out)
        throws IOException, TAPException, JSONException {
    // {
    out.object();

    // "name": "..."
    out.key("name").value(tapCol.getADQLName());

    // "description": "..." (if any)
    if (tapCol.getDescription() != null && tapCol.getDescription().trim().length() > 0)
        out.key("description").value(tapCol.getDescription());

    // "datatype": "..."
    VotType votType = new VotType(tapCol.getDatatype());
    out.key("datatype").value(votType.datatype);

    // "arraysize": "..." (if any)
    if (votType.arraysize != null)
        out.key("arraysize").value(votType.arraysize);

    // "xtype": "..." (if any)
    if (votType.xtype != null)
        out.key("xtype").value(votType.xtype);

    // "unit": "..." (if any)
    if (tapCol.getUnit() != null && tapCol.getUnit().length() > 0)
        out.key("unit").value(tapCol.getUnit());

    // "ucd": "..." (if any)
    if (tapCol.getUcd() != null && tapCol.getUcd().length() > 0)
        out.key("ucd").value(tapCol.getUcd());

    // "utype": "..." (if any)
    if (tapCol.getUtype() != null && tapCol.getUtype().length() > 0)
        out.key("utype").value(tapCol.getUtype());

    // }
    out.endObject();
}

From source file:charitypledge.Pledge.java

public void JsonImport() {

    try {//w w  w .ja va2s . c  o  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();
    }
}