Replaced all tabs with spaces in java and pom.xml
[so.git] / adapters / mso-vfc-adapter / src / main / java / org / onap / so / adapters / vfc / util / JsonUtil.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (C) 2018 IBM.
8  * Modifications Copyright (c) 2019 Samsung
9  * ================================================================================
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  * 
14  *      http://www.apache.org/licenses/LICENSE-2.0
15  * 
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.so.adapters.vfc.util;
25
26 import java.io.IOException;
27 import org.onap.so.adapters.vfc.constant.HttpCode;
28 import org.onap.so.adapters.vfc.exceptions.ApplicationException;
29 import org.onap.so.logger.ErrorCode;
30 import org.onap.so.logger.MessageEnum;
31 import com.fasterxml.jackson.annotation.JsonInclude.Include;
32 import com.fasterxml.jackson.core.type.TypeReference;
33 import com.fasterxml.jackson.databind.DeserializationFeature;
34 import com.fasterxml.jackson.databind.ObjectMapper;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39  * Interface for json analyzing.<br/>
40  * <p>
41  * </p>
42  * 
43  * @author
44  * @version ONAP Amsterdam Release 2017-9-6
45  */
46 public class JsonUtil {
47
48     /**
49      * Log service
50      */
51     private static final Logger logger = LoggerFactory.getLogger(JsonUtil.class);
52
53     /**
54      * Mapper.
55      */
56     private static final ObjectMapper MAPPER = new ObjectMapper();
57     private static final String UNMARSHAL_FAIL_MSG = "fail to unMarshal json";
58     static {
59         MAPPER.setConfig(MAPPER.getDeserializationConfig().without(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
60         MAPPER.setSerializationInclusion(Include.NON_NULL);
61     }
62
63     /**
64      * Constructor<br/>
65      * <p>
66      * </p>
67      * 
68      * @since ONAP Amsterdam Release 2017-9-6
69      */
70     private JsonUtil() {
71
72     }
73
74     /**
75      * Parse the string in form of json.<br/>
76      * 
77      * @param jsonstr json string.
78      * @param type that convert json string to
79      * @return model object
80      * @since ONAP Amsterdam Release 2017-9-6
81      */
82     public static <T> T unMarshal(String jsonstr, Class<T> type) throws ApplicationException {
83         try {
84             return MAPPER.readValue(jsonstr, type);
85         } catch (IOException e) {
86             logger.error("{} {} {}", MessageEnum.RA_NS_EXC.toString(), ErrorCode.BusinessProcesssError.getValue(),
87                     UNMARSHAL_FAIL_MSG, e);
88             throw new ApplicationException(HttpCode.BAD_REQUEST, UNMARSHAL_FAIL_MSG);
89         }
90     }
91
92     /**
93      * Parse the string in form of json.<br/>
94      * 
95      * @param jsonstr json string.
96      * @param type that convert json string to
97      * @return model object
98      * @since ONAP Amsterdam Release 2017-9-6
99      */
100     public static <T> T unMarshal(String jsonstr, TypeReference<T> type) throws ApplicationException {
101         try {
102             return MAPPER.readValue(jsonstr, type);
103         } catch (IOException e) {
104             logger.error("{} {} {}", MessageEnum.RA_NS_EXC.toString(), ErrorCode.BusinessProcesssError.getValue(),
105                     UNMARSHAL_FAIL_MSG, e);
106             throw new ApplicationException(HttpCode.BAD_REQUEST, UNMARSHAL_FAIL_MSG);
107         }
108     }
109
110     /**
111      * Convert object to json string.<br/>
112      * 
113      * @param srcObj data object
114      * @return json string
115      * @since ONAP Amsterdam Release 2017-9-6
116      */
117     public static String marshal(Object srcObj) throws ApplicationException {
118         try {
119             return MAPPER.writeValueAsString(srcObj);
120         } catch (IOException e) {
121             logger.error("{} {} {}", MessageEnum.RA_NS_EXC.toString(), ErrorCode.BusinessProcesssError.getValue(),
122                     "fail to marshal json", e);
123             throw new ApplicationException(HttpCode.BAD_REQUEST, "srcObj marshal failed!");
124         }
125     }
126
127     /**
128      * Get mapper.<br/>
129      * 
130      * @return mapper
131      * @since ONAP Amsterdam Release 2017-9-6
132      */
133     public static ObjectMapper getMapper() {
134         return MAPPER;
135     }
136 }