Example usage for org.json JSONStringer Object

List of usage examples for org.json JSONStringer Object

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public Object() 

Source Link

Document

Constructs a new object.

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);//from ww  w. j a v a2s  .  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 ww  .  j  a v  a 2s .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();//from ww  w . j a va2s. 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  ww w  . j ava2 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  . j a  v a 2 s.  com
        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 void createJSONString(Object value, ValueType type, JSONStringer stringer) throws JSONException {
    // TODO the sole purpose of this method is to get rid of "_type" information, which ValueType.toJSON
    // produces for value composites
    // So, change toJSON(...) to be configurable so that the caller can decide whether he wants type
    // information into json string or not
    if (type.isValue() || (type instanceof SerializableType && value instanceof ValueComposite)) {
        stringer.object();

        // Rest is partial copypasta from ValueCompositeType.toJSON(Object, JSONStringer)

        ValueComposite valueComposite = (ValueComposite) value;
        StateHolder state = valueComposite.state();
        final Map<QualifiedName, Object> values = new HashMap<QualifiedName, Object>();
        state.visitProperties(new StateHolder.StateVisitor<RuntimeException>() {
            public void visitProperty(QualifiedName name, Object value) {
                values.put(name, value);
            }/* w  w  w.  j  a  v  a  2s . c om*/
        });

        List<PropertyType> actualTypes = type.types();
        for (PropertyType propertyType : actualTypes) {
            stringer.key(propertyType.qualifiedName().name());

            Object propertyValue = values.get(propertyType.qualifiedName());
            if (propertyValue == null) {
                stringer.value(null);
            } else {
                this.createJSONString(propertyValue, propertyType.type(), stringer);
            }
        }
        stringer.endObject();
    } else {
        type.toJSON(value, stringer);
    }
}

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

public static void // modifies json_out
        toJson(Object object, JSONStringer json_out) throws Exception {
    json_out.object();
    // for each inherited class...
    for (Class c = object.getClass(); c != Object.class; c = c.getSuperclass()) {
        // for each member variable... 
        Field[] fields = c.getDeclaredFields();
        for (Field f : fields) {
            // if variable is static/private... skip it
            if (Modifier.isStatic(f.getModifiers())) {
                continue;
            }//w  w  w  .java 2s . c om
            if (Modifier.isPrivate(f.getModifiers())) {
                continue;
            }
            Object value = f.get(object);

            // if variable is a complex type... recurse on sub-objects
            if (value instanceof JsonAble) {
                json_out.key(f.getName());
                ((JsonAble) value).toJson(json_out);
                // if variable is an array... recurse on sub-objects
            } else if (value instanceof ArrayList) {
                json_out.key(f.getName());
                json_out.array();
                for (Object e : (ArrayList) value) {
                    toJson(e, json_out);
                }
                json_out.endArray();
                // if variable is a simple type... convert to json
            } else {
                json_out.key(f.getName()).value(value);
            }
        }
    }
    json_out.endObject();
}

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

/**
 * Writes MIDI device info into the specified JSON stringer.
 *
 * @param json//  ww w .j a  va  2 s. c  o m
 *            The JSON stringer.
 * @param info
 *            The MIDI device info.
 * @throws JSONException
 *             When JSON output fails.
 */
private void deviceInfo(final JSONStringer json, final Info info) throws JSONException {
    json.object();
    json.key("name").value(info.getName());
    json.key("description").value(info.getDescription());
    json.key("vendor").value(info.getVendor());
    json.key("version").value(info.getVersion());
    json.endObject();
}

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