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:de.dekarlab.moneybuilder.model.parser.JsonBookSaver.java

/**
 * Save period value./*from  ww w.  ja  v  a 2 s . co  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:de.dekarlab.moneybuilder.model.parser.JsonBookSaver.java

/**
 * Value object.//from   w ww . ja v  a2  s.c  o  m
 * 
 * @param value
 * @param jsonWriter
 */
protected static void saveValue(Value value, JSONWriter jsonWriter) {
    if (value != null && !value.isEmpty()) {
        jsonWriter.key("value");
        jsonWriter.object();
        if (value.getStartOfPeriod() != 0.0) {
            jsonWriter.key("startOfPeriod").value(value.getStartOfPeriod());
        }
        if (value.getDebit() != 0.0) {
            jsonWriter.key("debit").value(value.getDebit());
        }
        if (value.getCredit() != 0.0) {
            jsonWriter.key("credit").value(value.getCredit());
        }
        if (value.getEndOfPeriod() != 0.0) {
            jsonWriter.key("endOfPeriod").value(value.getEndOfPeriod());
        }
        if (value.getControl() != 0.0) {
            jsonWriter.key("control").value(value.getControl());
        }
        if (value.isManual()) {
            jsonWriter.key("manual").value("y");
        }
        jsonWriter.endObject();
    }
}

From source file:org.apache.felix.webconsole.internal.deppack.DepPackServlet.java

private void jsonKeyVal(JSONWriter jsonWriter, String key, Object value) throws JSONException {
    if (key != null && value != null) {
        jsonWriter.object();
        jsonWriter.key("key");
        jsonWriter.value(key);//from   ww  w.j  av a2 s  .  c o  m
        jsonWriter.key("value");
        jsonWriter.value(value);
        jsonWriter.endObject();
    }
}

From source file:util.CacheUtil.java

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

    if (extra.getAdsOn() == 0) {
        return jsonWriter.object().key("rations").array().endArray().endObject().toString();
    }/* w  w w . j  av a2s  . co  m*/

    jsonWriter = jsonWriter.object().key("extra").object().key("location_on").value(extra.getLocationOn())
            .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("cycle_time")
            .value(extra.getCycleTime()).key("transition").value(extra.getTransition()).endObject();

    jsonWriter = jsonWriter.key("rations").array();

    for (Ration ration : rations) {
        jsonWriter = jsonWriter.object().key("nid").value(ration.getNid()).key("type").value(ration.getType())
                .key("nname").value(ration.getNName()).key("weight").value(ration.getWeight()).key("priority")
                .value(ration.getPriority()).key("key");

        if (ration.getType() == AdWhirlUtil.NETWORKS.VIDEOEGG.ordinal()) {
            String[] temp = ration.getNetworkKey().split(AdWhirlUtil.KEY_SPLIT);

            jsonWriter = jsonWriter.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);

            if (temp.length == 3) {
                jsonWriter = jsonWriter.object().key("publisherID").value(temp[0]).key("siteID").value(temp[1])
                        .key("spotID").value(temp[2]).endObject();
            } else if (temp.length == 2) {
                jsonWriter = jsonWriter.object().key("publisherID").value(temp[0]).key("siteID").value(temp[1])
                        .endObject();
            } else {
                jsonWriter = jsonWriter.object().key("publisherID").value(temp[0]).endObject();
            }
        } else if (ration.getType() == AdWhirlUtil.NETWORKS.QUATTRO.ordinal()) {
            String[] temp = ration.getNetworkKey().split(AdWhirlUtil.KEY_SPLIT);

            jsonWriter = jsonWriter.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.object().key("appID").value(temp[0]).key("adCode").value(temp[1])
                        .endObject();
            } else {
                jsonWriter = jsonWriter.object().key("appID").value(temp[0]).endObject();
            }
        } else {
            jsonWriter = jsonWriter.value(ration.getNetworkKey());
        }

        jsonWriter = jsonWriter.endObject();
    }

    jsonWriter = jsonWriter.endArray();

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

From source file:util.CacheUtil.java

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

    if (extra.getAdsOn() == 0) {
        return jsonWriter.object().key("rations").array().endArray().endObject().toString();
    }/*  w w w  . j a v  a 2 s .c om*/

    jsonWriter = jsonWriter.object().key("extra").object().key("location_on").value(extra.getLocationOn())
            .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("cycle_time")
            .value(extra.getCycleTime()).key("transition").value(extra.getTransition()).endObject();

    jsonWriter = jsonWriter.key("rations").array();

    for (Ration ration : rations) {
        jsonWriter = jsonWriter.object().key("nid").value(ration.getNid()).key("type").value(ration.getType())
                .key("nname").value(ration.getNName()).key("weight").value(ration.getWeight()).key("priority")
                .value(ration.getPriority()).key("key");

        if (ration.getType() == AdWhirlUtil.NETWORKS.VIDEOEGG.ordinal()) {
            String[] temp = ration.getNetworkKey().split(AdWhirlUtil.KEY_SPLIT);

            jsonWriter = jsonWriter.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.value(temp[0]);
        } else if (ration.getType() == AdWhirlUtil.NETWORKS.QUATTRO.ordinal()) {
            String[] temp = ration.getNetworkKey().split(AdWhirlUtil.KEY_SPLIT);

            jsonWriter = jsonWriter.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.object().key("appID").value(temp[0]).key("adCode").value(temp[1])
                        .endObject();
            } else {
                jsonWriter = jsonWriter.object().key("appID").value(temp[0]).endObject();
            }
        } else {
            jsonWriter = jsonWriter.value(ration.getNetworkKey());
        }

        jsonWriter = jsonWriter.endObject();
    }

    jsonWriter = jsonWriter.endArray();

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

From source file:util.CacheUtil.java

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

    jsonWriter = jsonWriter.array();//w  ww.j  a v a  2s  . c  o  m

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

    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 {
        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();
}

From source file:util.CacheUtil.java

private static String genJsonCustomV127(CustomAd customAd) throws JSONException {
    JSONWriter jsonWriter = new JSONStringer();

    int launch_type;

    String s_link_type = customAd.getLinkType();
    int link_type = -1;
    try {//from w  w w .j  a v  a 2 s  . com
        link_type = Integer.parseInt(s_link_type);
    } catch (NumberFormatException e) {
        link_type = 1;
    }

    if (customAd.getLaunchType().equals("")) {
        if (link_type == 2) {
            launch_type = 1;
        } else {
            launch_type = 2;
        }
    } else {
        String s_launch_type = customAd.getLaunchType();
        launch_type = Integer.parseInt(s_launch_type);
    }

    jsonWriter = jsonWriter.object().key("img_url").value(customAd.getImageLink()).key("redirect_url")
            .value(customAd.getLink()).key("metrics_url")
            .value("http://" + AdWhirlUtil.SERVER + "/exclick.php?nid=" + customAd.getNid()
                    + "&appid=$aid&type=9&appver=200")
            .key("metrics_url2").value("").key("ad_type").value(customAd.getType()).key("ad_text")
            .value(customAd.getDescription()).key("link_type").value(link_type).key("launch_type")
            .value(launch_type).key("subtext").value("").key("webview_animation_type").value(4).endObject();

    return jsonWriter.toString();
}

From source file:com.kise.kairosdb.core.groupby.FiscalCalendarGroubBy.java

@Override
public GroupByResult getGroupByResult(int id) {
    return new GroupByResult() {
        @Override//from ww  w  .  j av  a 2  s . co  m
        public String toJson() throws FormatterException {
            StringWriter stringWriter = new StringWriter();
            try {
                JSONWriter writer = new JSONWriter(stringWriter);
                FiscalPeriod p = periods.get(id);
                writer.object();
                writer.key("name").value("fiscal_period");
                writer.key("group").object();
                writer.key("name").value("FY" + p.getFiscalYear() + "-P" + p.getFiscalPeriod());
                writer.key("fiscal_year").value(p.getFiscalYear());
                writer.key("quarter").value("Q" + p.getQuarter());
                writer.key("fiscal_period").value(p.getFiscalPeriod());
                writer.endObject();
                writer.endObject();
            } catch (JSONException e) {
                throw new FormatterException(e);
            }

            return stringWriter.toString();
        }
    };
}

From source file:com.vaynberg.wicket.select2.AjaxSettings.java

void toJson(JSONWriter writer) throws JSONException {
    writer.object();
    Json.writeFunction(writer, "data", data);
    Json.writeObject(writer, "dataType", dataType);
    Json.writeObject(writer, "quietMillis", quietMillis);
    Json.writeFunction(writer, "results", results);
    Json.writeObject(writer, "url", url);
    Json.writeObject(writer, "traditional", traditional);
    writer.endObject();//www .j  a v  a  2 s  . c  o  m
}