ConfigurationSerializable and how to use it

Discussion in 'Plugin Development' started by Mrawesomecookie, May 24, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Mrawesomecookie

    Hi, I was wondering how you use ConfigurationSerializable. The javadocs don't have much info about how to use it.
    Like:
    • How I get my deserialized object from the config
    • What goes in the Map<String, Object>
    • Do I need to type cast from Object?
    • Other then the methods required in the interface, is there anything else I need to add.
    • An example class using it.
     
  2. Offline

    NathanWolf

    The class comments actually do a pretty great job of saying what is required:

    http://jd.bukkit.org/rb/apidocs/org.../serialization/ConfigurationSerializable.html

    Code:
    These objects MUST implement one of the following, in addition to the methods as defined by this interface:
     
    A static method "deserialize" that accepts a single Map<String, Object> and returns the class.
    A static method "valueOf" that accepts a single Map<String, Object> and returns the class.
    A constructor that accepts a single Map<String, Object>.
    In addition to implementing this interface, you must register the class with ConfigurationSerialization.registerClass(Class).
    So, there are 2 things you must do beyond implementing the interface. You must implement a deserialization method- either a constructor, or a valueOf or deserialize method. And, you must register your class with ConfigurationSerialization.

    Here's a simple example class:

    https://raw.githubusercontent.com/N...raftbukkit/inventory/SerializeableObject.java

    I register the class in a static block, and implement the "Map<String, Object>" constructor.

    As for "what to put in the Map"- anything you need saved. You'll get the same map back in your constructor/deserialize method, so anything you want to be able to read in, you have to write out.

    Once you've got all this set up, you can read and write your object to and from a YamlConfiguration just like any other serializable object (e.g. ItemStack)
     
Thread Status:
Not open for further replies.

Share This Page