5a84d9d0ef42a1f0082104e69fb70dbc061bac39
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / util / PojoUtils.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 java.io.IOException;
25
26 import org.apache.commons.io.output.ByteArrayOutputStream;
27
28 import com.fasterxml.jackson.annotation.JsonInclude;
29 import com.fasterxml.jackson.core.JsonGenerationException;
30 import com.fasterxml.jackson.databind.DeserializationFeature;
31 import com.fasterxml.jackson.databind.JsonMappingException;
32 import com.fasterxml.jackson.databind.ObjectMapper;
33 import com.fasterxml.jackson.databind.SerializationFeature;
34 import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule;
35
36 public class PojoUtils {
37         /**
38          * Gets the json from object.
39          *
40          * @param <T> the generic type
41          * @param clazz the clazz
42          * @return the json from object
43          * @throws JsonGenerationException the json generation exception
44          * @throws JsonMappingException the json mapping exception
45          * @throws IOException Signals that an I/O exception has occurred.
46          */
47         public <T> String getJsonFromObject(T clazz) throws JsonGenerationException, JsonMappingException, IOException {
48                 return getJsonFromObject(clazz, false, true);
49         }
50         
51         /**
52          * Gets the json from object.
53          *
54          * @param <T> the generic type
55          * @param clazz the clazz
56          * @param wrapRoot the wrap root
57          * @param indent the indent
58          * @return the json from object
59          * @throws JsonGenerationException the json generation exception
60          * @throws JsonMappingException the json mapping exception
61          * @throws IOException Signals that an I/O exception has occurred.
62          */
63         public <T> String getJsonFromObject(T clazz, boolean wrapRoot, boolean indent) throws JsonGenerationException, JsonMappingException, IOException {
64                 ObjectMapper mapper = new ObjectMapper();
65
66         mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
67         
68         mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
69         mapper.configure(SerializationFeature.INDENT_OUTPUT, indent);
70         mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, wrapRoot);
71
72         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
73         mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, wrapRoot);
74
75         mapper.registerModule(new JaxbAnnotationModule());
76         
77         ByteArrayOutputStream baos = new ByteArrayOutputStream();
78         
79         mapper.writeValue(baos, clazz);
80     
81         return baos.toString();
82         }
83 }