mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
fix code
This commit is contained in:
parent
a3b39e4646
commit
5f34b941fa
@ -16,6 +16,7 @@ import cn.hutool.json.serialize.JSONWriter;
|
||||
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
@ -196,7 +197,7 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
|
||||
this(DEFAULT_CAPACITY, jsonConfig);
|
||||
init(object);
|
||||
}
|
||||
// -------------------------------------------------------------------------------------------------------------------- Constructor start
|
||||
// -------------------------------------------------------------------------------------------------------------------- Constructor end
|
||||
|
||||
@Override
|
||||
public JSONConfig getConfig() {
|
||||
@ -291,6 +292,11 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T toBean(Type type) {
|
||||
return JSON.super.toBean(type, config.isIgnoreError());
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据给定名列表,与其位置对应的值组成JSONObject
|
||||
*
|
||||
|
@ -22,6 +22,7 @@ import cn.hutool.json.serialize.JSONWriter;
|
||||
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
import java.lang.reflect.Type;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.util.Collection;
|
||||
@ -274,6 +275,11 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T toBean(Type type) {
|
||||
return JSON.super.toBean(type, this.config.isIgnoreError());
|
||||
}
|
||||
|
||||
/**
|
||||
* 将指定KEY列表的值组成新的JSONArray
|
||||
*
|
||||
|
@ -421,6 +421,21 @@ public class JSONUtil {
|
||||
return toBean(parseObj(jsonString), beanClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON字符串转为实体类对象,转换异常将被抛出<br>
|
||||
* 通过{@link JSONConfig}可选是否忽略大小写、忽略null等配置
|
||||
*
|
||||
* @param <T> Bean类型
|
||||
* @param jsonString JSON字符串
|
||||
* @param config JSON配置
|
||||
* @param beanClass 实体类对象
|
||||
* @return 实体类对象
|
||||
* @since 5.8.0
|
||||
*/
|
||||
public static <T> T toBean(String jsonString, JSONConfig config, Class<T> beanClass) {
|
||||
return toBean(parseObj(jsonString, config), beanClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* 转为实体类对象,转换异常将被抛出
|
||||
*
|
||||
@ -458,7 +473,7 @@ public class JSONUtil {
|
||||
* @since 4.3.2
|
||||
*/
|
||||
public static <T> T toBean(String jsonString, Type beanType, boolean ignoreError) {
|
||||
return toBean(parse(jsonString), beanType, ignoreError);
|
||||
return parse(jsonString, JSONConfig.create().setIgnoreError(ignoreError)).toBean(beanType);
|
||||
}
|
||||
|
||||
/**
|
||||
|
24
hutool-json/src/test/java/cn/hutool/json/IssueI50EGGTest.java
Executable file
24
hutool-json/src/test/java/cn/hutool/json/IssueI50EGGTest.java
Executable file
@ -0,0 +1,24 @@
|
||||
package cn.hutool.json;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class IssueI50EGGTest {
|
||||
|
||||
@Test
|
||||
public void toBeanTest(){
|
||||
String data = "{\"return_code\": 1, \"return_msg\": \"成功\", \"return_data\" : null}";
|
||||
final ApiResult<?> apiResult = JSONUtil.toBean(data, JSONConfig.create().setIgnoreCase(true), ApiResult.class);
|
||||
Assert.assertEquals(1, apiResult.getReturn_code());
|
||||
}
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
static class ApiResult<T>{
|
||||
private long Return_code;
|
||||
private String Return_msg;
|
||||
private T Return_data;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user