@SuppressWarnings("unchecked") @Override public <T> T getPath() { if(resultIndex == 0){ throw new PathNotFoundException("No results for path: " + path.toString()); } return (T)pathResult; }
LOG.warn("Couldn't read multi JSONPath from response - skipping multi value ({})", e.getMessage()); LOG.warn("Couldn't read single JSONPath from response - returning empty result ({})", e.getMessage()); return LookupResult.empty();
/** * * @param jsonString JSON String from which data is extracted * @param jsonPath JSON-PATH expression * @return List of JSON Strings of the extracted data * @throws ParseException when parsing fails */ public List<Object> extractWithJsonPath(String jsonString, String jsonPath) throws ParseException { JsonPath jsonPathParser = getJsonPath(jsonPath); List<Object> extractedObjects; try { extractedObjects = jsonPathParser.read(jsonString, DEFAULT_CONFIGURATION); } catch (PathNotFoundException e) { if(log.isDebugEnabled()) { log.debug("Could not find JSON Path {} in [{}]: {}", jsonPath, jsonString, e.getLocalizedMessage()); } return Collections.emptyList(); } List<Object> results = new ArrayList<>(extractedObjects.size()); for (Object obj: extractedObjects) { results.add(stringifyJSONObject(obj)); } return results; }
/** * Check if model is non-null and array. * @param currentPath * @param model * @param ctx * @return false if current evaluation call must be skipped, true otherwise * @throws PathNotFoundException if model is null and evaluation must be interrupted * @throws InvalidPathException if model is not an array and evaluation must be interrupted */ protected boolean checkArrayModel(String currentPath, Object model, EvaluationContextImpl ctx) { if (model == null){ if (! isUpstreamDefinite()) { return false; } else { throw new PathNotFoundException("The path " + currentPath + " is null"); } } if (!ctx.jsonProvider().isArray(model)) { if (! isUpstreamDefinite()) { return false; } else { throw new PathNotFoundException(format("Filter: %s can only be applied to arrays. Current context is: %s", toString(), model)); } } return true; } }
/** * Verifies that the jsonpath result contains a specific regex pattern. * * @param path jsonpath to search for * @param pattern expected regex pattern that is present at path * @return true or false */ public boolean verifyJsonElementContainsString(String path, String pattern) throws ConditionalException { boolean result = false; try { result = containsPattern(retrieveValueOfJsonElement(path).toString(), pattern); } catch (PathNotFoundException e) { throw new ConditionalException(e.getMessage()); } return result; }
@Override public void evaluate(String currentPath, PathRef parent, Object model, EvaluationContextImpl ctx) { // Can't assert it in ctor because isLeaf() could be changed later on. assert onlyOneIsTrueNonThrow(singlePropertyCase(), multiPropertyMergeCase(), multiPropertyIterationCase()); if (!ctx.jsonProvider().isMap(model)) { if (! isUpstreamDefinite()) { return; } else { String m = model == null ? "null" : model.getClass().getName(); throw new PathNotFoundException(String.format( "Expected to find an object with property %s in path %s but found '%s'. " + "This is not a json object according to the JsonProvider: '%s'.", getPathFragment(), currentPath, m, ctx.configuration().jsonProvider().getClass().getName())); } } if (singlePropertyCase() || multiPropertyMergeCase()) { handleObjectProperty(currentPath, model, ctx, properties); return; } assert multiPropertyIterationCase(); final List<String> currentlyHandledProperty = new ArrayList<String>(1); currentlyHandledProperty.add(null); for (final String property : properties) { currentlyHandledProperty.set(0, property); handleObjectProperty(currentPath, model, ctx, currentlyHandledProperty); } }
@Override public boolean filter(String json) { Object document = Configuration.defaultConfiguration().jsonProvider().parse(json); try { JsonPath.read(document, this.field); } catch (PathNotFoundException exception) { log.error("Error - {}", exception.getMessage(), exception); return false; } return true; }
@SuppressWarnings("unchecked") @Override public <T> T getValue(boolean unwrap) { if (path.isDefinite()) { if(resultIndex == 0){ throw new PathNotFoundException("No results for path: " + path.toString()); } int len = jsonProvider().length(valueResult); Object value = (len > 0) ? jsonProvider().getArrayIndex(valueResult, len-1) : null; if (value != null && unwrap){ value = jsonProvider().unwrap(value); } return (T) value; } return (T)valueResult; }
LOG.warn("Couldn't read multi JSONPath from response - skipping multi value ({})", e.getMessage()); LOG.warn("Couldn't read single JSONPath from response - returning empty result ({})", e.getMessage()); return LookupResult.empty();
return; } else { throw new PathNotFoundException("No results for path: " + evalPath); throw new PathNotFoundException("Missing property in path " + evalPath); propertyVal = null; } else if (ctx.options().contains(Option.REQUIRE_PROPERTIES)) { throw new PathNotFoundException("Missing property in path " + evalPath); } else { continue;
message = e.getMessage();
protected void renameInMap(Object targetMap, String oldKeyName, String newKeyName, Configuration configuration){ if(configuration.jsonProvider().isMap(targetMap)){ if(configuration.jsonProvider().getMapValue(targetMap, oldKeyName) == JsonProvider.UNDEFINED){ throw new PathNotFoundException("No results for Key "+oldKeyName+" found in map!"); } configuration.jsonProvider().setProperty(targetMap, newKeyName, configuration.jsonProvider().getMapValue(targetMap, oldKeyName)); configuration.jsonProvider().removeProperty(targetMap, oldKeyName); } else { throw new InvalidModificationException("Can only rename properties in a map"); } }
@SuppressWarnings("unchecked") @Override public <T> T getPath() { if(resultIndex == 0){ throw new PathNotFoundException("No results for path: " + path.toString()); } return (T)pathResult; }
/** * Check if model is non-null and array. * @param currentPath * @param model * @param ctx * @return false if current evaluation call must be skipped, true otherwise * @throws PathNotFoundException if model is null and evaluation must be interrupted * @throws InvalidPathException if model is not an array and evaluation must be interrupted */ protected boolean checkArrayModel(String currentPath, Object model, EvaluationContextImpl ctx) { if (model == null){ if (! isUpstreamDefinite()) { return false; } else { throw new PathNotFoundException("The path " + currentPath + " is null"); } } if (!ctx.jsonProvider().isArray(model)) { if (! isUpstreamDefinite()) { return false; } else { throw new PathNotFoundException(format("Filter: %s can only be applied to arrays. Current context is: %s", toString(), model)); } } return true; } }
@Override public void evaluate(String currentPath, PathRef parent, Object model, EvaluationContextImpl ctx) { // Can't assert it in ctor because isLeaf() could be changed later on. assert onlyOneIsTrueNonThrow(singlePropertyCase(), multiPropertyMergeCase(), multiPropertyIterationCase()); if (!ctx.jsonProvider().isMap(model)) { if (! isUpstreamDefinite()) { return; } else { String m = model == null ? "null" : model.getClass().getName(); throw new PathNotFoundException(String.format( "Expected to find an object with property %s in path %s but found '%s'. " + "This is not a json object according to the JsonProvider: '%s'.", getPathFragment(), currentPath, m, ctx.configuration().jsonProvider().getClass().getName())); } } if (singlePropertyCase() || multiPropertyMergeCase()) { handleObjectProperty(currentPath, model, ctx, properties); return; } assert multiPropertyIterationCase(); final List<String> currentlyHandledProperty = new ArrayList<String>(1); currentlyHandledProperty.add(null); for (final String property : properties) { currentlyHandledProperty.set(0, property); handleObjectProperty(currentPath, model, ctx, currentlyHandledProperty); } }
@SuppressWarnings("unchecked") @Override public <T> T getValue(boolean unwrap) { if (path.isDefinite()) { if(resultIndex == 0){ throw new PathNotFoundException("No results for path: " + path.toString()); } int len = jsonProvider().length(valueResult); Object value = (len > 0) ? jsonProvider().getArrayIndex(valueResult, len-1) : null; if (value != null && unwrap){ value = jsonProvider().unwrap(value); } return (T) value; } return (T)valueResult; }
return; } else { throw new PathNotFoundException("No results for path: " + evalPath); throw new PathNotFoundException("Missing property in path " + evalPath); propertyVal = null; } else if (ctx.options().contains(Option.REQUIRE_PROPERTIES)) { throw new PathNotFoundException("Missing property in path " + evalPath); } else { continue;
protected void renameInMap(Object targetMap, String oldKeyName, String newKeyName, Configuration configuration){ if(configuration.jsonProvider().isMap(targetMap)){ if(configuration.jsonProvider().getMapValue(targetMap, oldKeyName) == JsonProvider.UNDEFINED){ throw new PathNotFoundException("No results for Key "+oldKeyName+" found in map!"); } configuration.jsonProvider().setProperty(targetMap, newKeyName, configuration.jsonProvider().getMapValue(targetMap, oldKeyName)); configuration.jsonProvider().removeProperty(targetMap, oldKeyName); } else { throw new InvalidModificationException("Can only rename properties in a map"); } }
@SuppressWarnings("unchecked") @Override public <T> T getPath() { if(resultIndex == 0){ throw new PathNotFoundException("No results for path: " + path.toString()); } return (T)pathResult; }
/** * Check if model is non-null and array. * @param currentPath * @param model * @param ctx * @return false if current evaluation call must be skipped, true otherwise * @throws PathNotFoundException if model is null and evaluation must be interrupted * @throws InvalidPathException if model is not an array and evaluation must be interrupted */ protected boolean checkArrayModel(String currentPath, Object model, EvaluationContextImpl ctx) { if (model == null){ if (! isUpstreamDefinite()) { return false; } else { throw new PathNotFoundException("The path " + currentPath + " is null"); } } if (!ctx.jsonProvider().isArray(model)) { if (! isUpstreamDefinite()) { return false; } else { throw new PathNotFoundException(format("Filter: %s can only be applied to arrays. Current context is: %s", toString(), model)); } } return true; } }