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:net.zionsoft.obadiah.model.notification.PushNotificationHandler.java

private static boolean prepareForNewTranslation(Context context, NotificationCompat.Builder builder,
        String messageType, String messageAttrs) {
    try {//from ww  w.  ja v a2  s .  c  om
        final JSONObject jsonObject = new JSONObject(messageAttrs);
        final String translationName = jsonObject.getString("translationName");
        if (TextUtils.isEmpty(translationName)) {
            throw new JSONException("Malformed push message: " + messageAttrs);
        }

        final String contentText = context.getString(R.string.text_new_translation_available, translationName);
        builder.setContentIntent(PendingIntent.getActivity(context, 0,
                TranslationManagementActivity.newStartReorderToTopIntent(context, messageType),
                PendingIntent.FLAG_UPDATE_CURRENT))
                .setContentTitle(context.getString(R.string.text_new_translation)).setContentText(contentText)
                .setStyle(new NotificationCompat.BigTextStyle().bigText(contentText));
    } catch (Exception e) {
        Crashlytics.logException(e);
        return false;
    }

    return true;
}

From source file:com.link.hbhttpclient.JsonHttpResponseHandler.java

protected void handleSuccessJsonMessage(int statusCode, Object jsonResponse) {
    if (jsonResponse == null) {
        onFailure(new JSONException("json is null"), "json is null");
    }//w w w.  jav a2  s.  c o m
    if (jsonResponse instanceof JSONObject) {
        onSuccess(statusCode, (JSONObject) jsonResponse);
    } else if (jsonResponse instanceof JSONArray) {
        onSuccess(statusCode, (JSONArray) jsonResponse);
    } else {
        onFailure(new JSONException("Unexpected type " + jsonResponse.getClass().getName()), (JSONObject) null);
    }
}

From source file:ded.model.Diagram.java

/** Deserialize from 'o'. */
public Diagram(JSONObject o) throws JSONException {
    String type = o.getString("type");
    if (!type.equals(jsonType)) {
        throw new JSONException("unexpected file type: \"" + type + "\"");
    }/*w  w w . j a v  a 2s.  c  o  m*/

    int ver = (int) o.getLong("version");
    if (ver < 1) {
        throw new JSONException("Invalid file version: " + ver + ".  Valid version "
                + "numbers are and will always be positive.");
    } else if (ver > currentFileVersion) {
        throw new JSONException("The file has version " + ver
                + " but the largest version this program is capable of " + "reading is " + currentFileVersion
                + ".  You need to get " + "a later version of the program in order to read " + "this file.");
    }

    this.windowSize = AWTJSONUtil.dimensionFromJSON(o.getJSONObject("windowSize"));
    this.namedColors = makeDefaultColors();

    if (ver >= 3) {
        this.drawFileName = o.getBoolean("drawFileName");
    } else {
        this.drawFileName = true;
    }

    // Make the lists now; this is particularly useful for handling
    // older file formats.
    this.entities = new ArrayList<Entity>();
    this.inheritances = new ArrayList<Inheritance>();
    this.relations = new ArrayList<Relation>();

    // Map from serialized position to deserialized Entity.
    ArrayList<Entity> integerToEntity = new ArrayList<Entity>();

    // Entities.
    JSONArray a = o.getJSONArray("entities");
    for (int i = 0; i < a.length(); i++) {
        Entity e = new Entity(a.getJSONObject(i), ver);
        this.entities.add(e);
        integerToEntity.add(e);
    }

    if (ver >= 2) {
        // Map from serialized position to deserialized Inheritance.
        ArrayList<Inheritance> integerToInheritance = new ArrayList<Inheritance>();

        // Inheritances.
        a = o.getJSONArray("inheritances");
        for (int i = 0; i < a.length(); i++) {
            Inheritance inh = new Inheritance(a.getJSONObject(i), integerToEntity);
            this.inheritances.add(inh);
            integerToInheritance.add(inh);
        }

        // Relations.
        a = o.getJSONArray("relations");
        for (int i = 0; i < a.length(); i++) {
            Relation rel = new Relation(a.getJSONObject(i), integerToEntity, integerToInheritance, ver);
            this.relations.add(rel);
        }
    }
}

From source file:com.grillecube.common.utils.JSONHelper.java

/**
 * parse object to float/*  w  w w.  j  a  va2  s .  c o m*/
 * 
 * @see JSONArray getDouble(int index);
 **/
public static float jsonObjectToFloat(Object object) {
    try {
        if (object instanceof Number) {
            return (((Number) object).floatValue());
        }
        return (Float.parseFloat((String) object));
    } catch (Exception e) {
        throw new JSONException(object + " is not a number.");
    }
}

From source file:com.appjma.appdeployer.service.JSONHelper.java

private static void throwIfKeyIsNull(JSONObject json, String key) throws JSONException {
    if (json.isNull(key)) {
        throw new JSONException("Key \"" + key + "\" is null");
    }//from   w  w  w .jav a  2 s.  c  o  m
}

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

@Test
public void testSaveJsonErrors() {
    ScoresheetStore store = new ScoresheetStore(fileManager, jsonCodec, context);

    when(jsonCodec.toJson(any())).thenThrow(new JSONException(""));

    StoreResult result = store.save(new ScoresheetModel());

    assertFalse(result.success);/*ww w . j a  va2 s .c om*/
    assertEquals(JSONException.class, result.error.getClass());
}

From source file:org.cosmo.common.util.JSONList.java

public static byte[] valueToBytes(Object value) throws JSONException, IOException {
    if (value == null || value.equals(null)) {
        return Constants.NullByteMarker;
    }//  w  w  w. j av  a2  s  . c om
    // return raw bytes is value is byte[] - else use normal json string conversion
    if (value instanceof byte[]) {
        return (byte[]) value;
    }

    if (value instanceof long[]) {
        return Util.UTF8(longArrayString((long[]) value));
    }

    if (value instanceof JSONString) {
        Object o;
        try {
            o = ((JSONString) value).toJSONString();
        } catch (Exception e) {
            throw new JSONException(e);
        }
        if (o instanceof String) {
            return Util.UTF8(o);
        }
        throw new JSONException("Bad value from toJSONString: " + o);
    }
    if (value instanceof Number) {
        return Util.UTF8(JSONObject.numberToString((Number) value));
    }
    if (value instanceof Boolean || value instanceof JSONObject || value instanceof JSONArray) {
        return Util.UTF8(value);
    }
    if (value instanceof Map) {
        return Util.UTF8(new JSONObject((Map) value));
    }
    if (value instanceof JSONList) {
        return ((JSONList) value).getBytes().makeExact().bytes();
    }
    if (value instanceof List) {
        return new JSONList((List) value).getBytes().makeExact().bytes();
    }
    if (value instanceof Collection) {
        return Util.UTF8(new JSONArray((Collection) value));
    }
    if (value.getClass().isArray()) {
        return Util.UTF8(new JSONArray(value));
    }
    return Util.UTF8(JSONObject.quote(value.toString()));
}

From source file:com.facebook.config.FileJSONProvider.java

@Override
public JSONObject get() throws JSONException {
    try {// www .j a v a2  s .c o m
        return fileToJSON();
    } catch (IOException e) {
        throw new JSONException(e);
    }
}

From source file:org.apache.nifi.processors.ConvertJSONtoCSV.ConvertJSONtoCSV.java

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
    final String includeHeaders = context.getProperty(INCLUDE_HEADERS).getValue();

    FlowFile flowFile = session.get();//from  w  w  w  . ja  v  a 2  s  . c  o m
    if (flowFile == null) {
        return;
    }

    try {
        flowFile = session.write(flowFile, new StreamCallback() {
            @Override
            public void process(final InputStream inputStream, final OutputStream outputStream)
                    throws IOException {
                List<Map<String, String>> flatJson;
                try {
                    flatJson = JSONParser.parseJSON(IOUtils.toString(inputStream, "UTF-8"), getRemoveKeySet());
                    if (flatJson == null) {
                        throw new IOException(
                                "Unable to parse JSON file. Please check the file contains valid JSON structure");
                    }
                } catch (JSONException ex) {
                    throw new JSONException("Unable to parse as JSON appears to be malformed: " + ex);
                }
                outputStream.write(CSVGenerator.generateCSV(flatJson, delimiter, emptyFields, includeHeaders)
                        .toString().getBytes());
            }
        });

        session.transfer(flowFile, RELATIONSHIP_SUCCESS);
    } catch (ProcessException | JSONException ex) {
        getLogger().error("Error converting FlowFile to CSV due to {}", new Object[] { ex.getMessage() }, ex);
        session.transfer(flowFile, RELATIONSHIP_FAILURE);
    }
}

From source file:com.wallpaper.core.loopj.android.http.JsonHttpResponseHandler.java

@Override
protected void handleSuccessMessage(String responseBody) {
    super.handleSuccessMessage(responseBody);

    try {/*  ww w  .j  ava2 s. c  o m*/
        Object jsonResponse = parseResponse(responseBody);
        if (jsonResponse instanceof JSONObject) {
            onSuccess((JSONObject) jsonResponse);
        } else if (jsonResponse instanceof JSONArray) {
            onSuccess((JSONArray) jsonResponse);
        } else {
            throw new JSONException("Unexpected type " + jsonResponse.getClass().getName());
        }
    } catch (JSONException e) {
        onFailure(e, responseBody);
    }
}