|
@@ -0,0 +1,100 @@
|
|
|
+package com.llisoft.sms.util;
|
|
|
+
|
|
|
+import java.util.Objects;
|
|
|
+
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+
|
|
|
+import com.fasterxml.jackson.core.type.TypeReference;
|
|
|
+import com.fasterxml.jackson.databind.DeserializationFeature;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Json处理工具类
|
|
|
+ * @author YangJie [2018年12月26日]
|
|
|
+ */
|
|
|
+public class JsonUtil {
|
|
|
+
|
|
|
+ private static Logger logger = LoggerFactory.getLogger(JsonUtil.class);
|
|
|
+
|
|
|
+ public static ObjectMapper objectMapper = new ObjectMapper()
|
|
|
+ .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); // 忽略未知属性
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 对象转json
|
|
|
+ * @param object
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static String toJson(Object object) throws Exception{
|
|
|
+ try {
|
|
|
+ if (Objects.nonNull(object)) {
|
|
|
+ return objectMapper.writeValueAsString(object);
|
|
|
+ }
|
|
|
+ return "";
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error("object转json异常:" + object, e);
|
|
|
+ throw e;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * json转对象
|
|
|
+ * @param json
|
|
|
+ * @param valueType
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static <T> T toObject(String json, Class<T> valueType) throws Exception {
|
|
|
+ try {
|
|
|
+ if(Objects.nonNull(json) && !json.trim().isEmpty()) {
|
|
|
+ return objectMapper.readValue(json, valueType);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error("json转object异常:" + json, e);
|
|
|
+ throw e;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * json转对象(处理容器类型对象)
|
|
|
+ * List<bean> : json, List.class, Bean.class
|
|
|
+ * Map<bean1, bean2> : json, Map.class, Bean1.class, Bean2.class
|
|
|
+ * @param json
|
|
|
+ * @param valueTypeRef
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static <T> T toObject(String json, Class<?> collectionClass, Class<?>... elementClasses) throws Exception {
|
|
|
+ try {
|
|
|
+ if(Objects.nonNull(json) && !json.trim().isEmpty()) {
|
|
|
+ return objectMapper.readValue(json, objectMapper.getTypeFactory().constructParametricType(collectionClass, elementClasses));
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error("json转object异常:" + json, e);
|
|
|
+ throw e;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * json转对象(处理复杂类型对象)
|
|
|
+ * List<List<Object>> : (json, new TypeReference<List<List<Object>>>(){})
|
|
|
+ * Map<String, List<List<Object>>> : (json, new TypeReference<Map<String, List<List<Object>>>(){})
|
|
|
+ * @param json
|
|
|
+ * @param valueType
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static <T> T toObject(String json, TypeReference<T> typeReference) throws Exception {
|
|
|
+ try {
|
|
|
+ return objectMapper.readValue(json, typeReference);
|
|
|
+ } catch (Exception e) {
|
|
|
+ logger.error("json转object异常:" + json, e);
|
|
|
+ throw e;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|