Example usage for org.json JSONStringer JSONStringer

List of usage examples for org.json JSONStringer JSONStringer

Introduction

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

Prototype

public JSONStringer() 

Source Link

Document

Make a fresh JSONStringer.

Usage

From source file:util.CacheUtil.java

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

    jsonWriter = jsonWriter.array();//from   w w  w.j  a  va 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 {/*www  .  j  a v a2  s.  c  o  m*/
        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:pl.pwr.guide.interior.model.URLHistory.java

public void save(Writer out) throws JSONException, IOException {
    JSONStringer json = new JSONStringer().object();

    for (HistoryItem h : spareCopy) {
        h.emit(json);/*from w ww  .  j a v  a  2 s.  com*/
    }

    out.write(json.endObject().toString());
}

From source file:de.ailis.midi4js.Midi4JS.java

/**
 * Returns an array of information objects representing the set of all MIDI
 * devices available on the system./*w ww . j a  v a 2  s  .  c om*/
 *
 * @return The array of information objects about all available MIDI
 *         devices.
 * @throws JSONException
 *             When JSON string could not be constructed.
 */
public String getMidiDeviceInfo() throws JSONException {
    final JSONStringer js = new JSONStringer();
    js.array();
    for (final Info info : MidiSystem.getMidiDeviceInfo()) {
        js.object();
        js.key("name").value(info.getName());
        js.key("description").value(info.getDescription());
        js.key("vendor").value(info.getVendor());
        js.key("version").value(info.getVersion());
        js.endObject();
    }
    js.endArray();
    return js.toString();
}

From source file:de.ailis.midi4js.Midi4JS.java

/**
 * Returns all currently open receivers.
 *
 * @param deviceHandle/*from  w w w .  j av  a 2  s  .  com*/
 *            The device handle.
 * @return All currently open receivers in form of a JSON-encoded string
 *         which describes an array of receiver handles.
 * @throws JSONException
 *             When JSON data could not be constructed.
 */
public String getReceivers(final int deviceHandle) throws JSONException {
    final MidiDevice device = resolveDeviceHandle(deviceHandle);
    final JSONStringer json = new JSONStringer();
    json.array();
    for (final Receiver receiver : device.getReceivers()) {
        json.value(System.identityHashCode(receiver));
    }
    json.endArray();
    return json.toString();
}

From source file:de.ailis.midi4js.Midi4JS.java

/**
 * Returns all currently open transmitters.
 *
 * @param deviceHandle/*from   w ww .jav a 2  s  .  co m*/
 *            The device handle.
 * @return All currently open transmitters in form of a JSON-encoded string
 *         which describes an array of transmitter handles.
 * @throws JSONException
 *             When JSON data could not be constructed.
 */
public String getTransmitters(final int deviceHandle) throws JSONException {
    final MidiDevice device = resolveDeviceHandle(deviceHandle);
    final JSONStringer json = new JSONStringer();
    json.array();
    for (final Transmitter transmitter : device.getTransmitters()) {
        json.value(System.identityHashCode(transmitter));
    }
    json.endArray();
    return json.toString();
}

From source file:de.ailis.midi4js.Midi4JS.java

/**
 * Returns the device info of the specified device.
 *
 * @param deviceHandle//from www . j  a  v a  2  s . com
 *            The device handle.
 * @return The device info as a JSON string.
 * @throws JSONException
 *             When JSON output fails.
 */
public String getMidiDeviceInfo(final int deviceHandle) throws JSONException {
    final MidiDevice device = resolveDeviceHandle(deviceHandle);
    final JSONStringer json = new JSONStringer();
    deviceInfo(json, device.getDeviceInfo());
    return json.toString();
}

From source file:census.couchdroid.CouchDatabase.java

/**
* Runs an ad-hoc view from an AdHocView object.  You probably won't use this much, unless
* you want to add filtering to the view (reverse, startkey, etc...)
*
* @param view//  ww  w . ja va2  s  .  c  o m
* @return
*/
public CouchViewResults adhoc(final CouchAdHocView view) {
    String adHocBody = "";
    try {
        adHocBody = new JSONStringer().object().key("map").value("\"" + view.getFunction() + "\"").endObject()
                .toString();
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    // Bugfix - include query string for adhoc views to support
    // additional view options (setLimit, etc)
    CouchResponse resp = session.post(name + "/_temp_view", adHocBody, view.getQueryString());
    if (resp.isOk()) {
        CouchViewResults results = new CouchViewResults(view, resp.getBodyAsJSONObject());
        results.setDatabase(this);
        return results;
    } else {
        //log.warn("Error executing view - " + resp.getErrorId() + " " + resp.getErrorReason());
    }
    return null;
}