2 * ===============================LICENSE_START======================================
\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
11 * http://www.apache.org/licenses/LICENSE-2.0
\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
21 package org.openecomp.dcae.apod.analytics.model.util;
\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
29 import java.io.IOException;
\r
30 import java.io.InputStream;
\r
34 * @author Rajiv Singla . Creation Date: 11/7/2016.
\r
36 public abstract class AnalyticsModelJsonUtils {
\r
39 * Object mapper to be used for all TCA Json Parsing
\r
41 protected static final ObjectMapper ANALYTICS_MODEL_OBJECT_MAPPER =
\r
42 Suppliers.memoize(new AnalyticsModelObjectMapperSupplier()).get();
\r
46 * Converts Input Stream to given type reference object
\r
48 * @param inputStream input stream
\r
49 * @param valueTypeRef type reference
\r
50 * @param <T> type of type reference
\r
52 * @return parsed json object
\r
54 * @throws IOException IO Exception
\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
62 * Converts Input Stream to given target class object
\r
64 * @param inputStream input stream
\r
65 * @param targetClass target class type
\r
66 * @param <T> type of class
\r
68 * @return parsed json object
\r
70 * @throws IOException IO Exception
\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
78 * Converts given object to JSON string
\r
80 * @param value object that needs to converted to json string
\r
82 * @return json string
\r
83 * @throws JsonProcessingException Json Processing exception
\r
85 public static String writeValueAsString(Object value) throws JsonProcessingException {
\r
86 return ANALYTICS_MODEL_OBJECT_MAPPER.writeValueAsString(value);
\r
91 * Method to deserialize JSON content from given JSON content String.
\r
93 * @param jsonString JSON String
\r
94 * @param objectClass target object class
\r
95 * @param <T> object class type
\r
97 * @return converted Object from JSON String
\r
99 * @throws IOException IO Exception
\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