您好,登錄后才能下訂單哦!
我們知道 Java 是一個靜態類型的語言。通過輸入?Value
?MessagePack能夠實現動態的特性。
Value?
有方法來檢查自己的類型(isIntegerType()
,?isArrayType()
, 等...),同時也轉換為自己的類型 (asStringValue()
,?convert(Template))。
本代碼可以在?https://github.com/cwiki-us-demo/messagepack-6-demo-java/blob/master/src/test/java/com/insight/demo/msgpack/MessagePack6DynamicTyping.java?中查看。
package?com.insight.demo.msgpack; import?org.junit.Test; import?org.msgpack.MessagePack; import?org.msgpack.type.Value; import?org.msgpack.unpacker.Converter; import?org.slf4j.Logger; import?org.slf4j.LoggerFactory; import?java.util.ArrayList; import?java.util.List; import?static?org.msgpack.template.Templates.TString; import?static?org.msgpack.template.Templates.tList; /** ?*?MessagePack6Objects ?* ?*?@author?yhu ?*/ public?class?MessagePack6DynamicTyping?{ ????final?Logger?logger?=?LoggerFactory.getLogger(MessagePack6DynamicTyping.class); ????/** ?????*?Test?MessagePack6Objects ?????*/ ????@Test ????public?void?MessagePack6DynamicTyping()?{ ????????logger.debug("MessagePack6Objects?for?Objects"); ????????//?Create?serialize?objects. ????????List<String>?src?=?new?ArrayList<String>(); ????????src.add("msgpack"); ????????src.add("kumofs"); ????????src.add("viver"); ????????MessagePack?msgpack?=?new?MessagePack(); ????????? ????????try?{ ????????????//?Serialize ????????????byte[]?raw?=?msgpack.write(src); ????????????//?Deserialize?directly?using?a?template ????????????List<String>?dst1?=?msgpack.read(raw,?tList(TString)); ????????????//?Or,?Deserialze?to?Value?then?convert?type. ????????????Value?dynamic?=?msgpack.read(raw); ????????????List<String>?dst2?=?new?Converter(dynamic).read(tList(TString)); ????????}?catch?(Exception?ex)?{ ????????????logger.error("MessagePack?Serialization?And?Deserialization?error",?ex); ????????} ????} }
https://www.cwiki.us/display/Serialization/QuickStart+For+MessagePack+Java+0.6.X
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。