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 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 {
35 * @param <T> the generic type
36 * @param clazz the clazz
37 * @param value the value
39 * @throws AAIException the AAI exception
41 public static <T> T readAsObjectOf(Class<T> clazz, String value) throws AAIException {
42 com.fasterxml.jackson.databind.ObjectMapper MAPPER = new ObjectMapper();
44 return MAPPER.readValue(value, clazz);
45 } catch (Exception e) {
46 throw new AAIException("AAI_4007", e);
51 * Read with dashes as object of.
53 * @param <T> the generic type
54 * @param clazz the clazz
55 * @param value the value
57 * @throws AAIException the AAI exception
59 public static <T> T readWithDashesAsObjectOf(Class<T> clazz, String value) throws AAIException {
60 com.fasterxml.jackson.databind.ObjectMapper MAPPER = new ObjectMapper();
62 MAPPER.registerModule(new JaxbAnnotationModule());
63 MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
64 MAPPER.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, false);
66 return MAPPER.readValue(value, clazz);
67 } catch (Exception e) {
68 throw new AAIException("AAI_4007", e);
73 * Write as JSON string.
77 * @throws AAIException the AAI exception
79 public static String writeAsJSONString(Object obj) throws AAIException {
80 com.fasterxml.jackson.databind.ObjectMapper MAPPER = new ObjectMapper();
82 String s = MAPPER.writeValueAsString(obj);
84 //readValue(value, clazz);
85 } catch (Exception e) {
86 throw new AAIException("AAI_4008", e);
91 * Write as JSON string with dashes.
95 * @throws AAIException the AAI exception
97 public static String writeAsJSONStringWithDashes(Object obj) throws AAIException {
98 com.fasterxml.jackson.databind.ObjectMapper MAPPER = new ObjectMapper();
100 MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
102 MAPPER.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
103 MAPPER.configure(SerializationFeature.INDENT_OUTPUT, false);
104 MAPPER.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
106 MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
107 MAPPER.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, false);
109 MAPPER.registerModule(new JaxbAnnotationModule());
110 String s = MAPPER.writeValueAsString(obj);
112 } catch (Exception e) {
113 throw new AAIException("AAI_4008", e);