Replace gson mapper with jackson mapper
[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.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;
29
30 @Slf4j
31 @RequiredArgsConstructor
32 @Component
33 public class JsonObjectMapper {
34
35     private final ObjectMapper objectMapper;
36
37     /**
38      * Serializing generic java object to JSON using Jackson.
39      *
40      * @param object any java object value
41      * @return the generated JSON as a string.
42      */
43     public String asJsonString(final Object object) {
44         try {
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.",
49                     e.getMessage());
50         }
51     }
52
53     /**
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.
57      *
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'
62      */
63     public <T> T convertToValueType(final Object jsonObject, final Class<T> valueType) {
64         try {
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());
70         }
71     }
72
73     /**
74      * Deserialize JSON content from given JSON content String.
75      *
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'
80      */
81     public <T> T convertJsonString(final String jsonContent, final Class<T> valueType) {
82         try {
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());
88         }
89     }
90 }