Skip to content

KrLite/Pierced

Repository files navigation



Pierced is a lightweight Java library which handles basic TOML configuration files.

Implementation

Using Groovy
build.gradle
repositories {
	maven { url "https://jitpack.io" }
}

dependencies {
	implementation include("com.github.KrLite:Pierced:$project.pierced_version")
}
gradle.properties
pierced_version=?

[!NOTE] Replace ? with the latest tag name of Pierced.

Using Kotlin
build.gradle.kts
repositories {
	maven("https://jitpack.io")
}

dependencies {
	include("com.github.KrLite:Pierced:${property("piercedVersion")}")?.let {
		implementation(it)
	}
}
gradle.properties
piercedVersion=?

[!NOTE] Replace ? with the latest tag name of Pierced.

Once implemented, extend Pierced ↗1 class and add your own configuration fields.

Introduction

Pierced is only 16KB2. You can bring Pierced anywhere you want.

The sacrifice of such a small size is that Pierced is not a full-featured TOML parser. It only supports the most basic features of TOML, and so is not recommended for complex configuration files. Still, for those who want something fast and simple, Pierced is a solid choice.

Usage

1. Create a Config class which extends Pierced class:

public class Config extends Pierced {
	public String name;
	public int age;
	public boolean isMale;
	public String[] hobbies;
	public List<String> friends;
	public Map<String, Integer> scores;

	public Config(File file) {
		super(Config.class, file); // Config.class should be the same as this class
	}
}

2. Create a Config instance and load the configuration file:

Config config = new Config(new File("config.toml"));
config.load();

3. Use and save the configuration:

System.out.println(config.name);
config.age = 18;
config.save();

Features

Pierced supports most of the TOML features:

  • (±)nan and (±)infinity
  • Basic strings and literal strings3
  • Boolean values
  • Full-line comments and inline comments
  • Integer (bin, oct, dec and hex) values and float values with underscores4
  • Multiline basic strings and literal strings3
  • Tables (dotted keys)

Warning

Pierced does not support these features for now:

  • Array values
  • Dates and times

Annotations

Pierced supports annotations to customize the behavior of the options.

@Comment to comment:

@Comment("THIS CONFIGURATION FILE IS FOR DEMO")
@Comment("DO NOT USE IN PRODUCTION")
public class Config extends Pierced<Config> {
	public Config(File file) {
		super(Config.class, file);
	}
	
	@Comment("The name of the user")
	public String name = "Username";
}

Result:

# THIS CONFIGURATION FILE IS FOR DEMO
# DO NOT USE IN PRODUCTION

name = 'Username'
# The name of the user

@Table to categorize fields:

@Table("user")
public String name = "Username"; // Categorized

public int people = 100; // Uncategorized

Result:

people = 100

[user]
name = 'Username'

@Silent to hide fields from saving and loading:

public String name = "Username"; // Visible
public @Silent int age = 18; // Invisible

Important

Only fields not annotated with @Silent will be saved and loaded.

License

Pierced is licensed under the GNU Lesser General Public License v3.0.

Footnotes

  1. net.krlite.pierced.config.Pierced

  2. Currently, Pierced compiled and source JARs are only 16KB and 8KB, respectively.

  3. Literal strings can be read, but will be seen the same as basic strings for now. All strings are saved as literal strings in case of complex escaping. 2

  4. Scientific notation is not supported yet.