Example usage for org.json JSONObject accumulate

List of usage examples for org.json JSONObject accumulate

Introduction

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

Prototype

public JSONObject accumulate(String key, Object value) throws JSONException 

Source Link

Document

Accumulate values under a key.

Usage

From source file:com.cartlc.tracker.server.DCPing.java

void sendCrashLine(TableCrash.CrashLine line) {
    try {/*from  ww  w.  j a  va2  s  .c om*/
        JSONObject jsonObject = new JSONObject();
        jsonObject.accumulate("tech_id", PrefHelper.getInstance().getTechID());
        jsonObject.accumulate("date", line.date);
        jsonObject.accumulate("code", line.code);
        jsonObject.accumulate("message", line.message);
        jsonObject.accumulate("trace", line.trace);
        String result = post(MESSAGE, jsonObject);
        if (result != null && Integer.parseInt(result) == 0) {
            TableCrash.getInstance().setUploaded(line);
        } else {
            Log.e(TAG, "Unable to send previously trapped message: " + line.message);
        }
    } catch (Exception ex) {
        Log.e(TAG, ex.getMessage());
    }
}

From source file:cz.cuni.mff.d3s.tools.perfdoc.server.measuring.MethodMeasurer.java

/**
 * Returns results saved in JSONObject that can be sent to end-user. This
 * result will contain measured results and its units. 
 *
 * @param list measured BenchmarkResults
 * @param valuesInWhichWasMeasured/*from  www  . j a va 2 s.c o m*/
 * @return
 */
private JSONObject processBenchmarkResults(double[] valuesInWhichWasMeasured) {
    JSONObject jsonResults = new JSONObject();

    List<Long> computedMeans = new ArrayList<>();
    List<Long> computedMedians = new ArrayList<>();
    for (BenchmarkResult br : results) {
        computedMeans.add(br.getStatistics().computeMean());
        computedMedians.add(br.getStatistics().computeMedian());
    }

    String units = MeasuringUtils.convertUnits(computedMeans, computedMedians);
    for (int i = 0; i < results.size(); i++) {
        jsonResults.accumulate("data",
                new Object[] { valuesInWhichWasMeasured[i], computedMeans.get(i), computedMedians.get(i) });
    }
    jsonResults.accumulate("units", units);

    return jsonResults;
}

From source file:org.official.json.JSONML.java

/**
 * Parse XML values and store them in a JSONArray.
 * @param x       The XMLTokener containing the source string.
 * @param arrayForm true if array form, false if object form.
 * @param ja      The JSONArray that is containing the current tag or null
 *     if we are at the outermost level.
 * @return A JSONArray if the value is the outermost tag, otherwise null.
 * @throws JSONException/* w  w  w  .j a  v  a  2  s  .c o  m*/
 */
private static Object parse(XMLTokener x, boolean arrayForm, JSONArray ja) throws JSONException {
    String attribute;
    char c;
    String closeTag = null;
    int i;
    JSONArray newja = null;
    JSONObject newjo = null;
    Object token;
    String tagName = null;

    // Test for and skip past these forms:
    //      <!-- ... -->
    //      <![  ... ]]>
    //      <!   ...   >
    //      <?   ...  ?>

    while (true) {
        if (!x.more()) {
            throw x.syntaxError("Bad XML");
        }
        token = x.nextContent();
        if (token == XML.LT) {
            token = x.nextToken();
            if (token instanceof Character) {
                if (token == XML.SLASH) {

                    // Close tag </

                    token = x.nextToken();
                    if (!(token instanceof String)) {
                        throw new JSONException("Expected a closing name instead of '" + token + "'.");
                    }
                    if (x.nextToken() != XML.GT) {
                        throw x.syntaxError("Misshaped close tag");
                    }
                    return token;
                } else if (token == XML.BANG) {

                    // <!

                    c = x.next();
                    if (c == '-') {
                        if (x.next() == '-') {
                            x.skipPast("-->");
                        } else {
                            x.back();
                        }
                    } else if (c == '[') {
                        token = x.nextToken();
                        if (token.equals("CDATA") && x.next() == '[') {
                            if (ja != null) {
                                ja.put(x.nextCDATA());
                            }
                        } else {
                            throw x.syntaxError("Expected 'CDATA['");
                        }
                    } else {
                        i = 1;
                        do {
                            token = x.nextMeta();
                            if (token == null) {
                                throw x.syntaxError("Missing '>' after '<!'.");
                            } else if (token == XML.LT) {
                                i += 1;
                            } else if (token == XML.GT) {
                                i -= 1;
                            }
                        } while (i > 0);
                    }
                } else if (token == XML.QUEST) {

                    // <?

                    x.skipPast("?>");
                } else {
                    throw x.syntaxError("Misshaped tag");
                }

                // Open tag <

            } else {
                if (!(token instanceof String)) {
                    throw x.syntaxError("Bad tagName '" + token + "'.");
                }
                tagName = (String) token;
                newja = new JSONArray();
                newjo = new JSONObject();
                if (arrayForm) {
                    newja.put(tagName);
                    if (ja != null) {
                        ja.put(newja);
                    }
                } else {
                    newjo.put("tagName", tagName);
                    if (ja != null) {
                        ja.put(newjo);
                    }
                }
                token = null;
                for (;;) {
                    if (token == null) {
                        token = x.nextToken();
                    }
                    if (token == null) {
                        throw x.syntaxError("Misshaped tag");
                    }
                    if (!(token instanceof String)) {
                        break;
                    }

                    // attribute = value

                    attribute = (String) token;
                    if (!arrayForm && ("tagName".equals(attribute) || "childNode".equals(attribute))) {
                        throw x.syntaxError("Reserved attribute.");
                    }
                    token = x.nextToken();
                    if (token == XML.EQ) {
                        token = x.nextToken();
                        if (!(token instanceof String)) {
                            throw x.syntaxError("Missing value");
                        }
                        newjo.accumulate(attribute, XML.stringToValue((String) token));
                        token = null;
                    } else {
                        newjo.accumulate(attribute, "");
                    }
                }
                if (arrayForm && newjo.length() > 0) {
                    newja.put(newjo);
                }

                // Empty tag <.../>

                if (token == XML.SLASH) {
                    if (x.nextToken() != XML.GT) {
                        throw x.syntaxError("Misshaped tag");
                    }
                    if (ja == null) {
                        if (arrayForm) {
                            return newja;
                        } else {
                            return newjo;
                        }
                    }

                    // Content, between <...> and </...>

                } else {
                    if (token != XML.GT) {
                        throw x.syntaxError("Misshaped tag");
                    }
                    closeTag = (String) parse(x, arrayForm, newja);
                    if (closeTag != null) {
                        if (!closeTag.equals(tagName)) {
                            throw x.syntaxError("Mismatched '" + tagName + "' and '" + closeTag + "'");
                        }
                        tagName = null;
                        if (!arrayForm && newja.length() > 0) {
                            newjo.put("childNodes", newja);
                        }
                        if (ja == null) {
                            if (arrayForm) {
                                return newja;
                            } else {
                                return newjo;
                            }
                        }
                    }
                }
            }
        } else {
            if (ja != null) {
                ja.put(token instanceof String ? XML.stringToValue((String) token) : token);
            }
        }
    }
}

From source file:com.galactogolf.serialization.JSONSerializer.java

public static JSONObject toJSON(LevelSet levelSet) throws JSONException {

    JSONObject levelSetJSON = new JSONObject();
    levelSetJSON.put(JSONTags.LEVEL_SET_NAME, levelSet.getName());
    levelSetJSON.put(JSONTags.LEVEL_SET_DESCRIPTION, levelSet.getDescription());
    levelSetJSON.put(JSONTags.LEVEL_SET_ID, levelSet.getId());

    Iterator<LevelDefinition> iter = levelSet.getAllLevels().iterator();

    boolean first = true;
    while (iter.hasNext()) {
        LevelDefinition level = iter.next();
        JSONObject levelJSON = JSONSerializer.toJSON(level);
        if (first) {
            JSONArray array = new JSONArray();
            array.put(levelJSON);/*  w  ww  . j av a2  s .c o  m*/
            levelSetJSON.put(JSONTags.LEVEL_SET_LEVELS, array);
            first = false;
        } else {
            levelSetJSON.accumulate(JSONTags.LEVEL_SET_LEVELS, levelJSON);
        }

    }

    return levelSetJSON;
}

From source file:com.galactogolf.serialization.JSONSerializer.java

private static JSONObject toJSON(LevelDefinition level) throws JSONException {

    JSONObject levelJSON = new JSONObject();
    levelJSON.put(JSONTags.LEVEL_DEFINITION_NAME, level.getName());
    levelJSON.put(JSONTags.LEVEL_DEFINITION_PAR, level.getPar());
    levelJSON.put(JSONTags.LEVEL_DEFINITION_DESCRIPTION, level.getDescription());

    Iterator<EntityDefinition> iter = level.GetNPCDefinitions().iterator();

    boolean first = true;
    while (iter.hasNext()) {
        EntityDefinition entity = iter.next();
        JSONObject npcJSON = JSONSerializer.toJSON(entity);
        if (first) {
            JSONArray array = new JSONArray();
            array.put(npcJSON);//from  w ww. j  a v  a2 s.  co m
            levelJSON.put(JSONTags.LEVEL_DEFINITION_NPCS, array);
            first = false;
        } else {
            levelJSON.accumulate(JSONTags.LEVEL_DEFINITION_NPCS, npcJSON);
        }
    }

    levelJSON.put(JSONTags.LEVEL_DEFINITION_PLAYER, JSONSerializer.toJSON(level.GetPlayerDefinition()));

    return levelJSON;
}

From source file:com.galactogolf.serialization.JSONSerializer.java

private static JSONObject toJSON(EntityDefinition entity) throws JSONException {
    JSONObject entityJSON = new JSONObject();
    entityJSON.put(JSONTags.ENTITY_DEFINITION_NAME, entity.name);
    entityJSON.put(JSONTags.ENTITY_DEFINITION_TYPE, entity.type);

    JSONArray array = new JSONArray();
    array.put(JSONSerializer.toJSON(new float[] { entity.p1.x, entity.p1.y }));
    entityJSON.put(JSONTags.ENTITY_DEFINITION_POSITION, array);
    if (entity.p2 != null) {
        entityJSON.accumulate(JSONTags.ENTITY_DEFINITION_POSITION,
                JSONSerializer.toJSON(new float[] { entity.p2.x, entity.p2.y }));

    }/* w  w  w  .  ja  v  a2 s  . c o  m*/

    return entityJSON;
}