/** * Evaluates a JsonPath expression returning a single element. * @param json JSON value. * @param jsonPath expression to evaluate. * @return result result of expression. * @throws java.lang.RuntimeException if jsonPath would return multiple elements. */ public Object getJsonPath(String json, String jsonPath) { if (!JsonPath.isPathDefinite(jsonPath)) { throw new RuntimeException(jsonPath + " returns multiple results, not a single."); } return parseJson(json).read(jsonPath); }
/** * Retrieves the value of a json string or boolean element, if the element is not present this returns an empty string. * * @param path jsonpath to search for * @return found value if the element is present * @throws ConditionalException - StopTestCommandException if the path is not found and if FitnesseSystemSettings.stopTestOnException=true * @throws ConditionalException - CommandException if the path is not found and if FitnesseSystemSettings.stopTestOnException=false */ public Integer retrieveValueOfJsonIntElement(String path) throws ConditionalException { Integer result = null; try { if (!JsonPath.isPathDefinite(path)) { LOG.info("JsonPath has multiple possible results, returning the whole set."); } result = JsonPath.read(document, path); LOG.info("Found value for path '" + path + "' is: " + result); } catch (PathNotFoundException e) { throw new ConditionalException("Het json element '" + path + "' is niet gevonden. [PathNotFoundException]"); } return result; }
Object result = null; try { if (!JsonPath.isPathDefinite(path)) { LOG.info("JsonPath has multiple possible results, returning the whole set.");
/** * Evaluates a JsonPath expression returning a multiple elements. * @param json JSON value. * @param jsonPath expression to evaluate. * @return result results of expression. * @throws java.lang.RuntimeException if jsonPath would return a single element. */ public List<Object> getAllJsonPath(String json, String jsonPath) { List<Object> result; if (JsonPath.isPathDefinite(jsonPath)) { Object val = getJsonPath(json, jsonPath); if (val == null) { result = Collections.emptyList(); } else { result = Collections.singletonList(val); } } else { result = parseJson(json).read(jsonPath); } return result; }
@Nullable @Override public Object invoke(@SuppressWarnings("null") MethodInvocation invocation) throws Throwable { Method method = invocation.getMethod(); TypeInformation<Object> returnType = ClassTypeInformation.fromReturnTypeOf(method); ResolvableType type = ResolvableType.forMethodReturnType(method); boolean isCollectionResult = Collection.class.isAssignableFrom(type.getRawClass()); type = isCollectionResult ? type : ResolvableType.forClassWithGenerics(List.class, type); Iterable<String> jsonPaths = getJsonPaths(method); for (String jsonPath : jsonPaths) { try { if (returnType.getRequiredActualType().getType().isInterface()) { List<?> result = context.read(jsonPath); return result.isEmpty() ? null : result.get(0); } type = isCollectionResult && JsonPath.isPathDefinite(jsonPath) ? ResolvableType.forClassWithGenerics(List.class, type) : type; List<?> result = (List<?>) context.read(jsonPath, new ResolvableTypeRef(type)); if (isCollectionResult && JsonPath.isPathDefinite(jsonPath)) { result = (List<?>) result.get(0); } return isCollectionResult ? result : result.isEmpty() ? null : result.get(0); } catch (PathNotFoundException o_O) { // continue with next path } } return null; }
PathNotFoundException pathNotFoundException = null; try { if (JsonPath.isPathDefinite(expression)) { jsonPathResult = readerContext.read(expression); } else {