2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 Huawei Technologies Co., Ltd. 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.mso.adapters.vfc.util;
23 import java.io.IOException;
25 import org.openecomp.mso.adapters.vfc.constant.HttpCode;
26 import org.openecomp.mso.adapters.vfc.exceptions.ApplicationException;
27 import org.openecomp.mso.logger.MessageEnum;
28 import org.openecomp.mso.logger.MsoLogger;
30 import com.fasterxml.jackson.annotation.JsonInclude.Include;
31 import com.fasterxml.jackson.core.type.TypeReference;
32 import com.fasterxml.jackson.databind.DeserializationFeature;
33 import com.fasterxml.jackson.databind.ObjectMapper;
36 * Interface for json analyzing.<br/>
41 * @version ONAP Amsterdam Release 2017-9-6
43 public class JsonUtil {
48 private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.RA);
53 private static final ObjectMapper MAPPER = new ObjectMapper();
56 MAPPER.setConfig(MAPPER.getDeserializationConfig().without(
57 DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
58 MAPPER.setSerializationInclusion(Include.NON_NULL);
66 * @since ONAP Amsterdam Release 2017-9-6
73 * Parse the string in form of json.<br/>
75 * @param jsonstr json string.
76 * @param type that convert json string to
77 * @return model object
78 * @since ONAP Amsterdam Release 2017-9-6
80 public static <T> T unMarshal(String jsonstr, Class<T> type) throws ApplicationException {
82 return MAPPER.readValue(jsonstr, type);
83 } catch (IOException e) {
84 LOGGER.error(MessageEnum.RA_NS_EXC, "", "", MsoLogger.ErrorCode.BusinessProcesssError,
85 "fail to unMarshal json", e);
86 throw new ApplicationException(HttpCode.BAD_REQUEST, "fail to unMarshal json");
91 * Parse the string in form of json.<br/>
93 * @param jsonstr json string.
94 * @param type that convert json string to
95 * @return model object
96 * @since ONAP Amsterdam Release 2017-9-6
98 public static <T> T unMarshal(String jsonstr, TypeReference<T> type) throws ApplicationException {
100 return MAPPER.readValue(jsonstr, type);
101 } catch (IOException e) {
102 LOGGER.error(MessageEnum.RA_NS_EXC, "", "", MsoLogger.ErrorCode.BusinessProcesssError,
103 "fail to unMarshal json", e);
104 throw new ApplicationException(HttpCode.BAD_REQUEST, "fail to unMarshal json");
109 * Convert object to json string.<br/>
111 * @param srcObj data object
112 * @return json string
113 * @since ONAP Amsterdam Release 2017-9-6
115 public static String marshal(Object srcObj) throws ApplicationException {
117 return MAPPER.writeValueAsString(srcObj);
118 } catch (IOException e) {
119 LOGGER.error(MessageEnum.RA_NS_EXC, "", "", MsoLogger.ErrorCode.BusinessProcesssError,
120 "fail to marshal json", e);
121 throw new ApplicationException(HttpCode.BAD_REQUEST, "srcObj marshal failed!");
129 * @since ONAP Amsterdam Release 2017-9-6
131 public static ObjectMapper getMapper() {