Merge "CM SUBSCRIPTION: add new subscription for non existing xpath"
[cps.git] / cps-service / src / main / java / org / onap / cps / utils / JsonObjectMapper.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2022 Nordix Foundation
4  *  ================================================================================
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.utils;
22
23 import com.fasterxml.jackson.core.JsonProcessingException;
24 import com.fasterxml.jackson.databind.JsonNode;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import lombok.RequiredArgsConstructor;
27 import lombok.extern.slf4j.Slf4j;
28 import org.onap.cps.spi.exceptions.DataValidationException;
29 import org.springframework.stereotype.Component;
30
31 @Slf4j
32 @RequiredArgsConstructor
33 @Component
34 public class JsonObjectMapper {
35
36     private final ObjectMapper objectMapper;
37
38     /**
39      * Serializing generic java object to JSON using Jackson.
40      *
41      * @param object any java object value
42      * @return the generated JSON as a string.
43      */
44     public String asJsonString(final Object object) {
45         try {
46             return objectMapper.writeValueAsString(object);
47         } catch (final JsonProcessingException e) {
48             log.error("Parsing error occurred while converting Object to JSON string.");
49             throw new DataValidationException("Parsing error occurred while converting given object to JSON string.",
50                     e.getMessage());
51         }
52     }
53
54     /**
55      * Constructing JavaType out of given type (typically java.lang.Class).
56      * Allow efficient value conversions for structurally compatible json objects,
57      * according to standard Jackson configuration.
58      *
59      * @param jsonObject   structurally compatible json object
60      * @param valueType compatible Object class type
61      * @param <T>         type parameter
62      * @return a class object of specific class type 'T'
63      */
64     public <T> T convertToValueType(final Object jsonObject, final Class<T> valueType) {
65         try {
66             return objectMapper.convertValue(jsonObject, valueType);
67         } catch (final IllegalArgumentException e) {
68             log.error("Found structurally incompatible object while converting into value type.");
69             throw new DataValidationException("Found structurally incompatible object while converting "
70                     + "into value type.", e.getMessage());
71         }
72     }
73
74     /**
75      * Deserialize JSON content from given JSON content String.
76      *
77      * @param jsonContent   JSON content
78      * @param valueType compatible Object class type
79      * @param <T>       type parameter
80      * @return a class object of specific class type 'T'
81      */
82     public <T> T convertJsonString(final String jsonContent, final Class<T> valueType) {
83         try {
84             return objectMapper.readValue(jsonContent, valueType);
85         } catch (final JsonProcessingException e) {
86             log.error("Parsing error occurred while converting JSON content to specific class type.");
87             throw new DataValidationException("Parsing error occurred while converting "
88                     + "JSON content to specific class type.", e.getMessage());
89         }
90     }
91
92     /**
93      * Serializing generic json object to bytes using Jackson.
94      *
95      * @param jsonObject any json object value
96      * @return the generated JSON as a byte array.
97      */
98     public byte[] asJsonBytes(final Object jsonObject) {
99         try {
100             return objectMapper.writeValueAsBytes(jsonObject);
101         } catch (final JsonProcessingException jsonProcessingException) {
102             log.error("Parsing error occurred while converting JSON object to bytes.");
103             throw new DataValidationException("Parsing error occurred while converting given JSON object to bytes.",
104                     jsonProcessingException.getMessage());
105         }
106     }
107
108     /**
109      * Deserialize JSON content from given JSON content String to JsonNode.
110      *
111      * @param jsonContent   JSON content
112      * @return a json node
113      */
114     public JsonNode convertToJsonNode(final String jsonContent) {
115         try {
116             return objectMapper.readTree(jsonContent);
117         } catch (final JsonProcessingException e) {
118             log.error("Parsing error occurred while converting JSON content to Json Node.");
119             throw new DataValidationException("Parsing error occurred while converting "
120                     + "JSON content to Json Node.", e.getMessage());
121         }
122     }
123 }