From d8a687741273d121586b83745280c57f7e9d3297 Mon Sep 17 00:00:00 2001
From: XiaoRuby <3114200645@qq.com>
Date: 星期一, 07 八月 2023 17:51:28 +0800
Subject: [PATCH] Merge branch 'master' of https://gitee.com/yuanchu_code/lims-management-system

---
 framework/src/main/java/com/yuanchu/limslaboratory/utils/JackSonUtil.java |  131 +++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 131 insertions(+), 0 deletions(-)

diff --git a/framework/src/main/java/com/yuanchu/limslaboratory/utils/JackSonUtil.java b/framework/src/main/java/com/yuanchu/limslaboratory/utils/JackSonUtil.java
new file mode 100644
index 0000000..f272754
--- /dev/null
+++ b/framework/src/main/java/com/yuanchu/limslaboratory/utils/JackSonUtil.java
@@ -0,0 +1,131 @@
+package com.yuanchu.limslaboratory.utils;
+
+import com.fasterxml.jackson.core.JsonGenerationException;
+import com.fasterxml.jackson.core.JsonParseException;
+import com.fasterxml.jackson.databind.JsonMappingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.ObjectWriter;
+import org.springframework.stereotype.Component;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+/**
+ * JSON瑙f瀽澶勭悊
+ *
+ * @author 寮犲
+ */
+@Component
+public class JackSonUtil {
+    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
+    private static final ObjectWriter OBJECT_WRITER = OBJECT_MAPPER.writerWithDefaultPrettyPrinter();
+
+    public static void marshal(File file, Object value) throws Exception {
+        try {
+            OBJECT_WRITER.writeValue(file, value);
+        } catch (JsonGenerationException e) {
+            throw new Exception(e);
+        } catch (JsonMappingException e) {
+            throw new Exception(e);
+        } catch (IOException e) {
+            throw new Exception(e);
+        }
+    }
+
+    public static void marshal(OutputStream os, Object value) throws Exception {
+        try {
+            OBJECT_WRITER.writeValue(os, value);
+        } catch (JsonGenerationException e) {
+            throw new Exception(e);
+        } catch (JsonMappingException e) {
+            throw new Exception(e);
+        } catch (IOException e) {
+            throw new Exception(e);
+        }
+    }
+
+    public static String marshal(Object value) throws Exception {
+        try {
+            return OBJECT_WRITER.writeValueAsString(value);
+        } catch (JsonGenerationException e) {
+            throw new Exception(e);
+        } catch (JsonMappingException e) {
+            throw new Exception(e);
+        } catch (IOException e) {
+            throw new Exception(e);
+        }
+    }
+
+    public static byte[] marshalBytes(Object value) throws Exception {
+        try {
+            return OBJECT_WRITER.writeValueAsBytes(value);
+        } catch (JsonGenerationException e) {
+            throw new Exception(e);
+        } catch (JsonMappingException e) {
+            throw new Exception(e);
+        } catch (IOException e) {
+            throw new Exception(e);
+        }
+    }
+
+    public static <T> T unmarshal(File file, Class<T> valueType) throws Exception {
+        try {
+            return OBJECT_MAPPER.readValue(file, valueType);
+        } catch (JsonParseException e) {
+            throw new Exception(e);
+        } catch (JsonMappingException e) {
+            throw new Exception(e);
+        } catch (IOException e) {
+            throw new Exception(e);
+        }
+    }
+
+    public static <T> T unmarshal(InputStream is, Class<T> valueType) throws Exception {
+        try {
+            return OBJECT_MAPPER.readValue(is, valueType);
+        } catch (JsonParseException e) {
+            throw new Exception(e);
+        } catch (JsonMappingException e) {
+            throw new Exception(e);
+        } catch (IOException e) {
+            throw new Exception(e);
+        }
+    }
+
+    /**
+     * 瀛楃涓茶浆瀵硅薄
+     * @param str
+     * @param valueType
+     * @return
+     * @param <T>
+     * @throws Exception
+     */
+    public static <T> T unmarshal(String str, Class<T> valueType) throws Exception {
+        try {
+            return OBJECT_MAPPER.readValue(str, valueType);
+        } catch (JsonParseException e) {
+            throw new Exception(e);
+        } catch (JsonMappingException e) {
+            throw new Exception(e);
+        } catch (IOException e) {
+            throw new Exception(e);
+        }
+    }
+
+    public static <T> T unmarshal(byte[] bytes, Class<T> valueType) throws Exception {
+        try {
+            if (bytes == null) {
+                bytes = new byte[0];
+            }
+            return OBJECT_MAPPER.readValue(bytes, 0, bytes.length, valueType);
+        } catch (JsonParseException e) {
+            throw new Exception(e);
+        } catch (JsonMappingException e) {
+            throw new Exception(e);
+        } catch (IOException e) {
+            throw new Exception(e);
+        }
+    }
+}

--
Gitblit v1.9.3