Interface ConfigNode

All Known Implementing Classes:
JacksonConfigNode

public interface ConfigNode
A node in a configuration tree that provides dynamic access to values.

ConfigNode allows traversing and reading configuration data without defining a specific class structure. It supports nested objects, arrays, and all common primitive types.

Example Usage


 ConfigNode config = loader.load(new File("config.json"));

 // Get simple values
 String name = config.getString("serverName");
 int port = config.getInt("port", 8080);  // with default

 // Navigate nested objects
 ConfigNode database = config.getSection("database");
 String host = database.getString("host");

 // Or use dot notation
 String host = config.getString("database.host");

 // Work with lists
 List<String> mods = config.getStringList("enabledMods");

 // Check existence
 if (config.has("optionalFeature")) {
     // ...
 }
 
See Also:
  • Method Details

    • isObject

      boolean isObject()
      Checks if this node represents a JSON object (key-value pairs).
      Returns:
      true if this node is an object
    • isArray

      boolean isArray()
      Checks if this node represents a JSON array.
      Returns:
      true if this node is an array
    • isValue

      boolean isValue()
      Checks if this node represents a primitive value (string, number, boolean).
      Returns:
      true if this node is a value
    • isNull

      boolean isNull()
      Checks if this node is null or missing.
      Returns:
      true if this node is null
    • has

      boolean has(String key)
      Checks if a key exists in this object node.

      Supports dot notation for nested paths: "database.host"

      Parameters:
      key - the key or path to check
      Returns:
      true if the key exists
    • keys

      Set<String> keys()
      Returns all keys in this object node.
      Returns:
      set of keys, or empty set if not an object
    • size

      int size()
      Returns the number of elements in this node.

      For objects, returns the number of keys. For arrays, returns the number of elements. For values, returns 1.

      Returns:
      the size of this node
    • get

      ConfigNode get(String key)
      Gets a child node by key.

      Supports dot notation for nested paths: "database.host"

      Parameters:
      key - the key or path
      Returns:
      the child node, or a null node if not found
    • get

      ConfigNode get(int index)
      Gets a child node by array index.
      Parameters:
      index - the array index
      Returns:
      the child node, or a null node if out of bounds
    • getSection

      default ConfigNode getSection(String key)
      Gets a nested object section by key.

      Equivalent to get(String) but more readable for object access.

      Parameters:
      key - the key or path
      Returns:
      the section node, or a null node if not found
    • getString

      String getString(String key)
      Gets a string value by key.
      Parameters:
      key - the key or path
      Returns:
      the string value
      Throws:
      ConfigException - if the key doesn't exist or isn't a string
    • getString

      String getString(String key, String defaultValue)
      Gets a string value by key, with a default.
      Parameters:
      key - the key or path
      defaultValue - the default value if key doesn't exist
      Returns:
      the string value or default
    • getOptionalString

      Optional<String> getOptionalString(String key)
      Gets an optional string value by key.
      Parameters:
      key - the key or path
      Returns:
      an Optional containing the value, or empty if not found
    • getInt

      int getInt(String key)
      Gets an integer value by key.
      Parameters:
      key - the key or path
      Returns:
      the integer value
      Throws:
      ConfigException - if the key doesn't exist or isn't a number
    • getInt

      int getInt(String key, int defaultValue)
      Gets an integer value by key, with a default.
      Parameters:
      key - the key or path
      defaultValue - the default value if key doesn't exist
      Returns:
      the integer value or default
    • getOptionalInt

      Optional<Integer> getOptionalInt(String key)
      Gets an optional integer value by key.
      Parameters:
      key - the key or path
      Returns:
      an Optional containing the value, or empty if not found
    • getLong

      long getLong(String key)
      Gets a long value by key.
      Parameters:
      key - the key or path
      Returns:
      the long value
      Throws:
      ConfigException - if the key doesn't exist or isn't a number
    • getLong

      long getLong(String key, long defaultValue)
      Gets a long value by key, with a default.
      Parameters:
      key - the key or path
      defaultValue - the default value if key doesn't exist
      Returns:
      the long value or default
    • getOptionalLong

      Optional<Long> getOptionalLong(String key)
      Gets an optional long value by key.
      Parameters:
      key - the key or path
      Returns:
      an Optional containing the value, or empty if not found
    • getDouble

      double getDouble(String key)
      Gets a double value by key.
      Parameters:
      key - the key or path
      Returns:
      the double value
      Throws:
      ConfigException - if the key doesn't exist or isn't a number
    • getDouble

      double getDouble(String key, double defaultValue)
      Gets a double value by key, with a default.
      Parameters:
      key - the key or path
      defaultValue - the default value if key doesn't exist
      Returns:
      the double value or default
    • getOptionalDouble

      Optional<Double> getOptionalDouble(String key)
      Gets an optional double value by key.
      Parameters:
      key - the key or path
      Returns:
      an Optional containing the value, or empty if not found
    • getBoolean

      boolean getBoolean(String key)
      Gets a boolean value by key.
      Parameters:
      key - the key or path
      Returns:
      the boolean value
      Throws:
      ConfigException - if the key doesn't exist or isn't a boolean
    • getBoolean

      boolean getBoolean(String key, boolean defaultValue)
      Gets a boolean value by key, with a default.
      Parameters:
      key - the key or path
      defaultValue - the default value if key doesn't exist
      Returns:
      the boolean value or default
    • getOptionalBoolean

      Optional<Boolean> getOptionalBoolean(String key)
      Gets an optional boolean value by key.
      Parameters:
      key - the key or path
      Returns:
      an Optional containing the value, or empty if not found
    • getList

      List<ConfigNode> getList(String key)
      Gets a list of child nodes by key.
      Parameters:
      key - the key or path
      Returns:
      list of ConfigNode elements
    • getStringList

      List<String> getStringList(String key)
      Gets a list of strings by key.
      Parameters:
      key - the key or path
      Returns:
      list of strings
    • getIntList

      List<Integer> getIntList(String key)
      Gets a list of integers by key.
      Parameters:
      key - the key or path
      Returns:
      list of integers
    • as

      <T> T as(Class<T> type)
      Converts this node to a typed object.
      Type Parameters:
      T - the type
      Parameters:
      type - the class to convert to
      Returns:
      the converted object
      Throws:
      ConfigException - if conversion fails
    • get

      <T> T get(String key, Class<T> type)
      Converts a child node to a typed object.
      Type Parameters:
      T - the type
      Parameters:
      key - the key or path
      type - the class to convert to
      Returns:
      the converted object
      Throws:
      ConfigException - if key doesn't exist or conversion fails
    • rawValue

      Object rawValue()
      Gets this node's value as a raw Object.

      Returns:

      • String, Number, Boolean for primitives
      • List for arrays
      • Map for objects
      • null for null nodes
      Returns:
      the raw value
    • toJson

      String toJson()
      Returns the JSON string representation of this node.
      Returns:
      JSON string
    • toPrettyJson

      String toPrettyJson()
      Returns the pretty-printed JSON string representation of this node.
      Returns:
      pretty-printed JSON string