From fe676fd933d879700d05dca2f00d4aee5574b3d3 Mon Sep 17 00:00:00 2001 From: Looly Date: Thu, 12 Sep 2024 12:09:41 +0800 Subject: [PATCH] add SimpleJSONContext --- .../hutool/json/convert/JSONConverter.java | 11 +--- .../hutool/json/mapper/JSONArrayMapper.java | 3 +- .../hutool/json/mapper/JSONObjectMapper.java | 3 +- .../hutool/json/reader/JSONParser.java | 9 ++- .../json/serializer/SimpleJSONContext.java | 63 +++++++++++++++++++ 5 files changed, 72 insertions(+), 17 deletions(-) create mode 100644 hutool-json/src/main/java/org/dromara/hutool/json/serializer/SimpleJSONContext.java diff --git a/hutool-json/src/main/java/org/dromara/hutool/json/convert/JSONConverter.java b/hutool-json/src/main/java/org/dromara/hutool/json/convert/JSONConverter.java index e6fc5e953..1e8dc9f9e 100644 --- a/hutool-json/src/main/java/org/dromara/hutool/json/convert/JSONConverter.java +++ b/hutool-json/src/main/java/org/dromara/hutool/json/convert/JSONConverter.java @@ -158,16 +158,7 @@ public class JSONConverter implements Converter, Serializable { final JSONSerializer serializer = (JSONSerializer) SerializerManager.getInstance().getSerializer(obj); if (null != serializer) { - return serializer.serialize(obj, new JSONContext() { - @Override - public JSON getContextJson() { - return null; - } - @Override - public JSONConfig config() { - return JSONConverter.this.config; - } - }); + return serializer.serialize(obj, new SimpleJSONContext(null, this.config)); } if (obj instanceof Number || obj instanceof Boolean) { diff --git a/hutool-json/src/main/java/org/dromara/hutool/json/mapper/JSONArrayMapper.java b/hutool-json/src/main/java/org/dromara/hutool/json/mapper/JSONArrayMapper.java index e82467e99..85421f271 100644 --- a/hutool-json/src/main/java/org/dromara/hutool/json/mapper/JSONArrayMapper.java +++ b/hutool-json/src/main/java/org/dromara/hutool/json/mapper/JSONArrayMapper.java @@ -28,6 +28,7 @@ import org.dromara.hutool.json.reader.JSONParser; import org.dromara.hutool.json.reader.JSONTokener; import org.dromara.hutool.json.serializer.JSONSerializer; import org.dromara.hutool.json.serializer.SerializerManager; +import org.dromara.hutool.json.serializer.SimpleJSONContext; import java.io.InputStream; import java.io.Reader; @@ -91,7 +92,7 @@ public class JSONArrayMapper { // 自定义序列化 final JSONSerializer serializer = SerializerManager.getInstance().getSerializer(source.getClass()); if (null != serializer) { - serializer.serialize(source, ()-> jsonArray); + serializer.serialize(source, new SimpleJSONContext(jsonArray)); return; } diff --git a/hutool-json/src/main/java/org/dromara/hutool/json/mapper/JSONObjectMapper.java b/hutool-json/src/main/java/org/dromara/hutool/json/mapper/JSONObjectMapper.java index 78d523dd5..8733883f2 100644 --- a/hutool-json/src/main/java/org/dromara/hutool/json/mapper/JSONObjectMapper.java +++ b/hutool-json/src/main/java/org/dromara/hutool/json/mapper/JSONObjectMapper.java @@ -29,6 +29,7 @@ import org.dromara.hutool.json.reader.JSONParser; import org.dromara.hutool.json.reader.JSONTokener; import org.dromara.hutool.json.serializer.JSONSerializer; import org.dromara.hutool.json.serializer.SerializerManager; +import org.dromara.hutool.json.serializer.SimpleJSONContext; import org.dromara.hutool.json.xml.JSONXMLParser; import org.dromara.hutool.json.xml.ParseConfig; @@ -98,7 +99,7 @@ public class JSONObjectMapper { // 自定义序列化 final JSONSerializer serializer = SerializerManager.getInstance().getSerializer(source.getClass()); if (null != serializer) { - serializer.serialize(source, () -> jsonObject); + serializer.serialize(source, new SimpleJSONContext(jsonObject)); return; } diff --git a/hutool-json/src/main/java/org/dromara/hutool/json/reader/JSONParser.java b/hutool-json/src/main/java/org/dromara/hutool/json/reader/JSONParser.java index 8ede1483e..cbbf3008c 100644 --- a/hutool-json/src/main/java/org/dromara/hutool/json/reader/JSONParser.java +++ b/hutool-json/src/main/java/org/dromara/hutool/json/reader/JSONParser.java @@ -35,11 +35,6 @@ import java.util.function.Predicate; */ public class JSONParser { - /** - * JSON配置 - */ - private final JSONConfig config; - /** * 创建JSONParser * @@ -51,6 +46,10 @@ public class JSONParser { return new JSONParser(tokener, config); } + /** + * JSON配置 + */ + private final JSONConfig config; private final JSONTokener tokener; /** diff --git a/hutool-json/src/main/java/org/dromara/hutool/json/serializer/SimpleJSONContext.java b/hutool-json/src/main/java/org/dromara/hutool/json/serializer/SimpleJSONContext.java new file mode 100644 index 000000000..fa2e47d67 --- /dev/null +++ b/hutool-json/src/main/java/org/dromara/hutool/json/serializer/SimpleJSONContext.java @@ -0,0 +1,63 @@ +/* + * Copyright (c) 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.serializer; + +import org.dromara.hutool.core.lang.Assert; +import org.dromara.hutool.json.JSON; +import org.dromara.hutool.json.JSONConfig; + +/** + * 简单的JSON上下文,用于在JSON序列化时提供配置项 + * + * @author looly + * @since 6.0.0 + */ +public class SimpleJSONContext implements JSONContext { + + private final JSON json; + private final JSONConfig config; + + /** + * 构造 + * + * @param json JSON对象 + */ + public SimpleJSONContext(final JSON json) { + this(Assert.notNull(json), json.config()); + } + + /** + * 构造 + * + * @param json JSON对象 + * @param config 配置项 + */ + public SimpleJSONContext(final JSON json, final JSONConfig config) { + this.json = json; + this.config = config; + } + + @Override + public JSON getContextJson() { + return this.json; + } + + @Override + public JSONConfig config() { + return this.config; + } +}