TCA: Replace any openecomp reference by onap
[dcaegen2/analytics/tca.git] / dcae-analytics-model / src / main / java / org / onap / dcae / apod / analytics / model / util / AnalyticsModelJsonUtils.java
1 /*
2  * ===============================LICENSE_START======================================
3  *  dcae-analytics
4  * ================================================================================
5  *    Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  *  Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *   You may obtain a copy of the License at
10  *
11  *          http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS,
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  *  ============================LICENSE_END===========================================
19  */
20
21 package org.onap.dcae.apod.analytics.model.util;
22
23 import com.fasterxml.jackson.core.JsonProcessingException;
24 import com.fasterxml.jackson.core.type.TypeReference;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import com.google.common.base.Suppliers;
27 import org.onap.dcae.apod.analytics.model.util.json.AnalyticsModelObjectMapperSupplier;
28
29 import java.io.IOException;
30 import java.io.InputStream;
31
32 /**
33  *
34  * @author Rajiv Singla . Creation Date: 11/7/2016.
35  */
36 public abstract class AnalyticsModelJsonUtils {
37
38     /**
39      * Object mapper to be used for all TCA Json Parsing
40      */
41     protected static final ObjectMapper ANALYTICS_MODEL_OBJECT_MAPPER =
42             Suppliers.memoize(new AnalyticsModelObjectMapperSupplier()).get();
43
44
45     /**
46      * Converts Input Stream to given type reference object
47      *
48      * @param inputStream input stream
49      * @param valueTypeRef type reference
50      * @param <T> type of type reference
51      *
52      * @return parsed json object
53      *
54      * @throws IOException IO Exception
55      */
56     public static <T> T readValue(InputStream inputStream, TypeReference<T> valueTypeRef) throws IOException {
57         return ANALYTICS_MODEL_OBJECT_MAPPER.readValue(inputStream, valueTypeRef);
58     }
59
60
61     /**
62      * Converts Input Stream to given target class object
63      *
64      * @param inputStream input stream
65      * @param targetClass target class type
66      * @param <T> type of class
67      *
68      * @return parsed json object
69      *
70      * @throws IOException IO Exception
71      */
72     public static <T> T readValue(InputStream inputStream, Class<T> targetClass) throws IOException {
73         return ANALYTICS_MODEL_OBJECT_MAPPER.readValue(inputStream, targetClass);
74     }
75
76
77     /**
78      * Converts given object to JSON string
79      *
80      * @param value object that needs to converted to json string
81      *
82      * @return json string
83      * @throws JsonProcessingException Json Processing exception
84      */
85     public static String writeValueAsString(Object value) throws JsonProcessingException {
86         return ANALYTICS_MODEL_OBJECT_MAPPER.writeValueAsString(value);
87     }
88
89
90     /**
91      *  Method to deserialize JSON content from given JSON content String.
92      *
93      * @param jsonString JSON String
94      * @param objectClass target object class
95      * @param <T> object class type
96      *
97      * @return converted Object from JSON String
98      *
99      * @throws IOException IO Exception
100      */
101     public static <T> T readValue(final String jsonString, final Class<T> objectClass) throws IOException {
102         return ANALYTICS_MODEL_OBJECT_MAPPER.readValue(jsonString, objectClass);
103     }
104 }