Reformat catalog-dao
[sdc.git] / catalog-dao / src / main / java / org / openecomp / sdc / be / dao / utils / JsonUtil.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 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 package org.openecomp.sdc.be.dao.utils;
21
22 import com.fasterxml.jackson.annotation.JsonInclude;
23 import com.fasterxml.jackson.core.JsonParseException;
24 import com.fasterxml.jackson.core.type.TypeReference;
25 import com.fasterxml.jackson.databind.JavaType;
26 import com.fasterxml.jackson.databind.JsonMappingException;
27 import com.fasterxml.jackson.databind.ObjectMapper;
28 import com.fasterxml.jackson.databind.SerializationFeature;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.util.HashMap;
32 import java.util.List;
33 import java.util.Map;
34
35 /**
36  * Simple utility for JSon processing.
37  */
38 public final class JsonUtil {
39
40     private JsonUtil() {
41         // No instances allowed
42     }
43
44     private static ObjectMapper getOneObjectMapper() {
45         ObjectMapper mapper = new ObjectMapper();
46         mapper.enable(SerializationFeature.INDENT_OUTPUT);
47         mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
48         mapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
49         return mapper;
50     }
51
52     /**
53      * Deserialize json text to object
54      *
55      * @param objectText
56      * @param objectClass
57      * @return
58      * @throws JsonParseException
59      * @throws JsonMappingException
60      * @throws IOException
61      */
62     public static <T> T readObject(String objectText, Class<T> objectClass) throws IOException {
63         return getOneObjectMapper().readValue(objectText, objectClass);
64     }
65
66     /**
67      * Deserialize json stream to object
68      *
69      * @param jsonStream
70      * @param objectClass
71      * @return
72      * @throws JsonParseException
73      * @throws JsonMappingException
74      * @throws IOException
75      */
76     public static <T> T readObject(InputStream jsonStream, Class<T> objectClass) throws IOException {
77         return getOneObjectMapper().readValue(jsonStream, objectClass);
78     }
79
80     /**
81      * Deserialize json text to object
82      *
83      * @param objectText
84      * @return
85      * @throws JsonParseException
86      * @throws JsonMappingException
87      * @throws IOException
88      */
89     public static <T> T readObject(String objectText) throws IOException {
90         TypeReference<T> typeRef = new TypeReference<T>() {
91         };
92         return getOneObjectMapper().readValue(objectText, typeRef);
93     }
94
95     /**
96      * Deserialize the given json string to a map
97      *
98      * @param json json text
99      * @return map object
100      * @throws IOException
101      */
102     public static Map<String, Object> toMap(String json) throws IOException {
103         ObjectMapper mapper = getOneObjectMapper();
104         JavaType mapStringObjectType = mapper.getTypeFactory().constructParametricType(HashMap.class, String.class, Object.class);
105         return mapper.readValue(json, mapStringObjectType);
106     }
107
108     /**
109      * Deserialize the given json string to a map
110      *
111      * @param json
112      * @param keyTypeClass
113      * @param valueTypeClass
114      * @return
115      * @throws IOException
116      */
117     public static <K, V> Map<K, V> toMap(String json, Class<K> keyTypeClass, Class<V> valueTypeClass) throws IOException {
118         ObjectMapper mapper = getOneObjectMapper();
119         JavaType mapStringObjectType = mapper.getTypeFactory().constructParametricType(HashMap.class, keyTypeClass, valueTypeClass);
120         return mapper.readValue(json, mapStringObjectType);
121     }
122
123     public static <V> V[] toArray(String json, Class<V> valueTypeClass) throws IOException {
124         ObjectMapper mapper = getOneObjectMapper();
125         JavaType arrayStringObjectType = mapper.getTypeFactory().constructArrayType(valueTypeClass);
126         return mapper.readValue(json, arrayStringObjectType);
127     }
128
129     /**
130      * Deserialize the given json string to a list
131      *
132      * @param json json text
133      * @return list object
134      * @throws IOException
135      */
136     public static <T> List<T> toList(String json, Class<T> clazz) throws IOException {
137         ObjectMapper mapper = getOneObjectMapper();
138         JavaType type = mapper.getTypeFactory().constructCollectionType(List.class, clazz);
139         return mapper.readValue(json, type);
140     }
141
142     public static <T> List<T> toList(String json, Class<T> elementClass, Class<?> elementGenericClass) throws IOException {
143         ObjectMapper mapper = getOneObjectMapper();
144         JavaType elementType = mapper.getTypeFactory().constructParametricType(elementClass, elementGenericClass);
145         JavaType listType = mapper.getTypeFactory().constructCollectionType(List.class, elementType);
146         return mapper.readValue(json, listType);
147     }
148 }