diff --git a/hutool-json/src/main/java/org/dromara/hutool/json/InternalJSONUtil.java b/hutool-json/src/main/java/org/dromara/hutool/json/InternalJSONUtil.java
index 5ba0d2643..fbf72fc30 100644
--- a/hutool-json/src/main/java/org/dromara/hutool/json/InternalJSONUtil.java
+++ b/hutool-json/src/main/java/org/dromara/hutool/json/InternalJSONUtil.java
@@ -32,8 +32,6 @@ import org.dromara.hutool.core.util.ObjUtil;
import org.dromara.hutool.json.reader.JSONTokener;
import java.io.IOException;
-import java.io.StringWriter;
-import java.io.Writer;
import java.math.BigDecimal;
import java.util.*;
import java.util.function.Predicate;
@@ -210,7 +208,7 @@ public final class InternalJSONUtil {
* @since 3.3.1
*/
public static String quote(final CharSequence string, final boolean isWrap) {
- return quote(string, new StringWriter(), isWrap).toString();
+ return quote(string, new StringBuilder(), isWrap).toString();
}
/**
@@ -219,11 +217,11 @@ public final class InternalJSONUtil {
* JSON字符串中不能包含控制字符和未经转义的引号和反斜杠
*
* @param str 字符串
- * @param writer Writer
+ * @param appendable {@link Appendable}
* @throws IORuntimeException IO异常
*/
- public static void quote(final CharSequence str, final Writer writer) throws IORuntimeException {
- quote(str, writer, true);
+ public static void quote(final CharSequence str, final Appendable appendable) throws IORuntimeException {
+ quote(str, appendable, true);
}
/**
@@ -232,15 +230,15 @@ public final class InternalJSONUtil {
* JSON字符串中不能包含控制字符和未经转义的引号和反斜杠
*
* @param str 字符串
- * @param writer Writer
+ * @param appendable {@link Appendable}
* @param isWrap 是否使用双引号包装字符串
* @return Writer
* @throws IORuntimeException IO异常
* @since 3.3.1
*/
- public static Writer quote(final CharSequence str, final Writer writer, final boolean isWrap) throws IORuntimeException {
+ public static Appendable quote(final CharSequence str, final Appendable appendable, final boolean isWrap) throws IORuntimeException {
try {
- return _quote(str, writer, isWrap);
+ return _quote(str, appendable, isWrap);
} catch (final IOException e) {
throw new IORuntimeException(e);
}
@@ -337,10 +335,10 @@ public final class InternalJSONUtil {
* @throws IOException IO异常
* @since 3.3.1
*/
- private static Writer _quote(final CharSequence str, final Writer writer, final boolean isWrap) throws IOException {
+ private static Appendable _quote(final CharSequence str, final Appendable writer, final boolean isWrap) throws IOException {
if (StrUtil.isEmpty(str)) {
if (isWrap) {
- writer.write("\"\"");
+ writer.append("\"\"");
}
return writer;
}
@@ -348,22 +346,22 @@ public final class InternalJSONUtil {
char c; // 当前字符
final int len = str.length();
if (isWrap) {
- writer.write('"');
+ writer.append('"');
}
for (int i = 0; i < len; i++) {
c = str.charAt(i);
switch (c) {
case '\\':
case '"':
- writer.write("\\");
- writer.write(c);
+ writer.append("\\");
+ writer.append(c);
break;
default:
- writer.write(escape(c));
+ writer.append(escape(c));
}
}
if (isWrap) {
- writer.write('"');
+ writer.append('"');
}
return writer;
}
diff --git a/hutool-json/src/main/java/org/dromara/hutool/json/JSON.java b/hutool-json/src/main/java/org/dromara/hutool/json/JSON.java
index fe7fc9035..b9a3a67ba 100644
--- a/hutool-json/src/main/java/org/dromara/hutool/json/JSON.java
+++ b/hutool-json/src/main/java/org/dromara/hutool/json/JSON.java
@@ -19,14 +19,11 @@ package org.dromara.hutool.json;
import org.dromara.hutool.core.bean.path.BeanPath;
import org.dromara.hutool.core.convert.ConvertException;
import org.dromara.hutool.core.convert.Converter;
-import org.dromara.hutool.core.lang.mutable.MutableEntry;
import org.dromara.hutool.core.util.ObjUtil;
+import org.dromara.hutool.json.writer.JSONWriter;
import java.io.Serializable;
-import java.io.StringWriter;
-import java.io.Writer;
import java.lang.reflect.Type;
-import java.util.function.Predicate;
/**
* JSON树模型接口,表示树中的一个节点。实现包括:
@@ -155,20 +152,9 @@ public interface JSON extends Converter, Cloneable, Serializable {
* @throws JSONException 包含非法数抛出此异常
*/
default String toJSONString(final int indentFactor) throws JSONException {
- final StringWriter sw = new StringWriter();
- return this.write(sw, indentFactor, 0, null).toString();
- }
-
- /**
- * 将JSON内容写入Writer,无缩进
- * Warning: This method assumes that the data structure is acyclical.
- *
- * @param writer Writer
- * @return Writer
- * @throws JSONException JSON相关异常
- */
- default Writer write(final Writer writer) throws JSONException {
- return this.write(writer, 0, 0, null);
+ final JSONWriter jsonWriter = JSONWriter.of(new StringBuilder(), indentFactor, 0, config());
+ this.write(jsonWriter);
+ return jsonWriter.toString();
}
/**
@@ -176,13 +162,9 @@ public interface JSON extends Converter, Cloneable, Serializable {
* Warning: This method assumes that the data structure is acyclical.
*
* @param writer writer
- * @param indentFactor 缩进因子,定义每一级别增加的缩进量
- * @param indent 本级别缩进量
- * @param predicate 过滤器,可以修改值,key(index)无法修改,{@link Predicate#test(Object)}为{@code true}保留
- * @return Writer
* @throws JSONException JSON相关异常
*/
- Writer write(Writer writer, int indentFactor, int indent, final Predicate> predicate) throws JSONException;
+ void write(JSONWriter writer) throws JSONException;
/**
* 转为实体类对象
diff --git a/hutool-json/src/main/java/org/dromara/hutool/json/JSONArray.java b/hutool-json/src/main/java/org/dromara/hutool/json/JSONArray.java
index 6feb44ead..514030c70 100644
--- a/hutool-json/src/main/java/org/dromara/hutool/json/JSONArray.java
+++ b/hutool-json/src/main/java/org/dromara/hutool/json/JSONArray.java
@@ -29,8 +29,6 @@ import org.dromara.hutool.json.mapper.JSONArrayMapper;
import org.dromara.hutool.json.mapper.JSONValueMapper;
import org.dromara.hutool.json.writer.JSONWriter;
-import java.io.StringWriter;
-import java.io.Writer;
import java.util.*;
import java.util.function.Predicate;
@@ -564,20 +562,17 @@ public class JSONArray implements JSON, JSONGetter, List