mirror of
https://github.com/PowerJob/PowerJob.git
synced 2025-07-17 00:00:04 +08:00
fix: Java 8 date/time type java.time.LocalDateTime not supported by default #869
This commit is contained in:
parent
e21b171b98
commit
5e7751f092
@ -35,6 +35,13 @@ public class JsonUtils {
|
||||
|
||||
static {
|
||||
JSON_MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
||||
|
||||
// 非核心功能可降级,尽可能降低依赖冲突概率
|
||||
try {
|
||||
JSON_MAPPER.registerModule(new com.fasterxml.jackson.datatype.jsr310.JavaTimeModule());
|
||||
} catch (Exception e) {
|
||||
log.warn("[JsonUtils] registerJavaTimeModule failed, PowerJob can't process Java 8 date/time type now!", e);
|
||||
}
|
||||
}
|
||||
|
||||
private static final TypeReference<Map<String, Object>> MAP_TYPE_REFERENCE = new TypeReference<Map<String, Object>> () {};
|
||||
|
@ -0,0 +1,81 @@
|
||||
package tech.powerjob.common.serialize;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.experimental.Accessors;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* test json utils
|
||||
*
|
||||
* @author tjq
|
||||
* @since 2024/3/16
|
||||
*/
|
||||
@Slf4j
|
||||
class JsonUtilsTest {
|
||||
|
||||
@Test
|
||||
@SneakyThrows
|
||||
void simpleTest() {
|
||||
Person person = new Person().setName("mubao").setAge(18);
|
||||
String jsonString = JsonUtils.toJSONString(person);
|
||||
log.info("[JsonUtilsTest] person: {}, jsonString: {}", person, jsonString);
|
||||
assert jsonString != null;
|
||||
Person person2 = JsonUtils.parseObject(jsonString, Person.class);
|
||||
assert person2.equals(person);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SneakyThrows
|
||||
void testAdvanceApi() {
|
||||
PersonPlus personPlus = new PersonPlus();
|
||||
personPlus.setName("gongbao").setAge(3);
|
||||
personPlus.setBirthDay(LocalDateTime.now());
|
||||
|
||||
String jsonString = JsonUtils.toJSONString(personPlus);
|
||||
PersonPlus personPlus2 = JsonUtils.parseObject(jsonString, PersonPlus.class);
|
||||
assert personPlus2.equals(personPlus);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SneakyThrows
|
||||
void testMoreOrLessFields() {
|
||||
PersonPlus personPlus = new PersonPlus().setBirthDay(LocalDateTime.now());
|
||||
personPlus.setName("gongbao").setAge(3);
|
||||
|
||||
String originJsonStr = JsonUtils.toJSONString(personPlus);
|
||||
|
||||
Map<String, Object> personPlusMapMore = JsonUtils.parseMap(originJsonStr);
|
||||
personPlusMapMore.put("extraKey", System.currentTimeMillis());
|
||||
|
||||
PersonPlus personPlusByMoreFieldsJsonStr = JsonUtils.parseObject(JsonUtils.toJSONString(personPlusMapMore), PersonPlus.class);
|
||||
assert personPlusByMoreFieldsJsonStr.equals(personPlus);
|
||||
|
||||
Map<String, Object> personPlusMapLess = JsonUtils.parseMap(originJsonStr);
|
||||
personPlusMapLess.remove("birthDay");
|
||||
|
||||
PersonPlus personPlusByLessFieldsJsonStr = JsonUtils.parseObject(JsonUtils.toJSONString(personPlusMapLess), PersonPlus.class);
|
||||
assert personPlusByLessFieldsJsonStr.getName().equals(personPlus.getName());
|
||||
assert personPlusByLessFieldsJsonStr.getAge().equals(personPlus.getAge());
|
||||
}
|
||||
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
static class Person implements Serializable {
|
||||
private String name;
|
||||
private Integer age;
|
||||
}
|
||||
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
static class PersonPlus extends Person {
|
||||
private LocalDateTime birthDay;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user