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