382dc64be9e8725748658d2167a32977c8bf83af
[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       String errorMsg = "Class beanToJson faild";
69       throwsRouteException(errorMsg, e, "BEAN_TO_JSON_FAILD");
70     }
71     return json;
72   }
73
74
75
76   /**
77    * 将json字符串转换成java对象
78    * 
79    * @param json 准备转换的json字符串
80    * @param cls 准备转换的类
81    * @return
82    * @throws Exception
83    */
84   public static Object jsonToBean(String json, Class<?> cls) throws RouteException {
85     Object vo = null;
86     try {
87       ObjectMapper objectMapper = getMapperInstance();
88
89
90       vo = objectMapper.readValue(json, cls);
91
92     } catch (Exception e) {
93       String errorMsg = cls + " JsonTobean faild:" + e.getMessage();
94       throwsRouteException(errorMsg, e, "JSON_TO_BEAN_FAILD");
95     }
96     return vo;
97   }
98
99
100   /**
101    * 将json字符串转换成java集合对象
102    * 
103    * @param json 准备转换的json字符串
104    * @param cls 准备转换的类
105    * @return
106    * @throws Exception
107    */
108
109   public static <T> T jsonToListBean(String json, TypeReference<T> valueTypeRef) {
110     try {
111
112       ObjectMapper objectMapper = getMapperInstance();
113
114
115       return objectMapper.readValue(json, valueTypeRef);
116
117     } catch (Exception e) {
118       String errorMsg = " JsonTobean faild:" + e.getMessage();
119       logger.error(errorMsg);
120     }
121     return null;
122   }
123
124
125
126   private static void throwsRouteException(String errorMsg, Exception e, String errorCode)
127       throws RouteException {
128     String msg = errorMsg + ".errorMsg:" + e.getMessage();
129     logger.error(msg);
130     throw new RouteException(errorMsg, errorCode);
131   }
132 }