-
Notifications
You must be signed in to change notification settings - Fork 0
types
YamlAnnotations supports a wide variety of Java types for seamless YAML serialization and deserialization.
All Java primitives and their wrapper classes are supported:
| Primitive | Wrapper | Example YAML |
|---|---|---|
byte |
Byte |
value: 127 |
short |
Short |
value: 32767 |
int |
Integer |
value: 42 |
long |
Long |
value: 9223372036854775807 |
float |
Float |
value: 3.14 |
double |
Double |
value: 3.141592653589793 |
boolean |
Boolean |
value: true (also: yes, y, 1) |
char |
Character |
value: A |
public class PrimitiveConfig extends YamlFileInterface {
@YamlKey("count")
public int count = 0;
@YamlKey("ratio")
public double ratio = 0.5;
@YamlKey("enabled")
public boolean enabled = true;
@YamlKey("grade")
public char grade = 'A';
}@YamlKey("message")
public String message = "Hello, World!";message: Hello, World!UUIDs are automatically converted to/from strings:
@YamlKey("player-id")
public UUID playerId = UUID.randomUUID();player-id: 123e4567-e89b-12d3-a456-426614174000Enums are serialized as uppercase strings:
public enum Difficulty { EASY, NORMAL, HARD }
public class GameConfig extends YamlFileInterface {
@YamlKey("difficulty")
public Difficulty difficulty = Difficulty.NORMAL;
}difficulty: NORMAL@YamlKey("allowed-commands")
public List<String> allowedCommands = List.of("help", "home", "spawn");
@YamlKey("lucky-numbers")
public List<Integer> luckyNumbers = List.of(7, 13, 42);allowed-commands:
- help
- home
- spawn
lucky-numbers:
- 7
- 13
- 42Sets are serialized like lists. If elements are Comparable, they are sorted in the output:
@YamlKey("unique-ids")
public Set<Integer> uniqueIds = Set.of(1, 2, 3);unique-ids:
- 1
- 2
- 3@YamlKey("task-queue")
public Queue<String> taskQueue = new ArrayDeque<>(List.of("task1", "task2"));task-queue:
- task1
- task2Empty collections are preserved as empty (not converted to null):
@YamlKey("empty-list")
public List<String> emptyList = new ArrayList<>();empty-list: []@YamlKey("scores")
public Map<String, Integer> scores = Map.of(
"player1", 100,
"player2", 85
);scores:
player1: 100
player2: 85@YamlKey("config")
public Map<String, Map<String, Object>> config = new LinkedHashMap<>();config:
database:
host: localhost
port: 3306
cache:
enabled: true
ttl: 3600Map keys can be any supported primitive type:
@YamlKey("level-rewards")
public Map<Integer, String> levelRewards = Map.of(
10, "Bronze Badge",
25, "Silver Badge",
50, "Gold Badge"
);Records provide a clean way to define complex data structures. See Working with Records for detailed documentation.
public record ServerInfo(String name, String ip, int port) {}
public class Config extends YamlFileInterface {
@YamlKey("server")
public ServerInfo server = new ServerInfo("Main", "127.0.0.1", 25565);
}server:
name: Main
ip: 127.0.0.1
port: 25565Any class with a String constructor can be used:
public class CustomId {
private final String value;
public CustomId(String value) {
this.value = value;
}
@Override
public String toString() {
return value;
}
}
public class Config extends YamlFileInterface {
@YamlKey("custom-id")
public CustomId customId = new CustomId("abc-123");
}custom-id: abc-123For types with public static fields (like Bukkit's Sound enum-like classes):
// Assuming Sound has: public static Sound ENTITY_PLAYER_HURT = ...
@YamlKey("hurt-sound")
public Sound hurtSound = Sound.ENTITY_PLAYER_HURT;hurt-sound: ENTITY_PLAYER_HURTNull values are preserved:
@YamlKey("optional-value")
public String optionalValue = null;optional-value: nullNote: Primitive types (int, boolean, etc.) cannot be null. Use wrapper classes (Integer, Boolean) if you need nullable values.
See Leniency Modes for how type conversion behaves in strict versus lenient modes.