mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix toBean
This commit is contained in:
@@ -1,25 +1,17 @@
|
||||
package cn.hutool.json;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import cn.hutool.json.serialize.JSONObjectSerializer;
|
||||
import lombok.ToString;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.json.serialize.JSONDeserializer;
|
||||
import cn.hutool.json.serialize.JSONObjectSerializer;
|
||||
import lombok.ToString;
|
||||
import java.util.Date;
|
||||
|
||||
public class CustomSerializeTest {
|
||||
|
||||
@Test
|
||||
public void serializeTest() {
|
||||
JSONUtil.putSerializer(CustomBean.class, new JSONObjectSerializer<CustomBean>() {
|
||||
|
||||
@Override
|
||||
public void serialize(JSONObject json, CustomBean bean) {
|
||||
json.put("customName", bean.name);
|
||||
}
|
||||
});
|
||||
JSONUtil.putSerializer(CustomBean.class, (JSONObjectSerializer<CustomBean>) (json, bean) -> json.put("customName", bean.name));
|
||||
|
||||
CustomBean customBean = new CustomBean();
|
||||
customBean.name = "testName";
|
||||
@@ -30,15 +22,10 @@ public class CustomSerializeTest {
|
||||
|
||||
@Test
|
||||
public void deserializeTest() {
|
||||
JSONUtil.putDeserializer(CustomBean.class, new JSONDeserializer<CustomBean>() {
|
||||
|
||||
@Override
|
||||
public CustomBean deserialize(JSON json) {
|
||||
CustomBean customBean = new CustomBean();
|
||||
customBean.name = ((JSONObject)json).getStr("customName");
|
||||
return customBean;
|
||||
}
|
||||
|
||||
JSONUtil.putDeserializer(CustomBean.class, json -> {
|
||||
CustomBean customBean = new CustomBean();
|
||||
customBean.name = ((JSONObject)json).getStr("customName");
|
||||
return customBean;
|
||||
});
|
||||
|
||||
String jsonStr = "{\"customName\":\"testName\"}";
|
||||
|
@@ -31,14 +31,36 @@ public class Issue488Test {
|
||||
Assert.assertEquals("MeetingRoom219@abc.com", adds.get(3).getAddress());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toCollctionBeanTest() {
|
||||
String jsonStr = ResourceUtil.readUtf8Str("issue488Array.json");
|
||||
|
||||
List<ResultSuccess<List<EmailAddress>>> resultList = JSONUtil.toBean(jsonStr,
|
||||
new TypeReference<List<ResultSuccess<List<EmailAddress>>>>() {}, false);
|
||||
|
||||
ResultSuccess<List<EmailAddress>> result = resultList.get(0);
|
||||
|
||||
Assert.assertEquals("https://graph.microsoft.com/beta/$metadata#Collection(microsoft.graph.emailAddress)", result.getContext());
|
||||
|
||||
List<EmailAddress> adds = result.getValue();
|
||||
Assert.assertEquals("会议室101", adds.get(0).getName());
|
||||
Assert.assertEquals("MeetingRoom101@abc.com", adds.get(0).getAddress());
|
||||
Assert.assertEquals("会议室102", adds.get(1).getName());
|
||||
Assert.assertEquals("MeetingRoom102@abc.com", adds.get(1).getAddress());
|
||||
Assert.assertEquals("会议室103", adds.get(2).getName());
|
||||
Assert.assertEquals("MeetingRoom103@abc.com", adds.get(2).getAddress());
|
||||
Assert.assertEquals("会议室219", adds.get(3).getName());
|
||||
Assert.assertEquals("MeetingRoom219@abc.com", adds.get(3).getAddress());
|
||||
}
|
||||
|
||||
@Data
|
||||
public class ResultSuccess<T> {
|
||||
public static class ResultSuccess<T> {
|
||||
private String context;
|
||||
private T value;
|
||||
}
|
||||
|
||||
@Data
|
||||
public class EmailAddress {
|
||||
public static class EmailAddress {
|
||||
private String name;
|
||||
private String address;
|
||||
}
|
||||
|
@@ -1,20 +1,17 @@
|
||||
package cn.hutool.json;
|
||||
|
||||
import cn.hutool.core.io.resource.ResourceUtil;
|
||||
import cn.hutool.json.test.bean.ExamInfoDict;
|
||||
import cn.hutool.json.test.bean.PerfectEvaluationProductResVo;
|
||||
import cn.hutool.json.test.bean.UserInfoDict;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.core.io.resource.ResourceUtil;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import cn.hutool.json.test.bean.ExamInfoDict;
|
||||
import cn.hutool.json.test.bean.PerfectEvaluationProductResVo;
|
||||
import cn.hutool.json.test.bean.UserInfoDict;
|
||||
|
||||
/**
|
||||
* JSON转换单元测试
|
||||
*
|
||||
@@ -46,14 +43,14 @@ public class JSONConvertTest {
|
||||
examInfoDict2.setExamType(1);
|
||||
examInfoDict2.setAnswerIs(0);
|
||||
|
||||
List<ExamInfoDict> examInfoDicts = new ArrayList<ExamInfoDict>();
|
||||
List<ExamInfoDict> examInfoDicts = new ArrayList<>();
|
||||
examInfoDicts.add(examInfoDict);
|
||||
examInfoDicts.add(examInfoDict1);
|
||||
examInfoDicts.add(examInfoDict2);
|
||||
|
||||
userInfoDict.setExamInfoDict(examInfoDicts);
|
||||
|
||||
Map<String, Object> tempMap = new HashMap<String, Object>();
|
||||
Map<String, Object> tempMap = new HashMap<>();
|
||||
tempMap.put("userInfoDict", userInfoDict);
|
||||
tempMap.put("toSendManIdCard", 1);
|
||||
|
||||
|
@@ -161,7 +161,7 @@ public class JSONObjectTest {
|
||||
// 当JSON中为字符串"null"时应被当作字符串处理
|
||||
Assert.assertEquals("null", bean.getStrValue());
|
||||
// 当JSON中为字符串"null"时Bean中的字段类型不匹配应在ignoreError模式下忽略注入
|
||||
Assert.assertEquals(null, bean.getBeanValue());
|
||||
Assert.assertNull(bean.getBeanValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
16
hutool-json/src/test/resources/issue488Array.json
Normal file
16
hutool-json/src/test/resources/issue488Array.json
Normal file
@@ -0,0 +1,16 @@
|
||||
[{
|
||||
"context": "https://graph.microsoft.com/beta/$metadata#Collection(microsoft.graph.emailAddress)",
|
||||
"value": [{
|
||||
"name": "\u4f1a\u8bae\u5ba4101",
|
||||
"address": "MeetingRoom101@abc.com"
|
||||
}, {
|
||||
"name": "\u4f1a\u8bae\u5ba4102",
|
||||
"address": "MeetingRoom102@abc.com"
|
||||
}, {
|
||||
"name": "\u4f1a\u8bae\u5ba4103",
|
||||
"address": "MeetingRoom103@abc.com"
|
||||
}, {
|
||||
"name": "\u4f1a\u8bae\u5ba4219",
|
||||
"address": "MeetingRoom219@abc.com"
|
||||
}]
|
||||
}]
|
Reference in New Issue
Block a user