remove not required docs and .readthedocs.yaml
[msb/java-sdk.git] / src / main / java / org / onap / msb / sdk / discovery / util / JacksonJsonUtil.java
1 /*******************************************************************************
2  * Copyright 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 /**
15  * Copyright (C) 2015 ZTE, Inc. and others. All rights reserved. (ZTE)
16  *
17  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
18  * in compliance with the License. You may obtain a copy of the License at
19  *
20  * http://www.apache.org/licenses/LICENSE-2.0
21  *
22  * Unless required by applicable law or agreed to in writing, software distributed under the License
23  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
24  * or implied. See the License for the specific language governing permissions and limitations under
25  * the License.
26  */
27 package org.onap.msb.sdk.discovery.util;
28
29 import org.onap.msb.sdk.discovery.common.RouteException;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 import com.fasterxml.jackson.core.type.TypeReference;
34 import com.fasterxml.jackson.databind.ObjectMapper;
35
36 public class JacksonJsonUtil {
37
38     private static final Logger logger = LoggerFactory.getLogger(JacksonJsonUtil.class);
39
40     private static ObjectMapper mapper;
41
42     /**
43      * 获取ObjectMapper实例
44      * 
45      * @param createNew 方式:true,新实例;false,存在的mapper实例
46      * @return
47      */
48     public static synchronized ObjectMapper getMapperInstance() {
49         if (mapper == null) {
50             mapper = new ObjectMapper();
51         }
52         return mapper;
53     }
54
55     /**
56      * 将java对象转换成json字符串
57      * 
58      * @param obj 准备转换的对象
59      * @return json字符串
60      * @throws Exception
61      */
62     public static String beanToJson(Object obj) throws RouteException {
63         String json = null;
64         try {
65             ObjectMapper objectMapper = getMapperInstance();
66             json = objectMapper.writeValueAsString(obj);
67         } catch (Exception e) {
68             throwsRouteException("Class beanToJson faild", e, "BEAN_TO_JSON_FAILD");
69         }
70         return json;
71     }
72
73
74
75     /**
76      * 将json字符串转换成java对象
77      * 
78      * @param json 准备转换的json字符串
79      * @param cls 准备转换的类
80      * @return
81      * @throws Exception
82      */
83     public static Object jsonToBean(String json, Class<?> cls) throws RouteException {
84         Object vo = null;
85         try {
86             ObjectMapper objectMapper = getMapperInstance();
87
88
89             vo = objectMapper.readValue(json, cls);
90
91         } catch (Exception e) {
92             throwsRouteException(cls + " JsonTobean faild:" + e.getMessage(), e, "JSON_TO_BEAN_FAILD");
93         }
94         return vo;
95     }
96
97
98     private static void throwsRouteException(String errorMsg, Exception e, String errorCode) throws RouteException {
99         String msg = errorMsg + ".errorMsg:" + e.getMessage();
100         logger.error(msg);
101         throw new RouteException(errorMsg, errorCode);
102     }
103 }