This commit is contained in:
Looly
2024-09-22 18:15:39 +08:00
parent 43a0201a20
commit ff187f6fde
44 changed files with 831 additions and 664 deletions

View File

@@ -17,6 +17,7 @@
package org.dromara.hutool.json;
import lombok.ToString;
import org.dromara.hutool.core.lang.Console;
import org.dromara.hutool.json.serializer.JSONDeserializer;
import org.dromara.hutool.json.serializer.JSONSerializer;
import org.dromara.hutool.json.serializer.TypeAdapterManager;
@@ -31,8 +32,13 @@ public class CustomSerializeTest {
@BeforeEach
public void init() {
TypeAdapterManager.getInstance().register(CustomBean.class,
(JSONSerializer<CustomBean>) (bean, context) ->
((JSONObject)context.getContextJson()).set("customName", bean.name));
(JSONSerializer<CustomBean>) (bean, context) ->{
JSONObject contextJson = (JSONObject) context.getContextJson();
if(null == contextJson){
contextJson = JSONUtil.ofObj(context.config());
}
return contextJson.set("customName", bean.name);
});
}
@Test
@@ -41,6 +47,7 @@ public class CustomSerializeTest {
customBean.name = "testName";
final JSONObject obj = JSONUtil.parseObj(customBean);
Console.log(obj);
Assertions.assertEquals("testName", obj.getStr("customName"));
}

View File

@@ -20,10 +20,7 @@ import lombok.Data;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.*;
/**
* <a href="https://github.com/dromara/hutool/issues/2090">https://github.com/dromara/hutool/issues/2090</a>
@@ -76,6 +73,23 @@ public class Issue2090Test {
final JSONObject jsonObject = new JSONObject();
jsonObject.set("month", Month.JANUARY);
Assertions.assertEquals("{\"month\":1}", jsonObject.toString());
final JSON parse = JSONUtil.parse(Month.JANUARY);
Assertions.assertInstanceOf(JSONPrimitive.class, parse);
Assertions.assertTrue(((JSONPrimitive) parse).isNumber());
Assertions.assertEquals("1", parse.toString());
}
@Test
public void weekTest(){
final JSONObject jsonObject = new JSONObject();
jsonObject.set("week", DayOfWeek.SUNDAY);
Assertions.assertEquals("{\"week\":7}", jsonObject.toString());
final JSON parse = JSONUtil.parse(DayOfWeek.SUNDAY);
Assertions.assertInstanceOf(JSONPrimitive.class, parse);
Assertions.assertTrue(((JSONPrimitive) parse).isNumber());
Assertions.assertEquals("7", parse.toString());
}
@Data

View File

@@ -26,10 +26,10 @@ public class Issue2447Test {
@Test
public void addIntegerTest() {
Time time = new Time();
final Time time = new Time();
time.setTime(LocalDateTime.of(1970, 1, 2, 10, 0, 1, 0));
String timeStr = JSONUtil.toJsonStr(time);
Assertions.assertEquals(timeStr, "{\"time\":93601000}");
final String timeStr = JSONUtil.toJsonStr(time);
Assertions.assertEquals("{\"time\":93601000}", timeStr);
Assertions.assertEquals(JSONUtil.toBean(timeStr, Time.class).getTime(), time.getTime());
}

View File

@@ -30,7 +30,7 @@ public class Issue3681Test {
Assertions.assertEquals("\"abc\"", abc);
abc = JSONUtil.toJsonStr(Optional.of("123"));
Assertions.assertEquals("123", abc);
Assertions.assertEquals("\"123\"", abc);
}
@Test
@@ -45,6 +45,6 @@ public class Issue3681Test {
Assertions.assertEquals("\"abc\"", abc);
abc = JSONUtil.toJsonStr(Opt.of("123"));
Assertions.assertEquals("123", abc);
Assertions.assertEquals("\"123\"", abc);
}
}

View File

@@ -16,7 +16,6 @@
package org.dromara.hutool.json;
import org.dromara.hutool.core.bean.BeanUtil;
import lombok.Data;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
@@ -31,8 +30,7 @@ public class IssueI3BS4STest {
@Test
public void toBeanTest(){
final String jsonStr = "{date: '2021-03-17T06:31:33.99'}";
final Bean1 bean1 = new Bean1();
BeanUtil.copyProperties(JSONUtil.parseObj(jsonStr), bean1);
final Bean1 bean1 = JSONUtil.parseObj(jsonStr).toBean(Bean1.class);
Assertions.assertEquals("2021-03-17T06:31:33.990", bean1.getDate().toString());
}

View File

@@ -281,7 +281,7 @@ public class JSONArrayTest {
.set("value3")
.set(true);
final String s = json1.toJSONString(0, (pair) -> pair.getValue().equals("value2"));
final String s = json1.toJSONString(0, (pair) -> ((JSONPrimitive)pair.getValue()).getValue().equals("value2"));
assertEquals("[\"value2\"]", s);
}
@@ -293,7 +293,7 @@ public class JSONArrayTest {
.set("value3")
.set(true);
final String s = json1.toJSONString(0, (pair) -> !pair.getValue().equals("value2"));
final String s = json1.toJSONString(0, (pair) -> !((JSONPrimitive)pair.getValue()).getValue().equals("value2"));
assertEquals("[\"value1\",\"value3\",true]", s);
}

View File

@@ -22,6 +22,7 @@ import org.dromara.hutool.core.annotation.PropIgnore;
import org.dromara.hutool.core.collection.ListUtil;
import org.dromara.hutool.core.date.DatePattern;
import org.dromara.hutool.core.date.DateUtil;
import org.dromara.hutool.core.date.format.GlobalCustomFormat;
import org.dromara.hutool.core.io.resource.ResourceUtil;
import org.dromara.hutool.core.map.MapUtil;
import org.dromara.hutool.core.text.StrUtil;
@@ -113,7 +114,7 @@ public class JSONObjectTest {
@Test
public void parseStringTest() {
final String jsonStr = "{\"b\":\"value2\",\"c\":\"value3\",\"a\":\"value1\", \"d\": true, \"e\": null}";
final JSONObject jsonObject = JSONUtil.parseObj(jsonStr);
final JSONObject jsonObject = JSONUtil.parseObj(jsonStr, JSONConfig.of().setIgnoreNullValue(false));
assertEquals(jsonObject.getObj("a"), "value1");
assertEquals(jsonObject.getObj("b"), "value2");
assertEquals(jsonObject.getObj("c"), "value3");
@@ -226,6 +227,13 @@ public class JSONObjectTest {
assertNull(bean.getBeanValue());
}
@Test
void addListTest(){
final JSONObject json = JSONUtil.ofObj();
json.set("list", ListUtil.of(1, 2, 3));
Assertions.assertEquals("{\"list\":[1,2,3]}", json.toString());
}
@Test
public void toBeanTest2() {
final UserA userA = new UserA();
@@ -245,7 +253,7 @@ public class JSONObjectTest {
@Test
public void toBeanWithNullTest() {
final String jsonStr = "{'data':{'userName':'ak','password': null}}";
final UserWithMap user = JSONUtil.toBean(JSONUtil.parseObj(jsonStr), UserWithMap.class);
final UserWithMap user = JSONUtil.toBean(JSONUtil.parseObj(jsonStr, JSONConfig.of().setIgnoreNullValue(false)), UserWithMap.class);
Assertions.assertTrue(user.getData().containsKey("password"));
}
@@ -323,7 +331,7 @@ public class JSONObjectTest {
}
@Test
public void parseBeanTest2() {
public void parseBeanWithNumberListEnumTest() {
final TestBean bean = new TestBean();
bean.setDoubleValue(111.1);
bean.setIntValue(123);
@@ -333,8 +341,10 @@ public class JSONObjectTest {
final JSONObject json = JSONUtil.parseObj(bean,
JSONConfig.of().setIgnoreNullValue(false));
// 枚举转换检查更新枚举原样保存在writer时调用toString。
assertEquals(TestEnum.TYPE_B, json.getObj("testEnum"));
assertEquals(111.1, json.getObj("doubleValue"));
// 枚举转换检查默认序列化枚举为其name
assertEquals(TestEnum.TYPE_B.name(), json.getObj("testEnum"));
final TestBean bean2 = json.toBean(TestBean.class);
assertEquals(bean.toString(), bean2.toString());
@@ -479,9 +489,9 @@ public class JSONObjectTest {
}
@Test
public void setDateFormatTest3() {
public void setDateFormatSecondsTest() {
// 自定义格式为只有秒的时间戳一般用于JWT
final JSONConfig jsonConfig = JSONConfig.of().setDateFormat("#sss");
final JSONConfig jsonConfig = JSONConfig.of().setDateFormat(GlobalCustomFormat.FORMAT_SECONDS);
final Date date = DateUtil.parse("2020-06-05 11:16:11");
final JSONObject json = new JSONObject(jsonConfig);
@@ -497,7 +507,7 @@ public class JSONObjectTest {
@Test
public void setCustomDateFormatTest() {
final JSONConfig jsonConfig = JSONConfig.of();
jsonConfig.setDateFormat("#sss");
jsonConfig.setDateFormat(GlobalCustomFormat.FORMAT_SECONDS);
final Date date = DateUtil.parse("2020-06-05 11:16:11");
final JSONObject json = new JSONObject(jsonConfig);
@@ -609,7 +619,7 @@ public class JSONObjectTest {
@Test
public void createJSONObjectTest() {
Assertions.assertThrows(JSONException.class, ()->{
Assertions.assertThrows(ClassCastException.class, ()->{
// 集合类不支持转为JSONObject
JSONUtil.parseObj(new JSONArray(), JSONConfig.of());
});
@@ -692,7 +702,8 @@ public class JSONObjectTest {
final String s = json1.toJSONString(0, (pair) -> {
if ("b".equals(pair.getKey())) {
// 修改值为新值
pair.setValue(pair.getValue() + "_edit");
final JSONPrimitive value = (JSONPrimitive) pair.getValue();
pair.setValue(value.getValue() + "_edit");
return true;
}
// 除了"b",其他都去掉

View File

@@ -102,7 +102,7 @@ public class JSONUtilTest {
*/
@Test
public void parseNumberToJSONArrayTest() {
assertThrows(JSONException.class, () -> {
assertThrows(ClassCastException.class, () -> {
final JSONArray json = JSONUtil.parseArray(123L);
Assertions.assertNotNull(json);
});

View File

@@ -31,7 +31,7 @@ public class JSONWriterTest {
// 日期原样写入
final Date date = jsonObject.getDate("date");
Assertions.assertEquals("2022-09-30 00:00:00", date.toString());
Assertions.assertEquals("2022-09-30 00:00:00", DateUtil.date(date).toString());
// 自定义日期格式生效
Assertions.assertEquals("{\"date\":\"2022-09-30\"}", jsonObject.toString());

View File

@@ -161,7 +161,7 @@ public class JWTTest {
Assertions.assertEquals(bean, beanRes);
Assertions.assertEquals(numRes, num);
Assertions.assertEquals(username, strRes);
Assertions.assertEquals(list, listRes);
Assertions.assertEquals(list.toString(), listRes.toString());
final String formattedDate = DateUtil.format(date, "yyyy-MM-dd HH:mm:ss");
final String formattedRes = DateUtil.format(dateRes, "yyyy-MM-dd HH:mm:ss");

View File

@@ -39,7 +39,7 @@ public class XMLTest {
Assertions.assertEquals("{\"a\":\"\"}", jsonObject.toString());
final String xml2 = JSONXMLUtil.toXml(JSONUtil.parseObj(jsonObject));
final String xml2 = JSONXMLUtil.toXml(jsonObject);
Assertions.assertEquals(xml, xml2);
}
@@ -53,4 +53,11 @@ public class XMLTest {
xml = JSONXMLUtil.toXml(jsonObject, null, new String[0]);
Assertions.assertEquals("<content>123456</content>", xml);
}
@Test
public void xmlContentTest2(){
final JSONObject jsonObject = JSONUtil.ofObj().set("content","123456");
final String xml = JSONXMLUtil.toXml(jsonObject, null, new String[0]);
Assertions.assertEquals("<content>123456</content>", xml);
}
}