Merge "Reorder modifiers"
[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.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;
29
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;
34
35 /**
36  * Interface for json analyzing.<br/>
37  * <p>
38  * </p>
39  * 
40  * @author
41  * @version ONAP Amsterdam Release 2017-9-6
42  */
43 public class JsonUtil {
44
45   /**
46    * Log service
47    */
48   private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.RA);
49
50   /**
51    * Mapper.
52    */
53   private static final ObjectMapper MAPPER = new ObjectMapper();
54
55   static {
56     MAPPER.setConfig(MAPPER.getDeserializationConfig().without(
57         DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES));
58     MAPPER.setSerializationInclusion(Include.NON_NULL);
59   }
60
61   /**
62    * Constructor<br/>
63    * <p>
64    * </p>
65    * 
66    * @since ONAP Amsterdam Release 2017-9-6
67    */
68   private JsonUtil() {
69
70   }
71
72   /**
73    * Parse the string in form of json.<br/>
74    * 
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
79    */
80   public static <T> T unMarshal(String jsonstr, Class<T> type) throws ApplicationException {
81     try {
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");
87     }
88   }
89
90   /**
91    * Parse the string in form of json.<br/>
92    * 
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
97    */
98   public static <T> T unMarshal(String jsonstr, TypeReference<T> type) throws ApplicationException {
99     try {
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");
105     }
106   }
107
108   /**
109    * Convert object to json string.<br/>
110    * 
111    * @param srcObj data object
112    * @return json string
113    * @since ONAP Amsterdam Release 2017-9-6
114    */
115   public static String marshal(Object srcObj) throws ApplicationException {
116     try {
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!");
122     }
123   }
124
125   /**
126    * Get mapper.<br/>
127    * 
128    * @return mapper
129    * @since ONAP Amsterdam Release 2017-9-6
130    */
131   public static ObjectMapper getMapper() {
132     return MAPPER;
133   }
134 }