remove not required docs and .readthedocs.yaml
[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             logger.error(" JsonTobean faild:" + e.getMessage());
113         }
114         return vo;
115     }
116
117     public static <T> T jsonToListBean(String json, TypeReference<T> valueTypeRef) {
118         try {
119
120             ObjectMapper objectMapper = getMapperInstance();
121
122             return objectMapper.readValue(json, valueTypeRef);
123
124         } catch (Exception e) {
125             logger.error(" JsonTobean faild:" + e.getMessage());
126         }
127         return null;
128     }
129
130
131
132     /**
133      * 将json字符串转换成java集合对象
134      * 
135      * @param json 准备转换的json字符串
136      * @param cls 准备转换的类
137      * @return
138      * @throws Exception
139      */
140     public static Map<String, String[]> jsonToMapBean(String json) {
141         Map<String, String[]> vo = null;
142         try {
143
144             ObjectMapper objectMapper = getMapperInstance();
145
146
147             vo = objectMapper.readValue(json, new TypeReference<Map<String, String[]>>() {});
148
149         } catch (Exception e) {
150             logger.error("JsonTobean faild");
151         }
152         return vo;
153     }
154
155
156
157
158 }