https://github.com/kale-ko/ejcl
An advanced configuration library for Java with support for local files, mysql databases, and more.
https://github.com/kale-ko/ejcl
config configs configuration configuration-files data java java-serialization json mariadb mysql parser serialization serializer smile yaml
Last synced: about 2 months ago
JSON representation
An advanced configuration library for Java with support for local files, mysql databases, and more.
- Host: GitHub
- URL: https://github.com/kale-ko/ejcl
- Owner: Kale-Ko
- License: mit
- Created: 2023-02-09T04:58:11.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2025-01-04T00:43:16.000Z (6 months ago)
- Last Synced: 2025-04-19T09:58:50.375Z (2 months ago)
- Topics: config, configs, configuration, configuration-files, data, java, java-serialization, json, mariadb, mysql, parser, serialization, serializer, smile, yaml
- Language: Java
- Homepage: https://ejcl.kaleko.dev
- Size: 2.13 MB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# EJCL
EJCL is an advanced configuration library for Java with support for local files, MySQL databases, and more.
EJCL is fully documented at [ejcl.kaleko.dev/docs](https://ejcl.kaleko.dev/docs/)
## Config Types
EJCL has two basic config types, [Structured](https://ejcl.kaleko.dev/docs/io/github/kale_ko/ejcl/StructuredConfig.html) and [Unstructured](https://ejcl.kaleko.dev/docs/io/github/kale_ko/ejcl/UnstructuredConfig.html).
### Structured
Structured configs are created with a Java class used as the "structure".\
Calling [`config#get()`](https://ejcl.kaleko.dev/docs/io/github/kale_ko/ejcl/StructuredConfig.html#get()) will return an instance of the type passed to the constructor for easy use of the object.### Unstructured
Unstructured configs are not created with a defined "structure" and such can be used to store more dynamic data.\
To get or set data on an unstructured config you can call [`config#get(path)`](https://ejcl.kaleko.dev/docs/io/github/kale_ko/ejcl/UnstructuredConfig.html#get(java.lang.String)) or [`config#set(path, value)`](https://ejcl.kaleko.dev/docs/io/github/kale_ko/ejcl/UnstructuredConfig.html#set(java.lang.String,java.lang.Object)) to do so (`path` can be any string and `value` can be any primitive).## Storage Types
EJCL also offers a variety of storage options listed below.
### Local Filesystem
Configs can be stored locally on the filesystem using classes from the [`io.github.kale_ko.ejcl.file`](https://ejcl.kaleko.dev/docs/io/github/kale_ko/ejcl/file/package-summary.html) package.
This includes a [`SimpleFileConfig`](https://ejcl.kaleko.dev/docs/io/github/kale_ko/ejcl/file/simple/package-summary.html) which is a config that stores key/value pairs in a very simple format, `{key}={type}={value}`.
There is also [`BJSLFileConfig`](https://ejcl.kaleko.dev/docs/io/github/kale_ko/ejcl/file/bjsl/package-summary.html) which is a config that stores data in a few common formats (JSON, YAML, and Json Smile).\
Each of these different formats can be used by passing their respective parsers ([`{format}Parser`](https://bjsl.kaleko.dev/docs/io/github/kale_ko/bjsl/parsers/package-summary.html)) to the constructor.### MySQL/MariaDB Server
Configs can also be stored on a MySQL or MariaDB server using classes from the [`io.github.kale_ko.ejcl.mysql`](https://ejcl.kaleko.dev/docs/io/github/kale_ko/ejcl/mysql/package-summary.html) package.
The structured version is slower to load but can be easier to work with, while the unstructured version has the advantage of only fetching the data you need.
Note: The structured version only writes changed values to prevent overwriting others changes.
### In Memory
If you want you can also use EJCL to store a config in memory but there is not much point to this unless you are using an API that only accepts Config objects.
## Type Processors
Type processors are a way to convert between any Java type and object trees for storage. They can be created using `Builder#setProcessor(new ObjectProcessor.Builder().setX(y).build())`.
See [https://github.com/Kale-Ko/BJSL](https://github.com/Kale-Ko/BJSL?tab=readme-ov-file#type-processors) for information.
## Annotations and Conditions
Annotations and conditions are just ways to tell the processor what to serialize and what not to.
See [https://github.com/Kale-Ko/BJSL](https://github.com/Kale-Ko/BJSL?tab=readme-ov-file#annotations-and-conditions) for information.
## Full Example
```java
import java.io.IOException;
import java.nio.file.Path;import io.github.kale_ko.bjsl.parsers.JsonParser;
import io.github.kale_ko.bjsl.processor.ObjectProcessor;
import io.github.kale_ko.ejcl.file.bjsl.StructuredBJSLFileConfig;public class Example {
public static class Data { // For more see https://github.com/Kale-Ko/BJSL?tab=readme-ov-file#full-example
private double foo;
private double bar = 17.8;public double getFoo() {
return foo;
}public double getBar() {
return bar;
}
}public static void main(String[] args) throws IOException {
Path file = Path.of("config.json");JsonParser parser = new JsonParser.Builder().setPrettyPrint(true).build(); // Create a parser with default options
ObjectProcessor processor = new ObjectProcessor.Builder().build(); // Create a processor with default optionsStructuredBJSLFileConfig config = new StructuredBJSLFileConfig.Builder<>(Data.class, file, parser).setProcessor(processor).build(); // Create a StructuredBJSLFileConfig with our processor
config.load(); // Load the config from file
System.out.println("foo: " + config.get().foo); // Read some values
System.out.println("bar: " + config.get().bar);config.get().foo += 5.0; // Modify a value
config.save(); // Save the config back to file
config.close(); // Finally close the config
}
}
```