Keqing
Keqing is a Java library designed to help with language files and configuration files in complex projects.
It provides a way to create "priorities" between the files and fill out any missing data from lower priority files.
Using Keqing you can easily manage multiple language files and ensure that your application always has the necessary data to function correctly.
You may find all the documentation about Keqing on this page.
Quick example
Installation
Keqing is separated into multiple modules, so you can include only the parts you need. All modules are accessible via Maven Central.
Name | Description | Version |
---|---|---|
| The core module, required for all other modules. | |
| Module for working with JSON files using Gson. | |
| Module for working with YAML files using SnakeYAML. |
Loading files
Keqing supports loading files from Jar's resources as well as from the filesystem. If you want to load files from the resources, you may as well specify the classloader to use. By default, Keqing uses Thread's context classloader.
When loading files, you need to specify the base file path (without postfix and extension), the postfix separator, and the serializer to use.
Base file path
In order for Keqing to find the files, you need to specify the base file path. This is the path without the postfix separator and the file extension. Keqing will not look for files recursively.
/lang
/data/values
Postfix separator
Simply put: the character between the base file path and the postfix. This can be any character you want.
Serializer
Currently, Keqing supporsts Java Properties, JSON (via Gson), and YAML (via SnakeYAML). You can also implement your own serializer by implementing the Serializer
interface.
Serializer | File format |
---|---|
| Java Properties ( |
| JSON ( |
| YAML ( |
Reading properties
Once you have loaded the files, you can read their properties using multiple methods.
Method | Description |
---|---|
| Reads a single property by its postfix, key, and converts it into the type. |
| Reads a single property by default postfix, key, and converts it into the type. |
| Reads a list of properties by its postfix, key, and converts it into the |
| Reads a list of properties by default postfix, key, and converts it into the |
Postfix
When reading a property, you must specify the postfix. This detonates which file to prefer. If the property is not found in that file, Keqing will attempt to find it in lower priority files, until it reaches the default file.
You may specify the default postfix using the setDefaultPostfix
method. Without setting it, Keqing will throw an exception.
You may also specify the priorities of the postfixes. Defaultly, Keqing will try the specified/default postfix first, then any file without a postfix. You can change this behavior using the setPostfixPriorities
method.
Key
The key is the path to the property you want to read. You may use dot notation to access nested properties. For example, if you have following JSON structure:
You can read the host
property using the key database.host
:
Type
When reading a property, you must also specify the type to convert the property into. Keqing supports all primitive types including String
. When using GsonSerializer
or SnakeYamlSerializer
, you may also specify custom types.
Reading custom type with Gson
The JSON file:
The Java class:
Reading the property:
Merging properties
When reading a property, Keqing always attempts to find the property by the priorities.
Data | Procedure |
---|---|
Primitive types | Use the first found value. |
Custom types | Merge properties from all priority files. |
Arrays | Merge all entries from all priority files. |
Merging custom types
When using GsonSerializer
or SnakeYamlSerializer
, Keqing can merge properties from multiple files into a single object. For example, if you have following JSON files:
You can read the database
property and get a merged object:
Merging arrays
When reading an array property, Keqing will merge all entries from all priority files into a single list. For example, if you have the following JSON files:
You can read the servers
property and get a merged list: