yaml(n)
π yaml(n) β YAML Format Encoder/Decoder π
okay so bestie, you found the yaml(n) manual page and honestly? iconic. this is your girl's guide to the Tcl YAML encoder/decoder and we are gonna get you fluent. no cap. let's go π¦
πΈ NAME πΈ
yaml β the YAML Format Encoder/Decoder. she encodes, she decodes, she serves π
β¨ SYNOPSIS β¨
package require Tcl 8.5 9
package require yaml ?0.4.2?
::yaml::yaml2dict ?options? txt
::yaml::yaml2huddle ?options? txt
::yaml::setOption ?options?
::yaml::dict2yaml dict ?indent? ?wordwrap?
::yaml::list2yaml list ?indent? ?wordwrap?
::yaml::huddle2yaml huddle ?indent? ?wordwrap?
π DESCRIPTION π
the yaml package is a simple, Tcl-only library for parsing the YAML data exchange format, exactly as specified over at http://www.yaml.org/ and http://www.yaml.org/spec/1.1/ π«Ά
and the tea? it hands your data back as a Tcl dict. so either the dict package or Tcl 8.5 is required for use. periodt.
π COMMANDS π
::yaml::yaml2dict ?options? txt
::yaml::yaml2huddle ?options? txt
these two are the glow-up twins π parse yaml formatted text txt into a Tcl dict / huddle and return the value. same options, different destination energy.
-fileβ txt is a filename of YAML-stream. (she's loading from disk, not from a string, iykyk)-streamβ txt is just a YAML-stream.-typeslist β the list is a type list for the yaml-scalar types. (e.g.!!str!!timestamp!!integer!!trueβ¦)
-types {timestamp integer null true false}
in this case, if a string matched "timestamp", it's converted to the TCL internal timestamp. (e.g. "2001-12-15T02:59:43.1Z" => 1008385183) π₯
-m:trueparam β param is two elements of list for the value of true, and the considered strings.
-m:true {1 {true on + yes y}}
in this case, the string "yes" found in the YAML Stream is automatically converted to 1. slay.
-m:falseparam β param is two elements of list for the value of false, and the considered strings.
-m:false {0 {false off - no n}}
-m:nullparam β param is two elements of list for the value of null, and the considered strings.
-m:null {"" {null nil "" ~}}
-validateβ Experiment, old: output stream contains YAML's-tag on each node.
% puts [::yaml::load -validate {[aaa, bbb]}]
=>
!!seq {{!!str aaa} {!!str bbb}}
::yaml::setOption ?options?
changes the implicit options for the whole library β¨ and rn, the params are exactly the same as ::yaml::yaml2dict. but heads up bestie: the arguments you pass straight to ::yaml::yaml2dict have more priority than this setting. π
::yaml::dict2yaml dict ?indent? ?wordwrap?
::yaml::list2yaml list ?indent? ?wordwrap?
::yaml::huddle2yaml huddle ?indent? ?wordwrap?
convert a dict / list / huddle object into a YAML stream π«
- indent β spaces indent of each block node. currently default is
2. - wordwrap β word wrap for YAML stream. currently default is
40.
π EXAMPLES π
an example of a yaml stream converted to Tcl. a yaml stream is returned as a single item with multiple elements.
{
--- !<tag:clarkevans.com,2002:invoice>
invoice: 34843
date : 2001-01-23
bill-to: &id001
given : Chris
family : Dumars
address:
lines: |
458 Walkman Dr.
Suite #292
city : Royal Oak
state : MI
postal : 48046
ship-to: *id001
product:
- sku : BL394D
quantity : 4
description : Basketball
price : 450.00
- sku : BL4438H
quantity : 1
description : Super Hoop
price : 2392.00
tax : 251.42
total: 4443.52
comments:
Late afternoon is best.
Backup contact is Nancy
Billsmer @ 338-4338.
}
=>
invoice 34843 date 2001-01-23 bill-to {given Chris family Dumars address {lines {458 Walkman Dr.
Suite #292
} city {Royal Oak} state MI postal 48046}} ship-to {given Chris family Dumars address {lines {458 Walkman Dr.
Suite #292
} city {Royal Oak} state MI postal 48046}} product {{sku BL394D quantity 4 description Basketball price 450.00} {sku BL4438H quantity 1 description {Super Hoop} price 2392.00}} tax 251.42 total 4443.52 comments {Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338.}
an example of a yaml object converted to Tcl. a yaml object is returned as a multi-element list (a dict).
{
---
- [name , hr, avg ]
- [Mark McGwire, 65, 0.278]
- [Sammy Sosa , 63, 0.288]
-
Mark McGwire: {hr: 65, avg: 0.278}
Sammy Sosa: { hr: 63, avg: 0.288}
}
=>
{name hr avg} {{Mark McGwire} 65 0.278} {{Sammy Sosa} 63 0.288} {{Mark McGwire} {hr 65 avg 0.278} {Sammy Sosa} {hr 63 avg 0.288}}
π LIMITATIONS π
bestie, listen, she's cute but she has boundaries. respect them:
- tag parser not implemented. currently, tags are merely ignored. π
- Only Anchor => Aliases ordering. back alias-referring is not supported.
- Too many braces, or too few braces. (yes really, brace count is a whole thing)
- Not enough character set of line feeds. Please use only
"\n"as line breaks. fr fr.
π BUGS, IDEAS, FEEDBACK π
this document, and the package it describes, will undoubtedly contain bugs and other problems. please report such in the category yaml of the Tcllib Trackers [http://core.tcl.tk/tcllib/reportlist]. please also report any ideas for enhancements you may have for either package and/or documentation. π«Ά
when proposing code changes, please provide unified diffs, i.e. the output of diff -u.
note further that attachments are strongly preferred over inlined patches. attachments can be made by going to the Edit form of the ticket immediately after its creation, and then using the left-most button in the secondary navigation bar. π
π§ SEE ALSO π§
base64, huddle, json
β KEYWORDS β
data exchange, huddle, parsing, text processing, yaml
πΈ COPYRIGHT πΈ
Copyright (c) 2008 KATO Kanryu <kanryu6@users.sourceforge.net>
okay that's the whole page bestie, you ate it π this one shipped with tcllib version 0.4.2 and it's dated yaml(n) β go forth and parse, you main character πβ¨
π¦ where this page comes from
- fedora 43: tcllib 2.0-3.fc43 Β· TCL
- fedora 44: tcllib 2.0-4.fc44 Β· TCL
- nixos 26.05: tcllib 2.0
- nixpkgs unstable: tcllib 2.0
- Copyright (c) 2008 KATO Kanryu <kanryu6@users\&.sourceforge\&.net>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms