2 * Copyright © 2017-2018 AT&T Intellectual Property.
\r
3 * Modifications Copyright © 2018 IBM.
\r
5 * Licensed under the Apache License, Version 2.0 (the "License");
\r
6 * you may not use this file except in compliance with the License.
\r
7 * You may obtain a copy of the License at
\r
9 * http://www.apache.org/licenses/LICENSE-2.0
\r
11 * Unless required by applicable law or agreed to in writing, software
\r
12 * distributed under the License is distributed on an "AS IS" BASIS,
\r
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
14 * See the License for the specific language governing permissions and
\r
15 * limitations under the License.
\r
18 package org.onap.ccsdk.config.model.utils;
\r
20 import static com.jayway.jsonpath.JsonPath.using;
\r
21 import java.util.List;
\r
22 import com.fasterxml.jackson.databind.JsonNode;
\r
23 import com.jayway.jsonpath.Configuration;
\r
24 import com.jayway.jsonpath.Option;
\r
25 import com.jayway.jsonpath.spi.json.JacksonJsonNodeJsonProvider;
\r
26 import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider;
\r
28 @SuppressWarnings("squid:S1118")
\r
29 public class JsonParserUtils {
\r
31 public static final Configuration JACKSON_JSON_NODE_CONFIGURATION = Configuration.builder()
\r
32 .mappingProvider(new JacksonMappingProvider()).jsonProvider(new JacksonJsonNodeJsonProvider()).build();
\r
34 public static final Configuration PATH_CONFIGURATION = Configuration.builder().options(Option.AS_PATH_LIST).build();
\r
36 public static List<String> paths(String jsonContent, String expression) {
\r
37 return using(PATH_CONFIGURATION).parse(jsonContent).read(expression);
\r
40 public static List<String> paths(JsonNode jsonNode, String expression) {
\r
41 return paths(jsonNode.toString(), expression);
\r
44 public static JsonNode parse(String jsonContent, String expression) {
\r
45 return using(JACKSON_JSON_NODE_CONFIGURATION).parse(jsonContent).read(expression);
\r
48 public static JsonNode parse(JsonNode jsonNode, String expression) {
\r
49 return parse(jsonNode.toString(), expression);
\r
52 public static JsonNode parseNSet(String jsonContent, String expression, JsonNode value) {
\r
53 return using(JACKSON_JSON_NODE_CONFIGURATION).parse(jsonContent).set(expression, value).json();
\r
56 public static JsonNode parseNSet(JsonNode jsonNode, String expression, JsonNode valueNode) {
\r
57 return parseNSet(jsonNode.toString(), expression, valueNode);
\r