Search in sources :

Example 6 with JsonPathException

use of com.jayway.jsonpath.JsonPathException in project JsonPath by jayway.

the class JsonOrgJsonProvider method getPropertyKeys.

@Override
public Collection<String> getPropertyKeys(Object obj) {
    JSONObject jsonObject = toJsonObject(obj);
    List<String> keys = new ArrayList<String>();
    try {
        for (int i = 0; i < jsonObject.names().length(); i++) {
            String key = (String) jsonObject.names().get(i);
            keys.add(key);
        }
        return keys;
    } catch (JSONException e) {
        throw new JsonPathException(e);
    }
}
Also used : JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) JsonPathException(com.jayway.jsonpath.JsonPathException)

Example 7 with JsonPathException

use of com.jayway.jsonpath.JsonPathException in project JsonPath by jayway.

the class JsonOrgJsonProvider method setProperty.

@Override
public void setProperty(Object obj, Object key, Object value) {
    try {
        if (isMap(obj))
            toJsonObject(obj).put(key.toString(), createJsonElement(value));
        else {
            JSONArray array = toJsonArray(obj);
            int index;
            if (key != null) {
                index = key instanceof Integer ? (Integer) key : Integer.parseInt(key.toString());
            } else {
                index = array.length();
            }
            if (index == array.length()) {
                array.put(createJsonElement(value));
            } else {
                array.put(index, createJsonElement(value));
            }
        }
    } catch (JSONException e) {
        throw new JsonPathException(e);
    }
}
Also used : JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) JsonPathException(com.jayway.jsonpath.JsonPathException)

Aggregations

JsonPathException (com.jayway.jsonpath.JsonPathException)7 JSONException (org.json.JSONException)4 JSONObject (org.json.JSONObject)3 JsonObject (com.google.gson.JsonObject)2 ArrayList (java.util.ArrayList)2 JSONArray (org.json.JSONArray)2 ReadContext (com.jayway.jsonpath.ReadContext)1 Parameter (com.jayway.jsonpath.internal.function.Parameter)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1