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.jsongraph.utils;
23 import com.fasterxml.jackson.annotation.JsonInclude;
24 import com.fasterxml.jackson.core.type.TypeReference;
25 import com.fasterxml.jackson.databind.DeserializationFeature;
26 import com.fasterxml.jackson.databind.JavaType;
27 import com.fasterxml.jackson.databind.ObjectMapper;
28 import com.fasterxml.jackson.databind.SerializationFeature;
29 import com.google.common.base.Strings;
30 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
31 import org.openecomp.sdc.common.log.wrappers.Logger;
33 import java.io.IOException;
34 import java.util.List;
37 public class JsonParserUtils {
38 private static Logger log = Logger.getLogger(JsonParserUtils.class.getName());
39 private static final ObjectMapper mapper = buildObjectMapper();
41 private JsonParserUtils() {
42 // No instances allowed
45 private static ObjectMapper buildObjectMapper() {
46 return new ObjectMapper()
47 .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
48 .disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)
49 .setSerializationInclusion(JsonInclude.Include.NON_NULL);
52 public static <T> String toJson(T object) throws IOException {
53 return mapper.writer()
54 .writeValueAsString(object);
57 public static Map<String, Object> toMap(String json) {
58 if (Strings.isNullOrEmpty(json)) {
62 Map<String, Object> object = null;
64 TypeReference<Map<String, Object>> typeRef = new TypeReference<Map<String, Object>>() {
66 object = mapper.readerFor(typeRef)
70 log.debug("Failed to parse json {}", json, e);
75 public static <T extends ToscaDataDefinition> Map<String, T> toMap(String json, Class<T> clazz) {
76 if (Strings.isNullOrEmpty(json)) {
80 Map<String, T> object = null;
82 JavaType type = mapper.getTypeFactory()
83 .constructMapType(Map.class, String.class, clazz);
84 object = mapper.readerFor(type)
88 log.debug("Failed to parse json {} to map", json, e);
92 public static <T> List<T> toList(String json, Class<T> clazz) {
93 if (Strings.isNullOrEmpty(json)) {
96 List<T> object = null;
98 JavaType type = mapper.getTypeFactory()
99 .constructCollectionType(List.class, clazz);
101 object = mapper.readerFor(type)
104 catch (Exception e) {
105 log.debug("Failed to parse json {} to list", json, e);