Java 类com.alibaba.fastjson.JSONPath 实例源码

项目:GitHub    文件:JSONPath_deepScan_test2.java   
@SuppressWarnings({"unchecked" })
public void test_0() throws Exception {

    Root root = new Root();
    root.company = new Company();
    root.company.departs.add(new Department(1001));
    root.company.departs.add(new Department(1002));
    root.company.departs.add(new Department(1003));


    List<Object> ids = (List<Object>) JSONPath.eval(root, "$..id");
    Assert.assertEquals(3, ids.size());
    Assert.assertEquals(1001, ids.get(0));
    Assert.assertEquals(1002, ids.get(1));
    Assert.assertEquals(1003, ids.get(2));
}
项目:GitHub    文件:JSONPath_paths_test4.java   
public void test_map() throws Exception {
    List<Object> list = new ArrayList<Object>();
    list.add(1001);
    list.add("wenshao");

    list.add(Collections.singletonMap("type", "emp"));

    Map<String, Object> paths = JSONPath.paths(list);

    Assert.assertEquals(5, paths.size());
    Assert.assertSame(list, paths.get("/"));
    Assert.assertEquals(1001, paths.get("/0"));
    Assert.assertEquals("wenshao", paths.get("/1"));
    Assert.assertSame(list.get(2), paths.get("/2"));
    Assert.assertSame(((Map)list.get(2)).get("type"), paths.get("/2/type"));
}
项目:GitHub    文件:DeepScanTest.java   
public void test_when_deep_scanning_illegal_property_access_is_ignored() {
    Object result = JSONPath.eval(
            JSON.parseObject("{\"x\": {\"foo\": {\"bar\": 4}}, \"y\": {\"foo\": 1}}")
            , "$..foo");
    assertEquals(2, ((List) result).size());

    result = JSONPath.eval(
            JSON.parseObject("{\"x\": {\"foo\": {\"bar\": 4}}, \"y\": {\"foo\": 1}}")
            , "$..foo.bar");
    assertEquals(1, ((List) result).size());
    assertEquals(4, ((List) result).get(0));

    result = JSONPath.eval(
            JSON.parseObject("{\"x\": {\"foo\": {\"bar\": 4}}, \"y\": {\"foo\": 1}}")
            , "$..[*].foo.bar");
    assertEquals(1, ((List) result).size());
    assertEquals(4, ((List) result).get(0));

    result = JSONPath.eval(
            JSON.parseObject("{\"x\": {\"foo\": {\"baz\": 4}}, \"y\": {\"foo\": 1}}")
            , "$..[*].foo.bar");
    assertTrue(((List) result).isEmpty());
}
项目:GitHub    文件:JSONPath_deepScan_test.java   
@SuppressWarnings({ "rawtypes", "unchecked" })
public void test_0() throws Exception {
    Map root = Collections.singletonMap("company", //
                                        Collections.singletonMap("departs", //
                                                                 Arrays.asList( //
                                                                                Collections.singletonMap("id",
                                                                                                         1001), //
                                                                                Collections.singletonMap("id",
                                                                                                         1002), //
                                                                                Collections.singletonMap("id", 1003) //
                                                                 ) //
                                        ));

    List<Object> ids = (List<Object>) JSONPath.eval(root, "$..id");
    Assert.assertEquals(3, ids.size());
    Assert.assertEquals(1001, ids.get(0));
    Assert.assertEquals(1002, ids.get(1));
    Assert.assertEquals(1003, ids.get(2));
}
项目:GitHub    文件:JSONPath_list_range.java   
public void test_range() 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());
    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:4]");
    List<Object> result = (List<Object>) path.eval(list);
    Assert.assertEquals(3, result.size());
    Assert.assertSame(list.get(2), result.get(0));
    Assert.assertSame(list.get(3), result.get(1));
    Assert.assertSame(list.get(4), result.get(2));
}
项目:GitHub    文件:JSONPath_calenar_test.java   
public void test_map() throws Exception {
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.YEAR, 2017);
    calendar.set(Calendar.MONTH, 6);
    calendar.set(Calendar.DAY_OF_MONTH, 30);

    calendar.set(Calendar.HOUR_OF_DAY, 16);
    calendar.set(Calendar.MINUTE, 8);
    calendar.set(Calendar.SECOND, 43);

    assertEquals(2017, JSONPath.eval(calendar, "/year"));
    assertEquals(6, JSONPath.eval(calendar, "/month"));
    assertEquals(30, JSONPath.eval(calendar, "/day"));

    assertEquals(16, JSONPath.eval(calendar, "/hour"));
    assertEquals(8, JSONPath.eval(calendar, "/minute"));
    assertEquals(43, JSONPath.eval(calendar, "/second"));


}
项目:GitHub    文件:JSONPath_field_access_filter_in_int.java   
public void test_list_not_in_null() throws Exception {
    JSONPath path = new JSONPath("[id not in (null)]");

    List<Entity> entities = new ArrayList<Entity>();
    entities.add(new Entity(1001, "ljw2083"));
    entities.add(new Entity(1002, "wenshao"));
    entities.add(new Entity(1003, "yakolee"));
    entities.add(new Entity(1004, null));

    List<Object> result = (List<Object>) path.eval(entities);
    Assert.assertEquals(4, result.size());
    Assert.assertSame(entities.get(0), result.get(0));
    Assert.assertSame(entities.get(1), result.get(1));
    Assert.assertSame(entities.get(2), result.get(2));
    Assert.assertSame(entities.get(3), result.get(3));
}
项目:GitHub    文件:Issue1445.java   
public void test_for_issue() throws Exception {

        JSONObject obj = new JSONObject();
        obj.put("data", new JSONObject());
        obj.getJSONObject("data").put("data", new JSONObject());
        obj.getJSONObject("data").getJSONObject("data").put("map", new JSONObject());
        obj.getJSONObject("data").getJSONObject("data").getJSONObject("map").put("21160001", "abc");

        String json = JSON.toJSONString(obj);
        assertEquals("abc", JSONPath.read(json,"data.data.map.21160001"));
    }
项目:GitHub    文件:Issue1177_3.java   
public void test_for_issue() throws Exception {
    String text = "[{\"x\":\"y\"},{\"x\":\"y\"}]";
    List<Model> jsonObject = JSONObject.parseObject(text, new TypeReference<List<Model>>(){});
    System.out.println(JSON.toJSONString(jsonObject));
    String jsonpath = "$..x";
    String value="y2";
    JSONPath.set(jsonObject, jsonpath, value);
    assertEquals("[{\"x\":\"y2\"},{\"x\":\"y2\"}]", JSON.toJSONString(jsonObject));

}
项目:GitHub    文件:JSONPath_field_access_filter_in_int.java   
public void test_list_in_3_null() throws Exception {
    JSONPath path = new JSONPath("[id in (1001, 1003, null)]");

    List<Entity> entities = new ArrayList<Entity>();
    entities.add(new Entity(1001, "ljw2083"));
    entities.add(new Entity(1002, "wenshao"));
    entities.add(new Entity(1003, "yakolee"));
    entities.add(new Entity(null, null));

    List<Object> result = (List<Object>) path.eval(entities);
    Assert.assertEquals(3, result.size());
    Assert.assertSame(entities.get(0), result.get(0));
    Assert.assertSame(entities.get(2), result.get(1));
    Assert.assertSame(entities.get(3), result.get(2));
}
项目:GitHub    文件:TestSpecial_1.java   
public void test_special() throws Exception {
    String x = "{\"10.0.0.1\":{\"region\":\"xxx\"}}";
    Object o = JSON.parse(x);
    Assert.assertTrue(JSONPath.contains(o, "$.10\\.0\\.0\\.1"));
    Assert.assertEquals("{\"region\":\"xxx\"}", JSONPath.eval(o, "$.10\\.0\\.0\\.1").toString());
    Assert.assertTrue(JSONPath.contains(o, "$.10\\.0\\.0\\.1.region"));
    Assert.assertEquals("xxx", JSONPath.eval(o, "$.10\\.0\\.0\\.1.region"));
}
项目:GitHub    文件:JSONPath_field_access_filter_in_int.java   
public void test_list_in_2() throws Exception {
    JSONPath path = new JSONPath("[id in (1001, 1003)]");

    List<Entity> entities = new ArrayList<Entity>();
    entities.add(new Entity(1001, "ljw2083"));
    entities.add(new Entity(1002, "wenshao"));
    entities.add(new Entity(1003, "yakolee"));
    entities.add(new Entity(1004, null));

    List<Object> result = (List<Object>) path.eval(entities);
    Assert.assertEquals(2, result.size());
    Assert.assertSame(entities.get(0), result.get(0));
    Assert.assertSame(entities.get(2), result.get(1));
}
项目:GitHub    文件:JSONPath_paths_test3.java   
public void test_map() throws Exception {
    Model model = new Model();
    model.id = 1001;
    model.name = "wenshao";
    model.attributes.put("type", "employee");

    Map<String, Object> paths = JSONPath.paths(model);

    Assert.assertEquals(5, paths.size());
    Assert.assertSame(model, paths.get("/"));
    Assert.assertEquals(1001, paths.get("/id"));
    Assert.assertEquals("wenshao", paths.get("/name"));
    Assert.assertSame(model.attributes, paths.get("/attributes"));
    Assert.assertEquals("employee", paths.get("/attributes/type"));
}
项目:GitHub    文件:JSONPath_paths_test1.java   
public void test_map() throws Exception {
    List<Object> list = new ArrayList<Object>();
    list.add(1001);
    list.add("wenshao");


    Map<String, Object> paths = JSONPath.paths(list);

    Assert.assertEquals(3, paths.size());
    Assert.assertSame(list, paths.get("/"));
    Assert.assertEquals(1001, paths.get("/0"));
    Assert.assertEquals("wenshao", paths.get("/1"));
}
项目:GitHub    文件:JSONPath_paths_test5.java   
public void test_array() throws Exception {
    String[] array = new String[]{"1001", "wenshao"};

    Map<String, Object> paths = JSONPath.paths(array);

    Assert.assertEquals(3, paths.size());
    Assert.assertSame(array, paths.get("/"));
    Assert.assertEquals("1001", paths.get("/0"));
    Assert.assertEquals("wenshao", paths.get("/1"));
}
项目:GitHub    文件:JSONPath_field_access_filter_like.java   
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));
}
项目:GitHub    文件:JSONPath_field_access_filter_like.java   
public void test_list_not_like_extract() throws Exception {
    JSONPath path = new JSONPath("$[?(@.name not 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, "yakolee"));
    entities.add(new Entity(null, null));

    List<Object> result = (List<Object>) path.eval(entities);
    Assert.assertEquals(3, result.size());
    Assert.assertSame(entities.get(1), result.get(0));
    Assert.assertSame(entities.get(2), result.get(1));
    Assert.assertSame(entities.get(3), result.get(2));
}
项目:GitHub    文件:JSONPath_field_access_filter_like.java   
public void test_list_like_left_match() throws Exception {
    JSONPath path = new JSONPath("$[?(@.name like 'ljw%')]");

    List<Entity> entities = new ArrayList<Entity>();
    entities.add(new Entity(1001, "ljw2083"));
    entities.add(new Entity(1002, "wenshao"));
    entities.add(new Entity(1003, "yakolee"));
    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));
}
项目:GitHub    文件:JSONPath_field_access_filter_like.java   
public void test_list_like_right_match() throws Exception {
    JSONPath path = new JSONPath("$[?(@.name like '%2083')]");

    List<Entity> entities = new ArrayList<Entity>();
    entities.add(new Entity(1001, "ljw2083"));
    entities.add(new Entity(1002, "wenshao"));
    entities.add(new Entity(1003, "yakolee"));
    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));
}
项目:GitHub    文件:JSONPath_field_access_filter_like.java   
public void test_list_like_contains() throws Exception {
    JSONPath path = new JSONPath("$[?(@.name like '%208%')]");

    List<Entity> entities = new ArrayList<Entity>();
    entities.add(new Entity(1001, "ljw2083"));
    entities.add(new Entity(1002, "wenshao"));
    entities.add(new Entity(1003, "yakolee"));
    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));
}
项目:GitHub    文件:JSONPath_field_access_filter_like_simple.java   
public void test_list_like_right_not_match() throws Exception {
    JSONPath path = new JSONPath("$[name not like '%2083']");

    List<Entity> entities = new ArrayList<Entity>();
    entities.add(new Entity(1001, "ljw2083"));
    entities.add(new Entity(1002, "wenshao"));
    entities.add(new Entity(1003, "yakolee"));
    entities.add(new Entity(null, null));

    List<Object> result = (List<Object>) path.eval(entities);
    Assert.assertEquals(2, result.size());
    Assert.assertSame(entities.get(1), result.get(0));
    Assert.assertSame(entities.get(2), result.get(1));
}
项目:GitHub    文件:JSONPath_field_access_filter_like.java   
public void test_list_like_match_two_segement_2() throws Exception {
    JSONPath path = new JSONPath("$[?(@.name like 'ljw%w2083')]");

    List<Entity> entities = new ArrayList<Entity>();
    entities.add(new Entity(1001, "ljw2083"));
    entities.add(new Entity(1002, "wenshao"));
    entities.add(new Entity(1003, "yakolee"));
    entities.add(new Entity(null, null));

    List<Object> result = (List<Object>) path.eval(entities);
    Assert.assertEquals(0, result.size());
}
项目:GitHub    文件:JSONPath_field_access_filter_rlike.java   
public void test_list_like_extract() throws Exception {
    JSONPath path = new JSONPath("$[name rlike '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));
}
项目:GitHub    文件:JSONPath_toString.java   
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);
}
项目:GitHub    文件:JSONPath_containsValue_biginteger.java   
public void test_root() throws Exception {
    Model model = new Model();
    model.value = new BigInteger("1001");

    Assert.assertTrue(JSONPath.containsValue(model, "/value", 1001));
    Assert.assertTrue(JSONPath.containsValue(model, "/value", 1001L));
    Assert.assertTrue(JSONPath.containsValue(model, "/value", (short) 1001));

    Assert.assertFalse(JSONPath.containsValue(model, "/value", 1002));
    Assert.assertFalse(JSONPath.containsValue(model, "/value", 1002L));
    Assert.assertFalse(JSONPath.containsValue(model, "/value", (short) 1002));
}
项目:GitHub    文件:JSONPath_1.java   
public void test_path_empty() throws Exception {
    Throwable error = null;
    try {
        JSONPath.compile("");
    } catch (JSONPathException ex) {
        error = ex;
    }
    Assert.assertNotNull(error);
}
项目:GitHub    文件:JSONPath_array_put.java   
public void test_put_array_error_1() throws Exception {
    Exception error = null;
    try {
        JSONPath path = new JSONPath("$.values");
        path.arrayAdd(Collections.singletonMap("values", new Object()), 123);
    } catch (JSONException ex) {
        error = ex;
    }
    Assert.assertNotNull(error);
}
项目:GitHub    文件:JSONPath_list_size_3.java   
public void test_java_bean_field_null() throws Exception {
    Model model = new Model();
    model.id = 1001;
    model.name = null;
    JSONPath path = new JSONPath("$.size()");
    Integer result = (Integer) path.eval(model);
    Assert.assertEquals(1, result.intValue());
}
项目:GitHub    文件:JSONPath_field_access_filter_compare_int.java   
public void test_list_map_le() throws Exception {
    JSONPath path = new JSONPath("$[?(@.id <= 1002)]");

    List<Object> result = (List<Object>) path.eval(entities);
    Assert.assertEquals(2, result.size());
    Assert.assertSame(entities.get(0), result.get(0));
    Assert.assertSame(entities.get(1), result.get(1));
}
项目:GitHub    文件:JSONPath_field_access_filter_compare_string.java   
public void test_list_eq() throws Exception {
    JSONPath path = new JSONPath("$[?(@.name = '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));
}
项目:GitHub    文件:JSONPath_field_access_filter_rlike.java   
public void test_list_not_like_extract() throws Exception {
    JSONPath path = new JSONPath("$[name not rlike 'wenshao']");

    List<Entity> entities = new ArrayList<Entity>();
    entities.add(new Entity(1001, "ljw2083"));
    entities.add(new Entity(1002, "wenshao"));
    entities.add(new Entity(1003, "yakolee"));
    entities.add(new Entity(null, null));

    List<Object> result = (List<Object>) path.eval(entities);
    Assert.assertEquals(2, result.size());
    Assert.assertSame(entities.get(0), result.get(0));
    Assert.assertSame(entities.get(2), result.get(1));
}
项目:GitHub    文件:JSONPath_field_access_filter_compare_string.java   
public void test_list_eq_null() throws Exception {
    JSONPath path = new JSONPath("$[?(@.name = null)]");

    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(2, result.size());
    Assert.assertSame(entities.get(2), result.get(0));
    Assert.assertSame(entities.get(3), result.get(1));
}
项目:GitHub    文件:JSONPath_field_access_filter_compare_string.java   
public void test_list_not_null() throws Exception {
    JSONPath path = new JSONPath("$[?(@.name != null)]");

    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(2, result.size());
    Assert.assertSame(entities.get(0), result.get(0));
    Assert.assertSame(entities.get(1), result.get(1));
}
项目:GitHub    文件:JSONPath_set_test6.java   
public void test_jsonpath_1() throws Exception {
    JSONObject aa= new JSONObject();
    aa.put("app-a", "haj ");
    JSONPath.set(aa, "$.app\\-a\\.x", "123");
    assertEquals("haj ", aa.getString("app-a"));
    assertEquals("123", aa.getString("app-a.x"));
}
项目:GitHub    文件:JSONPath_list.java   
public void test_list_map() throws Exception {
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("val", new Object());
    List list = new ArrayList();
    list.add(map);
    Assert.assertSame(map.get("val"), new JSONPath("$[0].val").eval(list));
    Assert.assertSame(map.get("val"), new JSONPath("$[-1].val").eval(list));
}
项目:GitHub    文件:JSONPath_set_test2.java   
public void test_jsonpath() throws Exception {
    JSONObject rootObject = JSON.parseObject("{\"array\":[{},{},{},{}]}");
    JSONPath.set(rootObject, "$.array[0:].key", "123");

    JSONArray array = rootObject.getJSONArray("array");
    for (int i = 0; i < array.size(); ++i) {
        Assert.assertEquals("123", array.getJSONObject(i).get("key"));
    }
    System.out.println(rootObject);
}
项目:GitHub    文件:JSONPath_between_int.java   
public void test_between_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"));
    List<Object> result = (List<Object>) JSONPath.eval(list, "$[id between 101 and 102]");
    Assert.assertEquals(2, result.size());
    Assert.assertSame(list.get(0), result.get(0));
    Assert.assertSame(list.get(1), result.get(1));
}
项目:GitHub    文件:JSONPath_list_range.java   
public void test_range_2() 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());
    list.add(new Object());

    JSONPath path = new JSONPath("$[4:]");
    List<Object> result = (List<Object>) path.eval(list);
    Assert.assertEquals(2, result.size());
    Assert.assertSame(list.get(4), result.get(0));
    Assert.assertSame(list.get(5), result.get(1));
}
项目:GitHub    文件:JSONPath_field_access_filter_rlike.java   
public void test_list_like_right_match() throws Exception {
    JSONPath path = new JSONPath("$[?(@.name like '%2083')]");

    List<Entity> entities = new ArrayList<Entity>();
    entities.add(new Entity(1001, "ljw2083"));
    entities.add(new Entity(1002, "wenshao"));
    entities.add(new Entity(1003, "yakolee"));
    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));
}
项目:GitHub    文件:JSONPath_field_access_filter_in_string.java   
public void test_list_in() throws Exception {
    JSONPath path = new JSONPath("[name in ('ljw2083')]");

    List<Entity> entities = new ArrayList<Entity>();
    entities.add(new Entity(1001, "ljw2083"));
    entities.add(new Entity(1002, "wenshao"));
    entities.add(new Entity(1003, "yakolee"));
    entities.add(new Entity(1004, null));

    List<Object> result = (List<Object>) path.eval(entities);
    Assert.assertEquals(1, result.size());
    Assert.assertSame(entities.get(0), result.get(0));
}