2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright © 2018 IBM.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
22 package org.onap.aai.util;
24 import org.onap.aai.exceptions.AAIException;
25 import com.fasterxml.jackson.annotation.JsonInclude;
26 import com.fasterxml.jackson.databind.*;
27 import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule;
29 public class MapperUtil {
32 * Instantiates MapperUtil.
34 private MapperUtil() {
35 // prevent instantiation
41 * @param <T> the generic type
42 * @param clazz the clazz
43 * @param value the value
45 * @throws AAIException the AAI exception
47 public static <T> T readAsObjectOf(Class<T> clazz, String value) throws AAIException {
48 ObjectMapper mapper = new ObjectMapper();
50 return mapper.readValue(value, clazz);
51 } catch (Exception e) {
52 throw new AAIException("AAI_4007", e);
57 * Read with dashes as object of.
59 * @param <T> the generic type
60 * @param clazz the clazz
61 * @param value the value
63 * @throws AAIException the AAI exception
65 public static <T> T readWithDashesAsObjectOf(Class<T> clazz, String value) throws AAIException {
66 ObjectMapper mapper = new ObjectMapper();
68 mapper.registerModule(new JaxbAnnotationModule());
69 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
70 mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, false);
72 return mapper.readValue(value, clazz);
73 } catch (Exception e) {
74 throw new AAIException("AAI_4007", e);
79 * Write as JSON string.
83 * @throws AAIException the AAI exception
85 public static String writeAsJSONString(Object obj) throws AAIException {
86 ObjectMapper mapper = new ObjectMapper();
88 return mapper.writeValueAsString(obj);
89 } catch (Exception e) {
90 throw new AAIException("AAI_4008", e);
95 * Write as JSON string with dashes.
99 * @throws AAIException the AAI exception
101 public static String writeAsJSONStringWithDashes(Object obj) throws AAIException {
102 ObjectMapper mapper = new ObjectMapper();
104 mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
106 mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
107 mapper.configure(SerializationFeature.INDENT_OUTPUT, false);
108 mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
110 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
111 mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, false);
113 mapper.registerModule(new JaxbAnnotationModule());
114 return mapper.writeValueAsString(obj);
115 } catch (Exception e) {
116 throw new AAIException("AAI_4008", e);