From 92fd8f3dbd951d9bd0cdac20e8c409fbd5f00f26 Mon Sep 17 00:00:00 2001 From: songdragon Date: Sun, 27 Aug 2023 18:43:56 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20json=E7=BC=A9=E8=BF=9B=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E4=B8=BA4=E4=B8=AA=E7=A9=BA=E6=A0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/jcnc/jnotepad/tool/JsonUtil.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/jcnc/jnotepad/tool/JsonUtil.java b/src/main/java/org/jcnc/jnotepad/tool/JsonUtil.java index 1119123..59f920e 100644 --- a/src/main/java/org/jcnc/jnotepad/tool/JsonUtil.java +++ b/src/main/java/org/jcnc/jnotepad/tool/JsonUtil.java @@ -1,10 +1,15 @@ package org.jcnc.jnotepad.tool; import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.PrettyPrinter; +import com.fasterxml.jackson.core.util.DefaultIndenter; +import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import org.jcnc.jnotepad.exception.AppException; +import static com.fasterxml.jackson.core.util.DefaultIndenter.SYS_LF; + /** * jackson解析器的facade类,主要提供objectMapper对象 * @@ -14,7 +19,16 @@ public class JsonUtil { private JsonUtil() { } - public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT); + public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); + + static { + OBJECT_MAPPER.enable(SerializationFeature.INDENT_OUTPUT); + DefaultPrettyPrinter prettyPrinter = new DefaultPrettyPrinter(); + DefaultIndenter di = new DefaultIndenter(" ", SYS_LF); + prettyPrinter.indentArraysWith(di); + prettyPrinter.indentObjectsWith(di); + OBJECT_MAPPER.setDefaultPrettyPrinter(prettyPrinter); + } public static String toJsonString(Object o) { try {