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.codehaus.jackson.map.ObjectMapper;
26 import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;
27 import org.codehaus.jackson.type.TypeReference;
28 import org.openecomp.mso.adapters.vfc.constant.HttpCode;
29 import org.openecomp.mso.adapters.vfc.exceptions.ApplicationException;
30 import org.openecomp.mso.logger.MessageEnum;
31 import org.openecomp.mso.logger.MsoLogger;
34 * Interface for json analyzing.<br/>
39 * @version ONAP Amsterdam Release 2017-9-6
41 public class JsonUtil {
46 private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.RA);
51 private static final ObjectMapper MAPPER = new ObjectMapper();
54 MAPPER.setDeserializationConfig(MAPPER.getDeserializationConfig().without(
55 org.codehaus.jackson.map.DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES));
56 MAPPER.setSerializationInclusion(Inclusion.NON_NULL);
64 * @since ONAP Amsterdam Release 2017-9-6
71 * Parse the string in form of json.<br/>
73 * @param jsonstr json string.
74 * @param type that convert json string to
75 * @return model object
76 * @since ONAP Amsterdam Release 2017-9-6
78 public static <T> T unMarshal(String jsonstr, Class<T> type) throws ApplicationException {
80 return MAPPER.readValue(jsonstr, type);
81 } catch (IOException e) {
82 LOGGER.error(MessageEnum.RA_NS_EXC, "", "", MsoLogger.ErrorCode.BusinessProcesssError,
83 "fail to unMarshal json", e);
84 throw new ApplicationException(HttpCode.BAD_REQUEST, "fail to unMarshal json");
89 * Parse the string in form of json.<br/>
91 * @param jsonstr json string.
92 * @param type that convert json string to
93 * @return model object
94 * @since ONAP Amsterdam Release 2017-9-6
96 public static <T> T unMarshal(String jsonstr, TypeReference<T> type) throws ApplicationException {
98 return MAPPER.readValue(jsonstr, type);
99 } catch (IOException e) {
100 LOGGER.error(MessageEnum.RA_NS_EXC, "", "", MsoLogger.ErrorCode.BusinessProcesssError,
101 "fail to unMarshal json", e);
102 throw new ApplicationException(HttpCode.BAD_REQUEST, "fail to unMarshal json");
107 * Convert object to json string.<br/>
109 * @param srcObj data object
110 * @return json string
111 * @since ONAP Amsterdam Release 2017-9-6
113 public static String marshal(Object srcObj) throws ApplicationException {
115 return MAPPER.writeValueAsString(srcObj);
116 } catch (IOException e) {
117 LOGGER.error(MessageEnum.RA_NS_EXC, "", "", MsoLogger.ErrorCode.BusinessProcesssError,
118 "fail to marshal json", e);
119 throw new ApplicationException(HttpCode.BAD_REQUEST, "srcObj marshal failed!");
127 * @since ONAP Amsterdam Release 2017-9-6
129 public static ObjectMapper getMapper() {