Configurate
repository·trunk·Indexed 19 days ago
https://github.com/spongepowered/configurateA node-based configuration library for Java applications that provides a generic way to manipulate data across various formats, including JSON, HOCON, YAML, and XML. It features a modular architecture with a core API and specific loader modules, as well as extras for Kotlin, Google Guice, and Mojang's DataFixerUpper. The library includes a CLI tool for inspecting the internal data structures of configuration files.
What's inside Configurate
- Configurate Extras provides specialized modules that bridge Configurate with other popular libraries. These modules allow you to leverage existing ecosystems (like dependency injection or specific data formats) while using Configurate's configuration management.
Understand the Configurate module structure
trunkConfigurate is split into several modules to separate the core API from specific file format implementations and extra features:
Core
configurate-core: The base library containing the main APIs for manipulating configurations. It is format-agnostic.
Loaders
Each loader module provides implementation for a specific format. You must include the module for the format you need:
configurate-gson: JSON format using Gson.configurate-jackson: JSON format using Jackson.configurate-hocon: HOCON format using lightbend config.configurate-xml: XML format using JAXP.configurate-yaml: YAML format using SnakeYAML.
Extras
Additional modules for specific integration needs:
configurate-extra-dfu[2-4]: Integration with Mojang's DataFixerUpper.configurate-extra-kotlin: Kotlin extensions and support for Kotlin data classes in the object mapper.configurate-extra-guice: Uses a GuiceInjectorto create object instances in the object mapper.
Use Configurate with Kotlin
trunkThe Kotlin module provides extension functions designed to make working with Configurate more idiomatic and easier when developing in Kotlin.Integrate with Mojang's DataFixerUpper
trunkConfigurate provides integration modules for Mojang's
DataFixerUpperlibrary to assist with data migration and versioning.Note that modules are version-specific:
- Separate modules exist for DataFixerUpper versions 2, 4, and 7.
- New features are only added to the module supporting the latest version.
- Compatibility Note: The module for v4 is expected to be compatible with DataFixerUpper v5. The module for v7 is expected to be compatible with DataFixerUpper v8.
Use Guice for ObjectMapper instance creation
trunkIf you use Google Guice for dependency injection, you can use the Guice integration module to allow anObjectMapperto create object instances using a Guice injector.Install Configurate via Gradle or Maven
trunkTo use Configurate, ensure your project is configured for Java 8 or higher. Configurate is modular: you must depend on
configurate-core(usually pulled in transitively) and a specific loader module for the format you wish to use (e.g., HOCON, JSON, YAML).#### Gradle ```groovy repositories { mavenCentral() } dependencies { // Modify this line to target the loader you wish to use. implementation 'org.spongepowered:configurate-hocon:4.2.0' }Maven
<dependencies> <dependency> <groupId>org.spongepowered</groupId> <!-- Modify this line to target the loader you wish to use. --> <artifactId>configurate-hocon</artifactId> <version>4.2.0</version> </dependency> </dependencies>Use Kotlin data classes with ObjectMapper
trunkTheconfigurate-extra-kotlinmodule provides an extension toorg.spongepowered.configurate.objectmapping.ObjectMapperthat enables the use of Kotlin data classes as mapped types. This allows for more idiomatic Kotlin configuration mapping by leveraging data class constructors.Use the Configurate CLI tool to inspect configuration files
trunkThe Configurate CLI tool is a utility designed to display the internal data structures of configuration files. It helps developers visualize how Configurate's nodes (including attributes, comments, and hierarchy) are parsed from various file formats.
The tool supports several subcommands corresponding to different file formats:
xml,yaml,json, andhocon.# Example: Inspecting a YAML file # (Assuming the tool is compiled and available as 'configurate-tool') ./configurate-tool yaml path/to/config.ymlUse Kotlin builders for customized nodes
trunkTheorg.spongepowered.configurate.kotlinpackage includes severalbuildersdesigned to help create customized nodes more effectively within a Kotlin environment.Use Kotlin extensions for idiomatic configuration
trunkTheorg.spongepowered.configurate.kotlin.extensionspackage provides extension functions for both Configurate types and Kotlin standard library (stdlib) types. These extensions are intended to help developers write more idiomatic Kotlin code when interacting with the Configurate API.Use ConfigurationNodeSerializer to copy ConfigurationNodes
trunkThe
ConfigurationNodeSerializeris a specializedTypeSerializerdesigned to handleConfigurationNodeobjects directly. It allows you to bypass the standardObjectMapperwhen you need to work with nodes.Key Behaviors:
- Deserialization: When deserializing a
ConfigurationNode, it returns a copy of the provided node. Changes made to the resulting node will not affect the original source node. - Serialization: When serializing, it sets the provided object into the target node using
node.set(obj). - Empty Values: If an empty value is required, it returns a new root
BasicConfigurationNodeusing the providedConfigurationOptions.
// Note: This class is package-private in the source, but its behavior defines // how ConfigurationNodes are handled when used as types in serialization. // Deserialization behavior: ConfigurationNode newNode = serializer.deserialize(ConfigurationNode.class, originalNode); // newNode is a copy; originalNode remains unchanged. // Serialization behavior: serializer.serialize(ConfigurationNode.class, myNode, targetNode); // myNode is now set into targetNode.- Deserialization: When deserializing a
Inspect YAML configuration files
trunkUse the
yamlsubcommand to display the structure of a YAML file. This subcommand treats nodes asCommentedConfigurationNode, allowing it to display comments associated with nodes.Arguments:
path: The location of the YAML file to read.
Options:
--header-mode,--header-mode <MODE>: How to read a header from this file (usesHeaderModeenum). Defaults toPRESERVE.-i,--indent <INT>: How much to indent when outputting. Defaults to4.-s,--style <STYLE>: What node style to use (usesNodeStyleenum).
configurate-tool yaml path/to/file.yml --style BLOCK