AAI-common sonar fixes
[aai/aai-common.git] / aai-core / 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.*;
27 import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule;
28
29 import org.onap.aai.exceptions.AAIException;
30
31 public class MapperUtil {
32
33     /**
34      * Instantiates MapperUtil.
35      */
36     private MapperUtil() {
37         // prevent instantiation
38     }
39
40     /**
41      * Read as object of.
42      *
43      * @param <T> the generic type
44      * @param clazz the clazz
45      * @param value the value
46      * @return the t
47      * @throws AAIException the AAI exception
48      */
49     public static <T> T readAsObjectOf(Class<T> clazz, String value) throws AAIException {
50         ObjectMapper mapper = new ObjectMapper();
51         try {
52             return mapper.readValue(value, clazz);
53         } catch (Exception e) {
54             throw new AAIException("AAI_4007", e);
55         }
56     }
57
58     /**
59      * Read with dashes as object of.
60      *
61      * @param <T> the generic type
62      * @param clazz the clazz
63      * @param value the value
64      * @return the t
65      * @throws AAIException the AAI exception
66      */
67     public static <T> T readWithDashesAsObjectOf(Class<T> clazz, String value) throws AAIException {
68         ObjectMapper mapper = new ObjectMapper();
69         try {
70             mapper.registerModule(new JaxbAnnotationModule());
71             mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
72             mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, false);
73
74             return mapper.readValue(value, clazz);
75         } catch (Exception e) {
76             throw new AAIException("AAI_4007", e);
77         }
78     }
79
80     /**
81      * Write as JSON string.
82      *
83      * @param obj the obj
84      * @return the string
85      * @throws AAIException the AAI exception
86      */
87     public static String writeAsJSONString(Object obj) throws AAIException {
88         ObjectMapper mapper = new ObjectMapper();
89         try {
90             return mapper.writeValueAsString(obj);
91         } catch (Exception e) {
92             throw new AAIException("AAI_4008", e);
93         }
94     }
95
96     /**
97      * Write as JSON string with dashes.
98      *
99      * @param obj the obj
100      * @return the string
101      * @throws AAIException the AAI exception
102      */
103     public static String writeAsJSONStringWithDashes(Object obj) throws AAIException {
104         ObjectMapper mapper = new ObjectMapper();
105         try {
106             mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
107
108             mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
109             mapper.configure(SerializationFeature.INDENT_OUTPUT, false);
110             mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
111
112             mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
113             mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, false);
114
115             mapper.registerModule(new JaxbAnnotationModule());
116             return mapper.writeValueAsString(obj);
117         } catch (Exception e) {
118             throw new AAIException("AAI_4008", e);
119         }
120     }
121 }