Example usage for org.json JSONWriter value

List of usage examples for org.json JSONWriter value

Introduction

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

Prototype

public JSONWriter value(Object object) throws JSONException 

Source Link

Document

Append an object value.

Usage

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

private void listConfigAdminServices(final Writer writer) {
    JSONWriter jsonWriter = new JSONWriter(writer);
    jsonWriter.array();/*from w w w  . java  2s  . 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 printServiceSuggestions(final HttpServletResponse resp, final String configAdminPid,
        final String pid, final String attributeId, final String ldapQuery) {
    try {// w w w. jav a 2s  .  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 {/*from w  w  w  .j  av  a2  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: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 ww  w  .j av 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

/**
 * <p>Writes the given field value in JSON and into the given output.</p>
 * /*from w ww .j a va  2 s  . com*/
 * <p><i>note: special numeric values NaN and Inf (double or float) will be written as NULL values.</i></p>
 * 
 * @param value            The value to write.
 * @param column         The corresponding column metadata.
 * @param out            The stream in which the field value must be written.
 * 
 * @throws IOException      If there is an error while writing the given field value in the given stream.
 * @throws TAPException      If there is any other error (by default: never happen).
 */
protected void writeFieldValue(final Object value, final DBColumn column, final JSONWriter out)
        throws IOException, TAPException, JSONException {
    if (value instanceof Double && (((Double) value).isNaN() || ((Double) value).isInfinite()))
        out.value((Object) null);
    else if (value instanceof Float && (((Float) value).isNaN() || ((Float) value).isInfinite()))
        out.value((Object) null);
    else
        out.value(value);
}

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

public void toJSON(final JSONWriter writer) {
    writer.object();//  ww w .  j a va 2  s . com
    writer.key("name");
    writer.value(getDisplayedName());
    writer.key("description");
    writer.value(description);
    writer.key("bundleName");
    writer.value(bundleName);
    writer.key("location");
    writer.value(location);
    writer.key("pid");
    writer.value(pid);
    writer.key("factoryPid");
    writer.value(factoryPid);
    writer.key("boundBundleName");
    writer.value(boundBundleName);
    writer.endObject();
}

From source file:org.qi4j.entitystore.sql.SQLEntityStoreMixin.java

protected void writeEntityState(DefaultEntityState state, Writer writer, String version)
        throws EntityStoreException {
    try {/*from  ww w . j a  v  a  2  s . co  m*/
        JSONWriter json = new JSONWriter(writer);
        JSONWriter properties = json.object().key("identity").value(state.identity().identity())
                .key("application_version").value(application.version()).key("type")
                .value(state.entityDescriptor().entityType().type().name()).key("version").value(version)
                .key("modified").value(state.lastModified()).key("properties").object();
        EntityType entityType = state.entityDescriptor().entityType();
        for (PropertyType propertyType : entityType.properties()) {
            Object value = state.properties().get(propertyType.qualifiedName());
            json.key(propertyType.qualifiedName().name());
            if (value == null) {
                json.value(null);
            } else {
                propertyType.type().toJSON(value, json);
            }
        }

        JSONWriter associations = properties.endObject().key("associations").object();
        for (Map.Entry<QualifiedName, EntityReference> stateNameEntityReferenceEntry : state.associations()
                .entrySet()) {
            EntityReference value = stateNameEntityReferenceEntry.getValue();
            associations.key(stateNameEntityReferenceEntry.getKey().name())
                    .value(value != null ? value.identity() : null);
        }

        JSONWriter manyAssociations = associations.endObject().key("manyassociations").object();
        for (Map.Entry<QualifiedName, List<EntityReference>> stateNameListEntry : state.manyAssociations()
                .entrySet()) {
            JSONWriter assocs = manyAssociations.key(stateNameListEntry.getKey().name()).array();
            for (EntityReference entityReference : stateNameListEntry.getValue()) {
                assocs.value(entityReference.identity());
            }
            assocs.endArray();
        }

        JSONWriter namedAssociations = associations.endObject().key("namedassociations").object();
        for (Map.Entry<QualifiedName, Map<String, EntityReference>> stateNameListEntry : state
                .namedAssociations().entrySet()) {
            JSONWriter assocs = namedAssociations.key(stateNameListEntry.getKey().name()).object();
            Map<String, EntityReference> value = stateNameListEntry.getValue();
            for (Map.Entry<String, EntityReference> entry : value.entrySet()) {
                assocs.key(entry.getKey()).value(entry.getValue());
            }
            assocs.endObject();
        }
        manyAssociations.endObject().endObject();
    } catch (JSONException e) {
        throw new EntityStoreException("Could not store EntityState", e);
    }
}

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

private void optionsToJSON(final JSONWriter writer) {
    if (!options.isEmpty()) {
        writer.key("options");
        writer.object();/*from www  . j  a va 2  s.co  m*/
        for (String key : options.keySet()) {
            writer.key(key);
            writer.value(options.get(key));
        }
        writer.endObject();
    }
}

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

public void toJSON(final JSONWriter writer) {
    writer.object();/*  www  . j  a  v  a  2 s  .c om*/
    writer.key("id");
    writer.value(id);
    writer.key("name");
    writer.value(name);
    writer.key("description");
    writer.value(description);
    writer.key("value");
    valueToJSON(writer);
    writer.key("type");
    typeToJSON(writer);
    writer.endObject();
}

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

private void typeToJSON(final JSONWriter writer) {
    writer.object();/*from  ww w  .ja va2s  .  c om*/
    writer.key("baseType");
    writer.value(type);
    writer.key("maxOccurences");
    if (maxOccurences == Integer.MIN_VALUE || maxOccurences == Integer.MAX_VALUE) {
        writer.value("unbound");
    } else {
        writer.value(maxOccurences);
    }
    optionsToJSON(writer);
    writer.endObject();
}