Created
November 9, 2010 08:56
-
-
Save frsyuki/668878 to your computer and use it in GitHub Desktop.
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
// 既に定義されているクラス | |
// ライブラリから提供されていて変更できない | |
// or 他のモジュールのクラスなので変更したくない | |
public static class ProvidedClass { | |
public String name; | |
public String nickname; | |
public int age = -1; | |
} | |
// ↑これをシリアライズしたいとき... | |
public static class Main { | |
public static void main(String args[]) { | |
ProvidedClass c = new ProvidedClass(); | |
c.name = "me"; | |
c.nickname = null; | |
// 現行だとこう書く | |
{ | |
List<FieldOption> list = new ArrayList(); | |
list.add(new FieldOption("name", TString)); // ProvidedClassのメンバの型の詳細と、 | |
list.add(new FieldOption("nickname", tNullable(TString))); // Templateの知識が必要 | |
list.add(new FieldOption("age", tOptional(TInteger))); | |
MessagePack.register(ProvidedClass.class, | |
DynamicTemplate.create(ProvidedClass.class, list)); | |
} | |
// こう書けたら嬉しい | |
{ | |
FieldList list = new FieldList(); | |
list.add("name", FieldOption.REQUIRED); | |
list.add("nickname", FieldOption.NULLABLE); | |
list.add("age", FieldOption.OPTIONAL); | |
MessagePack.register(ProvidedClass.class, list); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment