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.ObjectMapper;
25 import lombok.RequiredArgsConstructor;
26 import lombok.extern.slf4j.Slf4j;
27 import org.onap.cps.spi.exceptions.DataValidationException;
28 import org.springframework.stereotype.Component;
31 @RequiredArgsConstructor
33 public class JsonObjectMapper {
35 private final ObjectMapper objectMapper;
38 * Serializing generic java object to JSON using Jackson.
40 * @param object any java object value
41 * @return the generated JSON as a string.
43 public String asJsonString(final Object object) {
45 return objectMapper.writeValueAsString(object);
46 } catch (final JsonProcessingException e) {
47 log.error("Parsing error occurred while converting Object to JSON string.");
48 throw new DataValidationException("Parsing error occurred while converting given object to JSON string.",
54 * Constructing JavaType out of given type (typically java.lang.Class).
55 * Allow efficient value conversions for structurally compatible json objects,
56 * according to standard Jackson configuration.
58 * @param jsonObject structurally compatible json object
59 * @param valueType compatible Object class type
60 * @param <T> type parameter
61 * @return a class object of specific class type 'T'
63 public <T> T convertToValueType(final Object jsonObject, final Class<T> valueType) {
65 return objectMapper.convertValue(jsonObject, valueType);
66 } catch (final IllegalArgumentException e) {
67 log.error("Found structurally incompatible object while converting into value type.");
68 throw new DataValidationException("Found structurally incompatible object while converting "
69 + "into value type.", e.getMessage());
74 * Deserialize JSON content from given JSON content String.
76 * @param jsonContent JSON content
77 * @param valueType compatible Object class type
78 * @param <T> type parameter
79 * @return a class object of specific class type 'T'
81 public <T> T convertJsonString(final String jsonContent, final Class<T> valueType) {
83 return objectMapper.readValue(jsonContent, valueType);
84 } catch (final JsonProcessingException e) {
85 log.error("Parsing error occurred while converting JSON content to specific class type.");
86 throw new DataValidationException("Parsing error occurred while converting "
87 + "JSON content to specific class type.", e.getMessage());