mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
add config
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* Copyright (c) 2024. looly(loolly@aliyun.com)
|
||||
* Hutool is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* https://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
package org.dromara.hutool.json.engine;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Date;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class BeanWithDate {
|
||||
private Date date1;
|
||||
private LocalDateTime date2;
|
||||
}
|
@@ -1,5 +1,8 @@
|
||||
package org.dromara.hutool.json.engine;
|
||||
|
||||
import org.dromara.hutool.core.date.DateTime;
|
||||
import org.dromara.hutool.core.date.DateUtil;
|
||||
import org.dromara.hutool.core.date.TimeUtil;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -20,4 +23,17 @@ public class FastJSONTest {
|
||||
" \"gender\":true\n" +
|
||||
"}", jsonString);
|
||||
}
|
||||
|
||||
@Test
|
||||
void writeDateFormatTest() {
|
||||
final DateTime date = DateUtil.parse("2024-01-01 01:12:21");
|
||||
final BeanWithDate bean = new BeanWithDate(date, TimeUtil.of(date));
|
||||
final JSONEngine engine = JSONEngineFactory.createEngine("fastjson");
|
||||
|
||||
final String jsonString = engine.toJsonString(bean);
|
||||
Assertions.assertEquals("{\"date1\":1704042741000,\"date2\":1704042741000}", jsonString);
|
||||
|
||||
engine.init(JSONEngineConfig.of().setDateFormat("yyyy-MM-dd HH:mm:ss"));
|
||||
Assertions.assertEquals("{\"date1\":\"2024-01-01 01:12:21\",\"date2\":\"2024-01-01 01:12:21\"}", engine.toJsonString(bean));
|
||||
}
|
||||
}
|
||||
|
@@ -1,5 +1,8 @@
|
||||
package org.dromara.hutool.json.engine;
|
||||
|
||||
import org.dromara.hutool.core.date.DateTime;
|
||||
import org.dromara.hutool.core.date.DateUtil;
|
||||
import org.dromara.hutool.core.date.TimeUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@@ -21,4 +24,17 @@ public class GsonTest {
|
||||
" \"gender\": true\n" +
|
||||
"}", jsonString);
|
||||
}
|
||||
|
||||
@Test
|
||||
void writeDateFormatTest() {
|
||||
final DateTime date = DateUtil.parse("2024-01-01 01:12:21");
|
||||
final BeanWithDate bean = new BeanWithDate(date, TimeUtil.of(date));
|
||||
final JSONEngine engine = JSONEngineFactory.createEngine("gson");
|
||||
|
||||
final String jsonString = engine.toJsonString(bean);
|
||||
Assertions.assertEquals("{\"date1\":1704042741000,\"date2\":1704042741000}", jsonString);
|
||||
|
||||
engine.init(JSONEngineConfig.of().setDateFormat("yyyy-MM-dd HH:mm:ss"));
|
||||
Assertions.assertEquals("{\"date1\":\"2024-01-01 01:12:21\",\"date2\":\"2024-01-01 01:12:21\"}", engine.toJsonString(bean));
|
||||
}
|
||||
}
|
||||
|
@@ -1,5 +1,8 @@
|
||||
package org.dromara.hutool.json.engine;
|
||||
|
||||
import org.dromara.hutool.core.date.DateTime;
|
||||
import org.dromara.hutool.core.date.DateUtil;
|
||||
import org.dromara.hutool.core.date.TimeUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@@ -17,4 +20,17 @@ public class HutoolJSONTest {
|
||||
" \"gender\": true\n" +
|
||||
"}", jsonString);
|
||||
}
|
||||
|
||||
@Test
|
||||
void writeDateFormatTest() {
|
||||
final DateTime date = DateUtil.parse("2024-01-01 01:12:21");
|
||||
final BeanWithDate bean = new BeanWithDate(date, TimeUtil.of(date));
|
||||
final JSONEngine engine = JSONEngineFactory.createEngine("hutool");
|
||||
|
||||
final String jsonString = engine.toJsonString(bean);
|
||||
Assertions.assertEquals("{\"date1\":1704042741000,\"date2\":1704042741000}", jsonString);
|
||||
|
||||
engine.init(JSONEngineConfig.of().setDateFormat("yyyy-MM-dd HH:mm:ss"));
|
||||
Assertions.assertEquals("{\"date1\":\"2024-01-01 01:12:21\",\"date2\":\"2024-01-01 01:12:21\"}", engine.toJsonString(bean));
|
||||
}
|
||||
}
|
||||
|
@@ -1,5 +1,8 @@
|
||||
package org.dromara.hutool.json.engine;
|
||||
|
||||
import org.dromara.hutool.core.date.DateTime;
|
||||
import org.dromara.hutool.core.date.DateUtil;
|
||||
import org.dromara.hutool.core.date.TimeUtil;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -23,4 +26,18 @@ public class JacksonTest {
|
||||
" \"gender\" : true\n" +
|
||||
"}", jsonString);
|
||||
}
|
||||
|
||||
@Test
|
||||
void writeDateFormatTest() {
|
||||
final DateTime date = DateUtil.parse("2024-01-01 01:12:21");
|
||||
final BeanWithDate bean = new BeanWithDate(date, TimeUtil.of(date));
|
||||
final JSONEngine engine = JSONEngineFactory.createEngine("jackson");
|
||||
|
||||
final String jsonString = engine.toJsonString(bean);
|
||||
Assertions.assertEquals("{\"date1\":1704042741000,\"date2\":[2024,1,1,1,12,21]}", jsonString);
|
||||
|
||||
//TODO LocalDateTime的格式化未解决
|
||||
engine.init(JSONEngineConfig.of().setDateFormat("yyyy-MM-dd HH:mm:ss"));
|
||||
Assertions.assertEquals("{\"date1\":\"2024-01-01 01:12:21\",\"date2\":\"2024-01-01T01:12:21\"}", engine.toJsonString(bean));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user