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:hu.bme.iit.quiz.endpoint.PollEndpoint.java

private String createFollowersUpdateString(Integer presentationID) {
    JSONStringer creator = new JSONStringer();
    creator.object().key("type").value("FOLLOWERSUPDATE").key("followers").array();
    Map<String, User> users = quizzesInOngoingPhase.get(presentationID).getFollowers();
    for (String key : users.keySet()) {
        User user = users.get(key);// ww w  .j  a  v a 2s .c o  m
        creator.object().key("username").value(user.getLogin()).endObject();
    }
    creator.endArray().endObject();
    return creator.toString();
}

From source file:hu.bme.iit.quiz.endpoint.PollEndpoint.java

private String createQuizQuestionUpdateString(Integer presentationID, Integer questionID) {
    logger.debug("sendQuizQestion createQuizQuestionUpdateString start: questionID: " + questionID);
    JSONStringer creator = new JSONStringer();
    Question question = quizzesInOngoingPhase.get(presentationID).getQuestionData(questionID);
    creator.object().key("type").value("QUESTION").key("questionTitle").value(question.getQuestionTitle())
            .key("questionID").value(questionID).key("questionBody").value(question.getQuestionBody())
            .key("options").array();
    for (QuestionOption option : question.getQuestionContent().getQuestionOption()) {
        creator.object().key("optionBody").value(option.getQuestionOptionBody()).endObject();
    }//from   w  w w  . ja v a2 s  . c  o m
    creator.endArray().endObject();
    logger.debug("sendQuizQestion createQuizQuestionUpdateString finish");
    return creator.toString();
}

From source file:hu.bme.iit.quiz.endpoint.PollEndpoint.java

private String createUserAnswerUpdateString(Integer presentationID) {
    JSONStringer creator = new JSONStringer();
    QuizRoom quiz = quizzesInOngoingPhase.get(presentationID);
    creator.object().key("type").value("ANSWERUPDATE").key("answers").array();
    for (String secretkey : userAnswers.keySet()) {
        User user = EndpointUtil.getUserByKey(secretkey);
        creator.object().key("username").value(user.getLogin()).key("selected").value(
                userAnswers.get(secretkey)[quizzesInOngoingPhase.get(presentationID).getActualQuestion()])
                .endObject();//ww  w  .j  a v a  2s  .c  o m
    }
    creator.endArray().endObject();
    return creator.toString();
}

From source file:hu.bme.iit.quiz.endpoint.PollEndpoint.java

private String createQuizCorrectionString(List<Integer> corrects) {
    JSONStringer creator = new JSONStringer();
    creator.object().key("type").value("CORRECTION").key("corrects").array();
    for (Integer correct : corrects) {
        creator.object().key("optionID").value(correct).endObject();
    }//from   w ww .  j a v  a2 s.co m
    creator.endArray().endObject();
    return creator.toString();
}

From source file:org.sc.probro.servlets.RequestListServlet.java

public static String formatRequestsAsJSON(Request[] reqs) throws BrokerException {
    JSONStringer stringer = new JSONStringer();
    try {// w w  w  .  java 2  s  . c  om
        stringer.object();

        stringer.key("vals");
        BrokerData.stringJSONArray(stringer, reqs);

        stringer.endObject();
    } catch (JSONException e) {
        throw new BrokerException(e);
    }

    return stringer.toString();
}

From source file:org.qi4j.index.rdf.query.internal.RdfQueryParserImpl.java

private String createAndEscapeJSONString(Object value, PropertyReference<?> propertyRef) throws JSONException {
    ValueType type = ValueTypeFactory.instance().newValueType(value.getClass(), propertyRef.propertyType(),
            propertyRef.propertyDeclaringType());

    JSONStringer json = new JSONStringer();
    json.array();/*from  www. j  a  v  a  2s.c  om*/
    this.createJSONString(value, type, json);
    json.endArray();
    String result = json.toString();
    result = result.substring(1, result.length() - 1);

    result = this.escapeJSONString(result);

    return result;
}

From source file:com.yahoo.egads.data.JsonEncoder.java

public static String toJson(Object object) throws Exception {
    JSONStringer jsonOut = new JSONStringer();
    toJson(object, jsonOut);/*from  w  w w.ja v  a  2s . c  o m*/
    return jsonOut.toString();
}

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();
    }/*from ww  w  . ja 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);

            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();
    }/* www  .j av a  2 s .  com*/

    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();/*from  w  w w . j  a  v  a  2s.  com*/

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