public JSONPath(String path, SerializeConfig serializeConfig, ParserConfig parserConfig){ if (path == null || path.length() == 0) { throw new JSONPathException("json-path can not be null or empty"); } this.path = path; this.serializeConfig = serializeConfig; this.parserConfig = parserConfig; }
public JSONPath(String path, SerializeConfig serializeConfig, ParserConfig parserConfig){ if (path == null || path.length() == 0) { throw new JSONPathException("json-path can not be null or empty"); } this.path = path; this.serializeConfig = serializeConfig; this.parserConfig = parserConfig; }
public static JSONPath compile(String path) { if (path == null) { throw new JSONPathException("jsonpath can not be null"); } JSONPath jsonpath = pathCache.get(path); if (jsonpath == null) { jsonpath = new JSONPath(path); if (pathCache.size() < 1024) { pathCache.putIfAbsent(path, jsonpath); jsonpath = pathCache.get(path); } } return jsonpath; }
@SuppressWarnings("rawtypes") public boolean removeArrayItem(JSONPath path, Object currentObject, int index) { if (currentObject instanceof List) { List list = (List) currentObject; if (index >= 0) { if (index >= list.size()) { return false; } list.remove(index); } else { int newIndex = list.size() + index; if (newIndex < 0) { return false; } list.remove(newIndex); } return true; } Class<?> clazz = currentObject.getClass(); throw new JSONPathException("unsupported set operation." + clazz); }
@SuppressWarnings({ "unchecked", "rawtypes" }) public boolean setArrayItem(JSONPath path, Object currentObject, int index, Object value) { if (currentObject instanceof List) { List list = (List) currentObject; if (index >= 0) { list.set(index, value); } else { list.set(list.size() + index, value); } return true; } Class<?> clazz = currentObject.getClass(); if (clazz.isArray()) { int arrayLenth = Array.getLength(currentObject); if (index >= 0) { if (index < arrayLenth) { Array.set(currentObject, index, value); } } else { if (Math.abs(index) <= arrayLenth) { Array.set(currentObject, arrayLenth + index, value); } } return true; } throw new JSONPathException("unsupported set operation." + clazz); }
void accept(char expect) { if (ch != expect) { throw new JSONPathException("expect '" + expect + ", but '" + ch + "'"); } if (!isEOF()) { next(); } }
@SuppressWarnings({ "rawtypes", "unchecked" }) protected Collection<Object> getPropertyValues(final Object currentObject) { final Class<?> currentClass = currentObject.getClass(); JavaBeanSerializer beanSerializer = getJavaBeanSerializer(currentClass); if (beanSerializer != null) { try { return beanSerializer.getFieldValues(currentObject); } catch (Exception e) { throw new JSONPathException("jsonpath error, path " + path, e); } } if (currentObject instanceof Map) { Map map = (Map) currentObject; return map.values(); } if (currentObject instanceof Collection) { return (Collection) currentObject; } throw new UnsupportedOperationException(); }
@SuppressWarnings({"rawtypes", "unchecked"}) Set<?> evalKeySet(Object currentObject) { if (currentObject == null) { return null; } if (currentObject instanceof Map) { // For performance reasons return keySet directly, without filtering null-value key. return ((Map)currentObject).keySet(); } if (currentObject instanceof Collection || currentObject instanceof Object[] || currentObject.getClass().isArray()) { return null; } JavaBeanSerializer beanSerializer = getJavaBeanSerializer(currentObject.getClass()); if (beanSerializer == null) { return null; } try { return beanSerializer.getFieldNames(currentObject); } catch (Exception e) { throw new JSONPathException("evalKeySet error : " + path, e); } }
return beanSerializer.getSize(currentObject); } catch (Exception e) { throw new JSONPathException("evalSize error : " + path, e);
public static JSONPath compile(String path) { if (path == null) { throw new JSONPathException("jsonpath can not be null"); } JSONPath jsonpath = pathCache.get(path); if (jsonpath == null) { jsonpath = new JSONPath(path); if (pathCache.size() < 1024) { pathCache.putIfAbsent(path, jsonpath); jsonpath = pathCache.get(path); } } return jsonpath; }
@SuppressWarnings("rawtypes") public boolean removeArrayItem(JSONPath path, Object currentObject, int index) { if (currentObject instanceof List) { List list = (List) currentObject; if (index >= 0) { if (index >= list.size()) { return false; } list.remove(index); } else { int newIndex = list.size() + index; if (newIndex < 0) { return false; } list.remove(newIndex); } return true; } Class<?> clazz = currentObject.getClass(); throw new JSONPathException("unsupported set operation." + clazz); }
protected void deepGetPropertyValues(final Object currentObject, List<Object> outValues) { final Class<?> currentClass = currentObject.getClass(); JavaBeanSerializer beanSerializer = getJavaBeanSerializer(currentClass); Collection collection = null; if (beanSerializer != null) { try { collection = beanSerializer.getFieldValues(currentObject); } catch (Exception e) { throw new JSONPathException("jsonpath error, path " + path, e); } } else if (currentObject instanceof Map) { Map map = (Map) currentObject; collection = map.values(); } else if (currentObject instanceof Collection) { collection = (Collection) currentObject; } if (collection != null) { for (Object fieldValue : collection) { if (fieldValue == null || ParserConfig.isPrimitive2(fieldValue.getClass())) { outValues.add(fieldValue); } else { deepGetPropertyValues(fieldValue, outValues); } } return; } throw new UnsupportedOperationException(currentClass.getName()); }
String readName() { skipWhitespace(); if (ch != '\\' && !Character.isJavaIdentifierStart(ch)) { throw new JSONPathException("illeal jsonpath syntax. " + path); } StringBuilder buf = new StringBuilder(); while (!isEOF()) { if (ch == '\\') { next(); buf.append(ch); if (isEOF()) { return buf.toString(); } next(); continue; } boolean identifierFlag = Character.isJavaIdentifierPart(ch); if (!identifierFlag) { break; } buf.append(ch); next(); } if (isEOF() && Character.isJavaIdentifierPart(ch)) { buf.append(ch); } return buf.toString(); }
void accept(char expect) { if (ch != expect) { throw new JSONPathException("expect '" + expect + ", but '" + ch + "'"); } if (!isEOF()) { next(); } }
throw new JSONPathException("value not found in path " + path);
protected Object readValue() { skipWhitespace(); if (isDigitFirst(ch)) { return readLongValue(); } if (ch == '"' || ch == '\'') { return readString(); } if (ch == 'n') { String name = readName(); if ("null".equals(name)) { return null; } else { throw new JSONPathException(path); } } throw new UnsupportedOperationException(); }
throw new JSONPathException("jsonpath error, path " + path + ", segement " + propertyName, e);
throw new JSONPathException("jsonpath error, path " + path + ", segement " + propertyName, e);
protected Object readValue() { skipWhitespace(); if (isDigitFirst(ch)) { return readLongValue(); } if (ch == '"' || ch == '\'') { return readString(); } if (ch == 'n') { String name = readName(); if ("null".equals(name)) { return null; } else { throw new JSONPathException(path); } } throw new UnsupportedOperationException(); }
throw new JSONPathException("not support jsonpath : " + path); throw new JSONPathException("not support jsonpath : " + path); throw new JSONPathException("not support jsonpath : " + path);