2 * ============LICENSE_START=======================================================
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
14 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
24 package org.onap.so.adapters.vfc.util;
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;
39 * Interface for json analyzing.<br/>
44 * @version ONAP Amsterdam Release 2017-9-6
46 public class JsonUtil {
51 private static final Logger logger = LoggerFactory.getLogger(JsonUtil.class);
56 private static final ObjectMapper MAPPER = new ObjectMapper();
57 private static final String UNMARSHAL_FAIL_MSG = "fail to unMarshal json";
59 MAPPER.setConfig(MAPPER.getDeserializationConfig().without(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
60 MAPPER.setSerializationInclusion(Include.NON_NULL);
68 * @since ONAP Amsterdam Release 2017-9-6
75 * Parse the string in form of json.<br/>
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
82 public static <T> T unMarshal(String jsonstr, Class<T> type) throws ApplicationException {
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);
93 * Parse the string in form of json.<br/>
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
100 public static <T> T unMarshal(String jsonstr, TypeReference<T> type) throws ApplicationException {
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);
111 * Convert object to json string.<br/>
113 * @param srcObj data object
114 * @return json string
115 * @since ONAP Amsterdam Release 2017-9-6
117 public static String marshal(Object srcObj) throws ApplicationException {
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!");
131 * @since ONAP Amsterdam Release 2017-9-6
133 public static ObjectMapper getMapper() {