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