Springboot2以代码的方式统一配置Jackson教程
程序员文章站
2022-06-22 13:30:11
目录说明7个序列化和反序列化规则jackson统一配置类jackson工具类总结说明本文主要配置jackson对long、date、localdate、localdatetime的序列化和反序列化。由...
说明
本文主要配置jackson对long、date、localdate、localdatetime的序列化和反序列化。
由于js中整数可精确表示范围小于java long的范围,故将long序列化为string,反序列化jackson会自动处理string转long,故不需要显式配置。
由于时间戳在各语言间的兼容性良好,故将date、localdate、localdatetime序列化为long(目前时间戳类的long类型在js的可精确表示范围内),将long反序列化为date、localdate、localdatetime。将string空字符串反序列化为null。
本文除了配置springboot整合的jackson,还配置了功能一致的jackson工具类,方便在代码中显示使用jackson的序列化和反序列化功能。
7个序列化和反序列化规则
package com.xxx.config.serializers; import java.io.ioexception; import java.util.date; import com.fasterxml.jackson.core.jsongenerator; import com.fasterxml.jackson.databind.jsonserializer; import com.fasterxml.jackson.databind.serializerprovider; public class dateserializer extends jsonserializer<date> { @override public void serialize(date value, jsongenerator gen, serializerprovider serializers) throws ioexception { if (value != null) { gen.writenumber(value.gettime()); } } }
package com.xxx.config.serializers; import java.io.ioexception; import java.time.localdate; import java.time.zoneid; import com.fasterxml.jackson.core.jsongenerator; import com.fasterxml.jackson.databind.jsonserializer; import com.fasterxml.jackson.databind.serializerprovider; public class localdateserializer extends jsonserializer<localdate> { @override public void serialize(localdate value, jsongenerator gen, serializerprovider serializers) throws ioexception { if (value != null) { long millis = value.atstartofday(zoneid.of("+8")).toinstant().toepochmilli(); gen.writenumber(millis); } } }
package com.xxx.config.serializers; import java.io.ioexception; import java.time.localdatetime; import java.time.zoneid; import com.fasterxml.jackson.core.jsongenerator; import com.fasterxml.jackson.databind.jsonserializer; import com.fasterxml.jackson.databind.serializerprovider; public class localdatetimeserializer extends jsonserializer<localdatetime> { @override public void serialize(localdatetime value, jsongenerator gen, serializerprovider serializers) throws ioexception { if (value != null) { long millis = value.atzone(zoneid.of("+8")).toinstant().toepochmilli(); gen.writenumber(millis); } } }
package com.xxx.config.serializers; import java.io.ioexception; import com.fasterxml.jackson.core.jsonparser; import com.fasterxml.jackson.core.jsonprocessingexception; import com.fasterxml.jackson.databind.deserializationcontext; import com.fasterxml.jackson.databind.jsondeserializer; public class stringdeserializer extends jsondeserializer<string> { @override public string deserialize(jsonparser p, deserializationcontext ctxt) throws ioexception, jsonprocessingexception { string text = p.gettext(); if ("".equals(text)) { return null; } return text; } }
package com.xxx.config.serializers; import java.io.ioexception; import java.util.date; import com.fasterxml.jackson.core.jsonparser; import com.fasterxml.jackson.core.jsonprocessingexception; import com.fasterxml.jackson.databind.deserializationcontext; import com.fasterxml.jackson.databind.jsondeserializer; public class datedeserializer extends jsondeserializer<date> { @override public date deserialize(jsonparser p, deserializationcontext ctxt) throws ioexception, jsonprocessingexception { long timestamp = p.getvalueaslong(); if (timestamp > 0){ return new date(timestamp); }else{ return null; } } }
package com.xxx.config.serializers; import java.io.ioexception; import java.time.instant; import java.time.localdate; import java.time.localdatetime; import java.time.zoneid; import com.fasterxml.jackson.core.jsonparser; import com.fasterxml.jackson.core.jsonprocessingexception; import com.fasterxml.jackson.databind.deserializationcontext; import com.fasterxml.jackson.databind.jsondeserializer; public class localdatedeserializer extends jsondeserializer<localdate> { @override public localdate deserialize(jsonparser p, deserializationcontext ctxt) throws ioexception, jsonprocessingexception { long timestamp = p.getvalueaslong(); if (timestamp > 0){ return localdatetime.ofinstant(instant.ofepochmilli(timestamp),zoneid.of("+8")).tolocaldate(); }else{ return null; } } }
package com.xxx.config.serializers; import java.io.ioexception; import java.time.instant; import java.time.localdatetime; import java.time.zoneid; import com.fasterxml.jackson.core.jsonparser; import com.fasterxml.jackson.core.jsonprocessingexception; import com.fasterxml.jackson.databind.deserializationcontext; import com.fasterxml.jackson.databind.jsondeserializer; public class localdatetimedeserializer extends jsondeserializer<localdatetime> { @override public localdatetime deserialize(jsonparser p, deserializationcontext ctxt) throws ioexception, jsonprocessingexception { long timestamp = p.getvalueaslong(); if (timestamp > 0){ return localdatetime.ofinstant(instant.ofepochmilli(timestamp),zoneid.of("+8")); }else{ return null; } } }
jackson统一配置类
package com.xxx.config; import java.time.localdate; import java.time.localdatetime; import java.util.date; import org.springframework.boot.autoconfigure.jackson.jackson2objectmapperbuildercustomizer; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.configuration; import org.springframework.http.converter.json.jackson2objectmapperbuilder; import com.xxx.config.serializers.datedeserializer; import com.xxx.config.serializers.dateserializer; import com.xxx.config.serializers.localdatedeserializer; import com.xxx.config.serializers.localdateserializer; import com.xxx.config.serializers.localdatetimedeserializer; import com.xxx.config.serializers.localdatetimeserializer; import com.xxx.config.serializers.stringdeserializer; import com.fasterxml.jackson.databind.ser.std.tostringserializer; @configuration public class jacksonconfig { @bean public jackson2objectmapperbuildercustomizer jackson2objectmapperbuildercustomizer() { jackson2objectmapperbuildercustomizer customizer = new jackson2objectmapperbuildercustomizer() { @override public void customize(jackson2objectmapperbuilder jacksonobjectmapperbuilder) { jacksonobjectmapperbuilder .serializerbytype(long.type, tostringserializer.instance) .serializerbytype(long.class, tostringserializer.instance) .serializerbytype(date.class, new dateserializer()) .serializerbytype(localdate.class, new localdateserializer()) .serializerbytype(localdatetime.class, new localdatetimeserializer()) .deserializerbytype(string.class, new stringdeserializer()) .deserializerbytype(date.class, new datedeserializer()) .deserializerbytype(localdate.class, new localdatedeserializer()) .deserializerbytype(localdatetime.class, new localdatetimedeserializer()) .failonemptybeans(false) .failonunknownproperties(false); } }; return customizer; } }
jackson工具类
package com.xxx.util; import java.io.ioexception; import java.time.localdate; import java.time.localdatetime; import java.util.date; import java.util.timezone; import com.xxx.config.serializers.stringdeserializer; import com.xxx.config.serializers.datedeserializer; import com.xxx.config.serializers.dateserializer; import com.xxx.config.serializers.localdatedeserializer; import com.xxx.config.serializers.localdateserializer; import com.xxx.config.serializers.localdatetimedeserializer; import com.xxx.config.serializers.localdatetimeserializer; import com.fasterxml.jackson.core.jsonparser.feature; import com.fasterxml.jackson.core.type.typereference; import com.fasterxml.jackson.databind.deserializationfeature; import com.fasterxml.jackson.databind.jsonnode; import com.fasterxml.jackson.databind.objectmapper; import com.fasterxml.jackson.databind.serializationfeature; import com.fasterxml.jackson.databind.ser.std.tostringserializer; import com.fasterxml.jackson.datatype.jsr310.javatimemodule; public class jsonutils { private static final objectmapper objectmapper; static { objectmapper = new objectmapper(); // 去掉默认的时间戳格式 objectmapper.configure(serializationfeature.write_dates_as_timestamps, false); // 设置为中国北京时区 objectmapper.settimezone(timezone.gettimezone("gmt+8")); // 空值不序列化 // objectmapper.setserializationinclusion(include.non_null);//已过时 // objectmapper.setdefaultpropertyinclusion(include.non_null);//新api // 反序列化时,属性不存在的兼容处理 objectmapper.getdeserializationconfig().withoutfeatures(deserializationfeature.fail_on_unknown_properties); objectmapper.configure(serializationfeature.fail_on_empty_beans, false); objectmapper.configure(deserializationfeature.fail_on_unknown_properties, false); // 单引号处理 objectmapper.configure(feature.allow_single_quotes, true); javatimemodule javatimemodule = new javatimemodule(); javatimemodule.addserializer(long.type, tostringserializer.instance); javatimemodule.addserializer(long.class, tostringserializer.instance); javatimemodule.addserializer(date.class, new dateserializer()); javatimemodule.addserializer(localdate.class, new localdateserializer()); javatimemodule.addserializer(localdatetime.class, new localdatetimeserializer()); javatimemodule.adddeserializer(string.class, new stringdeserializer()); javatimemodule.adddeserializer(date.class, new datedeserializer()); javatimemodule.adddeserializer(localdate.class, new localdatedeserializer()); javatimemodule.adddeserializer(localdatetime.class, new localdatetimedeserializer()); objectmapper.registermodule(javatimemodule); } /** * json 转换成 object * * @param json * @param clazz * @return */ public static <t> t json2object(string json, class<t> clazz) { try { return objectmapper.readvalue(json, clazz); } catch (ioexception e) { throw new runtimeexception("解析json错误"); } } @suppresswarnings("unchecked") public static <t> t json2object(string json, typereference<t> tr) { try { return (t)objectmapper.readvalue(json, tr); } catch (ioexception e) { throw new runtimeexception("解析json错误", e); } } /** * obj 转换成json * * @param entity * @return */ public static <t> string object2json(t entity) { try { return objectmapper.writevalueasstring(entity); } catch (ioexception e) { throw new runtimeexception("转换json错误"); } } /** * obj对象 转换成树型json * * @param obj * @return */ public static jsonnode object2treejson(object obj) { try { return objectmapper.valuetotree(obj); } catch (exception e) { throw new runtimeexception("转换json错误"); } } /** * 解码json串成对象 * * @param <t> * @param json * @param valuetype * @return t */ @suppresswarnings("unchecked") public static <t> t decode(string json, class<?> valuetype) { try { return (t) objectmapper.readvalue(json, valuetype); } catch (exception e) { throw new runtimeexception(json, e); } } /** * 解码json串成jsonnode * @param json * @return */ public static jsonnode readtree(string json) { try { return objectmapper.readtree(json); } catch (exception e) { throw new runtimeexception(json, e); } } }
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。
上一篇: chat.asp聊天程序的编写方法
下一篇: 不是说孩子