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:com.google.android.apps.muzei.util.IOUtil.java

public static JSONObject readJsonObject(String json) throws IOException, JSONException {
    JSONTokener tokener = new JSONTokener(json);
    Object val = tokener.nextValue();
    if (!(val instanceof JSONObject)) {
        throw new JSONException("Expected JSON object.");
    }//from w w w  .j  a  v a2  s.  com
    return (JSONObject) val;
}

From source file:com.google.blockly.android.control.BlocklyEvent.java

public static BlocklyEvent fromJson(JSONObject json) throws JSONException {
    String typename = json.getString(JSON_TYPE);
    switch (typename) {
    case TYPENAME_CHANGE:
        return new ChangeEvent(json);
    case TYPENAME_CREATE:
        return new CreateEvent(json);
    case TYPENAME_DELETE:
        return new DeleteEvent(json);
    case TYPENAME_MOVE:
        return new MoveEvent(json);
    case TYPENAME_UI:
        return new UIEvent(json);

    default:/* ww w  .  j a  va 2  s. com*/
        throw new JSONException("Unknown event type: " + typename);
    }
}

From source file:entities.JsonParser.java

public static Device createDeviceFromJson(JSONObject json) throws JSONException {
    if (json == null)
        throw new JSONException("Invalid input json");

    String id = (String) json.get("ID");
    String name = (String) json.get("NAME");
    String role = (String) json.get("ROLE");
    double lat = json.getDouble("LATITUDE");
    double lon = json.getDouble("LONGITUDE");
    double lux = json.getDouble("LUMINOSITY");
    double temperature = json.getDouble("TEMPERATURE");
    double[] acceleration = new double[3];

    JSONArray jArr = json.getJSONArray("ACCELERATION");
    if (jArr.length() != 3) {
        throw new JSONException("Invalid acceleration length");
    }//w  ww  .j a  v  a  2  s.  c om
    acceleration[0] = jArr.getDouble(0);
    acceleration[1] = jArr.getDouble(1);
    acceleration[2] = jArr.getDouble(2);

    return new Device(id, name, role, lat, lon, lux, temperature, acceleration);
}

From source file:entities.JsonParser.java

public static JSONObject createJsonFromDevice(Device device) throws JSONException {
    if (device == null)
        throw new JSONException("Invalid input device");
    JSONObject jsonDevice = new JSONObject();
    jsonDevice.put("ID", device.getMACAddress());
    jsonDevice.put("NAME", device.getName());
    jsonDevice.put("ROLE", device.getRole());
    jsonDevice.put("LATITUDE", device.getLatitude());
    jsonDevice.put("LONGITUDE", device.getLongitude());
    jsonDevice.put("LUMINOSITY", device.getLuminosity());
    jsonDevice.put("TEMPERATURE", device.getTemperature());
    jsonDevice.put("ACCELERATION", device.getAcceleration());
    return jsonDevice;
}

From source file:entities.JsonParser.java

public static Media createPictureMediaFromJson(JSONObject json) throws JSONException {
    if (json == null)
        throw new JSONException("Invalid input json");

    String filename = json.getString("PIC_NAME");
    String data = json.getString("PIC_DATA");

    return new Media(filename, Base64.decodeBase64(data));
}

From source file:entities.JsonParser.java

public static JSONObject createJsonFromPictureMedia(Media pic) throws JSONException {
    if (pic == null)
        throw new JSONException("Invalid audio media in input");

    JSONObject jsonPic = new JSONObject();
    jsonPic.put("PIC_NAME", pic.getFilename());
    jsonPic.put("PIC_DATA", Base64.encodeBase64String(pic.getData()));

    return jsonPic;
}

From source file:entities.JsonParser.java

public static Media createAudioMediaFromJson(JSONObject json) throws JSONException {
    if (json == null)
        throw new JSONException("Invalid input json");

    String filename = json.getString("AUDIO_NAME");
    String data = json.getString("AUDIO_DATA");

    return new Media(filename, Base64.decodeBase64(data));
}

From source file:entities.JsonParser.java

public static JSONObject createJsonFromAudioMedia(Media audio) throws JSONException {
    if (audio == null)
        throw new JSONException("Invalid audio media in input");

    JSONObject jsonAudio = new JSONObject();
    jsonAudio.put("AUDIO_NAME", audio.getFilename());
    jsonAudio.put("AUDIO_DATA", Base64.encodeBase64String(audio.getData()));

    return jsonAudio;
}

From source file:entities.JsonParser.java

public static TreasureStatus createTresureStatusFromJson(JSONObject json) throws JSONException {
    if (json == null)
        throw new JSONException("Invalid input json");

    Boolean status = Boolean.parseBoolean(json.getString("STATUS"));
    String filename = json.getString("PIC_NAME");
    String data = json.getString("PIC_DATA");

    Media winner = new Media(filename, Base64.decodeBase64(data));

    TreasureStatus t_status = new TreasureStatus(status);
    t_status.setWinner(winner);// w  w  w.j av a 2s  .co  m
    return t_status;

}

From source file:entities.JsonParser.java

public static JSONObject createJsonFromTreasureStatus(TreasureStatus status) throws JSONException {
    if (status == null)
        throw new JSONException("Invalid input status");

    JSONObject jsonEndgame = new JSONObject();
    Media winner = treasureStatus.getWinner();
    String filename = "", data = "";
    if (winner != null && winner.getData() != null) {
        filename = winner.getFilename();
        data = Base64.encodeBase64String(winner.getData());
    }//  w w w.  jav a  2  s.c om

    jsonEndgame.put("STATUS", treasureStatus.isFound());
    jsonEndgame.put("PIC_NAME", filename);
    jsonEndgame.put("PIC_DATA", data);

    return jsonEndgame;
}