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 org.openecomp.aai.exceptions.AAIException;
24 import com.fasterxml.jackson.annotation.JsonInclude;
25 import com.fasterxml.jackson.databind.*;
26 import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule;
28 public class MapperUtil {
34 * @param <T> the generic type
35 * @param clazz the clazz
36 * @param value the value
38 * @throws AAIException the AAI exception
40 public static <T> T readAsObjectOf(Class<T> clazz, String value) throws AAIException {
41 com.fasterxml.jackson.databind.ObjectMapper MAPPER = new ObjectMapper();
43 return MAPPER.readValue(value, clazz);
44 } catch (Exception e) {
45 throw new AAIException("AAI_4007", e);
50 * Read with dashes as object of.
52 * @param <T> the generic type
53 * @param clazz the clazz
54 * @param value the value
56 * @throws AAIException the AAI exception
58 public static <T> T readWithDashesAsObjectOf(Class<T> clazz, String value) throws AAIException {
59 com.fasterxml.jackson.databind.ObjectMapper MAPPER = new ObjectMapper();
61 MAPPER.registerModule(new JaxbAnnotationModule());
62 MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
63 MAPPER.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, false);
65 return MAPPER.readValue(value, clazz);
66 } catch (Exception e) {
67 throw new AAIException("AAI_4007", e);
72 * Write as JSON string.
76 * @throws AAIException the AAI exception
78 public static String writeAsJSONString(Object obj) throws AAIException {
79 com.fasterxml.jackson.databind.ObjectMapper MAPPER = new ObjectMapper();
81 String s = MAPPER.writeValueAsString(obj);
83 //readValue(value, clazz);
84 } catch (Exception e) {
85 throw new AAIException("AAI_4008", e);
90 * Write as JSON string with dashes.
94 * @throws AAIException the AAI exception
96 public static String writeAsJSONStringWithDashes(Object obj) throws AAIException {
97 com.fasterxml.jackson.databind.ObjectMapper MAPPER = new ObjectMapper();
99 MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
101 MAPPER.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
102 MAPPER.configure(SerializationFeature.INDENT_OUTPUT, false);
103 MAPPER.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
105 MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
106 MAPPER.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, false);
108 MAPPER.registerModule(new JaxbAnnotationModule());
109 String s = MAPPER.writeValueAsString(obj);
111 } catch (Exception e) {
112 throw new AAIException("AAI_4008", e);