mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix code
This commit is contained in:
@@ -29,6 +29,7 @@ import org.dromara.hutool.core.reflect.TypeReference;
|
||||
import org.dromara.hutool.core.reflect.TypeUtil;
|
||||
import org.dromara.hutool.core.reflect.kotlin.KClassUtil;
|
||||
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.JSONParser;
|
||||
import org.dromara.hutool.json.reader.JSONTokener;
|
||||
@@ -59,13 +60,6 @@ public class JSONConverter implements Converter, Serializable {
|
||||
*/
|
||||
public static final JSONConverter INSTANCE = new JSONConverter(null);
|
||||
|
||||
static {
|
||||
final RegisterConverter converter = RegisterConverter.getInstance();
|
||||
converter.register(JSONObject.class, INSTANCE);
|
||||
converter.register(JSONArray.class, INSTANCE);
|
||||
converter.register(JSONPrimitive.class, INSTANCE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建JSON转换器
|
||||
*
|
||||
@@ -73,17 +67,25 @@ public class JSONConverter implements Converter, Serializable {
|
||||
* @return JSONConverter
|
||||
*/
|
||||
public static JSONConverter of(final JSONConfig config) {
|
||||
return new JSONConverter(config);
|
||||
final JSONConverter jsonConverter = new JSONConverter(config);
|
||||
jsonConverter.registerConverter = new RegisterConverter(jsonConverter)
|
||||
.register(JSONObject.class, INSTANCE)
|
||||
.register(JSONArray.class, INSTANCE)
|
||||
.register(JSONPrimitive.class, INSTANCE);
|
||||
jsonConverter.specialConverter = new SpecialConverter(jsonConverter);
|
||||
return jsonConverter;
|
||||
}
|
||||
|
||||
private final JSONConfig config;
|
||||
private RegisterConverter registerConverter;
|
||||
private SpecialConverter specialConverter;
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param config JSON配置
|
||||
*/
|
||||
public JSONConverter(final JSONConfig config) {
|
||||
private JSONConverter(final JSONConfig config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
@@ -150,7 +152,7 @@ public class JSONConverter implements Converter, Serializable {
|
||||
obj = ((Opt<?>) obj).getOrNull();
|
||||
}
|
||||
|
||||
if(obj instanceof JSON){
|
||||
if (obj instanceof JSON) {
|
||||
return (JSON) obj;
|
||||
}
|
||||
|
||||
@@ -242,33 +244,41 @@ public class JSONConverter implements Converter, Serializable {
|
||||
|
||||
final Object value;
|
||||
// JSON原始类型
|
||||
if(json instanceof JSONPrimitive){
|
||||
if (json instanceof JSONPrimitive) {
|
||||
value = ((JSONPrimitive) json).getValue();
|
||||
} else {
|
||||
value = json;
|
||||
}
|
||||
|
||||
// 标准转换器
|
||||
final Converter converter = RegisterConverter.getInstance().getConverter(targetType, value, true);
|
||||
if (null != converter) {
|
||||
return (T) converter.convert(targetType, value);
|
||||
}
|
||||
final JSONConfig config = ObjUtil.defaultIfNull(json.config(), JSONConfig::of);
|
||||
final boolean ignoreError = config.isIgnoreError();
|
||||
try {
|
||||
// 标准转换器
|
||||
final Converter converter = registerConverter.getConverter(targetType, value, true);
|
||||
if (null != converter) {
|
||||
return (T) converter.convert(targetType, value);
|
||||
}
|
||||
|
||||
// 特殊类型转换,包括Collection、Map、强转、Array等
|
||||
final T result = (T) SpecialConverter.getInstance().convert(targetType, rawType, value);
|
||||
if (null != result) {
|
||||
return result;
|
||||
// 特殊类型转换,包括Collection、Map、强转、Array等
|
||||
final T result = (T) specialConverter.convert(targetType, rawType, value);
|
||||
if (null != result) {
|
||||
return result;
|
||||
}
|
||||
} catch (final ConvertException e) {
|
||||
if (ignoreError) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// 尝试转Bean
|
||||
if (BeanUtil.isWritableBean(rawType)) {
|
||||
return BeanCopier.of(value,
|
||||
ConstructorUtil.newInstanceIfPossible(rawType), targetType,
|
||||
InternalJSONUtil.toCopyOptions(json.config())).copy();
|
||||
InternalJSONUtil.toCopyOptions(config)).copy();
|
||||
}
|
||||
|
||||
// 跳过异常时返回null
|
||||
if (json.config().isIgnoreError()) {
|
||||
if (ignoreError) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@@ -16,28 +16,41 @@
|
||||
|
||||
package org.dromara.hutool.json;
|
||||
|
||||
import lombok.Data;
|
||||
import org.dromara.hutool.core.io.resource.ResourceUtil;
|
||||
import org.dromara.hutool.core.lang.Console;
|
||||
import org.dromara.hutool.json.test.bean.ResultBean;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 测试在bean转换时使用BeanConverter,默认忽略转换失败的字段。
|
||||
* 现阶段Converter的问题在于,无法更细粒度的控制转换失败的范围,例如Bean的一个字段为List,
|
||||
* list任意一个item转换失败都会导致这个list为null。
|
||||
* <p>
|
||||
* TODO 需要在Converter中添加ConvertOption,用于更细粒度的控制转换规则
|
||||
* 转换失败,则在设置setIgnoreError(true)不报错
|
||||
*/
|
||||
public class Issue1200Test {
|
||||
|
||||
@Test
|
||||
@Disabled
|
||||
public void toBeanTest(){
|
||||
final JSONObject jsonObject = JSONUtil.parseObj(ResourceUtil.readUtf8Str("issue1200.json"));
|
||||
Console.log(jsonObject);
|
||||
public void toBeanTest() {
|
||||
final JSONObject jsonObject = JSONUtil.parseObj(
|
||||
ResourceUtil.readUtf8Str("issue1200.json"),
|
||||
JSONConfig.of().setIgnoreError(true));
|
||||
|
||||
final ResultBean resultBean = jsonObject.toBean(ResultBean.class);
|
||||
Console.log(resultBean);
|
||||
Assertions.assertNull(resultBean.getItems().get(0).get(0));
|
||||
}
|
||||
|
||||
@Data
|
||||
static
|
||||
class ResultBean {
|
||||
public List<List<List<ResultBean.ItemsBean>>> items;
|
||||
|
||||
@Data
|
||||
public static class ItemsBean {
|
||||
public ResultBean.ItemsBean.DetailBean detail;
|
||||
|
||||
@Data
|
||||
public static class DetailBean {
|
||||
public String visitorStatus;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -193,7 +193,7 @@ public class JSONArrayTest {
|
||||
|
||||
@Test
|
||||
public void toListWithErrorTest() {
|
||||
Assertions.assertThrows(ConvertException.class, ()->{
|
||||
Assertions.assertThrows(JSONException.class, ()->{
|
||||
final String json = "[['aaa',{'akey':'avalue','bkey':'bvalue'}]]";
|
||||
final JSONArray ja = JSONUtil.parseArray(json);
|
||||
|
||||
|
@@ -1,36 +0,0 @@
|
||||
/*
|
||||
* 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.test.bean;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class ResultBean {
|
||||
public List<List<List<ItemsBean>>> items;
|
||||
|
||||
@Data
|
||||
public static class ItemsBean {
|
||||
public DetailBean detail;
|
||||
|
||||
@Data
|
||||
public static class DetailBean {
|
||||
public String visitorStatus;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user