re base code
[sdc.git] / catalog-dao / src / main / java / org / openecomp / sdc / be / dao / jsongraph / utils / JsonParserUtils.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.jsongraph.utils;
22
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;
32
33 import java.io.IOException;
34 import java.util.List;
35 import java.util.Map;
36
37 public class JsonParserUtils {
38     private static Logger log = Logger.getLogger(JsonParserUtils.class.getName());
39     private static final ObjectMapper mapper = buildObjectMapper();
40
41     private JsonParserUtils() {
42         // No instances allowed
43     }
44
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);
50     }
51
52     public static <T> String toJson(T object) throws IOException {
53         return mapper.writer()
54                      .writeValueAsString(object);
55     }
56
57     public static Map<String, Object> toMap(String json) {
58         if (Strings.isNullOrEmpty(json)) {
59             return null;
60         }
61
62         Map<String, Object> object = null;
63         try {
64             TypeReference<Map<String, Object>> typeRef = new TypeReference<Map<String, Object>>() {
65             };
66             object = mapper.readerFor(typeRef)
67                            .readValue(json);
68         }
69         catch (Exception e) {
70             log.debug("Failed to parse json {}", json, e);
71         }
72         return object;
73     }
74
75     public static <T extends ToscaDataDefinition> Map<String, T> toMap(String json, Class<T> clazz) {
76         if (Strings.isNullOrEmpty(json)) {
77             return null;
78         }
79
80         Map<String, T> object = null;
81         try {
82             JavaType type = mapper.getTypeFactory()
83                                   .constructMapType(Map.class, String.class, clazz);
84             object = mapper.readerFor(type)
85                            .readValue(json);
86         }
87         catch (Exception e) {
88             log.debug("Failed to parse json {} to map", json, e);
89         }
90         return object;
91     }
92     public static <T> List<T> toList(String json, Class<T> clazz) {
93         if (Strings.isNullOrEmpty(json)) {
94             return null;
95         }
96         List<T> object = null;
97         try {
98             JavaType type = mapper.getTypeFactory()
99                                   .constructCollectionType(List.class, clazz);
100
101             object = mapper.readerFor(type)
102                     .readValue(json);
103         }
104         catch (Exception e) {
105             log.debug("Failed to parse json {} to list", json, e);
106         }
107         return object;
108     }
109 }