2 * ============LICENSE_START=======================================================
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
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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 package org.onap.aai.util;
24 import java.io.IOException;
26 import org.apache.commons.io.output.ByteArrayOutputStream;
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;
36 public class PojoUtils {
38 * Gets the json from object.
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.
47 public <T> String getJsonFromObject(T clazz) throws JsonGenerationException, JsonMappingException, IOException {
48 return getJsonFromObject(clazz, false, true);
52 * Gets the json from object.
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.
63 public <T> String getJsonFromObject(T clazz, boolean wrapRoot, boolean indent) throws JsonGenerationException, JsonMappingException, IOException {
64 ObjectMapper mapper = new ObjectMapper();
66 mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
68 mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
69 mapper.configure(SerializationFeature.INDENT_OUTPUT, indent);
70 mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, wrapRoot);
72 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
73 mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, wrapRoot);
75 mapper.registerModule(new JaxbAnnotationModule());
77 ByteArrayOutputStream baos = new ByteArrayOutputStream();
79 mapper.writeValue(baos, clazz);
81 return baos.toString();