mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
add SimpleJSONContext
This commit is contained in:
parent
5409001e34
commit
fe676fd933
@ -158,16 +158,7 @@ public class JSONConverter implements Converter, Serializable {
|
|||||||
final JSONSerializer<Object> serializer =
|
final JSONSerializer<Object> serializer =
|
||||||
(JSONSerializer<Object>) SerializerManager.getInstance().getSerializer(obj);
|
(JSONSerializer<Object>) SerializerManager.getInstance().getSerializer(obj);
|
||||||
if (null != serializer) {
|
if (null != serializer) {
|
||||||
return serializer.serialize(obj, new JSONContext() {
|
return serializer.serialize(obj, new SimpleJSONContext(null, this.config));
|
||||||
@Override
|
|
||||||
public JSON getContextJson() {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
@Override
|
|
||||||
public JSONConfig config() {
|
|
||||||
return JSONConverter.this.config;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (obj instanceof Number || obj instanceof Boolean) {
|
if (obj instanceof Number || obj instanceof Boolean) {
|
||||||
|
@ -28,6 +28,7 @@ import org.dromara.hutool.json.reader.JSONParser;
|
|||||||
import org.dromara.hutool.json.reader.JSONTokener;
|
import org.dromara.hutool.json.reader.JSONTokener;
|
||||||
import org.dromara.hutool.json.serializer.JSONSerializer;
|
import org.dromara.hutool.json.serializer.JSONSerializer;
|
||||||
import org.dromara.hutool.json.serializer.SerializerManager;
|
import org.dromara.hutool.json.serializer.SerializerManager;
|
||||||
|
import org.dromara.hutool.json.serializer.SimpleJSONContext;
|
||||||
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.Reader;
|
import java.io.Reader;
|
||||||
@ -91,7 +92,7 @@ public class JSONArrayMapper {
|
|||||||
// 自定义序列化
|
// 自定义序列化
|
||||||
final JSONSerializer<Object> serializer = SerializerManager.getInstance().getSerializer(source.getClass());
|
final JSONSerializer<Object> serializer = SerializerManager.getInstance().getSerializer(source.getClass());
|
||||||
if (null != serializer) {
|
if (null != serializer) {
|
||||||
serializer.serialize(source, ()-> jsonArray);
|
serializer.serialize(source, new SimpleJSONContext(jsonArray));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,6 +29,7 @@ import org.dromara.hutool.json.reader.JSONParser;
|
|||||||
import org.dromara.hutool.json.reader.JSONTokener;
|
import org.dromara.hutool.json.reader.JSONTokener;
|
||||||
import org.dromara.hutool.json.serializer.JSONSerializer;
|
import org.dromara.hutool.json.serializer.JSONSerializer;
|
||||||
import org.dromara.hutool.json.serializer.SerializerManager;
|
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.JSONXMLParser;
|
||||||
import org.dromara.hutool.json.xml.ParseConfig;
|
import org.dromara.hutool.json.xml.ParseConfig;
|
||||||
|
|
||||||
@ -98,7 +99,7 @@ public class JSONObjectMapper {
|
|||||||
// 自定义序列化
|
// 自定义序列化
|
||||||
final JSONSerializer<Object> serializer = SerializerManager.getInstance().getSerializer(source.getClass());
|
final JSONSerializer<Object> serializer = SerializerManager.getInstance().getSerializer(source.getClass());
|
||||||
if (null != serializer) {
|
if (null != serializer) {
|
||||||
serializer.serialize(source, () -> jsonObject);
|
serializer.serialize(source, new SimpleJSONContext(jsonObject));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,11 +35,6 @@ import java.util.function.Predicate;
|
|||||||
*/
|
*/
|
||||||
public class JSONParser {
|
public class JSONParser {
|
||||||
|
|
||||||
/**
|
|
||||||
* JSON配置
|
|
||||||
*/
|
|
||||||
private final JSONConfig config;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建JSONParser
|
* 创建JSONParser
|
||||||
*
|
*
|
||||||
@ -51,6 +46,10 @@ public class JSONParser {
|
|||||||
return new JSONParser(tokener, config);
|
return new JSONParser(tokener, config);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* JSON配置
|
||||||
|
*/
|
||||||
|
private final JSONConfig config;
|
||||||
private final JSONTokener tokener;
|
private final JSONTokener tokener;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user