| | |
| | | import java.time.LocalDateTime; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * JSON 工具类 |
| | |
| | | |
| | | private static ObjectMapper buildObjectMapper() { |
| | | SimpleModule simpleModule = new SimpleModule() |
| | | // 解决 LocalDateTime 的序列化 |
| | | .addSerializer(LocalDateTime.class, TimestampLocalDateTimeSerializer.INSTANCE) |
| | | .addDeserializer(LocalDateTime.class, TimestampLocalDateTimeDeserializer.INSTANCE); |
| | | return JsonMapper.builder() |
| | | .disable(SerializationFeature.FAIL_ON_EMPTY_BEANS) |
| | | .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) |
| | | // 强制将整数反序列化为 Long,解决 Redis 缓存反序列化时 Integer 转 Long 的问题 |
| | | .enable(DeserializationFeature.USE_LONG_FOR_INTS) |
| | | .changeDefaultPropertyInclusion(value -> JsonInclude.Value.construct(JsonInclude.Include.NON_NULL, JsonInclude.Include.NON_NULL)) |
| | | .addModule(simpleModule) |
| | | .build(); |
| | |
| | | } |
| | | |
| | | /** |
| | | * 解析 JSON 字符串成 Map,空字符串或解析失败返回 null |
| | | * |
| | | * @param text JSON 字符串 |
| | | * @return Map 对象 |
| | | */ |
| | | public static Map<String, Object> parseMap(String text) { |
| | | if (StrUtil.isEmpty(text)) { |
| | | return null; |
| | | } |
| | | try { |
| | | return objectMapper.readValue(text, new TypeReference<Map<String, Object>>() {}); |
| | | } catch (JacksonException e) { |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 解析 JSON 字符串成指定类型的对象,如果解析失败,则返回 null |
| | | * |
| | | * @param text 字符串 |
| | |
| | | } |
| | | } |
| | | |
| | | public static String getText(JsonNode node, String fieldName) { |
| | | if (node == null) { |
| | | return null; |
| | | } |
| | | JsonNode value = node.get(fieldName); |
| | | return value != null && !value.isNull() ? value.asText() : null; |
| | | } |
| | | |
| | | public static boolean isJson(String text) { |
| | | return JSONUtil.isTypeJSON(text); |
| | | } |