Skip to content

Instantly share code, notes, and snippets.

@frsyuki
Created November 9, 2010 08:56
Show Gist options
  • Save frsyuki/668878 to your computer and use it in GitHub Desktop.
Save frsyuki/668878 to your computer and use it in GitHub Desktop.
// 既に定義されているクラス
// ライブラリから提供されていて変更できない
// 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