2 * ============LICENSE_START=======================================================
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
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=========================================================
21 package org.openecomp.aai.util;
23 import java.io.IOException;
25 import org.apache.commons.io.output.ByteArrayOutputStream;
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;
35 public class PojoUtils {
37 * Gets the json from object.
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.
46 public <T> String getJsonFromObject(T clazz) throws JsonGenerationException, JsonMappingException, IOException {
47 return getJsonFromObject(clazz, false, true);
51 * Gets the json from object.
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.
62 public <T> String getJsonFromObject(T clazz, boolean wrapRoot, boolean indent) throws JsonGenerationException, JsonMappingException, IOException {
63 ObjectMapper mapper = new ObjectMapper();
65 mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
67 mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
68 mapper.configure(SerializationFeature.INDENT_OUTPUT, indent);
69 mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, wrapRoot);
71 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
72 mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, wrapRoot);
74 mapper.registerModule(new JaxbAnnotationModule());
76 ByteArrayOutputStream baos = new ByteArrayOutputStream();
78 mapper.writeValue(baos, clazz);
80 return baos.toString();