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