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;
28 import org.onap.so.adapters.vfc.constant.HttpCode;
29 import org.onap.so.adapters.vfc.exceptions.ApplicationException;
30 import org.onap.so.logger.ErrorCode;
31 import org.onap.so.logger.MessageEnum;
33 import com.fasterxml.jackson.annotation.JsonInclude.Include;
34 import com.fasterxml.jackson.core.type.TypeReference;
35 import com.fasterxml.jackson.databind.DeserializationFeature;
36 import com.fasterxml.jackson.databind.ObjectMapper;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * Interface for json analyzing.<br/>
46 * @version ONAP Amsterdam Release 2017-9-6
48 public class JsonUtil {
53 private static final Logger logger = LoggerFactory.getLogger(JsonUtil.class);
58 private static final ObjectMapper MAPPER = new ObjectMapper();
59 private static final String UNMARSHAL_FAIL_MSG="fail to unMarshal json";
61 MAPPER.setConfig(MAPPER.getDeserializationConfig().without(
62 DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
63 MAPPER.setSerializationInclusion(Include.NON_NULL);
71 * @since ONAP Amsterdam Release 2017-9-6
78 * Parse the string in form of json.<br/>
80 * @param jsonstr json string.
81 * @param type that convert json string to
82 * @return model object
83 * @since ONAP Amsterdam Release 2017-9-6
85 public static <T> T unMarshal(String jsonstr, Class<T> type) throws ApplicationException {
87 return MAPPER.readValue(jsonstr, type);
88 } catch (IOException e) {
89 logger.error("{} {} {}", MessageEnum.RA_NS_EXC.toString(), ErrorCode.BusinessProcesssError.getValue(),
90 UNMARSHAL_FAIL_MSG, e);
91 throw new ApplicationException(HttpCode.BAD_REQUEST, UNMARSHAL_FAIL_MSG);
96 * Parse the string in form of json.<br/>
98 * @param jsonstr json string.
99 * @param type that convert json string to
100 * @return model object
101 * @since ONAP Amsterdam Release 2017-9-6
103 public static <T> T unMarshal(String jsonstr, TypeReference<T> type) throws ApplicationException {
105 return MAPPER.readValue(jsonstr, type);
106 } catch (IOException e) {
107 logger.error("{} {} {}", MessageEnum.RA_NS_EXC.toString(), ErrorCode.BusinessProcesssError.getValue(),
108 UNMARSHAL_FAIL_MSG, e);
109 throw new ApplicationException(HttpCode.BAD_REQUEST, UNMARSHAL_FAIL_MSG);
114 * Convert object to json string.<br/>
116 * @param srcObj data object
117 * @return json string
118 * @since ONAP Amsterdam Release 2017-9-6
120 public static String marshal(Object srcObj) throws ApplicationException {
122 return MAPPER.writeValueAsString(srcObj);
123 } catch (IOException e) {
124 logger.error("{} {} {}", MessageEnum.RA_NS_EXC.toString(), ErrorCode.BusinessProcesssError.getValue(),
125 "fail to marshal json", e);
126 throw new ApplicationException(HttpCode.BAD_REQUEST, "srcObj marshal failed!");
134 * @since ONAP Amsterdam Release 2017-9-6
136 public static ObjectMapper getMapper() {