2 * Copyright © 2018 IBM.
3 * Modifications Copyright © 2018 - 2019 IBM, Bell Canada.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package org.onap.ccsdk.cds.controllerblueprints.core.utils
21 import com.fasterxml.jackson.databind.JsonNode
22 import com.jayway.jsonpath.Configuration
23 import com.jayway.jsonpath.JsonPath
24 import com.jayway.jsonpath.Option
25 import com.jayway.jsonpath.spi.json.JacksonJsonNodeJsonProvider
26 import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider
28 class JsonParserUtils {
31 //TODO("Optimise this")
32 val JACKSON_JSON_NODE_CONFIGURATION = Configuration.builder()
33 .mappingProvider(JacksonMappingProvider()).jsonProvider(JacksonJsonNodeJsonProvider()).build()
35 val PATH_CONFIGURATION = Configuration.builder().options(Option.AS_PATH_LIST).build()
37 fun paths(jsonContent: String, expression: String): List<String> {
38 return JsonPath.using(PATH_CONFIGURATION).parse(jsonContent).read(expression)
41 fun paths(jsonNode: JsonNode, expression: String): List<String> {
42 return paths(jsonNode, expression)
45 fun parse(jsonContent: String, expression: String): JsonNode {
46 return JsonPath.using(JACKSON_JSON_NODE_CONFIGURATION).parse(jsonContent).read(expression)
49 fun parse(jsonNode: JsonNode, expression: String): JsonNode {
50 return parse(jsonNode.toString(), expression)
53 fun parseNSet(jsonContent: String, expression: String, value: JsonNode): JsonNode {
54 return JsonPath.using(JACKSON_JSON_NODE_CONFIGURATION).parse(jsonContent).set(expression, value).json()
57 fun parseNSet(jsonNode: JsonNode, expression: String, valueNode: JsonNode): JsonNode {
59 return parseNSet(jsonNode, expression, valueNode)