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