Class RawYamlLoader
Utility class for loading and saving raw YAML files as Map structures.
This class provides low-level YAML operations using SnakeYAML, allowing direct manipulation of YAML data without mapping to Java objects. This is essential for the migration system, which needs to transform YAML structures before they can be loaded into typed configuration classes.
Features
- Safe loading: Uses
SafeConstructorto prevent arbitrary code execution - Order preservation: Uses
LinkedHashMapto maintain key insertion order - Pretty output: Configures block style formatting with proper indentation
- Flexible input: Supports loading from
Path,InputStream,Reader, orString
Usage
// Load YAML from file
Path configPath = Path.of("config.yml");
Map<String, Object> data = RawYamlLoader.load(configPath);
// Modify the data
data.put("version", 2);
data.put("newField", "newValue");
// Save back to file
RawYamlLoader.save(configPath, data);
Thread Safety
Each method creates a new Yaml instance, making this class thread-safe.
However, the returned Map objects are not thread-safe and should be
synchronized externally if accessed from multiple threads.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic intextractVersion(Map<String, Object> data) Extracts the version field from YAML data.load(InputStream inputStream) Loads YAML content from an input stream.Loads YAML content from a reader.Loads YAML content from a file path.loadFromString(String content) Loads YAML content from a string.static intreadVersion(Path path) Reads the version field from a YAML file.static voidsave(OutputStream outputStream, Map<String, Object> data) Saves YAML data to an output stream.static voidSaves YAML data to a writer.static voidSaves YAML data to a file path atomically.static StringsaveToString(Map<String, Object> data) Converts YAML data to a string.
-
Method Details
-
load
Loads YAML content from a file path.
If the file does not exist, returns an empty
LinkedHashMap.- Parameters:
path- the path to the YAML file- Returns:
- a mutable map containing the YAML data, or empty map if file doesn't exist
- Throws:
MigrationException- if the file cannot be read or parsedNullPointerException- if path is null
-
load
Loads YAML content from an input stream.
The caller is responsible for closing the input stream.
- Parameters:
inputStream- the input stream containing YAML content- Returns:
- a mutable map containing the YAML data
- Throws:
MigrationException- if the content cannot be parsedNullPointerException- if inputStream is null
-
load
Loads YAML content from a reader.
The caller is responsible for closing the reader.
- Parameters:
reader- the reader containing YAML content- Returns:
- a mutable map containing the YAML data
- Throws:
MigrationException- if the content cannot be parsedNullPointerException- if reader is null
-
loadFromString
Loads YAML content from a string.- Parameters:
content- the YAML content as a string- Returns:
- a mutable map containing the YAML data
- Throws:
MigrationException- if the content cannot be parsedNullPointerException- if content is null
-
save
Saves YAML data to a file path atomically.
Creates parent directories if they don't exist. Overwrites any existing file using an atomic write-to-temp-then-rename strategy to prevent corruption.
- Parameters:
path- the path to save the YAML filedata- the data to save- Throws:
MigrationException- if the file cannot be writtenNullPointerException- if path or data is null
-
save
Saves YAML data to an output stream.
The caller is responsible for closing the output stream.
- Parameters:
outputStream- the output stream to write todata- the data to save- Throws:
MigrationException- if the data cannot be serializedNullPointerException- if outputStream or data is null
-
save
Saves YAML data to a writer.
The caller is responsible for closing the writer.
- Parameters:
writer- the writer to write todata- the data to save- Throws:
MigrationException- if the data cannot be serializedNullPointerException- if writer or data is null
-
saveToString
Converts YAML data to a string.- Parameters:
data- the data to convert- Returns:
- the YAML content as a string
- Throws:
MigrationException- if the data cannot be serializedNullPointerException- if data is null
-
readVersion
Reads the version field from a YAML file.
This is a convenience method for quickly checking the version of a configuration file without loading the entire content.
- Parameters:
path- the path to the YAML file- Returns:
- the version number, or 0 if the file doesn't exist or has no version field
- Throws:
MigrationException- if the file cannot be read or the version is not a valid integerNullPointerException- if path is null
-
extractVersion
Extracts the version field from YAML data.- Parameters:
data- the YAML data map- Returns:
- the version number, or 0 if no version field exists
- Throws:
MigrationException- if the version field is not a valid integerNullPointerException- if data is null
-