Search in sources :

Example 81 with JSONPath

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

the class JSONPath_field_access_filter_like method test_list_like_extract.

public void test_list_like_extract() throws Exception {
    JSONPath path = new JSONPath("$[?(@.name like 'ljw2083')]");
    List<Entity> entities = new ArrayList<Entity>();
    entities.add(new Entity(1001, "ljw2083"));
    entities.add(new Entity(1002, "wenshao"));
    entities.add(new Entity(1003, null));
    entities.add(new Entity(null, null));
    List<Object> result = (List<Object>) path.eval(entities);
    Assert.assertEquals(1, result.size());
    Assert.assertSame(entities.get(0), result.get(0));
}
Also used : JSONPath(com.alibaba.fastjson.JSONPath) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList)

Example 82 with JSONPath

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

the class JSONPath_size method test_path_size_2.

public void test_path_size_2() throws Exception {
    List list = new ArrayList();
    list.add(new Entity(101, "kiki"));
    list.add(new Entity(102, "ljw2083"));
    list.add(new Entity(103, "ljw2083"));
    JSONObject root = new JSONObject();
    root.put("values", list);
    JSONPath path = JSONPath.compile("$.values");
    Assert.assertEquals(3, path.size(root));
}
Also used : Entity(com.alibaba.json.bvt.path.JSONPath_between_int.Entity) JSONPath(com.alibaba.fastjson.JSONPath) JSONObject(com.alibaba.fastjson.JSONObject) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList)

Example 83 with JSONPath

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

the class JSONPath_size method test_path_size.

public void test_path_size() throws Exception {
    JSONPath path = JSONPath.compile("$");
    Assert.assertEquals(-1, path.size(null));
}
Also used : JSONPath(com.alibaba.fastjson.JSONPath)

Example 84 with JSONPath

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

the class JSONPath_toString method test_toJSONString.

public void test_toJSONString() throws Exception {
    Model model = new Model();
    model.path = new JSONPath("$");
    String text = JSON.toJSONString(model);
    Assert.assertEquals("{\"path\":\"$\"}", text);
    JSON.parseObject(text, Model.class);
}
Also used : JSONPath(com.alibaba.fastjson.JSONPath)

Example 85 with JSONPath

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

the class JSONPath_list_range method test_range_step.

public void test_range_step() throws Exception {
    List list = new ArrayList();
    list.add(new Object());
    list.add(new Object());
    list.add(new Object());
    list.add(new Object());
    list.add(new Object());
    JSONPath path = new JSONPath("$[2:8:2]");
    List<Object> result = (List<Object>) path.eval(list);
    Assert.assertEquals(2, result.size());
    Assert.assertSame(list.get(2), result.get(0));
    Assert.assertSame(list.get(4), result.get(1));
}
Also used : JSONPath(com.alibaba.fastjson.JSONPath) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList)

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