This commit is contained in:
Looly
2022-09-30 21:45:32 +08:00
parent e7be2fed6b
commit 5f61405464
4 changed files with 65 additions and 57 deletions

View File

@@ -0,0 +1,27 @@
package cn.hutool.json;
import cn.hutool.core.date.DateUtil;
import org.junit.Assert;
import org.junit.Test;
import java.util.Date;
public class JSONWriterTest {
@Test
public void writeDateTest() {
final JSONObject jsonObject = JSONUtil.ofObj(JSONConfig.of().setDateFormat("yyyy-MM-dd"))
.set("date", DateUtil.parse("2022-09-30"));
// 日期原样写入
final Date date = jsonObject.getDate("date");
Assert.assertEquals("2022-09-30 00:00:00", date.toString());
// 自定义日期格式生效
Assert.assertEquals("{\"date\":\"2022-09-30\"}", jsonObject.toString());
// 自定义日期格式解析生效
final JSONObject parse = JSONUtil.parseObj(jsonObject.toString(), JSONConfig.of().setDateFormat("yyyy-MM-dd"));
Assert.assertEquals(String.class, parse.get("date").getClass());
}
}