Created
March 2, 2011 06:36
-
-
Save kimukou/850562 to your computer and use it in GitHub Desktop.
msgpacktest2.groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
//refarence http://d.hatena.ne.jp/viver/20101025/p1 | |
// | |
@GrabResolver(name="msgpack-repo", root="http://msgpack.org/maven2/") | |
@Grab("org.msgpack:msgpack:0.5.1-devel") | |
import java.util.ArrayList | |
import java.util.List | |
import org.msgpack.MessagePack | |
import static org.msgpack.Templates.* | |
// シリアライズするオブジェクトを作成 | |
def src = new ArrayList<String>() | |
src.add("msgpack") | |
src.add("kumofs") | |
src.add("viver") | |
// バイト列にシリアライズ | |
byte[] raw = MessagePack.pack(src) | |
println "raw=$raw" | |
// 方法1: テンプレートを指定して直接デシリアライズ | |
//List<String> dst1 = (List<String>)MessagePack.unpack(raw, tList(TString)) | |
def dst1 = MessagePack.unpack(raw, tList(TString)) | |
println "dst1=${dst1.dump()}" | |
// 方法2: いったん動的型付けオブジェクトにデシリアライズしてから型変換 | |
def dynamic = MessagePack.unpack(raw) | |
//List<String> dst2 = (List<String>)dynamic.convert(tList(TString)) | |
def dst2 = dynamic.convert(tList(TString)) | |
println "dst2=${dst2.dump()}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment