Reduce the number of problems in aai-common by removing unused imports
[aai/aai-common.git] / aai-els-onap-logging / src / main / java / org / onap / aai / util / MapperUtil.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  *  Modifications Copyright © 2018 IBM.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *    http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.aai.util;
24
25 import com.fasterxml.jackson.annotation.JsonInclude;
26 import com.fasterxml.jackson.databind.DeserializationFeature;
27 import com.fasterxml.jackson.databind.ObjectMapper;
28 import com.fasterxml.jackson.databind.SerializationFeature;
29 import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule;
30
31 import org.onap.aai.exceptions.AAIException;
32
33 public class MapperUtil {
34
35     /**
36      * Instantiates MapperUtil.
37      */
38     private MapperUtil() {
39         // prevent instantiation
40     }
41
42     /**
43      * Read as object of.
44      *
45      * @param <T> the generic type
46      * @param clazz the clazz
47      * @param value the value
48      * @return the t
49      * @throws AAIException the AAI exception
50      */
51     public static <T> T readAsObjectOf(Class<T> clazz, String value) throws AAIException {
52         ObjectMapper mapper = new ObjectMapper();
53         try {
54             return mapper.readValue(value, clazz);
55         } catch (Exception e) {
56             throw new AAIException("AAI_4007", e);
57         }
58     }
59
60     /**
61      * Read with dashes as object of.
62      *
63      * @param <T> the generic type
64      * @param clazz the clazz
65      * @param value the value
66      * @return the t
67      * @throws AAIException the AAI exception
68      */
69     public static <T> T readWithDashesAsObjectOf(Class<T> clazz, String value) throws AAIException {
70         ObjectMapper mapper = new ObjectMapper();
71         try {
72             mapper.registerModule(new JaxbAnnotationModule());
73             mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
74             mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, false);
75
76             return mapper.readValue(value, clazz);
77         } catch (Exception e) {
78             throw new AAIException("AAI_4007", e);
79         }
80     }
81
82     /**
83      * Write as JSON string.
84      *
85      * @param obj the obj
86      * @return the string
87      * @throws AAIException the AAI exception
88      */
89     public static String writeAsJSONString(Object obj) throws AAIException {
90         ObjectMapper mapper = new ObjectMapper();
91         try {
92             return mapper.writeValueAsString(obj);
93         } catch (Exception e) {
94             throw new AAIException("AAI_4008", e);
95         }
96     }
97
98     /**
99      * Write as JSON string with dashes.
100      *
101      * @param obj the obj
102      * @return the string
103      * @throws AAIException the AAI exception
104      */
105     public static String writeAsJSONStringWithDashes(Object obj) throws AAIException {
106         ObjectMapper mapper = new ObjectMapper();
107         try {
108             mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
109
110             mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
111             mapper.configure(SerializationFeature.INDENT_OUTPUT, false);
112             mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
113
114             mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
115             mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, false);
116
117             mapper.registerModule(new JaxbAnnotationModule());
118             return mapper.writeValueAsString(obj);
119         } catch (Exception e) {
120             throw new AAIException("AAI_4008", e);
121         }
122     }
123 }