update link to upper-constraints.txt
[msb/apigateway.git] / apiroute / apiroute-service / src / main / java / org / onap / msb / apiroute / wrapper / util / JacksonJsonUtil.java
1 /*******************************************************************************
2  * Copyright 2016-2017 ZTE, Inc. and others.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  * 
7  * http://www.apache.org/licenses/LICENSE-2.0
8  * 
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  ******************************************************************************/
14 package org.onap.msb.apiroute.wrapper.util;
15
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 import com.fasterxml.jackson.core.type.TypeReference;
20 import com.fasterxml.jackson.databind.ObjectMapper;
21 import com.fasterxml.jackson.databind.SerializationFeature;
22
23
24 public class JacksonJsonUtil {
25
26     private static final Logger logger = LoggerFactory.getLogger(JacksonJsonUtil.class);
27
28     private volatile static ObjectMapper mapper = null;
29
30     private static ObjectMapper getMapperInstance() {
31         if (mapper == null) {
32             synchronized (JacksonJsonUtil.class) {
33                 if (mapper == null) {
34                     mapper = new ObjectMapper();
35                 }
36             }
37         }
38         return mapper;
39     }
40
41     /**
42      * from java object to json
43      * 
44      * @param obj
45      * @return json
46      * @throws Exception
47      */
48     public static String beanToJson(Object obj) throws Exception {
49         String json = null;
50
51         ObjectMapper objectMapper = getMapperInstance();
52         objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
53         json = objectMapper.writeValueAsString(obj);
54
55         return json;
56     }
57
58
59
60     /**
61      * from json to java object
62      * 
63      * @param json
64      * @param cls
65      * @return
66      * @throws Exception
67      */
68     public static Object jsonToBean(String json, Class<?> cls) throws Exception {
69         Object vo = null;
70         try {
71             ObjectMapper objectMapper = getMapperInstance();
72             objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
73             vo = objectMapper.readValue(json, cls);
74
75         } catch (Exception e) {
76             logger.error(cls + " JsonTobean faild");
77             throw new Exception(cls + " JsonTobean faild");
78         }
79         return vo;
80     }
81
82     /**
83      * from json to java List
84      * 
85      * @param json
86      * @return
87      * @throws Exception
88      */
89
90     public static <T> T jsonToListBean(String json, TypeReference<T> valueTypeRef) {
91         try {
92
93             ObjectMapper objectMapper = getMapperInstance();
94
95
96             return objectMapper.readValue(json, valueTypeRef);
97
98         } catch (Exception e) {
99             String errorMsg = " JsonTobean faild:" + e.getMessage();
100             logger.error(errorMsg);
101         }
102         return null;
103     }
104
105
106
107 }