2 * Copyright 2016-2017 ZTE, Inc. and others.
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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
14 package org.onap.msb.sdclient.wrapper.util;
16 import java.util.List;
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;
24 import com.fasterxml.jackson.core.type.TypeReference;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import com.fasterxml.jackson.databind.SerializationFeature;
29 public class JacksonJsonUtil {
31 private static final Logger logger = LoggerFactory.getLogger(JacksonJsonUtil.class);
33 private static ObjectMapper mapper;
38 * @param createNew 方式:true,新实例;false,存在的mapper实例
41 public static synchronized ObjectMapper getMapperInstance() {
43 mapper = new ObjectMapper();
55 public static String beanToJson(Object obj) throws Exception {
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());
73 * @param json 准备转换的json字符串
78 public static Object jsonToBean(String json, Class<?> cls) throws Exception {
81 ObjectMapper objectMapper = getMapperInstance();
82 objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
83 vo = objectMapper.readValue(json, cls);
85 } catch (Exception e) {
86 logger.error(cls + " JsonTobean faild:" + e.getMessage());
87 throw new Exception(cls + " JsonTobean faild:" + e.getMessage());
97 * @param json 准备转换的json字符串
102 public static List<CatalogService> jsonToListBean(String json) {
103 List<CatalogService> vo = null;
106 ObjectMapper objectMapper = getMapperInstance();
109 vo = objectMapper.readValue(json, new TypeReference<List<CatalogService>>() {});
111 } catch (Exception e) {
112 String errorMsg = " JsonTobean faild:" + e.getMessage();
113 logger.error(errorMsg);
118 public static <T> T jsonToListBean(String json, TypeReference<T> valueTypeRef) {
121 ObjectMapper objectMapper = getMapperInstance();
124 return objectMapper.readValue(json, valueTypeRef);
126 } catch (Exception e) {
127 String errorMsg = " JsonTobean faild:" + e.getMessage();
128 logger.error(errorMsg);
136 * 将json字符串转换成java集合对象
138 * @param json 准备转换的json字符串
143 public static Map<String, String[]> jsonToMapBean(String json) {
144 Map<String, String[]> vo = null;
147 ObjectMapper objectMapper = getMapperInstance();
150 vo = objectMapper.readValue(json, new TypeReference<Map<String, String[]>>() {});
152 } catch (Exception e) {
153 String errorMsg = " JsonTobean faild";
154 logger.error(errorMsg);
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);