Example usage for org.json JSONException JSONException

List of usage examples for org.json JSONException JSONException

Introduction

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

Prototype

public JSONException(Throwable t) 

Source Link

Usage

From source file:uk.ac.imperial.presage2.web.export.ColumnDefinition.java

/**
 * Create a {@link ColumnDefinition} from a {@link JSONObject}.
 * /*w w  w.j  ava  2 s  .c o m*/
 * @param column
 * @param sources
 * @return
 * @throws JSONException
 */
static ColumnDefinition createColumn(JSONObject column, boolean timeSeries) throws JSONException {
    if (column.getString(JSON_TYPE_KEY).equalsIgnoreCase("ENV")) {
        return new EnvironmentPropertyColumn(column, timeSeries);
    } else if (column.getString(JSON_TYPE_KEY).equalsIgnoreCase("AGENT")) {
        return new AgentPropertyColumn(column, timeSeries);
    } else {
        throw new JSONException("Invalid column type: '" + column.getString(JSON_TYPE_KEY));
    }
}

From source file:com.github.jberkel.pay.me.model.SkuDetails.java

public SkuDetails(String jsonSkuDetails) throws JSONException {
    mJson = jsonSkuDetails;/*from  www.  java 2s  .co m*/
    JSONObject json = new JSONObject(mJson);
    mSku = json.optString(PRODUCT_ID);
    if (TextUtils.isEmpty(mSku)) {
        throw new JSONException("SKU cannot be empty");
    }
    mType = json.optString(TYPE);
    mPrice = json.optString(PRICE);
    mTitle = json.optString(TITLE);
    mDescription = json.optString(DESCRIPTION);
    mItemType = ItemType.fromString(mType);
}

From source file:de.badaix.snapcast.control.RPCRequest.java

JSONObject toJson() {
    JSONObject response = new JSONObject();
    try {/*from  w  w w  .  j a v a2s . c  o  m*/
        response.put("jsonrpc", "2.0");
        if (error != null)
            response.put("error", error);
        else if (result != null)
            response.put("result", result);
        else
            throw new JSONException("error and result are null");

        response.put("id", id);
        return response;
    } catch (JSONException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:de.badaix.snapcast.control.RPCRequest.java

void fromJson(JSONObject json) throws JSONException {
    id = json.getLong("id");
    if (json.has("error")) {
        error = json.getJSONObject("error");
        result = null;/* w  ww .  ja  va 2  s .  c  o  m*/
    } else if (json.has("result")) {
        result = json.getJSONObject("result");
        error = null;
    } else
        throw new JSONException("error and result are null");
}

From source file:org.steveleach.scoresheet.support.JsonCodec.java

/**
 * Populates a model from a JSON string representation.
 *
 * @param model//ww  w. j  av a 2  s .  c  o  m
 *          the model to load the data into
 * @param json
 * @throws JSONException
 */
public void fromJson(ScoresheetModel model, String json) throws JSONException {
    model.getEvents().clear();

    if ((json == null) || (json.length() == 0)) {
        throw new JSONException("No content provided for JSON decoder");
    }

    JSONObject root = new JSONObject(json);

    if (root.has("homeTeam")) {
        loadTeam(root.getJSONObject("homeTeam"), model.getHomeTeam());
        loadTeam(root.getJSONObject("awayTeam"), model.getAwayTeam());
    } else {
        model.getHomeTeam().setName(root.optString("homeTeamName", "Home"));
        model.getAwayTeam().setName(root.optString("awayTeamName", "Home"));
    }
    model.setGameLocation(root.optString("location", ""));

    model.setGameDateTime(getDate(root.optString("gameDate", "")));

    JSONArray jsonEvents = root.getJSONArray("events");
    for (int n = 0; n < jsonEvents.length(); n++) {
        JSONObject jsonEvent = (JSONObject) jsonEvents.get(n);
        String eventType = jsonEvent.getString("eventType");
        GameEvent event = null;
        if (eventType.equals("Goal")) {
            GoalEvent goal = new GoalEvent();
            goal.setAssist1(jsonEvent.optInt("assist1", 0));
            goal.setAssist2(jsonEvent.optInt("assist2", 0));
            event = goal;
        } else if (eventType.equals("Penalty")) {
            PenaltyEvent penalty = new PenaltyEvent();
            penalty.setMinutes(jsonEvent.optInt("minutes", 2));
            penalty.setPlusMins(jsonEvent.optInt("plusMins", 0));
            event = penalty;
        } else if (eventType.equals("Period end")) {
            event = new PeriodEndEvent();
        }
        event.setSubType(jsonEvent.getString("subType"));
        event.setPeriod(jsonEvent.getInt("period"));
        event.setGameTime(jsonEvent.getString("gameTime"));
        event.setTeam(jsonEvent.getString("team"));
        event.setPlayer(getInt(jsonEvent.get("player")));

        model.addEvent(event);
    }
}

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//from w  w  w.ja  v a2  s. co 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.pivotal.gemfire.tools.pulse.internal.controllers.PulseController.java

@RequestMapping(value = "/pulseUpdate", method = RequestMethod.POST)
public void getPulseUpdate(HttpServletRequest request, HttpServletResponse response) throws IOException {

    String pulseData = request.getParameter("pulseData");

    JSONObject responseMap = new JSONObject();

    JSONObject requestMap = null;//from w  ww.  j  a  v  a 2s  .c  o  m

    try {
        requestMap = new JSONObject(pulseData);
        Iterator<?> keys = requestMap.keys();

        // Execute Services
        while (keys.hasNext()) {
            String serviceName = keys.next().toString();
            try {
                PulseService pulseService = pulseServiceFactory.getPulseServiceInstance(serviceName);
                responseMap.put(serviceName, pulseService.execute(request));
            } catch (Exception serviceException) {
                LOGGER.warning("serviceException = " + serviceException.getMessage());
                responseMap.put(serviceName, NoDataJSON);
            }
        }

    } catch (JSONException eJSON) {
        LOGGER.logJSONError(new JSONException(eJSON),
                new String[] { "requestMap:" + ((requestMap == null) ? "" : requestMap) });
    } catch (Exception e) {
        if (LOGGER.fineEnabled()) {
            LOGGER.fine("Exception Occured : " + e.getMessage());
        }
    }

    // Create Response
    response.getOutputStream().write(responseMap.toString().getBytes());
}

From source file:ded.model.Entity.java

/** Return the value to which 'index' is mapped in 'integerToEntity'. */
public static Entity fromJSONRef(ArrayList<Entity> integerToEntity, long index) throws JSONException {
    if (0 <= index && index < integerToEntity.size()) {
        return integerToEntity.get((int) index);
    } else {/*from w  w  w  .  j ava  2 s . co  m*/
        throw new JSONException("invalid entity ref " + index);
    }
}

From source file:com.zotoh.core.util.JSONUte.java

/**
 * @param json/*from w w  w.  j  av  a  2 s .  com*/
 * @return
 * @throws JSONException
 */
public static JSONObject read(File json) throws JSONException {
    InputStream inp = null;
    try {
        inp = readStream(json);
        return new JSONObject(new JSONTokener(inp));
    } catch (IOException e) {
        throw new JSONException(e);
    } finally {
        close(inp);
    }
}

From source file:org.eclipse.orion.server.cf.utils.MagicJSONObject.java

public JSONObject putOnce(String key, Object value) throws JSONException {
    Object storedValue;/*ww w .  ja  v  a 2  s .  c o  m*/
    if (key != null && value != null) {
        if ((storedValue = this.opt(key)) != null) {
            if (!storedValue.equals(value)) // Throw if values are different
                throw new JSONException("Duplicate key \"" + key + "\"");
            else
                return this;
        }
        this.put(key, value);
    }
    return this;
}