Update the license for 2017-2018 license
[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         com.fasterxml.jackson.databind.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           com.fasterxml.jackson.databind.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           com.fasterxml.jackson.databind.ObjectMapper MAPPER = new ObjectMapper();
79             try {
80              String s =  MAPPER.writeValueAsString(obj);
81              return s;           
82                          //readValue(value, clazz);
83             } catch (Exception e) {
84                 throw new AAIException("AAI_4008", e);
85             }
86           }
87   
88   /**
89    * Write as JSON string with dashes.
90    *
91    * @param obj the obj
92    * @return the string
93    * @throws AAIException the AAI exception
94    */
95   public static String writeAsJSONStringWithDashes(Object obj) throws AAIException  {
96           com.fasterxml.jackson.databind.ObjectMapper MAPPER = new ObjectMapper();
97             try {
98                 MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
99             
100             MAPPER.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
101             MAPPER.configure(SerializationFeature.INDENT_OUTPUT, false);
102             MAPPER.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
103
104             MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
105             MAPPER.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, false);
106
107             MAPPER.registerModule(new JaxbAnnotationModule());
108             String s =  MAPPER.writeValueAsString(obj);
109             return s;            
110             } catch (Exception e) {
111                 throw new AAIException("AAI_4008", e);
112             }
113           }
114 }