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