2 * ============LICENSE_START=======================================================
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
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.openecomp.sdc.be.dao.utils;
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;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.util.HashMap;
34 import java.util.List;
38 * Simple utility for JSon processing.
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);
50 // No instances allowed
54 * Deserialize json text to object
59 * @throws JsonParseException
60 * @throws JsonMappingException
63 public static <T> T readObject(String objectText, Class<T> objectClass) throws IOException {
64 return getOneObjectMapper().readValue(objectText, objectClass);
68 * Deserialize json stream to object
73 * @throws JsonParseException
74 * @throws JsonMappingException
77 public static <T> T readObject(InputStream jsonStream, Class<T> objectClass) throws IOException {
78 return getOneObjectMapper().readValue(jsonStream, objectClass);
82 * Deserialize json text to object
86 * @throws JsonParseException
87 * @throws JsonMappingException
90 public static <T> T readObject(String objectText) throws IOException {
91 TypeReference<T> typeRef = new TypeReference<T>() {
93 return getOneObjectMapper().readValue(objectText, typeRef);
97 * Deserialize the given json string to a map
102 * @throws IOException
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,
108 return mapper.readValue(json, mapStringObjectType);
112 * Deserialize the given json string to a map
115 * @param keyTypeClass
116 * @param valueTypeClass
118 * @throws IOException
120 public static <K, V> Map<K, V> toMap(String json, Class<K> keyTypeClass, Class<V> valueTypeClass)
122 ObjectMapper mapper = getOneObjectMapper();
123 JavaType mapStringObjectType = mapper.getTypeFactory().constructParametricType(HashMap.class, keyTypeClass,
125 return mapper.readValue(json, mapStringObjectType);
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);
135 * Deserialize the given json string to a list
139 * @return list object
140 * @throws IOException
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);
148 public static <T> List<T> toList(String json, Class<T> elementClass, Class<?> elementGenericClass)
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);