2234e1911010cbc50b6c967e4390682140d32e61
[vfc/nfvo/wfengine.git] / rest-client / src / main / java / org / openo / baseservice / roa / util / clientsdk / JsonUtil.java
1 /*
2  * Copyright (c) 2016, Huawei Technologies Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.openo.baseservice.roa.util.clientsdk;
17
18 import org.codehaus.jackson.map.ObjectMapper;
19 import org.codehaus.jackson.type.TypeReference;
20
21 import net.sf.json.JSON;
22
23 import java.io.IOException;
24
25 /**
26  * JSON parse utilities.
27  * <br/>
28  * <p>
29  * </p>
30  * 
31  * @author
32  * @version SDNO 0.5 28-May-2016
33  */
34 public final class JsonUtil {
35
36     private static final ObjectMapper MAPPER = new ObjectMapper();
37
38     static {
39         MAPPER.setDeserializationConfig(MAPPER.getDeserializationConfig()
40                 .without(org.codehaus.jackson.map.DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES));
41     }
42
43     private JsonUtil() {
44     }
45
46     /**
47      * Parse JSON formated string.<br/>
48      * 
49      * @param jsonstr: JSON formated string.
50      * @param type: result type.
51      * @return parsed object.
52      * @throws IOException incase error.
53      * @since SDNO 0.5
54      */
55     public static <T> T unMarshal(final String jsonstr, final Class<T> type) throws IOException {
56         return MAPPER.readValue(jsonstr, type);
57     }
58
59     /**
60      * Parse JSON formatted string (for a generic target type).
61      * <br/>
62      * 
63      * @param jsonstr request data.
64      * @param type target type.
65      * @return result object.
66      * @throws IOException incase error.
67      * @since SDNO 0.5
68      */
69     public static <T> T unMarshal(final String jsonstr, final TypeReference<T> type) throws IOException {
70         return MAPPER.readValue(jsonstr, type);
71     }
72
73     /**
74      * Convert object to JSON format string.
75      * <br/>
76      * 
77      * @param srcObj source object.
78      * @return JSON format string.
79      * @throws IOException incase of error.
80      * @since SDNO 0.5
81      */
82     public static String marshal(final Object srcObj) throws IOException {
83         if(srcObj instanceof JSON) {
84             return srcObj.toString();
85         }
86         return MAPPER.writeValueAsString(srcObj);
87     }
88
89     /**
90      * Get parsing mapper
91      * <br/>
92      * 
93      * @return parsing mapper
94      * @since SDNO 0.5
95      */
96     public static ObjectMapper getMapper() {
97         return MAPPER;
98     }
99 }