Class ConfigSerializer

java.lang.Object
studio.magemonkey.codex.mccore.config.ConfigSerializer

public class ConfigSerializer extends Object

Utility class for serializing objects with control via annotations

The two annotations used by this serializer are SerializableField and ExcludeField

To exclude a field from serialization, annotate it with ExcludeField

To make a field be serialized further, annotate it with SerializableField

You can define flags and flag groups to change when fields can be serialized


IMPORTANT: You should exclude any fields that have two-way pointers (I.E. a child element that has its parent as a field when its parent has the child as a field). If you don't, you will likely get a StackOverflowException when serializing! If you need the references, exclude only the child so that when you serialize the parent, it will still serialize the child but not cause the problem.

Also, primitive arrays such as int[] give the config problems. Where possible, use array lists instead for better compatibility.

  • Field Details

  • Constructor Details

    • ConfigSerializer

      public ConfigSerializer()
  • Method Details

    • defineFlag

      public static void defineFlag(String name)
      Defines a new flag for field exclusion
      Parameters:
      name - flag name
    • defineFlagGroup

      public static void defineFlagGroup(String name, String... members)
      Defines a new flag group for field exclusion
      Parameters:
      name - flag name
      members - names of flags to include in the group
    • serialize

      public static void serialize(Object obj, org.bukkit.configuration.ConfigurationSection config)
      Serializes an object excluding fields with any exclude annotations
      Parameters:
      obj - object to serialize
      config - config to serialize to
    • serialize

      public static void serialize(Object obj, org.bukkit.configuration.ConfigurationSection config, String flag)

      Serializes an object excluding fields that match the provided exclude flag

      Matching a flag requires the two flags to share any flag. This means that two flag groups will match if at least one of their children match and two regular flags will match only when they are the same.

      Parameters:
      obj - object to serialize
      config - config to serialize into
      flag - flag name