1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Spigot-1.9.4] ConfigurationSerializableを使ったシリアライズメモ

Last updated at Posted at 2016-10-12

##概要
ConfigurationSectionに対して、以下のような入出力が可能なクラスの作成を行います。

void write(ConfigurationSection config, String tag, Data data) {
  config.set(tag, data);
}
String read(ConfigurationSection config, String tag) throws Exception {
  Object raw = config.get(tag);

  if (raw instanceof Data)
    return ((Data) raw).getText();
  else
    throw new Exception();
}

##クラス作成
シリアライズ可能クラスの作成には、ConfigurationSerializableを利用します。

Data.java
@SerializableAs("Data")
public class Data implements ConfigurationSerializable {

  public static final String TAG_TEXT = "text";

  private String text;

  public Data(String str) {
    this.text = str;
  }

  public Data(Map<String, Object> args) {
    if (args.containsKey(TAG_TEXT))
      this.text = args.get(TAG_TEXT).toString();
  }

  public String getText() {
    return this.text;
  }

  @Override
  public Map<String, Object> serialize() {
    Map<String, Object> result = new LinkedHashMap<String, Object>();

    result.put(TAG_TEXT, this.text);

    return result;
  }

}

※SerializableAsアノテーションは省略可能です。

最後に

ConfigurationSerialization.registerClass(Data.class);

を利用して、作成したクラスをConfigurationSerializationクラスに登録します。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?