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