This commit is contained in:
Looly 2024-09-26 23:30:55 +08:00
parent f7a2741aa3
commit 1c18ccf660
5 changed files with 14 additions and 66 deletions

View File

@ -210,17 +210,6 @@ public interface JSON extends Serializable {
return this.toJSONString(2);
}
/**
* 格式化输出JSON字符串
*
* @param indentFactor 每层缩进空格数
* @return JSON字符串
* @throws JSONException 包含非法数抛出此异常
*/
default String toJSONString(final int indentFactor) throws JSONException {
return toJSONString(indentFactor, null);
}
/**
* 格式化输出JSON字符串
*
@ -235,6 +224,17 @@ public interface JSON extends Serializable {
return jsonWriter.toString();
}
/**
* 格式化输出JSON字符串
*
* @param indentFactor 每层缩进空格数
* @return JSON字符串
* @throws JSONException 包含非法数抛出此异常
*/
default String toJSONString(final int indentFactor) throws JSONException {
return toJSONString(indentFactor, null);
}
/**
* 将JSON内容写入Writer<br>
* Warning: This method assumes that the data structure is acyclical.

View File

@ -27,7 +27,6 @@ import org.dromara.hutool.json.serializer.impl.IterTypeAdapter;
import org.dromara.hutool.json.writer.JSONWriter;
import java.util.*;
import java.util.function.Predicate;
/**
* JSON数组<br>
@ -258,21 +257,6 @@ public class JSONArray extends ListWrapper<JSON> implements JSON, JSONGetter<Int
return this.toJSONString(0);
}
/**
* 返回JSON字符串<br>
* 支持过滤器即选择哪些字段或值不写出
*
* @param indentFactor 每层缩进空格数
* @param predicate 过滤器可以修改值keyindex无法修改{@link Predicate#test(Object)}{@code true}保留
* @return JSON字符串
* @since 5.7.15
*/
public String toJSONString(final int indentFactor, final Predicate<MutableEntry<Object, Object>> predicate) {
final JSONWriter jsonWriter = JSONWriter.of(new StringBuilder(), indentFactor, 0, this.config).setPredicate(predicate);
this.write(jsonWriter);
return jsonWriter.toString();
}
@Override
public void write(final JSONWriter writer) throws JSONException {
writer.beginArray();

View File

@ -177,4 +177,6 @@ public class JSONFactory {
return mapper.toBean(json, type);
}
// endregion
}

View File

@ -51,7 +51,7 @@ public class JSONMapper implements Serializable {
private static final long serialVersionUID = -6714488573738940582L;
/**
* 创建ObjectMapper
* 创建JSONMapper
*
* @param jsonConfig 来源对象
* @param predicate 键值对过滤编辑器可以通过实现此接口完成解析前对键值对的过滤和修改操作{@link Predicate#test(Object)}{@code true}保留

View File

@ -1,38 +0,0 @@
/*
* Copyright (c) 2013-2024 Hutool Team and hutool.cn
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dromara.hutool.json.writer;
import java.util.function.Predicate;
/**
* JSON的值自定义写出通过自定义实现此接口实现对象自定义写出字符串形式<br>
* 如自定义的一个CustomBean我只希望输出id的值此时自定义此接口<br>
* 其中{@link ValueWriter#test(Object)}负责判断何种对象使用此规则{@link ValueWriter#write(JSONWriter, Object)}负责写出规则
*
* @author looly
* @since 6.0.0
*/
public interface ValueWriter extends Predicate<Object> {
/**
* 使用{@link JSONWriter} 写出对象
*
* @param writer {@link JSONWriter}
* @param value 被写出的值
*/
void write(JSONWriter writer, Object value);
}