您好,登錄后才能下訂單哦!
這篇文章主要介紹“jackson怎么通用反序列化組件”,在日常操作中,相信很多人在jackson怎么通用反序列化組件問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”jackson怎么通用反序列化組件”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
Java生態圈中有很多處理JSON和XML格式化的類庫,Jackson是其中比較著名的一個</br> 官址:https://github.com/FasterXML<br> Jackson提供了Jackson-databind來操控JSON處理,提供了比較豐富的API,和入門指南<br> 文檔:https://github.com/FasterXML/jackson-databind <br> JAVA是一種靈活性,擴展性比較強的語言,繼承,實現,范型,多態等都是很好的體現,Jackson-databind作為JAVA生態圈的類庫,當然會對這些特性有對應的實現,具體可以在官方指導文檔中,或者是在指導測試中找到相關的介紹。
你會發現官方給出的基本上都是基于單個Bean的,或者是某一類型的定制方案。官方暫未提供通用的解決方案,
對于有些基于JSON數據傳輸的系統,通過JACKSON進行BEAN的反序列化時,如果沒有定制Deserializer行為,Jackson默認的BeanDeserializer
,此時需要明確此Bean類結構,Bean中的屬性類型,枚舉以及嵌套類(內部類)中必須顯式包含'無參構造器'(NON-DEFAULT CONSTRUCTOR)
,否則反序列化會失敗,具體異常描述"No suitable constructor found for type [XXXX]"
針對上述的2個問題,jackson-ext-beanInstantiation
提供解決方案,并且提供基于jackson-databind simple module
的集成方案 具體代碼參考:https://github.com/dylan1108/jackson-ext-beanInstantiation
Bean序列化時,對于含有多態類型的引用,將此引用的具體實例對象類的信息作為輔助信息,以Key-Value的方式保存到JNode中 Bean反序列化時,會根據JNode中的引用類型的類的輔助信息,來決定使用具體的類來進行反序列化綁定。
JVM unsafe mechanism
機制無需依賴構造器的方式實例化對象,然后在對Bean對象進行屬性設置
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會提供想對應的序列化和反序列化接口即可
sun.misc.Unsafe
的JDK版本的支持
到此,關于“jackson怎么通用反序列化組件”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。