An easy-to-use YAML parsing and manipulation library for the Ring programming language. This library is built as a wrapper around the libyaml C library.
- Load YAML Data: Effortlessly load YAML data from files or directly from strings.
- Complex Data Structures: Seamlessly parse intricate and deeply nested YAML structures.
- Intuitive Data Access: Access your data with ease using dot notation via the
yaml_getfunction. - Cross-Platform Compatibility: Works flawlessly across Windows, Linux, macOS, and FreeBSD.
- Ring Language: Ensure you have Ring version 1.23 or higher installed. You can download it from the official Ring website.
The recommended way to install Ring YAML is through the Ring Package Manager (RingPM).
ringpm install yaml from ysdragonFirst, you need to load the library in your Ring script:
load "yaml.ring"yamlData = yaml_load("path/to/your/file.yaml")
if (isNull(yamlData)) {
print("Error loading YAML: " + yaml_lasterror() + "\n")
else
print(yamlData) // Print the entire data structure
}yamlString = `
product: Laptop
price: 1299.99
specs:
processor: i7
memory: 16GB
items:
- name: Mouse
- name: Keyboard
`
data = yaml_load(yamlString)
? dataYou can access your YAML data in two convenient ways:
1. Using Standard Ring List Syntax:
// Access nested values
product = data[:product] // "Laptop"
processor = data[:specs][:processor] // "i7"
// Access array elements
firstItem = data[:items][1][:name] // "Mouse"2. Using the yaml_get function with Dot Notation:
// Access nested values
product = yaml_get(data, "product") // "Laptop"
processor = yaml_get(data, "specs.processor") // "i7"
// Access array elements (1-based indexing)
firstItem = yaml_get(data, "items[1].name") // "Mouse"
// or
firstItem = yaml_get(data, "items.[1].name") // "Mouse"It's good practice to check for errors, especially when dealing with file I/O.
data = yaml_load("nonexistent.yaml")
if (isNull(data)) {
? "Failed to load YAML file: " + yaml_lasterror()
}Loads YAML data from a file or a string.
- Parameters:
source(string): The file path (if it ends with.yamlor.yml) or the YAML string to be parsed.
- Returns: A Ring list containing the parsed key-value pairs, or
NULLif an error occurs.
Accesses nested data within a parsed YAML structure using dot notation.
- Parameters:
data: The YAML data loaded by theyaml_loadfunction.path(string): A dot-separated path to the desired value (e.g.,"product.specs.processor").
- Returns: The value at the specified path, or
NULLif the path is not found.
Retrieves the last error message that occurred.
- Returns: A string containing the details of the last error.
Retrieves the libyaml version.
- Returns: A string representing the libyaml version.
For more in-depth examples, please refer to the examples/ directory in the repository.
If you wish to contribute to the development of Ring YAML or build it from the source, follow these steps.
- CMake: Version 3.16 or higher.
- C Compiler: A C compiler compatible with your platform (e.g., GCC, Clang, MSVC).
- Ring Source Code: You will need to have the Ring language source code available on your machine.
-
Clone the Repository:
git clone https://github.com/ysdragon/yaml.git --recursive
Note: If you installed the library via RingPM, you can skip this step.
-
Set the
RINGEnvironment Variable: This variable must point to the root directory of the Ring language source code.- Windows (Command Prompt):
set RING=X:\path\to\ring
- Windows (PowerShell):
$env:RING = "X:\path\to\ring"
- Unix-like Systems (Linux, macOS or FreeBSD):
export RING=/path/to/ring
- Windows (Command Prompt):
-
Configure with CMake: Create a build directory and run CMake from within it.
mkdir build cd build cmake .. -
Build the Project: Compile the source code using the build toolchain configured by CMake.
cmake --build .The compiled library will be available in the
lib/<os>/<arch>directory.
Contributions are always welcome! If you have suggestions for improvements or have identified a bug, please feel free to open an issue or submit a pull request.
This project is licensed under the MIT License. See the LICENSE file for more details.