69ad951aa558b2337dbf30b530e1a8301f7a02ce
[so.git] / adapters / mso-vfc-adapter / src / main / java / org / openecomp / mso / 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  * 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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.mso.adapters.vfc.util;
22
23 import java.io.IOException;
24
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;
32
33 /**
34  * Interface for json analyzing.<br/>
35  * <p>
36  * </p>
37  * 
38  * @author
39  * @version ONAP Amsterdam Release 2017-9-6
40  */
41 public class JsonUtil {
42
43   /**
44    * Log service
45    */
46   private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.RA);
47
48   /**
49    * Mapper.
50    */
51   private static final ObjectMapper MAPPER = new ObjectMapper();
52
53   static {
54     MAPPER.setDeserializationConfig(MAPPER.getDeserializationConfig().without(
55         org.codehaus.jackson.map.DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES));
56     MAPPER.setSerializationInclusion(Inclusion.NON_NULL);
57   }
58
59   /**
60    * Constructor<br/>
61    * <p>
62    * </p>
63    * 
64    * @since ONAP Amsterdam Release 2017-9-6
65    */
66   private JsonUtil() {
67
68   }
69
70   /**
71    * Parse the string in form of json.<br/>
72    * 
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
77    */
78   public static <T> T unMarshal(String jsonstr, Class<T> type) throws ApplicationException {
79     try {
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");
85     }
86   }
87
88   /**
89    * Parse the string in form of json.<br/>
90    * 
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
95    */
96   public static <T> T unMarshal(String jsonstr, TypeReference<T> type) throws ApplicationException {
97     try {
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");
103     }
104   }
105
106   /**
107    * Convert object to json string.<br/>
108    * 
109    * @param srcObj data object
110    * @return json string
111    * @since ONAP Amsterdam Release 2017-9-6
112    */
113   public static String marshal(Object srcObj) throws ApplicationException {
114     try {
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!");
120     }
121   }
122
123   /**
124    * Get mapper.<br/>
125    * 
126    * @return mapper
127    * @since ONAP Amsterdam Release 2017-9-6
128    */
129   public static ObjectMapper getMapper() {
130     return MAPPER;
131   }
132 }