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 org.onap.aai.exceptions.AAIException;
23 import com.fasterxml.jackson.annotation.JsonInclude;
24 import com.fasterxml.jackson.databind.*;
25 import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule;
27 public class MapperUtil {
33 * @param <T> the generic type
34 * @param clazz the clazz
35 * @param value the value
37 * @throws AAIException the AAI exception
39 public static <T> T readAsObjectOf(Class<T> clazz, String value) throws AAIException {
40 com.fasterxml.jackson.databind.ObjectMapper MAPPER = new ObjectMapper();
42 return MAPPER.readValue(value, clazz);
43 } catch (Exception e) {
44 throw new AAIException("AAI_4007", e);
49 * Read with dashes as object of.
51 * @param <T> the generic type
52 * @param clazz the clazz
53 * @param value the value
55 * @throws AAIException the AAI exception
57 public static <T> T readWithDashesAsObjectOf(Class<T> clazz, String value) throws AAIException {
58 com.fasterxml.jackson.databind.ObjectMapper MAPPER = new ObjectMapper();
60 MAPPER.registerModule(new JaxbAnnotationModule());
61 MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
62 MAPPER.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, false);
64 return MAPPER.readValue(value, clazz);
65 } catch (Exception e) {
66 throw new AAIException("AAI_4007", e);
71 * Write as JSON string.
75 * @throws AAIException the AAI exception
77 public static String writeAsJSONString(Object obj) throws AAIException {
78 com.fasterxml.jackson.databind.ObjectMapper MAPPER = new ObjectMapper();
80 String s = MAPPER.writeValueAsString(obj);
82 //readValue(value, clazz);
83 } catch (Exception e) {
84 throw new AAIException("AAI_4008", e);
89 * Write as JSON string with dashes.
93 * @throws AAIException the AAI exception
95 public static String writeAsJSONStringWithDashes(Object obj) throws AAIException {
96 com.fasterxml.jackson.databind.ObjectMapper MAPPER = new ObjectMapper();
98 MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
100 MAPPER.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
101 MAPPER.configure(SerializationFeature.INDENT_OUTPUT, false);
102 MAPPER.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
104 MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
105 MAPPER.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, false);
107 MAPPER.registerModule(new JaxbAnnotationModule());
108 String s = MAPPER.writeValueAsString(obj);
110 } catch (Exception e) {
111 throw new AAIException("AAI_4008", e);