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