All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.javacrumbs.jsonunit.jsonpath.InternalJsonPathUtils Maven / Gradle / Ivy

/**
 * Copyright 2009-2019 the original author or authors.
 * 

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at *

* http://www.apache.org/licenses/LICENSE-2.0 *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.javacrumbs.jsonunit.jsonpath; import static com.jayway.jsonpath.JsonPath.using; import static java.util.stream.Collectors.toList; import com.jayway.jsonpath.Option; import java.util.Collection; import java.util.List; import java.util.stream.Stream; import net.javacrumbs.jsonunit.core.Configuration; import net.javacrumbs.jsonunit.core.internal.JsonUtils; import net.javacrumbs.jsonunit.core.internal.PathOption; import org.jetbrains.annotations.NotNull; public class InternalJsonPathUtils { private InternalJsonPathUtils() {} @NotNull public static Configuration resolveJsonPaths(@NotNull Object json, @NotNull Configuration configuration) { Collection pathsToBeIgnored = resolveJsonPaths(json, configuration.getPathsToBeIgnored()); List pathOptions = configuration.getPathOptions().stream() .map(po -> { List newPoPaths = resolveJsonPaths(json, po.getPaths()); return po.withPaths(newPoPaths); }) .collect(toList()); return configuration.whenIgnoringPaths(pathsToBeIgnored).withPathOptions(pathOptions); } @NotNull private static List resolveJsonPaths(@NotNull Object json, @NotNull Collection paths) { com.jayway.jsonpath.Configuration conf = com.jayway.jsonpath.Configuration.builder() .options(Option.AS_PATH_LIST, Option.SUPPRESS_EXCEPTIONS) .build(); return paths.stream() .flatMap(path -> { if (path.startsWith("$")) { List resolvedPaths = readValue(conf, json, path); return resolvedPaths.stream().map(InternalJsonPathUtils::fromBracketNotation); } else { return Stream.of(path); } }) .collect(toList()); } static T readValue(com.jayway.jsonpath.Configuration conf, Object json, String path) { if (json instanceof String) { return using(conf).parse((String) json).read(path); } else { return using(conf) .parse(JsonUtils.convertToJson(json, "actual").getValue()) .read(path); } } static String fromBracketNotation(String path) { return path.replace("['", ".").replace("']", ""); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy