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      *
44      * @param createNew create a new instanse if truer, otherwise return the existing instance
45      * @return
46      */
47     public static synchronized ObjectMapper getMapperInstance() {
48         if (mapper == null) {
49             mapper = new ObjectMapper();
50         }
51         return mapper;
52     }
53
54     /**
55      * conver java object to json
56      *
57      * @param obj
58      * @return
59      * @throws Exception
60      */
61     public static String beanToJson(Object obj) throws RouteException {
62         String json = null;
63         try {
64             ObjectMapper objectMapper = getMapperInstance();
65             json = objectMapper.writeValueAsString(obj);
66         } catch (Exception e) {
67             throwsRouteException("Class beanToJson faild", e, "BEAN_TO_JSON_FAILD");
68         }
69         return json;
70     }
71
72
73
74     /**
75      * convert json string to java object
76      *
77      * @param json
78      * @param cls
79      * @return
80      * @throws Exception
81      */
82     public static Object jsonToBean(String json, Class<?> cls) throws RouteException {
83         Object vo = null;
84         try {
85             ObjectMapper objectMapper = getMapperInstance();
86
87
88             vo = objectMapper.readValue(json, cls);
89
90         } catch (Exception e) {
91             throwsRouteException(cls + " JsonTobean faild:" + e.getMessage(), e, "JSON_TO_BEAN_FAILD");
92         }
93         return vo;
94     }
95
96
97     private static void throwsRouteException(String errorMsg, Exception e, String errorCode) throws RouteException {
98         String msg = errorMsg + ".errorMsg:" + e.getMessage();
99         logger.error(msg);
100         throw new RouteException(errorMsg, errorCode);
101     }
102 }