2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.aai.util;
22 import java.io.IOException;
24 import org.apache.commons.io.output.ByteArrayOutputStream;
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;
34 public class PojoUtils {
36 * Gets the json from object.
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.
45 public <T> String getJsonFromObject(T clazz) throws JsonGenerationException, JsonMappingException, IOException {
46 return getJsonFromObject(clazz, false, true);
50 * Gets the json from object.
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.
61 public <T> String getJsonFromObject(T clazz, boolean wrapRoot, boolean indent) throws JsonGenerationException, JsonMappingException, IOException {
62 ObjectMapper mapper = new ObjectMapper();
64 mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
66 mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
67 mapper.configure(SerializationFeature.INDENT_OUTPUT, indent);
68 mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, wrapRoot);
70 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
71 mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, wrapRoot);
73 mapper.registerModule(new JaxbAnnotationModule());
75 ByteArrayOutputStream baos = new ByteArrayOutputStream();
77 mapper.writeValue(baos, clazz);
79 return baos.toString();