diff options
Diffstat (limited to 'catalog-dao/src/main/java/org/openecomp/sdc/be/dao/utils/JsonUtil.java')
-rw-r--r-- | catalog-dao/src/main/java/org/openecomp/sdc/be/dao/utils/JsonUtil.java | 211 |
1 files changed, 102 insertions, 109 deletions
diff --git a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/utils/JsonUtil.java b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/utils/JsonUtil.java index 92ba61bcc7..ebcb1d5271 100644 --- a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/utils/JsonUtil.java +++ b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/utils/JsonUtil.java @@ -7,9 +7,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -17,7 +17,6 @@ * limitations under the License. * ============LICENSE_END========================================================= */ - package org.openecomp.sdc.be.dao.utils; import com.fasterxml.jackson.annotation.JsonInclude; @@ -27,7 +26,6 @@ import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; - import java.io.IOException; import java.io.InputStream; import java.util.HashMap; @@ -38,118 +36,113 @@ import java.util.Map; * Simple utility for JSon processing. */ public final class JsonUtil { - private static ObjectMapper getOneObjectMapper() { - ObjectMapper mapper = new ObjectMapper(); - mapper.enable(SerializationFeature.INDENT_OUTPUT); - mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); - mapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false); - return mapper; - } - private JsonUtil() { - // No instances allowed - } + private JsonUtil() { + // No instances allowed + } + + private static ObjectMapper getOneObjectMapper() { + ObjectMapper mapper = new ObjectMapper(); + mapper.enable(SerializationFeature.INDENT_OUTPUT); + mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); + mapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false); + return mapper; + } - /** - * Deserialize json text to object - * - * @param objectText - * @param objectClass - * @return - * @throws JsonParseException - * @throws JsonMappingException - * @throws IOException - */ - public static <T> T readObject(String objectText, Class<T> objectClass) throws IOException { - return getOneObjectMapper().readValue(objectText, objectClass); - } + /** + * Deserialize json text to object + * + * @param objectText + * @param objectClass + * @return + * @throws JsonParseException + * @throws JsonMappingException + * @throws IOException + */ + public static <T> T readObject(String objectText, Class<T> objectClass) throws IOException { + return getOneObjectMapper().readValue(objectText, objectClass); + } - /** - * Deserialize json stream to object - * - * @param jsonStream - * @param objectClass - * @return - * @throws JsonParseException - * @throws JsonMappingException - * @throws IOException - */ - public static <T> T readObject(InputStream jsonStream, Class<T> objectClass) throws IOException { - return getOneObjectMapper().readValue(jsonStream, objectClass); - } + /** + * Deserialize json stream to object + * + * @param jsonStream + * @param objectClass + * @return + * @throws JsonParseException + * @throws JsonMappingException + * @throws IOException + */ + public static <T> T readObject(InputStream jsonStream, Class<T> objectClass) throws IOException { + return getOneObjectMapper().readValue(jsonStream, objectClass); + } - /** - * Deserialize json text to object - * - * @param objectText - * @return - * @throws JsonParseException - * @throws JsonMappingException - * @throws IOException - */ - public static <T> T readObject(String objectText) throws IOException { - TypeReference<T> typeRef = new TypeReference<T>() { - }; - return getOneObjectMapper().readValue(objectText, typeRef); - } + /** + * Deserialize json text to object + * + * @param objectText + * @return + * @throws JsonParseException + * @throws JsonMappingException + * @throws IOException + */ + public static <T> T readObject(String objectText) throws IOException { + TypeReference<T> typeRef = new TypeReference<T>() { + }; + return getOneObjectMapper().readValue(objectText, typeRef); + } - /** - * Deserialize the given json string to a map - * - * @param json - * json text - * @return map object - * @throws IOException - */ - public static Map<String, Object> toMap(String json) throws IOException { - ObjectMapper mapper = getOneObjectMapper(); - JavaType mapStringObjectType = mapper.getTypeFactory().constructParametricType(HashMap.class, String.class, - Object.class); - return mapper.readValue(json, mapStringObjectType); - } + /** + * Deserialize the given json string to a map + * + * @param json json text + * @return map object + * @throws IOException + */ + public static Map<String, Object> toMap(String json) throws IOException { + ObjectMapper mapper = getOneObjectMapper(); + JavaType mapStringObjectType = mapper.getTypeFactory().constructParametricType(HashMap.class, String.class, Object.class); + return mapper.readValue(json, mapStringObjectType); + } - /** - * Deserialize the given json string to a map - * - * @param json - * @param keyTypeClass - * @param valueTypeClass - * @return - * @throws IOException - */ - public static <K, V> Map<K, V> toMap(String json, Class<K> keyTypeClass, Class<V> valueTypeClass) - throws IOException { - ObjectMapper mapper = getOneObjectMapper(); - JavaType mapStringObjectType = mapper.getTypeFactory().constructParametricType(HashMap.class, keyTypeClass, - valueTypeClass); - return mapper.readValue(json, mapStringObjectType); - } + /** + * Deserialize the given json string to a map + * + * @param json + * @param keyTypeClass + * @param valueTypeClass + * @return + * @throws IOException + */ + public static <K, V> Map<K, V> toMap(String json, Class<K> keyTypeClass, Class<V> valueTypeClass) throws IOException { + ObjectMapper mapper = getOneObjectMapper(); + JavaType mapStringObjectType = mapper.getTypeFactory().constructParametricType(HashMap.class, keyTypeClass, valueTypeClass); + return mapper.readValue(json, mapStringObjectType); + } - public static <V> V[] toArray(String json, Class<V> valueTypeClass) throws IOException { - ObjectMapper mapper = getOneObjectMapper(); - JavaType arrayStringObjectType = mapper.getTypeFactory().constructArrayType(valueTypeClass); - return mapper.readValue(json, arrayStringObjectType); - } + public static <V> V[] toArray(String json, Class<V> valueTypeClass) throws IOException { + ObjectMapper mapper = getOneObjectMapper(); + JavaType arrayStringObjectType = mapper.getTypeFactory().constructArrayType(valueTypeClass); + return mapper.readValue(json, arrayStringObjectType); + } - /** - * Deserialize the given json string to a list - * - * @param json - * json text - * @return list object - * @throws IOException - */ - public static <T> List<T> toList(String json, Class<T> clazz) throws IOException { - ObjectMapper mapper = getOneObjectMapper(); - JavaType type = mapper.getTypeFactory().constructCollectionType(List.class, clazz); - return mapper.readValue(json, type); - } + /** + * Deserialize the given json string to a list + * + * @param json json text + * @return list object + * @throws IOException + */ + public static <T> List<T> toList(String json, Class<T> clazz) throws IOException { + ObjectMapper mapper = getOneObjectMapper(); + JavaType type = mapper.getTypeFactory().constructCollectionType(List.class, clazz); + return mapper.readValue(json, type); + } - public static <T> List<T> toList(String json, Class<T> elementClass, Class<?> elementGenericClass) - throws IOException { - ObjectMapper mapper = getOneObjectMapper(); - JavaType elementType = mapper.getTypeFactory().constructParametricType(elementClass, elementGenericClass); - JavaType listType = mapper.getTypeFactory().constructCollectionType(List.class, elementType); - return mapper.readValue(json, listType); - } + public static <T> List<T> toList(String json, Class<T> elementClass, Class<?> elementGenericClass) throws IOException { + ObjectMapper mapper = getOneObjectMapper(); + JavaType elementType = mapper.getTypeFactory().constructParametricType(elementClass, elementGenericClass); + JavaType listType = mapper.getTypeFactory().constructCollectionType(List.class, elementType); + return mapper.readValue(json, listType); + } } |