Search in sources :

Example 1 with JSONPath

use of com.alibaba.fastjson.JSONPath in project fastjson by alibaba.

the class JSONPath_array_put method test_put_array_long.

public void test_put_array_long() throws Exception {
    Map<String, Object> root = new HashMap<String, Object>();
    root.put("values", new long[0]);
    JSONPath path = new JSONPath("$.values");
    path.arrayAdd(root, 123);
    long[] array = (long[]) root.get("values");
    Assert.assertEquals(1, array.length);
    Assert.assertEquals(123, array[0]);
}
Also used : JSONPath(com.alibaba.fastjson.JSONPath) HashMap(java.util.HashMap)

Example 2 with JSONPath

use of com.alibaba.fastjson.JSONPath in project fastjson by alibaba.

the class JSONPath_array_put method test_set.

public void test_set() throws Exception {
    List<int[]> list = new ArrayList<int[]>();
    list.add(new int[0]);
    list.add(new int[0]);
    JSONPath path = new JSONPath("$[0]");
    path.arrayAdd(list, 123);
    Assert.assertEquals(1, list.get(0).length);
    Assert.assertEquals(123, ((int[]) list.get(0))[0]);
}
Also used : JSONPath(com.alibaba.fastjson.JSONPath) ArrayList(java.util.ArrayList)

Example 3 with JSONPath

use of com.alibaba.fastjson.JSONPath in project fastjson by alibaba.

the class JSONPath_array_put method test_put_array_error_0.

public void test_put_array_error_0() throws Exception {
    Exception error = null;
    try {
        JSONPath path = new JSONPath("$.values");
        path.arrayAdd(new Object(), 123);
    } catch (JSONException ex) {
        error = ex;
    }
    Assert.assertNotNull(error);
}
Also used : JSONPath(com.alibaba.fastjson.JSONPath) JSONException(com.alibaba.fastjson.JSONException) JSONException(com.alibaba.fastjson.JSONException)

Example 4 with JSONPath

use of com.alibaba.fastjson.JSONPath in project fastjson by alibaba.

the class JSONPath_array_put method test_set_2.

public void test_set_2() throws Exception {
    Object[] list = new Object[2];
    list[0] = new int[0];
    list[0] = new int[0];
    JSONPath path = new JSONPath("$[0]");
    path.arrayAdd(list, 123);
    Assert.assertEquals(1, ((int[]) list[0]).length);
    Assert.assertEquals(123, ((int[]) list[0])[0]);
}
Also used : JSONPath(com.alibaba.fastjson.JSONPath)

Example 5 with JSONPath

use of com.alibaba.fastjson.JSONPath in project fastjson by alibaba.

the class JSONPath_array_put method test_put_array_int.

public void test_put_array_int() throws Exception {
    Map<String, Object> root = new HashMap<String, Object>();
    root.put("values", new int[0]);
    JSONPath path = new JSONPath("$.values");
    path.arrayAdd(root, 123);
    int[] array = (int[]) root.get("values");
    Assert.assertEquals(1, array.length);
    Assert.assertEquals(123, array[0]);
}
Also used : JSONPath(com.alibaba.fastjson.JSONPath) HashMap(java.util.HashMap)

Aggregations

JSONPath (com.alibaba.fastjson.JSONPath)96 List (java.util.List)77 ArrayList (java.util.ArrayList)76 HashMap (java.util.HashMap)6 JSONException (com.alibaba.fastjson.JSONException)2 Entity (com.alibaba.json.bvt.path.JSONPath_between_int.Entity)2 JSONObject (com.alibaba.fastjson.JSONObject)1 Collection (java.util.Collection)1 LinkedHashMap (java.util.LinkedHashMap)1