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