Example usage for org.json JSONWriter array

List of usage examples for org.json JSONWriter array

Introduction

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

Prototype

public JSONWriter array() throws JSONException 

Source Link

Document

Begin appending a new array.

Usage

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

public void toJSON(final JSONWriter writer) {
    writer.object();//from   w  ww . java2  s.c  o 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:de.dekarlab.moneybuilder.model.parser.JsonBookSaver.java

/**
 * Save folder./* w  w w.j  a  va2  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. jav a  2 s.com*/
 * 
 * @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.//  w w  w . j  a  va  2 s . c o m
 * 
 * @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  w  w.  j a  va 2 s.com
 * 
 * @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.// www  .  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();
    }
}

From source file:uk.ac.horizon.ug.mrcreator.http.CRUDServlet.java

private void doList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    List results = null;//from w  ww. j  a  v  a  2s  .c  o m
    EntityManager em = EMF.get().createEntityManager();
    try {
        Class clazz = getObjectClass();
        Query q = null;
        if (!filterByCreator) {
            if (listFilterPropertyName == null) {
                q = em.createQuery("SELECT x FROM " + clazz.getSimpleName() + " x");
            } else {
                q = em.createQuery("SELECT x FROM " + clazz.getSimpleName() + " x WHERE x."
                        + listFilterPropertyName + " = :" + listFilterPropertyName);
                q.setParameter(listFilterPropertyName, listFilterPropertyValue);
            }
        } else {
            // filter
            String creator = getRequestCreator(req);
            String prop = getCreatorPropertyName();
            if (listFilterPropertyName == null) {
                q = em.createQuery(
                        "SELECT x FROM " + clazz.getSimpleName() + " x WHERE x." + prop + " = :creator");
                q.setParameter("creator", creator);
            } else {
                q = em.createQuery(
                        "SELECT x FROM " + clazz.getSimpleName() + " x WHERE x." + listFilterPropertyName
                                + " = :" + listFilterPropertyName + " AND x." + prop + " = :creator");
                q.setParameter(listFilterPropertyName, listFilterPropertyValue);
                q.setParameter("creator", creator);
            }
        }
        // TODO order?
        results = q.getResultList();
        // warning: lazy
        resp.setCharacterEncoding(ENCODING);
        resp.setContentType(JSON_MIME_TYPE);
        Writer w = new OutputStreamWriter(resp.getOutputStream(), ENCODING);
        JSONWriter jw = new JSONWriter(w);
        jw.array();
        for (Object o : results) {
            listObject(jw, o);
        }
        jw.endArray();
        w.close();
    } catch (Exception e) {
        logger.log(Level.WARNING, "Getting object of type " + getObjectClass(), e);
        resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString());
        return;
    } finally {
        em.close();
    }
}

From source file:util.CacheUtil.java

private static String genJsonConfigV127(Extra extra, List<Ration> rations) throws JSONException {
    JSONWriter jsonWriter = new JSONStringer();

    jsonWriter = jsonWriter.array();

    if (extra.getAdsOn() == 0) {
        jsonWriter = jsonWriter.object().key("empty_ration").value(100).endObject().object().key("empty_ration")
                .value("empty_ration").endObject().object().key("empty_ration").value(1).endObject();
    } else {/*www.  j  ava 2s  .c om*/
        jsonWriter = jsonWriter.object();
        double customWeight = 0;
        for (Ration ration : rations) {
            if (ration.getNName().equals("custom")) {
                customWeight += ration.getWeight();
                continue;
            }

            // Takes care of MdotM legacy support
            String rationName;
            if (ration.getType() == AdWhirlUtil.NETWORKS.MDOTM.ordinal()) {
                rationName = "adrollo";
            } else {
                rationName = ration.getNName();
            }

            jsonWriter = jsonWriter.key(rationName + "_ration").value(ration.getWeight());

        }

        if (customWeight != 0) {
            jsonWriter = jsonWriter.key("custom_ration").value(customWeight);
        }

        jsonWriter = jsonWriter.endObject();

        jsonWriter = jsonWriter.object();
        for (Ration ration : rations) {
            if (ration.getNName().equals("custom")) {
                continue;
            } else if (ration.getType() == AdWhirlUtil.NETWORKS.VIDEOEGG.ordinal()) {
                String[] temp = ration.getNetworkKey().split(AdWhirlUtil.KEY_SPLIT);

                jsonWriter = jsonWriter.key(ration.getNName() + "_key").object().key("publisher").value(temp[0])
                        .key("area").value(temp[1]).endObject();
            } else if (ration.getType() == AdWhirlUtil.NETWORKS.JUMPTAP.ordinal()) {
                String[] temp = ration.getNetworkKey().split(AdWhirlUtil.KEY_SPLIT);

                jsonWriter = jsonWriter.key(ration.getNName() + "_key").value(temp[0]);
            } else if (ration.getType() == AdWhirlUtil.NETWORKS.QUATTRO.ordinal()) {
                String[] temp = ration.getNetworkKey().split(AdWhirlUtil.KEY_SPLIT);

                jsonWriter = jsonWriter.key(ration.getNName() + "_key").object().key("siteID").value(temp[0])
                        .key("publisherID").value(temp[1]).endObject();
            } else if (ration.getType() == AdWhirlUtil.NETWORKS.MOBCLIX.ordinal()) {
                String[] temp = ration.getNetworkKey().split(AdWhirlUtil.KEY_SPLIT);

                if (temp.length == 2) {
                    jsonWriter = jsonWriter.key(ration.getNName() + "_key").object().key("appID").value(temp[0])
                            .key("adCode").value(temp[1]).endObject();
                } else {
                    jsonWriter = jsonWriter.key(ration.getNName() + "_key").object().key("appID").value(temp[0])
                            .endObject();
                }
            } else {

                // Takes care of MdotM legacy support
                String rationName;
                if (ration.getType() == AdWhirlUtil.NETWORKS.MDOTM.ordinal()) {
                    rationName = "adrollo";
                } else {
                    rationName = ration.getNName();
                }

                jsonWriter = jsonWriter.key(rationName + "_key").value(ration.getNetworkKey());
            }
        }

        if (customWeight != 0) {
            jsonWriter = jsonWriter.key("dontcare_key").value(customWeight);
        }
        jsonWriter = jsonWriter.endObject();

        jsonWriter = jsonWriter.object();
        int customPriority = Integer.MAX_VALUE;
        for (Ration ration : rations) {
            if (ration.getNName().equals("custom")) {
                if (customPriority > ration.getPriority()) {
                    customPriority = ration.getPriority();
                }
                continue;
            }

            // Takes care of MdotM legacy support
            String rationName;
            if (ration.getType() == AdWhirlUtil.NETWORKS.MDOTM.ordinal()) {
                rationName = "adwhirl_12";
            } else {
                rationName = ration.getNName();
            }

            jsonWriter = jsonWriter.key(rationName + "_priority").value(ration.getPriority());
        }
        if (customWeight != 0) {
            jsonWriter = jsonWriter.key("custom_priority").value(customPriority);
        }
        jsonWriter = jsonWriter.endObject();
    }

    jsonWriter = jsonWriter.object().key("background_color_rgb").object().key("red").value(extra.getBg_red())
            .key("green").value(extra.getBg_green()).key("blue").value(extra.getBg_blue()).key("alpha")
            .value(extra.getBg_alpha()).endObject().key("text_color_rgb").object().key("red")
            .value(extra.getFg_red()).key("green").value(extra.getFg_green()).key("blue")
            .value(extra.getFg_blue()).key("alpha").value(extra.getFg_alpha()).endObject()
            .key("refresh_interval").value(extra.getCycleTime()).key("location_on").value(extra.getLocationOn())
            .key("banner_animation_type").value(extra.getTransition()).key("fullscreen_wait_interval")
            .value(extra.getFullscreen_wait_interval()).key("fullscreen_max_ads")
            .value(extra.getFullscreen_max_ads()).key("metrics_url").value(extra.getMetrics_url())
            .key("metrics_flag").value(extra.getMetrics_flag()).endObject();

    return jsonWriter.endArray().toString();
}

From source file:util.CacheUtil.java

private static String genJsonConfigV103(Extra extra, List<Ration> rations) throws JSONException {
    JSONWriter jsonWriter = new JSONStringer();

    jsonWriter = jsonWriter.array();

    if (extra.getAdsOn() == 0) {
        jsonWriter = jsonWriter.object().key("empty_ration").value(100).endObject().object().key("empty_ration")
                .value("empty_ration").endObject().object().key("empty_ration").value(1).endObject();
    } else {/*from   w ww .  java  2 s .c om*/
        jsonWriter = jsonWriter.object();
        double customWeight = 0;
        for (Ration ration : rations) {
            if (ration.getNName().equals("custom")) {
                customWeight += ration.getWeight();
                continue;
            }

            // Takes care of MdotM legacy support
            String rationName;
            if (ration.getType() == AdWhirlUtil.NETWORKS.MDOTM.ordinal()) {
                rationName = "adrollo";
            } else {
                rationName = ration.getNName();
            }

            jsonWriter = jsonWriter.key(rationName + "_ration").value(ration.getWeight());

        }
        if (customWeight != 0) {
            jsonWriter = jsonWriter.key("custom_ration").value(customWeight);
        }
        jsonWriter = jsonWriter.endObject();

        jsonWriter = jsonWriter.object();
        for (Ration ration : rations) {
            if (ration.getNName().equals("custom")) {
                continue;
            } else if (ration.getType() == AdWhirlUtil.NETWORKS.VIDEOEGG.ordinal()) {
                String[] temp = ration.getNetworkKey().split(AdWhirlUtil.KEY_SPLIT);

                jsonWriter = jsonWriter.key(ration.getNName() + "_key").object().key("publisher").value(temp[0])
                        .key("area").value(temp[1]).endObject();
            } else if (ration.getType() == AdWhirlUtil.NETWORKS.JUMPTAP.ordinal()) {
                String[] temp = ration.getNetworkKey().split(AdWhirlUtil.KEY_SPLIT);

                jsonWriter = jsonWriter.key(ration.getNName() + "_key").value(temp[0]);
            } else if (ration.getType() == AdWhirlUtil.NETWORKS.QUATTRO.ordinal()) {
                String[] temp = ration.getNetworkKey().split(AdWhirlUtil.KEY_SPLIT);

                if (temp.length == 2) {
                    jsonWriter = jsonWriter.key(ration.getNName() + "_key").object().key("siteID")
                            .value(temp[0]).key("publisherID").value(temp[1]).endObject();
                } else {

                    jsonWriter = jsonWriter.object().key("appID").value(temp[0]).endObject();
                }

            } else {
                // Takes care of MdotM legacy support
                String rationName;
                if (ration.getType() == AdWhirlUtil.NETWORKS.MDOTM.ordinal()) {
                    rationName = "adrollo";
                } else {
                    rationName = ration.getNName();
                }

                jsonWriter = jsonWriter.key(rationName + "_key").value(ration.getNetworkKey());
            }
        }

        if (customWeight != 0) {
            jsonWriter = jsonWriter.key("dontcare_key").value(customWeight);
        }
        jsonWriter = jsonWriter.endObject();

        jsonWriter = jsonWriter.object();
        int customPriority = Integer.MAX_VALUE;
        for (Ration ration : rations) {
            if (ration.getNName().equals("custom")) {
                if (customPriority > ration.getPriority()) {
                    customPriority = ration.getPriority();
                }
                continue;
            }

            // Takes care of MdotM legacy support
            String rationName;
            if (ration.getType() == AdWhirlUtil.NETWORKS.MDOTM.ordinal()) {
                rationName = "adrollo";
            } else {
                rationName = ration.getNName();
            }

            jsonWriter = jsonWriter.key(rationName + "_priority").value(ration.getPriority());
        }
        if (customWeight != 0) {
            jsonWriter = jsonWriter.key("custom_priority").value(customPriority);
        }
        jsonWriter = jsonWriter.endObject();
    }

    jsonWriter = jsonWriter.object().key("background_color_rgb").object().key("red").value(extra.getBg_red())
            .key("green").value(extra.getBg_green()).key("blue").value(extra.getBg_blue()).key("alpha")
            .value(extra.getBg_alpha()).endObject().key("text_color_rgb").object().key("red")
            .value(extra.getFg_red()).key("green").value(extra.getFg_green()).key("blue")
            .value(extra.getFg_blue()).key("alpha").value(extra.getFg_alpha()).endObject()
            .key("refresh_interval").value(extra.getCycleTime()).key("location_on").value(extra.getLocationOn())
            .key("banner_animation_type").value(extra.getTransition()).key("fullscreen_wait_interval")
            .value(extra.getFullscreen_wait_interval()).key("fullscreen_max_ads")
            .value(extra.getFullscreen_max_ads()).key("metrics_url").value(extra.getMetrics_url())
            .key("metrics_flag").value(extra.getMetrics_flag()).endObject();

    return jsonWriter.endArray().toString();
}