Fix java check style issue
[msb/discovery.git] / sdclient / discovery-service / src / main / java / org / onap / msb / sdclient / 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.sdclient.wrapper.util;
15
16 import java.util.List;
17 import java.util.Map;
18
19 import org.onap.msb.sdclient.core.CatalogService;
20 import org.onap.msb.sdclient.core.HealthService;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 import com.fasterxml.jackson.core.type.TypeReference;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import com.fasterxml.jackson.databind.SerializationFeature;
27
28
29 public class JacksonJsonUtil {
30
31     private static final Logger logger = LoggerFactory.getLogger(JacksonJsonUtil.class);
32
33     private static ObjectMapper mapper;
34
35     /**
36      * 获取ObjectMapper实例
37      * 
38      * @param createNew 方式:true,新实例;false,存在的mapper实例
39      * @return
40      */
41     public static synchronized ObjectMapper getMapperInstance() {
42         if (mapper == null) {
43             mapper = new ObjectMapper();
44         }
45         return mapper;
46     }
47
48     /**
49      * 将java对象转换成json字符串
50      * 
51      * @param obj 准备转换的对象
52      * @return json字符串
53      * @throws Exception
54      */
55     public static String beanToJson(Object obj) throws Exception {
56         String json = null;
57         try {
58             ObjectMapper objectMapper = getMapperInstance();
59             objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
60             json = objectMapper.writeValueAsString(obj);
61         } catch (Exception e) {
62             logger.error("Class beanToJson faild:" + e.getMessage());
63             throw new Exception("Class beanToJson faild:" + e.getMessage());
64         }
65         return json;
66     }
67
68
69
70     /**
71      * 将json字符串转换成java对象
72      * 
73      * @param json 准备转换的json字符串
74      * @param cls 准备转换的类
75      * @return
76      * @throws Exception
77      */
78     public static Object jsonToBean(String json, Class<?> cls) throws Exception {
79         Object vo = null;
80         try {
81             ObjectMapper objectMapper = getMapperInstance();
82             objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
83             vo = objectMapper.readValue(json, cls);
84
85         } catch (Exception e) {
86             logger.error(cls + " JsonTobean faild:" + e.getMessage());
87             throw new Exception(cls + " JsonTobean faild:" + e.getMessage());
88         }
89         return vo;
90     }
91
92
93
94     /**
95      * 将json字符串转换成java集合对象
96      * 
97      * @param json 准备转换的json字符串
98      * @param cls 准备转换的类
99      * @return
100      * @throws Exception
101      */
102     public static List<CatalogService> jsonToListBean(String json) {
103         List<CatalogService> vo = null;
104         try {
105
106             ObjectMapper objectMapper = getMapperInstance();
107
108
109             vo = objectMapper.readValue(json, new TypeReference<List<CatalogService>>() {});
110
111         } catch (Exception e) {
112             String errorMsg = " JsonTobean faild:" + e.getMessage();
113             logger.error(errorMsg);
114         }
115         return vo;
116     }
117
118     public static <T> T jsonToListBean(String json, TypeReference<T> valueTypeRef) {
119         try {
120
121             ObjectMapper objectMapper = getMapperInstance();
122
123
124             return objectMapper.readValue(json, valueTypeRef);
125
126         } catch (Exception e) {
127             String errorMsg = " JsonTobean faild:" + e.getMessage();
128             logger.error(errorMsg);
129         }
130         return null;
131     }
132
133
134
135     /**
136      * 将json字符串转换成java集合对象
137      * 
138      * @param json 准备转换的json字符串
139      * @param cls 准备转换的类
140      * @return
141      * @throws Exception
142      */
143     public static Map<String, String[]> jsonToMapBean(String json) {
144         Map<String, String[]> vo = null;
145         try {
146
147             ObjectMapper objectMapper = getMapperInstance();
148
149
150             vo = objectMapper.readValue(json, new TypeReference<Map<String, String[]>>() {});
151
152         } catch (Exception e) {
153             String errorMsg = " JsonTobean faild";
154             logger.error(errorMsg);
155         }
156         return vo;
157     }
158
159
160     public static void main(String[] args) {
161         String json = "[{\"Node\":{\"Node\":\"A23179111\",\"Address\":\"10.74.44.27\",\"CreateIndex\":3,\"ModifyIndex\":318},\"Service\":{\"ID\":\"oo_10.74.56.36_5656\",\"Service\":\"oo\",\"Tags\":[\"url:/root\",\"protocol:REST\",\"version:\",\"visualRange:0|1\",\"ttl:-1\",\"status:1\",\"lb_policy:client_custom\",\"lb_server_params:weight=1 max_fails=1 fail_timeout=16s\",\"checkType:TCP\",\"checkInterval:10\",\"checkUrl:10.56.23.63:8989\"],\"Address\":\"10.74.56.36\",\"Port\":5656,\"EnableTagOverride\":false,\"CreateIndex\":314,\"ModifyIndex\":318},\"Checks\":[{\"Node\":\"A23179111\",\"CheckID\":\"serfHealth\",\"Name\":\"Serf Health Status\",\"Status\":\"passing\",\"Notes\":\"\",\"Output\":\"Agent alive and reachable\",\"ServiceID\":\"\",\"ServiceName\":\"\",\"CreateIndex\":3,\"ModifyIndex\":3},{\"Node\":\"A23179111\",\"CheckID\":\"service:oo_10.74.56.36_5656\",\"Name\":\"Service 'oo' check\",\"Status\":\"critical\",\"Notes\":\"\",\"Output\":\"\",\"ServiceID\":\"oo_10.74.56.36_5656\",\"ServiceName\":\"oo\",\"CreateIndex\":314,\"ModifyIndex\":318}]},{\"Node\":{\"Node\":\"A23179111\",\"Address\":\"10.74.44.27\",\"CreateIndex\":3,\"ModifyIndex\":318},\"Service\":{\"ID\":\"oo_10.78.36.36_111\",\"Service\":\"oo\",\"Tags\":[\"url:/root\",\"protocol:REST\",\"version:\",\"visualRange:0|1\",\"ttl:-1\",\"status:1\",\"lb_policy:client_custom\"],\"Address\":\"10.78.36.36\",\"Port\":111,\"EnableTagOverride\":false,\"CreateIndex\":315,\"ModifyIndex\":315},\"Checks\":[{\"Node\":\"A23179111\",\"CheckID\":\"serfHealth\",\"Name\":\"Serf Health Status\",\"Status\":\"passing\",\"Notes\":\"\",\"Output\":\"Agent alive and reachable\",\"ServiceID\":\"\",\"ServiceName\":\"\",\"CreateIndex\":3,\"ModifyIndex\":3}]}]";
162         List<HealthService> list = jsonToListBean(json, new TypeReference<List<HealthService>>() {});
163         System.out.println(list);
164
165     }
166
167
168
169 }