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=========================================================
20 package org.openecomp.sdc.be.dao.utils;
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;
36 * Simple utility for JSon processing.
38 public final class JsonUtil {
41 // No instances allowed
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);
53 * Deserialize json text to object
58 * @throws JsonParseException
59 * @throws JsonMappingException
62 public static <T> T readObject(String objectText, Class<T> objectClass) throws IOException {
63 return getOneObjectMapper().readValue(objectText, objectClass);
67 * Deserialize json stream to object
72 * @throws JsonParseException
73 * @throws JsonMappingException
76 public static <T> T readObject(InputStream jsonStream, Class<T> objectClass) throws IOException {
77 return getOneObjectMapper().readValue(jsonStream, objectClass);
81 * Deserialize json text to object
85 * @throws JsonParseException
86 * @throws JsonMappingException
89 public static <T> T readObject(String objectText) throws IOException {
90 TypeReference<T> typeRef = new TypeReference<T>() {
92 return getOneObjectMapper().readValue(objectText, typeRef);
96 * Deserialize the given json string to a map
98 * @param json json text
100 * @throws IOException
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);
109 * Deserialize the given json string to a map
112 * @param keyTypeClass
113 * @param valueTypeClass
115 * @throws IOException
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);
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);
130 * Deserialize the given json string to a list
132 * @param json json text
133 * @return list object
134 * @throws IOException
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);
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);