From 808cd1e9dafa631b29ad2a80e2c62a25ca4cf608 Mon Sep 17 00:00:00 2001 From: YuCheng Hu Date: Wed, 7 Aug 2019 00:40:32 -0400 Subject: [PATCH] =?UTF-8?q?=E5=8A=A8=E6=80=81=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../msgpack/MessagePack6DynamicTyping.java | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/test/java/com/insight/demo/msgpack/MessagePack6DynamicTyping.java diff --git a/src/test/java/com/insight/demo/msgpack/MessagePack6DynamicTyping.java b/src/test/java/com/insight/demo/msgpack/MessagePack6DynamicTyping.java new file mode 100644 index 0000000..c2a9d4f --- /dev/null +++ b/src/test/java/com/insight/demo/msgpack/MessagePack6DynamicTyping.java @@ -0,0 +1,56 @@ +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 src = new ArrayList(); + 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 dst1 = msgpack.read(raw, tList(TString)); + + // Or, Deserialze to Value then convert type. + Value dynamic = msgpack.read(raw); + List dst2 = new Converter(dynamic).read(tList(TString)); + + } catch (Exception ex) { + logger.error("MessagePack Serialization And Deserialization error", ex); + } + } +}