night-config icon indicating copy to clipboard operation
night-config copied to clipboard

Support for arbitrary HashMap de/serialization

Open Binero opened this issue 1 year ago • 2 comments

Sometimes which exact keys will exist in a config file are up to the user. I am not sure whether I am doing something wrong, or whether this is not implemented, but it doesn't seem possible to do in night-config.

public class MyConfig
    @Path("commands")
    @PreserveNotNull
    public Map<String, CustomCommandConfigEntry> commands = new HashMap<>();
}

This will throw the following exception:

Caused by: com.electronwill.nightconfig.core.io.WritingException: Unsupported value type: class java.util.HashMap

Binero avatar Sep 05 '22 19:09 Binero

Subconfigs are supported, they must be Config not HashMap:

public class MyConfig {
  public Config commands;
}

TheElectronWill avatar Sep 06 '22 05:09 TheElectronWill

If you or someone else wonder why, let me explain :)

A configuration is a hierarchical data structure. You can access its different levels from anywhere with config.get("parent.child.grandchild"). To walk through the hierarchy, it's simpler and more efficient to enforce a single type of parent node. On top of that, it allows to store additional information such as comments.

TheElectronWill avatar Sep 06 '22 06:09 TheElectronWill