package com.tcm.training.admin.common;
|
|
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.SerializationFeature;
|
import com.fasterxml.jackson.databind.json.JsonMapper;
|
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
|
import java.util.Collections;
|
import java.util.List;
|
|
public class Jsons {
|
private static final ObjectMapper MAPPER = JsonMapper.builder()
|
.addModule(new JavaTimeModule())
|
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
|
.build();
|
|
private Jsons() {
|
}
|
|
public static List<String> parseStringList(String json) {
|
if (json == null || json.trim().isEmpty()) {
|
return Collections.emptyList();
|
}
|
try {
|
return MAPPER.readValue(json, new TypeReference<List<String>>() {
|
});
|
} catch (Exception e) {
|
return Collections.emptyList();
|
}
|
}
|
|
/**
|
* 将对象序列化为 JSON 字符串,序列化失败时返回空对象字符串。
|
*/
|
public static String toJsonString(Object value) {
|
try {
|
return MAPPER.writeValueAsString(value);
|
} catch (Exception e) {
|
return "{}";
|
}
|
}
|
}
|