Example usage for org.json JSONWriter endArray

List of usage examples for org.json JSONWriter endArray

Introduction

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

Prototype

public JSONWriter endArray() throws JSONException 

Source Link

Document

End an array.

Usage

From source file:uk.org.rivernile.edinburghbustracker.server.livedata.LiveBusStopData.java

/**
 * Get all of the bus stop information available in this object and output
 * it in JSON format to the supplied Writer stream.
 *
 * @param out The stream to write the JSON text to.
 *///  ww  w. ja v  a  2  s.c o m
public void writeJSONToStream(final Writer out) {
    if (out == null)
        throw new IllegalArgumentException("The Writer object" + " cannot be null.");

    JSONWriter jw = new JSONWriter(out);
    try {
        jw.object().key("stopCode").value(thisStopCode).key("stopName").value(thisStopName).key("services")
                .array();
        for (BusService s : busServices) {
            jw.object().key("serviceName").value(s.getServiceName()).key("route").value(s.getRoute())
                    .key("buses").array();
            for (LiveBus b : s.buses) {
                jw.object().key("destination").value(b.getDestination()).key("arrivalTime")
                        .value(b.getArrivalTime()).key("accessible").value(b.getAccessible()).endObject();
            }
            jw.endArray().endObject();
        }
        jw.endArray().endObject();
    } catch (JSONException e) {
        System.err.println("A JSON exception has occurred. The exception " + "reported was:");
        System.err.println(e.toString());
    }
}

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

protected void writeEntityState(DefaultEntityState state, Writer writer, String version)
        throws EntityStoreException {
    try {//from   w ww.j a va2  s  .c o 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 valueToJSON(final JSONWriter writer) {
    writer.array();// w  w  w  . ja  va  2 s  .  c o  m
    for (String value : Optional.ofNullable(this.value).orElse(new String[0])) {
        if (type.equals("boolean")) {
            writer.value(value.equals("true"));
        } else {
            writer.value(value);
        }
    }
    writer.endArray();
}

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

public void toJSON(final JSONWriter writer) {
    writer.object();//from   ww  w . j a  v a 2  s . co m
    writer.key("serviceClass");
    writer.value(serviceClass);
    writer.key("id");
    writer.value(serviceId);
    writer.key("properties");
    writer.array();
    for (String key : serviceProperties.keySet()) {
        writer.object();
        writer.key("key");
        writer.value(key);
        writer.key("value");
        writer.value(serviceProperties.get(key));
        writer.endObject();
    }
    writer.endArray();
    writer.endObject();
}

From source file:com.composum.osgi.core.test.httpinterface.TestExecutorServlet.java

/**
 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
 *///w  w  w  . ja v a2s .c o  m
@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp)
        throws ServletException, IOException {
    final List<Result> runTests = this.testExecutorService.runTests();
    final JSONWriter jsonWriter = new org.json.JSONWriter(resp.getWriter());
    try {
        JSONWriter tests = jsonWriter.object().key("tests").array();
        for (final Result result : runTests) {
            final String name = result.getDescription().getDisplayName();
            final boolean success = result.isSuccess();
            final Failure failure = result.getFailure();
            final String message = success ? "ok" : failure.getMessage();
            final String trace = success ? "" : failure.getTrace();
            tests = tests.object().key("name").value(name).key("success").value(success).key("message")
                    .value(message).key("trace").value(trace).endObject();
        }
        tests.endArray().endObject();
    } catch (final JSONException e) {
        throw new ServletException("error writing JSON response: " + e.getMessage(), e);
    }
}

From source file:de.dekarlab.moneybuilder.model.parser.JsonBookSaver.java

/**
 * Save folder.//from   w  w  w.j  a v a2 s .c o  m
 * 
 * @param account
 * @param jsonWriter
 */
protected static void saveAccount(Account account, JSONWriter jsonWriter) {
    jsonWriter.object();
    jsonWriter.key("name").value(account.getName());
    jsonWriter.key("type").value(account.getType());
    if (account instanceof Folder) {
        jsonWriter.key("folder").value("y");
    }
    if (account.isHidden()) {
        jsonWriter.key("hidden").value("y");
        if (account.getHiddenDate() != null) {
            jsonWriter.key("hiddenDate").value(Formatter.formatDate(account.getHiddenDate()));
        }
    }
    savePeriodBudget(account.getPeriodBudget(), jsonWriter);
    savePeriodValue(account.getPeriodValue(), jsonWriter);
    if (account instanceof Folder) {
        jsonWriter.key("list");
        jsonWriter.array();
        for (Account child : ((Folder) account).getList()) {
            saveAccount(child, jsonWriter);
        }
        jsonWriter.endArray();
    }
    jsonWriter.endObject();
}

From source file:de.dekarlab.moneybuilder.model.parser.JsonBookSaver.java

/**
 * Save period list./*from w  w  w  .  j  ava 2s .c  o m*/
 * 
 * @param periodList
 * @param jsonWriter
 */
protected static void savePeriodList(List<Period> periodList, JSONWriter jsonWriter) {
    jsonWriter.array();
    for (Period period : periodList) {
        jsonWriter.object();
        jsonWriter.key("id").value(period.getId());
        jsonWriter.key("date").value(Formatter.formatDate(period.getDate()));
        saveValue(period.getValue(), jsonWriter);
        saveTransactions(period.getTransactions(), jsonWriter);
        jsonWriter.endObject();
    }
    jsonWriter.endArray();
}

From source file:de.dekarlab.moneybuilder.model.parser.JsonBookSaver.java

/**
 * Save transactions.//from  w ww  .ja v a 2s  . c  om
 * 
 * @param trans
 * @param jsonWriter
 */
protected static void saveTransactions(List<Transaction> trans, JSONWriter jsonWriter) {
    if (trans.size() > 0) {
        jsonWriter.key("transactions");
        jsonWriter.array();
        for (Transaction tr : trans) {
            jsonWriter.object();
            jsonWriter.key("from").value(tr.getFrom().getAbsName());
            jsonWriter.key("to").value(tr.getTo().getAbsName());
            jsonWriter.key("description").value(tr.getDescription());
            jsonWriter.key("value").value(tr.getValue());
            jsonWriter.endObject();
        }
        jsonWriter.endArray();
    }
}

From source file:de.dekarlab.moneybuilder.model.parser.JsonBookSaver.java

/**
 * Save period budget.//from  w  ww  .  ja v a 2 s . c o  m
 * 
 * @param periodBudget
 * @param jsonWriter
 */
protected static void savePeriodBudget(Map<String, Double> periodBudget, JSONWriter jsonWriter) {
    if (!isEmptyBudget(periodBudget)) {
        jsonWriter.key("periodBudget");
        jsonWriter.array();
        Iterator<String> it = periodBudget.keySet().iterator();
        while (it.hasNext()) {
            String id = it.next();
            Double value = periodBudget.get(id);
            if (value.doubleValue() != 0.0) {
                jsonWriter.object();
                jsonWriter.key("periodId").value(id);
                jsonWriter.key("budget").value(value);
                jsonWriter.endObject();
            }
        }
        jsonWriter.endArray();
    }
}

From source file:de.dekarlab.moneybuilder.model.parser.JsonBookSaver.java

/**
 * Save period value.// w  w w . ja va 2s .c o  m
 * 
 * @param periodBudget
 * @param jsonWriter
 */
protected static void savePeriodValue(Map<String, Value> periodValue, JSONWriter jsonWriter) {
    if (!isEmpty(periodValue)) {
        jsonWriter.key("periodValue");
        jsonWriter.array();
        Iterator<String> it = periodValue.keySet().iterator();
        while (it.hasNext()) {
            String id = it.next();
            Value value = periodValue.get(id);
            if (!value.isEmpty()) {
                jsonWriter.object();
                jsonWriter.key("periodId").value(id);
                saveValue(value, jsonWriter);
                jsonWriter.endObject();
            }
        }
        jsonWriter.endArray();
    }
}