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
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.utils;
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;
32 @RequiredArgsConstructor
34 public class JsonObjectMapper {
36 private final ObjectMapper objectMapper;
39 * Serializing generic java object to JSON using Jackson.
41 * @param object any java object value
42 * @return the generated JSON as a string.
44 public String asJsonString(final Object object) {
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.",
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.
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'
64 public <T> T convertToValueType(final Object jsonObject, final Class<T> valueType) {
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());
75 * Deserialize JSON content from given JSON content String.
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'
82 public <T> T convertJsonString(final String jsonContent, final Class<T> valueType) {
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());
93 * Serializing generic json object to bytes using Jackson.
95 * @param jsonObject any json object value
96 * @return the generated JSON as a byte array.
98 public byte[] asJsonBytes(final Object jsonObject) {
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());
109 * Deserialize JSON content from given JSON content String to JsonNode.
111 * @param jsonContent JSON content
112 * @return a json node
114 public JsonNode convertToJsonNode(final String jsonContent) {
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());