JSON { public static boolean predicate(String jsonpath, Object data) { if (jsonpath == null || jsonpath.trim().isEmpty()) { return false; } String json = serialize(data); if (json != null) { Object result = JsonPath.parse(json).read(jsonpath); if (result != null) { if (result.getClass() == Boolean.class) { return (Boolean)result; } else if (result.getClass() == String.class) { return Boolean.valueOf((String)result); } else if (result.getClass() == JSONArray.class) { return !((JSONArray) result).isEmpty(); } } } return false; }  static String serialize(Object data); static boolean predicate(String jsonpath, Object data); static String evaluate(String jsonpath, Object data);  }
@Test public void testPredicateString() { String json = "{ \"enabled\": \"true\" }"; String expr = "$.enabled"; boolean result = JSON.predicate(expr, json); assertTrue(result); }
@Test public void testPredicateBoolean() { String json = "{ \"enabled\": true }"; String expr = "$.enabled"; boolean result = JSON.predicate(expr, json); assertTrue(result); }
@Test public void testPredicateExpression() { String json = "{ \"orders\": [{ \"orderId\": \"1\" }] }"; String expr = "$.orders[?(@.orderId)]"; boolean result = JSON.predicate(expr, json); assertTrue(result); }
@Test public void testPredicateObject() { Order order = new Order(); order.setEnabled(true); String expr = "$.enabled"; boolean result = JSON.predicate(expr, order); assertTrue(result); }