Divide the MSB source codes into two repos
[msb/apigateway.git] / apiroute / apiroute-service / src / main / java / org / onap / msb / apiroute / wrapper / util / JacksonJsonUtil.java
1 /**
2  * Copyright 2016 ZTE, Inc. and others.
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
17 package org.onap.msb.apiroute.wrapper.util;
18
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 import com.fasterxml.jackson.core.type.TypeReference;
23 import com.fasterxml.jackson.databind.ObjectMapper;
24 import com.fasterxml.jackson.databind.SerializationFeature;
25
26
27 public class JacksonJsonUtil {
28
29   private static final Logger logger = LoggerFactory.getLogger(JacksonJsonUtil.class);
30
31   private volatile static ObjectMapper mapper = null;
32
33   private static ObjectMapper getMapperInstance() {
34     if (mapper == null) {
35       synchronized (JacksonJsonUtil.class) {
36         if (mapper == null) {
37           mapper = new ObjectMapper();
38         }
39       }
40     }
41     return mapper;
42   }
43
44   /**
45    * from java object to json
46    * 
47    * @param obj
48    * @return json
49    * @throws Exception
50    */
51   public static String beanToJson(Object obj) throws Exception {
52       String json = null;
53
54       ObjectMapper objectMapper = getMapperInstance();
55       objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
56       json = objectMapper.writeValueAsString(obj);
57     
58       return json;
59   }
60
61
62
63   /**
64    * from json to java object
65    * 
66    * @param json
67    * @param cls
68    * @return
69    * @throws Exception
70    */
71   public static Object jsonToBean(String json, Class<?> cls) throws Exception {
72     Object vo = null;
73     try {
74       ObjectMapper objectMapper = getMapperInstance();
75       objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
76       vo = objectMapper.readValue(json, cls);
77
78     } catch (Exception e) {
79       logger.error(cls + " JsonTobean faild");
80       throw new Exception(cls + " JsonTobean faild");
81     }
82     return vo;
83   }
84
85   /**
86    * from json to java List
87    * 
88    * @param json
89    * @return
90    * @throws Exception
91    */
92
93   public static <T> T jsonToListBean(String json, TypeReference<T> valueTypeRef) {
94     try {
95
96       ObjectMapper objectMapper = getMapperInstance();
97
98
99       return objectMapper.readValue(json, valueTypeRef);
100
101     } catch (Exception e) {
102       String errorMsg = " JsonTobean faild:" + e.getMessage();
103       logger.error(errorMsg);
104     }
105     return null;
106   }
107
108
109
110 }