Skip to main content

YAML Encoding and Decoding

DotHRB provides functions for converting YAML data. Use these functions to seamlessly transform YAML strings into accessible hashes and vice versa.

FunctionSignatureDescription
yamldecodeyamldecode(yaml_string) -> hashConverts a YAML-formatted string into an hash.
yamlencodeyamlencode(hash) -> yaml_stringConverts a programming hash into a compliant YAML string.
#include "dothrb.ch"

function main()
local cYaml
local hYaml

text to var cYaml
## YAML Test Fixture
# Scalars, Mappings, Sequences, Dates, Nulls, Anchors

string_plain: This is a simple string.
string_quoted: "Quoted strings can use special characters like a colon: and preserve leading/trailing spaces. "
string_literal: |
This is a literal block scalar.
It preserves newlines and ignores trailing whitespace.

string_folded: >
This is a folded block scalar.
It converts newlines into spaces, making the text flow as a single line, but still wrapping neatly in the editor.

integer: 100
float: 3.14159
scientific_notation: 1.23e-04
boolean_true: true
boolean_false: No
null_value_explicit: null
null_value_implicit: ~
date: 2025-11-30
datetime: 2025-11-30T19:01:45+01:00

user_profile:
id: 42
username: "jdoe"
is_active: true
roles:
- admin
- developer

favorite_colors:
- Red
- Green
- Blue

# Anchor definition for testing data reuse
&common_metadata
version: 1.0
created_by: system

document_a:
title: First Document
<<: *common_metadata

document_b:
title: Second Document
<<: *common_metadata
status: published
endtext

hYaml := yamldecode(cYaml)

outstd(hb_jsonencode(hYaml, true), hb_eol())

outstd(yamlencode(hYaml), hb_eol())

return nil