处理fastJson 序列化时间问题
2021/4/26 18:28:16
本文主要是介绍处理fastJson 序列化时间问题,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
解决方案:使用 jackson
1.创建utils类
package com.mybatis.plus.utils; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import lombok.NonNull; import lombok.extern.slf4j.Slf4j; @Slf4j public class JsonUtils { private static ObjectMapper mapper = new ObjectMapper(); static { // 对于空的对象转json的时候不抛出错误 mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); // 允许属性名称没有引号 mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true); // 允许单引号 mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true); // 设置输入时忽略在json字符串中存在但在java对象实际没有的属性 mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); // 设置输出时包含属性的风格 mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); } /** * 序列化,将对象转化为json字符串 * * @param data * @return */ public static String toJsonString(Object data) { if (data == null) { return null; } String json = null; try { json = mapper.writeValueAsString(data); } catch (JsonProcessingException e) { log.error("[{}] toJsonString error:{{}}", data.getClass().getSimpleName(), e); } return json; } /** * 反序列化,将json字符串转化为对象 * * @param json * @param clazz * @param <T> * @return */ public static <T> T parse(@NonNull String json, Class<T> clazz) { T t = null; try { t = mapper.readValue(json, clazz); } catch (Exception e) { log.error(" parse json [{}] to class [{}] error:{{}}", json, clazz.getSimpleName(), e); } return t; } }
调用效果
这篇关于处理fastJson 序列化时间问题的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-09-28基于Python+Vue开发的口腔牙科预约管理系统
- 2024-09-28基于Python+Vue开发的酒店客房预订管理系统
- 2024-09-27使用js将ETH账户的资产打散其他账户web3
- 2024-09-27我轻松地将我的 React.js 应用程序翻译成了多种语言。下面是我是如何做到的... ??
- 2024-09-27?? 使用 useMemo 和 useCallback 加速 React:告别缓慢的重新渲染!??
- 2024-09-27Vue CLI多环境配置教程:新手入门指南
- 2024-09-27Vue CLI多环境配置教程:快速入门指南
- 2024-09-27Vue CLI教程:新手入门指南
- 2024-09-27Vue CLI教程:初学者快速入门指南
- 2024-09-27Vue3公共组件教程:入门与实践