mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add ClassTypeAdapter
This commit is contained in:
parent
11824599eb
commit
5c163941fe
@ -37,13 +37,22 @@ import java.util.function.Predicate;
|
||||
*/
|
||||
public interface JSON extends Serializable {
|
||||
|
||||
/**
|
||||
* 获取JSON工厂
|
||||
*
|
||||
* @return JSON工厂
|
||||
*/
|
||||
JSONFactory getFactory();
|
||||
|
||||
/**
|
||||
* 获取JSON配置
|
||||
*
|
||||
* @return {@link JSONConfig}
|
||||
* @since 5.3.0
|
||||
*/
|
||||
JSONConfig config();
|
||||
default JSONConfig config(){
|
||||
return getFactory().getConfig();
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON大小,对于JSONObject,是键值对的多少,JSONArray则是元素的个数,JSON原始数据为1
|
||||
@ -250,6 +259,6 @@ public interface JSON extends Serializable {
|
||||
* @return 实体类对象
|
||||
*/
|
||||
default <T> T toBean(final Type type) {
|
||||
return JSONFactory.of(config(), null).toBean(this, type);
|
||||
return getFactory().toBean(this, type);
|
||||
}
|
||||
}
|
||||
|
@ -103,8 +103,8 @@ public class JSONArray extends ListWrapper<JSON> implements JSON, JSONGetter<Int
|
||||
// endregion
|
||||
|
||||
@Override
|
||||
public JSONConfig config() {
|
||||
return factory.getConfig();
|
||||
public JSONFactory getFactory() {
|
||||
return this.factory;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -22,6 +22,9 @@ import org.dromara.hutool.core.util.ObjUtil;
|
||||
import org.dromara.hutool.json.reader.JSONParser;
|
||||
import org.dromara.hutool.json.reader.JSONTokener;
|
||||
import org.dromara.hutool.json.serializer.JSONMapper;
|
||||
import org.dromara.hutool.json.serializer.MatcherJSONDeserializer;
|
||||
import org.dromara.hutool.json.serializer.MatcherJSONSerializer;
|
||||
import org.dromara.hutool.json.serializer.TypeAdapter;
|
||||
import org.dromara.hutool.json.support.JSONNodeBeanFactory;
|
||||
import org.dromara.hutool.json.writer.JSONWriter;
|
||||
|
||||
@ -54,6 +57,16 @@ public class JSONFactory {
|
||||
return InstanceHolder.INSTANCE;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建JSON工厂
|
||||
*
|
||||
* @param config JSON配置
|
||||
* @return JSON工厂
|
||||
*/
|
||||
public static JSONFactory of(final JSONConfig config) {
|
||||
return of(config, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建JSON工厂
|
||||
*
|
||||
@ -144,6 +157,31 @@ public class JSONFactory {
|
||||
return this.mapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册自定义类型适配器,用于自定义对象序列化和反序列化
|
||||
*
|
||||
* @param type 类型
|
||||
* @param typeAdapter 自定义序列化器,{@code null}表示移除
|
||||
* @return this
|
||||
*/
|
||||
public JSONFactory register(final Type type, final TypeAdapter typeAdapter) {
|
||||
getMapper().register(type, typeAdapter);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册自定义类型适配器,用于自定义对象序列化和反序列化<br>
|
||||
* 提供的适配器必须为实现{@link MatcherJSONSerializer}或{@link MatcherJSONDeserializer}接口<br>
|
||||
* 当两个接口都实现时,同时注册序列化和反序列化器
|
||||
*
|
||||
* @param typeAdapter 自定义类型适配器
|
||||
* @return this
|
||||
*/
|
||||
public JSONFactory register(final TypeAdapter typeAdapter) {
|
||||
getMapper().register(typeAdapter);
|
||||
return this;
|
||||
}
|
||||
|
||||
// region ----- of
|
||||
|
||||
/**
|
||||
|
@ -31,12 +31,10 @@ import java.util.List;
|
||||
public interface JSONGetter<K> extends TypeGetter<K> {
|
||||
|
||||
/**
|
||||
* 获取JSON配置
|
||||
*
|
||||
* @return {@link JSONConfig}
|
||||
* @since 5.3.0
|
||||
* 获取JSON工厂
|
||||
* @return JSON工厂
|
||||
*/
|
||||
JSONConfig config();
|
||||
JSONFactory getFactory();
|
||||
|
||||
/**
|
||||
* key对应值是否为{@code null}或无此key
|
||||
@ -85,7 +83,7 @@ public interface JSONGetter<K> extends TypeGetter<K> {
|
||||
}
|
||||
|
||||
if (json instanceof JSONObject) {
|
||||
return JSONUtil.parseArray(json, config());
|
||||
return getFactory().parseArray(json);
|
||||
}
|
||||
|
||||
return json.asJSONArray();
|
||||
|
@ -100,8 +100,8 @@ public class JSONObject extends MapWrapper<String, JSON> implements JSON, JSONGe
|
||||
// endregion
|
||||
|
||||
@Override
|
||||
public JSONConfig config() {
|
||||
return this.factory.getConfig();
|
||||
public JSONFactory getFactory() {
|
||||
return this.factory;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -119,9 +119,14 @@ public class JSONPrimitive implements Wrapper<Object>, JSON {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得JSON工厂类
|
||||
*
|
||||
* @return JSON工厂类
|
||||
*/
|
||||
@Override
|
||||
public JSONConfig config() {
|
||||
return this.factory.getConfig();
|
||||
public JSONFactory getFactory() {
|
||||
return this.factory;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -22,6 +22,10 @@ import org.dromara.hutool.core.lang.Assert;
|
||||
import org.dromara.hutool.core.lang.mutable.MutableEntry;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
import org.dromara.hutool.core.util.ObjUtil;
|
||||
import org.dromara.hutool.json.serializer.MatcherJSONDeserializer;
|
||||
import org.dromara.hutool.json.serializer.MatcherJSONSerializer;
|
||||
import org.dromara.hutool.json.serializer.TypeAdapter;
|
||||
import org.dromara.hutool.json.serializer.TypeAdapterManager;
|
||||
import org.dromara.hutool.json.support.JSONStrFormatter;
|
||||
import org.dromara.hutool.json.xml.JSONXMLUtil;
|
||||
|
||||
@ -404,7 +408,7 @@ public class JSONUtil {
|
||||
* @return 实体类对象
|
||||
* @since 4.3.2
|
||||
*/
|
||||
public static <T> T toBean(final Object obj, final JSONConfig config, Type type) {
|
||||
public static <T> T toBean(final Object obj, final JSONConfig config, final Type type) {
|
||||
if (null == obj) {
|
||||
return null;
|
||||
}
|
||||
@ -634,4 +638,27 @@ public class JSONUtil {
|
||||
return StrUtil.isWrap(StrUtil.trim(str), '[', ']');
|
||||
}
|
||||
// endregion
|
||||
|
||||
// region ----- registerTypeAdapter
|
||||
/**
|
||||
* 全局注册自定义类型适配器,用于自定义对象序列化和反序列化
|
||||
*
|
||||
* @param type 类型
|
||||
* @param typeAdapter 自定义序列化器,{@code null}表示移除
|
||||
*/
|
||||
public void registerTypeAdapter(final Type type, final TypeAdapter typeAdapter) {
|
||||
TypeAdapterManager.getInstance().register(type, typeAdapter);
|
||||
}
|
||||
|
||||
/**
|
||||
* 全局注册自定义类型适配器,用于自定义对象序列化和反序列化<br>
|
||||
* 提供的适配器必须为实现{@link MatcherJSONSerializer}或{@link MatcherJSONDeserializer}接口<br>
|
||||
* 当两个接口都实现时,同时注册序列化和反序列化器
|
||||
*
|
||||
* @param typeAdapter 自定义类型适配器
|
||||
*/
|
||||
public void registerTypeAdapter(final TypeAdapter typeAdapter) {
|
||||
TypeAdapterManager.getInstance().register(typeAdapter);
|
||||
}
|
||||
// endregion
|
||||
}
|
||||
|
@ -22,6 +22,7 @@ import org.dromara.hutool.core.text.StrUtil;
|
||||
import org.dromara.hutool.core.util.ObjUtil;
|
||||
import org.dromara.hutool.json.*;
|
||||
import org.dromara.hutool.json.reader.JSONTokener;
|
||||
import org.dromara.hutool.json.serializer.impl.DefaultDeserializer;
|
||||
import org.dromara.hutool.json.xml.JSONXMLParser;
|
||||
import org.dromara.hutool.json.xml.ParseConfig;
|
||||
|
||||
@ -58,7 +59,7 @@ public class JSONMapper implements Serializable {
|
||||
}
|
||||
|
||||
private final JSONFactory factory;
|
||||
private TypeAdapterManager typeAdapterManager;
|
||||
private volatile TypeAdapterManager typeAdapterManager;
|
||||
|
||||
/**
|
||||
* 构造
|
||||
@ -89,6 +90,31 @@ public class JSONMapper implements Serializable {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册自定义类型适配器,用于自定义对象序列化和反序列化
|
||||
*
|
||||
* @param type 类型
|
||||
* @param typeAdapter 自定义序列化器,{@code null}表示移除
|
||||
* @return this
|
||||
*/
|
||||
public JSONMapper register(final Type type, final TypeAdapter typeAdapter) {
|
||||
initTypeAdapterManager().register(type, typeAdapter);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册自定义类型适配器,用于自定义对象序列化和反序列化<br>
|
||||
* 提供的适配器必须为实现{@link MatcherJSONSerializer}或{@link MatcherJSONDeserializer}接口<br>
|
||||
* 当两个接口都实现时,同时注册序列化和反序列化器
|
||||
*
|
||||
* @param typeAdapter 自定义类型适配器
|
||||
* @return this
|
||||
*/
|
||||
public JSONMapper register(final TypeAdapter typeAdapter) {
|
||||
initTypeAdapterManager().register(typeAdapter);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 转为实体类对象
|
||||
*
|
||||
@ -121,10 +147,7 @@ public class JSONMapper implements Serializable {
|
||||
}
|
||||
final boolean ignoreError = ObjUtil.defaultIfNull(this.factory.getConfig(), JSONConfig::isIgnoreError, false);
|
||||
if (null == deserializer) {
|
||||
if (ignoreError) {
|
||||
return null;
|
||||
}
|
||||
throw new JSONException("No deserializer for type: " + type);
|
||||
deserializer = DefaultDeserializer.INSTANCE;
|
||||
}
|
||||
|
||||
try {
|
||||
@ -314,4 +337,20 @@ public class JSONMapper implements Serializable {
|
||||
private JSON mapFromTokener(final JSONTokener tokener) {
|
||||
return this.factory.ofParser(tokener).parse();
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化类型转换器管理器,如果尚未初始化,则初始化,否则直接返回
|
||||
*
|
||||
* @return {@link TypeAdapterManager}
|
||||
*/
|
||||
private TypeAdapterManager initTypeAdapterManager() {
|
||||
if (null == this.typeAdapterManager) {
|
||||
synchronized (this) {
|
||||
if (null == this.typeAdapterManager) {
|
||||
this.typeAdapterManager = TypeAdapterManager.of();
|
||||
}
|
||||
}
|
||||
}
|
||||
return this.typeAdapterManager;
|
||||
}
|
||||
}
|
||||
|
@ -225,7 +225,8 @@ public class TypeAdapterManager {
|
||||
}
|
||||
}
|
||||
|
||||
return DefaultDeserializer.INSTANCE;
|
||||
// 此处返回null,错误处理在mapper中
|
||||
return null;
|
||||
}
|
||||
// endregion
|
||||
|
||||
|
@ -0,0 +1,54 @@
|
||||
/*
|
||||
* 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.impl;
|
||||
|
||||
import org.dromara.hutool.core.reflect.ClassUtil;
|
||||
import org.dromara.hutool.json.JSON;
|
||||
import org.dromara.hutool.json.serializer.JSONContext;
|
||||
import org.dromara.hutool.json.serializer.JSONDeserializer;
|
||||
import org.dromara.hutool.json.serializer.JSONSerializer;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
/**
|
||||
* Class类型适配器,用于将Class对象序列化为字符串,反序列化为Class对象<br>
|
||||
* 注意:考虑安全问题,此类并不作为默认的适配器,如需启用,需:
|
||||
* <pre>{@code
|
||||
* final JSONFactory factory = JSONFactory.of(null, null);
|
||||
* factory.register(Class<?>.class, ClassTypeAdapter.INSTANCE);
|
||||
* }</pre>
|
||||
*
|
||||
* @author looly
|
||||
* @since 6.0.0
|
||||
*/
|
||||
public class ClassTypeAdapter implements JSONSerializer<Class<?>>, JSONDeserializer<Class<?>> {
|
||||
|
||||
/**
|
||||
* 单例
|
||||
*/
|
||||
public static final ClassTypeAdapter INSTANCE = new ClassTypeAdapter();
|
||||
|
||||
@Override
|
||||
public JSON serialize(final Class<?> bean, final JSONContext context) {
|
||||
return context.getOrCreatePrimitive(bean.getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?> deserialize(final JSON json, final Type deserializeType) {
|
||||
return ClassUtil.forName((String) json.asJSONPrimitive().getValue(), true, null);
|
||||
}
|
||||
}
|
@ -29,7 +29,7 @@ import java.util.List;
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
public class ParseBeanTest {
|
||||
public class ParseBeanWithListTest {
|
||||
|
||||
@Test
|
||||
public void parseBeanTest() {
|
@ -17,6 +17,8 @@
|
||||
package org.dromara.hutool.json.issues;
|
||||
|
||||
import lombok.Data;
|
||||
import org.dromara.hutool.json.JSONConfig;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -16,10 +16,13 @@
|
||||
|
||||
package org.dromara.hutool.json.issues;
|
||||
|
||||
import lombok.Data;
|
||||
import org.dromara.hutool.core.bean.BeanUtil;
|
||||
import org.dromara.hutool.core.convert.ConvertUtil;
|
||||
import org.dromara.hutool.core.reflect.TypeReference;
|
||||
import lombok.Data;
|
||||
import org.dromara.hutool.json.JSONArray;
|
||||
import org.dromara.hutool.json.JSONObject;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -18,6 +18,9 @@ package org.dromara.hutool.json.issues;
|
||||
|
||||
import lombok.Data;
|
||||
import org.dromara.hutool.core.io.resource.ResourceUtil;
|
||||
import org.dromara.hutool.json.JSONConfig;
|
||||
import org.dromara.hutool.json.JSONObject;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -17,6 +17,8 @@
|
||||
package org.dromara.hutool.json.issues;
|
||||
|
||||
import lombok.Data;
|
||||
import org.dromara.hutool.json.JSONObject;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -17,6 +17,10 @@
|
||||
package org.dromara.hutool.json.issues;
|
||||
|
||||
import lombok.Data;
|
||||
import org.dromara.hutool.json.JSON;
|
||||
import org.dromara.hutool.json.JSONObject;
|
||||
import org.dromara.hutool.json.JSONPrimitive;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -16,10 +16,12 @@
|
||||
|
||||
package org.dromara.hutool.json.issues;
|
||||
|
||||
import org.dromara.hutool.core.collection.ListUtil;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.experimental.Accessors;
|
||||
import org.dromara.hutool.core.collection.ListUtil;
|
||||
import org.dromara.hutool.json.JSONObject;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -17,6 +17,8 @@
|
||||
package org.dromara.hutool.json.issues;
|
||||
|
||||
import lombok.Data;
|
||||
import org.dromara.hutool.json.JSONConfig;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -16,6 +16,8 @@
|
||||
|
||||
package org.dromara.hutool.json.issues;
|
||||
|
||||
import org.dromara.hutool.json.JSONArray;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -17,6 +17,7 @@
|
||||
package org.dromara.hutool.json.issues;
|
||||
|
||||
import lombok.Data;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -16,6 +16,8 @@
|
||||
|
||||
package org.dromara.hutool.json.issues;
|
||||
|
||||
import org.dromara.hutool.json.JSONObject;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
@ -17,6 +17,9 @@
|
||||
package org.dromara.hutool.json.issues;
|
||||
|
||||
import lombok.Data;
|
||||
import org.dromara.hutool.json.JSON;
|
||||
import org.dromara.hutool.json.JSONObject;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.dromara.hutool.json.serializer.JSONContext;
|
||||
import org.dromara.hutool.json.serializer.JSONDeserializer;
|
||||
import org.dromara.hutool.json.serializer.JSONSerializer;
|
||||
|
@ -17,6 +17,8 @@
|
||||
package org.dromara.hutool.json.issues;
|
||||
|
||||
import lombok.Data;
|
||||
import org.dromara.hutool.json.JSONConfig;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -17,6 +17,7 @@
|
||||
package org.dromara.hutool.json.issues;
|
||||
|
||||
import org.dromara.hutool.core.reflect.TypeReference;
|
||||
import org.dromara.hutool.json.JSONObject;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -17,6 +17,8 @@
|
||||
package org.dromara.hutool.json.issues;
|
||||
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
import org.dromara.hutool.json.JSONException;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -16,6 +16,8 @@
|
||||
|
||||
package org.dromara.hutool.json.issues;
|
||||
|
||||
import org.dromara.hutool.json.JSONObject;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
@ -16,6 +16,8 @@
|
||||
|
||||
package org.dromara.hutool.json.issues;
|
||||
|
||||
import org.dromara.hutool.json.JSONConfig;
|
||||
import org.dromara.hutool.json.JSONObject;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -16,6 +16,7 @@
|
||||
|
||||
package org.dromara.hutool.json.issues;
|
||||
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -16,6 +16,9 @@
|
||||
|
||||
package org.dromara.hutool.json.issues;
|
||||
|
||||
import org.dromara.hutool.json.JSONConfig;
|
||||
import org.dromara.hutool.json.JSONObject;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -17,6 +17,9 @@
|
||||
package org.dromara.hutool.json.issues;
|
||||
|
||||
import lombok.Data;
|
||||
import org.dromara.hutool.json.JSONConfig;
|
||||
import org.dromara.hutool.json.JSONObject;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -16,6 +16,7 @@
|
||||
|
||||
package org.dromara.hutool.json.issues;
|
||||
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -18,6 +18,8 @@ package org.dromara.hutool.json.issues;
|
||||
|
||||
import lombok.Data;
|
||||
import org.dromara.hutool.core.collection.ListUtil;
|
||||
import org.dromara.hutool.json.JSON;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.dromara.hutool.json.serializer.JSONContext;
|
||||
import org.dromara.hutool.json.serializer.JSONSerializer;
|
||||
import org.dromara.hutool.json.serializer.TypeAdapterManager;
|
||||
|
@ -18,6 +18,8 @@ package org.dromara.hutool.json.issues;
|
||||
|
||||
import lombok.Data;
|
||||
import org.dromara.hutool.core.collection.ListUtil;
|
||||
import org.dromara.hutool.json.JSONObject;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.dromara.hutool.json.serializer.JSONDeserializer;
|
||||
import org.dromara.hutool.json.serializer.TypeAdapterManager;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
|
@ -16,6 +16,8 @@
|
||||
|
||||
package org.dromara.hutool.json.issues;
|
||||
|
||||
import org.dromara.hutool.json.JSONObject;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -18,6 +18,10 @@ package org.dromara.hutool.json.issues;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.Getter;
|
||||
import org.dromara.hutool.json.JSONArray;
|
||||
import org.dromara.hutool.json.JSONConfig;
|
||||
import org.dromara.hutool.json.JSONObject;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -16,6 +16,8 @@
|
||||
|
||||
package org.dromara.hutool.json.issues;
|
||||
|
||||
import org.dromara.hutool.json.JSONException;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -19,6 +19,8 @@ package org.dromara.hutool.json.issues;
|
||||
import lombok.Data;
|
||||
import org.dromara.hutool.core.reflect.ClassUtil;
|
||||
import org.dromara.hutool.core.util.ObjUtil;
|
||||
import org.dromara.hutool.json.JSONPrimitive;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.dromara.hutool.json.serializer.JSONContext;
|
||||
import org.dromara.hutool.json.serializer.JSONDeserializer;
|
||||
import org.dromara.hutool.json.serializer.JSONSerializer;
|
||||
|
@ -17,6 +17,9 @@
|
||||
package org.dromara.hutool.json.issues;
|
||||
|
||||
import lombok.Data;
|
||||
import org.dromara.hutool.json.JSONFactory;
|
||||
import org.dromara.hutool.json.JSONObject;
|
||||
import org.dromara.hutool.json.serializer.impl.ClassTypeAdapter;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@ -24,21 +27,28 @@ public class Issue3506Test {
|
||||
|
||||
@Test
|
||||
void toBeanTest() {
|
||||
final JSONFactory factory = JSONFactory.of(null, null);
|
||||
factory.register(Class.class, ClassTypeAdapter.INSTANCE);
|
||||
|
||||
final Languages languages = new Languages();
|
||||
languages.setLanguageType(Java.class);
|
||||
|
||||
final String hutoolJSONString = JSONUtil.toJsonStr(languages);
|
||||
final Languages bean = JSONUtil.toBean(hutoolJSONString, Languages.class);
|
||||
final String hutoolJSONString = factory.parseObj(languages).toString();
|
||||
Assertions.assertEquals("{\"languageType\":\"org.dromara.hutool.json.issues.Issue3506Test$Java\"}", hutoolJSONString);
|
||||
|
||||
final JSONObject jsonObject = factory.parseObj(hutoolJSONString);
|
||||
final Languages bean = jsonObject.toBean(Languages.class);
|
||||
|
||||
Assertions.assertNotNull(bean);
|
||||
Assertions.assertEquals(bean.getLanguageType(), Java.class);
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class Languages {
|
||||
static class Languages {
|
||||
private Class<? extends Language> languageType;
|
||||
}
|
||||
|
||||
public interface Language {
|
||||
interface Language {
|
||||
}
|
||||
|
||||
public static class Java implements Language {
|
||||
|
@ -17,6 +17,8 @@
|
||||
package org.dromara.hutool.json.issues;
|
||||
|
||||
import lombok.Data;
|
||||
import org.dromara.hutool.json.JSONConfig;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -16,6 +16,9 @@
|
||||
|
||||
package org.dromara.hutool.json.issues;
|
||||
|
||||
import org.dromara.hutool.json.JSONConfig;
|
||||
import org.dromara.hutool.json.JSONObject;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -17,6 +17,8 @@
|
||||
package org.dromara.hutool.json.issues;
|
||||
|
||||
import lombok.Data;
|
||||
import org.dromara.hutool.json.JSONConfig;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -18,6 +18,7 @@ package org.dromara.hutool.json.issues;
|
||||
|
||||
import org.dromara.hutool.core.lang.Opt;
|
||||
import org.dromara.hutool.core.map.MapUtil;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -16,8 +16,10 @@
|
||||
|
||||
package org.dromara.hutool.json.issues;
|
||||
|
||||
import org.dromara.hutool.core.date.TimeUtil;
|
||||
import lombok.Data;
|
||||
import org.dromara.hutool.core.date.TimeUtil;
|
||||
import org.dromara.hutool.json.JSONObject;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -16,8 +16,9 @@
|
||||
|
||||
package org.dromara.hutool.json.issues;
|
||||
|
||||
import org.dromara.hutool.core.date.DateUtil;
|
||||
import lombok.Data;
|
||||
import org.dromara.hutool.core.date.DateUtil;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -18,6 +18,8 @@ package org.dromara.hutool.json.issues;
|
||||
|
||||
import lombok.Data;
|
||||
import org.dromara.hutool.core.annotation.Alias;
|
||||
import org.dromara.hutool.json.JSONObject;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -16,8 +16,10 @@
|
||||
|
||||
package org.dromara.hutool.json.issues;
|
||||
|
||||
import org.dromara.hutool.core.collection.ListUtil;
|
||||
import lombok.Data;
|
||||
import org.dromara.hutool.core.collection.ListUtil;
|
||||
import org.dromara.hutool.json.JSONArray;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -17,6 +17,7 @@
|
||||
package org.dromara.hutool.json.issues;
|
||||
|
||||
import lombok.Data;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -17,6 +17,7 @@
|
||||
package org.dromara.hutool.json.issues;
|
||||
|
||||
import lombok.Data;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -17,6 +17,7 @@
|
||||
package org.dromara.hutool.json.issues;
|
||||
|
||||
import lombok.Data;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -17,6 +17,7 @@
|
||||
package org.dromara.hutool.json.issues;
|
||||
|
||||
import lombok.Data;
|
||||
import org.dromara.hutool.json.JSONObject;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -16,9 +16,10 @@
|
||||
|
||||
package org.dromara.hutool.json.issues;
|
||||
|
||||
import org.dromara.hutool.core.convert.ConvertUtil;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import org.dromara.hutool.core.convert.ConvertUtil;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -16,6 +16,9 @@
|
||||
|
||||
package org.dromara.hutool.json.issues;
|
||||
|
||||
import org.dromara.hutool.json.JSONConfig;
|
||||
import org.dromara.hutool.json.JSONObject;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -16,8 +16,9 @@
|
||||
|
||||
package org.dromara.hutool.json.issues;
|
||||
|
||||
import org.dromara.hutool.core.annotation.Alias;
|
||||
import lombok.Data;
|
||||
import org.dromara.hutool.core.annotation.Alias;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -18,6 +18,8 @@ package org.dromara.hutool.json.issues;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import org.dromara.hutool.json.JSONConfig;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -16,6 +16,9 @@
|
||||
|
||||
package org.dromara.hutool.json.issues;
|
||||
|
||||
import org.dromara.hutool.json.JSONArray;
|
||||
import org.dromara.hutool.json.JSONObject;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -17,6 +17,8 @@
|
||||
package org.dromara.hutool.json.issues;
|
||||
|
||||
import lombok.Data;
|
||||
import org.dromara.hutool.json.JSONObject;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -18,6 +18,8 @@ package org.dromara.hutool.json.issues;
|
||||
|
||||
import org.dromara.hutool.core.collection.ListUtil;
|
||||
import org.dromara.hutool.core.io.resource.ResourceUtil;
|
||||
import org.dromara.hutool.json.JSONObject;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
@ -19,6 +19,8 @@ package org.dromara.hutool.json.issues;
|
||||
import org.dromara.hutool.core.io.resource.ResourceUtil;
|
||||
import org.dromara.hutool.core.xml.XPathUtil;
|
||||
import org.dromara.hutool.core.xml.XmlUtil;
|
||||
import org.dromara.hutool.json.JSONObject;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.dromara.hutool.json.xml.JSONXMLSerializer;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
@ -17,6 +17,7 @@
|
||||
package org.dromara.hutool.json.issues;
|
||||
|
||||
import lombok.Data;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -16,6 +16,9 @@
|
||||
|
||||
package org.dromara.hutool.json.issues;
|
||||
|
||||
import org.dromara.hutool.json.JSON;
|
||||
import org.dromara.hutool.json.JSONPrimitive;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
@ -18,6 +18,7 @@ package org.dromara.hutool.json.issues;
|
||||
|
||||
import org.dromara.hutool.core.lang.mutable.MutableEntry;
|
||||
import org.dromara.hutool.core.reflect.TypeReference;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -18,6 +18,8 @@ package org.dromara.hutool.json.issues;
|
||||
|
||||
import lombok.Data;
|
||||
import org.dromara.hutool.core.reflect.TypeReference;
|
||||
import org.dromara.hutool.json.JSONObject;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -16,6 +16,7 @@
|
||||
|
||||
package org.dromara.hutool.json.issues;
|
||||
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -16,6 +16,8 @@
|
||||
|
||||
package org.dromara.hutool.json.issues;
|
||||
|
||||
import org.dromara.hutool.json.JSONObject;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -16,6 +16,7 @@
|
||||
|
||||
package org.dromara.hutool.json.issues;
|
||||
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -17,6 +17,9 @@
|
||||
package org.dromara.hutool.json.issues;
|
||||
|
||||
import org.dromara.hutool.core.reflect.TypeReference;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.dromara.hutool.json.serializer.JSONDeserializer;
|
||||
import org.dromara.hutool.json.serializer.TypeAdapterManager;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@ -29,6 +32,15 @@ public class IssueI7FQ29Test {
|
||||
|
||||
@Test
|
||||
void toMapTest() {
|
||||
// Class不添加默认反序列化器,防止可能的安全问题
|
||||
TypeAdapterManager.getInstance().register(Class.class, (JSONDeserializer<Class<?>>) (json, deserializeType) -> {
|
||||
try {
|
||||
return Class.forName(json.asJSONPrimitive().getValue().toString());
|
||||
} catch (final ClassNotFoundException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
});
|
||||
|
||||
final String jsonStr = "{\"trans_no\": \"java.lang.String\"}";
|
||||
final Map<String, Class<?>> map = JSONUtil.toBean(jsonStr, new TypeReference<Map<String, Class<?>>>() {
|
||||
});
|
||||
|
@ -20,6 +20,7 @@ import org.dromara.hutool.core.lang.tuple.Pair;
|
||||
import org.dromara.hutool.core.lang.tuple.Triple;
|
||||
import org.dromara.hutool.core.lang.tuple.Tuple;
|
||||
import org.dromara.hutool.core.reflect.TypeReference;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -19,6 +19,8 @@ package org.dromara.hutool.json.issues;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import org.dromara.hutool.core.reflect.TypeReference;
|
||||
import org.dromara.hutool.json.JSON;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.dromara.hutool.json.serializer.JSONDeserializer;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
@ -16,6 +16,7 @@
|
||||
|
||||
package org.dromara.hutool.json.issues;
|
||||
|
||||
import org.dromara.hutool.json.JSONObject;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -16,6 +16,7 @@
|
||||
|
||||
package org.dromara.hutool.json.issues;
|
||||
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -19,6 +19,7 @@ package org.dromara.hutool.json.issues;
|
||||
import lombok.Data;
|
||||
import org.dromara.hutool.core.io.resource.ResourceUtil;
|
||||
import org.dromara.hutool.core.reflect.TypeReference;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -16,6 +16,7 @@
|
||||
|
||||
package org.dromara.hutool.json.issues;
|
||||
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -18,6 +18,8 @@ package org.dromara.hutool.json.issues;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.ToString;
|
||||
import org.dromara.hutool.json.JSONConfig;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -17,6 +17,8 @@
|
||||
package org.dromara.hutool.json.issues;
|
||||
|
||||
import lombok.Data;
|
||||
import org.dromara.hutool.json.JSONConfig;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -17,6 +17,8 @@
|
||||
package org.dromara.hutool.json.issues;
|
||||
|
||||
import lombok.Getter;
|
||||
import org.dromara.hutool.json.JSONObject;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -17,6 +17,9 @@
|
||||
package org.dromara.hutool.json.issues;
|
||||
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
import org.dromara.hutool.json.JSONConfig;
|
||||
import org.dromara.hutool.json.JSONFactory;
|
||||
import org.dromara.hutool.json.JSONObject;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -17,6 +17,9 @@
|
||||
package org.dromara.hutool.json.issues;
|
||||
|
||||
import lombok.Data;
|
||||
import org.dromara.hutool.json.JSONConfig;
|
||||
import org.dromara.hutool.json.JSONObject;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -17,6 +17,8 @@
|
||||
package org.dromara.hutool.json.issues;
|
||||
|
||||
import org.dromara.hutool.core.date.DateUtil;
|
||||
import org.dromara.hutool.json.JSONConfig;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.dromara.hutool.json.writer.NumberWriteMode;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
@ -17,6 +17,7 @@
|
||||
package org.dromara.hutool.json.issues;
|
||||
|
||||
import lombok.Data;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -16,6 +16,8 @@
|
||||
|
||||
package org.dromara.hutool.json.issues;
|
||||
|
||||
import org.dromara.hutool.json.JSONObject;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -18,6 +18,7 @@ package org.dromara.hutool.json.issues;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
@ -19,6 +19,8 @@ package org.dromara.hutool.json.issues;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.dromara.hutool.json.JSONObject;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.dromara.hutool.json.serializer.JSONDeserializer;
|
||||
import org.dromara.hutool.json.serializer.TypeAdapterManager;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
|
@ -17,6 +17,8 @@
|
||||
package org.dromara.hutool.json.issues;
|
||||
|
||||
import org.dromara.hutool.core.reflect.TypeReference;
|
||||
import org.dromara.hutool.json.JSONObject;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Map;
|
||||
|
@ -14,8 +14,9 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.dromara.hutool.json;
|
||||
package org.dromara.hutool.json.issues;
|
||||
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
@ -14,11 +14,14 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.dromara.hutool.json;
|
||||
package org.dromara.hutool.json.issues;
|
||||
|
||||
import lombok.Data;
|
||||
import org.dromara.hutool.core.io.resource.ResourceUtil;
|
||||
import org.dromara.hutool.core.reflect.TypeReference;
|
||||
import org.dromara.hutool.json.JSONArray;
|
||||
import org.dromara.hutool.json.JSONObject;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
@ -14,15 +14,22 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.dromara.hutool.json;
|
||||
package org.dromara.hutool.json.issues;
|
||||
|
||||
import org.dromara.hutool.json.JSONFactory;
|
||||
import org.dromara.hutool.json.JSONObject;
|
||||
import org.dromara.hutool.json.serializer.JSONSerializer;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class Pr3507Test {
|
||||
@Test
|
||||
void writeClassTest() {
|
||||
final JSONObject set = JSONUtil.ofObj().putObj("name", Pr3507Test.class);
|
||||
Assertions.assertEquals("{\"name\":\"org.dromara.hutool.json.Pr3507Test\"}", set.toString());
|
||||
// 考虑安全问题,不提供默认的Class的序列化器,此处局部自定义
|
||||
final JSONFactory factory = JSONFactory.of(null, null);
|
||||
factory.register(Class.class, (JSONSerializer<Class<?>>) (bean, context) -> context.getOrCreatePrimitive(bean.getName()));
|
||||
|
||||
final JSONObject set = factory.ofObj().putObj("name", Pr3507Test.class);
|
||||
Assertions.assertEquals("{\"name\":\"org.dromara.hutool.json.issues.Pr3507Test\"}", set.toString());
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user