您好,登錄后才能下訂單哦!
這篇文章主要介紹“jackson怎么通用反序列化組件”,在日常操作中,相信很多人在jackson怎么通用反序列化組件問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”jackson怎么通用反序列化組件”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!
Java生態(tài)圈中有很多處理JSON和XML格式化的類庫,Jackson是其中比較著名的一個(gè)</br> 官址:https://github.com/FasterXML<br> Jackson提供了Jackson-databind來操控JSON處理,提供了比較豐富的API,和入門指南<br> 文檔:https://github.com/FasterXML/jackson-databind <br> JAVA是一種靈活性,擴(kuò)展性比較強(qiáng)的語言,繼承,實(shí)現(xiàn),范型,多態(tài)等都是很好的體現(xiàn),Jackson-databind作為JAVA生態(tài)圈的類庫,當(dāng)然會(huì)對(duì)這些特性有對(duì)應(yīng)的實(shí)現(xiàn),具體可以在官方指導(dǎo)文檔中,或者是在指導(dǎo)測(cè)試中找到相關(guān)的介紹。
你會(huì)發(fā)現(xiàn)官方給出的基本上都是基于單個(gè)Bean的,或者是某一類型的定制方案。官方暫未提供通用的解決方案,
對(duì)于有些基于JSON數(shù)據(jù)傳輸?shù)南到y(tǒng),通過JACKSON進(jìn)行BEAN的反序列化時(shí),如果沒有定制Deserializer行為,Jackson默認(rèn)的BeanDeserializer
,此時(shí)需要明確此Bean類結(jié)構(gòu),Bean中的屬性類型,枚舉以及嵌套類(內(nèi)部類)中必須顯式包含'無參構(gòu)造器'(NON-DEFAULT CONSTRUCTOR)
,否則反序列化會(huì)失敗,具體異常描述"No suitable constructor found for type [XXXX]"
針對(duì)上述的2個(gè)問題,jackson-ext-beanInstantiation
提供解決方案,并且提供基于jackson-databind simple module
的集成方案 具體代碼參考:https://github.com/dylan1108/jackson-ext-beanInstantiation
Bean序列化時(shí),對(duì)于含有多態(tài)類型的引用,將此引用的具體實(shí)例對(duì)象類的信息作為輔助信息,以Key-Value的方式保存到JNode中 Bean反序列化時(shí),會(huì)根據(jù)JNode中的引用類型的類的輔助信息,來決定使用具體的類來進(jìn)行反序列化綁定。
JVM unsafe mechanism
機(jī)制無需依賴構(gòu)造器的方式實(shí)例化對(duì)象,然后在對(duì)Bean對(duì)象進(jìn)行屬性設(shè)置
public class UnsafeAllocator { private static Unsafe unsafe; static{ try { Class<?> unsafeClass = Class.forName("sun.misc.Unsafe"); Field f = unsafeClass.getDeclaredField("theUnsafe"); f.setAccessible(true); unsafe = (Unsafe) f.get(null); } catch (Exception ignored) { ignored.printStackTrace(); } } public static Object beanAllocatorByJvmUnsafe(Class<?> clazz) throws InstantiationException { if(unsafe==null){ throw new InstantiationException(String.format("Jvm Unsafe could't find,Make sure load unsafe security in [%s]",clazz.getName())); } return unsafe.allocateInstance(clazz); } }
@Override public Object deserializeFromObjectUsingNonDefault(JsonParser p,DeserializationContext ctxt) throws IOException{ JsonDeserializer<Object> delegateDeser = _delegateDeserializer; if (delegateDeser == null) { delegateDeser = _arrayDelegateDeserializer; } if (delegateDeser != null) { return _valueInstantiator.createUsingDelegate(ctxt, delegateDeser.deserialize(p, ctxt)); } if (_propertyBasedCreator != null) { return _deserializeUsingPropertyBased(p, ctxt); } /* // 25-Jan-2017, tatu: We do not actually support use of Creators for non-static // inner classes -- with one and only one exception; that of default constructor! // -- so let's indicate it //=== Unsafe Allocator support Bean instantiate with Non default creator,Include the non-static inner class ===== Class<?> raw = _beanType.getRawClass(); if (ClassUtil.isNonStaticInnerClass(raw)) { return ctxt.handleMissingInstantiator(raw, null, p, "can only instantiate non-static inner class by using default, no-argument constructor"); }*/ return _deserializeNonDefaultWithUnsafeAllocator(p,ctxt); } private void fillBeanFieldValue(JsonParser p, DeserializationContext ctxt, Object bean) throws IOException { p.setCurrentValue(bean); if (p.hasTokenId(JsonTokenId.ID_FIELD_NAME)) { String propName = p.getCurrentName(); do { p.nextToken(); SettableBeanProperty prop = _beanProperties.find(propName); if (prop != null) { // normal case try { prop.deserializeAndSet(p, ctxt, bean); } catch (Exception e) { wrapAndThrow(e, bean, propName, ctxt); } continue; } handleUnknownVanilla(p, ctxt, bean, propName); } while ((propName = p.nextFieldName()) != null); } }
public class MyBeanDeserializerModifier extends BeanDeserializerModifier { @Override public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config, BeanDescription beanDesc, JsonDeserializer<?> deserializer) { //replace default BeanDeserializer to support instantiate bean with Non-default creator. if (deserializer instanceof BeanDeserializer) { return new MyBeanDeserializer((BeanDeserializer) deserializer); } return deserializer; } }
{ "@class": "com.jackson.ext.test.vo.MockBizDataContext", "bizDataMap": { "@class": "java.util.HashMap", "{\"@class\":\"java.lang.String\",\"value\":\"MockPojoWithNonDefaultConstructorList\"}": ["java.util.ArrayList", [{ "@class": "com.jackson.ext.test.vo.MockPojoWithNonDefaultConstructor", "poiId": 123, "comment": "test1", "userName": "yilami1", "phone": "11111111" }, { "@class": "com.jackson.ext.test.vo.MockPojoWithNonDefaultConstructor", "poiId": 234, "comment": "test2", "userName": "yilami2", "phone": "22222222" }]], "{\"@class\":\"java.lang.String\",\"value\":\"MockPojoWithNonStaticInnerPojo_Outer\"}": { "@class": "com.jackson.ext.test.vo.MockPojoWithNonStaticInnerPojo_Outer", "outerName": "Outer[1]", "mockPojoWithNonStaticInnerPojo_inner": { "@class": "com.jackson.ext.test.vo.MockPojoWithNonStaticInnerPojo_Outer$MockPojoWithNonStaticInnerPojo_Inner", "innerName": "Inner[1]" } }, "{\"@class\":\"java.lang.String\",\"value\":\"createTime\"}": ["java.util.Date", 1576838321729], "{\"@class\":\"java.lang.String\",\"value\":\"MockPojoWithNonDefaultConstructor1\"}": { "@class": "com.jackson.ext.test.vo.MockPojoWithNonDefaultConstructor", "poiId": 123, "comment": "test1", "userName": "yilami1", "phone": "11111111" }, "{\"@class\":\"java.lang.String\",\"value\":\"MockPojoWithNonDefaultConstructor2\"}": { "@class": "com.jackson.ext.test.vo.MockPojoWithNonDefaultConstructor", "poiId": 234, "comment": "test2", "userName": "yilami2", "phone": "22222222" }, "{\"@class\":\"java.lang.String\",\"value\":\"MockEnumWithNonDefaultCreator\"}": ["com.jackson.ext.test.Enum.MockEnumWithNonDefaultCreator", "NO"], "{\"@class\":\"java.lang.String\",\"value\":\"MockPojoWithNonStaticInnerPojo_Inner\"}": { "@class": "java.lang.Object", "innerName": "Inner[1]" } } }
<properties> ... <!-- Use the latest version whenever possible. --> <jackson.version>2.10.0</jackson.version> ... </properties> <dependencies> ... <dependency> <groupId>com.jackson.ext</groupId> <artifactId>jackson-ext-beanInstantiation</artifactId> <version>${jackson.version}</version> </dependency> ... </dependencies>
使用Maven編譯和打包jackson-ext-beanInstantiation之后,使用Jackson_Enhance_Utils會(huì)提供想對(duì)應(yīng)的序列化和反序列化接口即可
sun.misc.Unsafe
的JDK版本的支持
到此,關(guān)于“jackson怎么通用反序列化組件”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。