YAML, TOML, JSON and XML

Jennifer

One small config in four formats: how each maps to Jennifer values, where they differ, and what happens when the input is wrong or hostile.

November 2026, by @mplx.
Based on Jennifer 0.27.0

Powered by Jennifer's Cantrip - Markdown into slide decks, https://github.com/jennifer-language/cantrip
1 / 18

Four libraries, one shape

FormatEnabledecode returnsAddressing
JSONuse json;json.ValueJSON Pointer
TOMLuse toml;toml.ValueJSON Pointer
YAMLuse yaml;yaml.ValueJSON Pointer
XMLuse xml;xml.Valuepath steps
2 / 18

The config: JSON and TOML

{"name": "shop", "port": 8080, "debug": false,
 "started": "2026-11-19T08:00:00Z", "tags": ["web", "eu"],
 "db": {"host": "localhost", "pool": 4}}
name = "shop"
port = 8080
debug = false
started = 2026-11-19T08:00:00Z
tags = ["web", "eu"]
[db]
host = "localhost"
pool = 4
3 / 18

The config: YAML and XML

name: shop
port: 8080
debug: false
started: 2026-11-19T08:00:00Z
tags: [web, eu]
db: {host: localhost, pool: 4}
<app name="shop" port="8080" debug="false">
  <started>2026-11-19T08:00:00Z</started>
  <tag>web</tag><tag>eu</tag>
  <db host="localhost" pool="4"/>
</app>
4 / 18

Reading into one struct

def t as toml.Value init toml.decode(fs.readString("app.toml"));
def b as Config init Config{name: toml.asString($t, "/name"),
    port: toml.asInt($t, "/port"), debug: toml.asBool($t, "/debug"),
    pool: toml.asInt($t, "/db/pool")};
def x as xml.Value init xml.decode(fs.readString("app.xml"));
def d as Config init Config{name: xml.attr($x, "name"),
    port: convert.toInt(xml.attr($x, "port")),
    debug: xml.attr($x, "debug") == "true",
    pool: convert.toInt(xml.attr(xml.get($x, "db"), "pool"))};
Config{name: "shop", port: 8080, debug: false, pool: 4}   (all four equal)
5 / 18

What each format can hold

Node typeJSONTOMLYAMLXML
int, float, boolyesyesyestext
stringyesyesyestext
nullyesnoyesno
datetimeno4 formsyestext
bytesnono!!binaryno
list, mapyesyes (root: table)yesrepeated elements
json string, toml datetime, yaml datetime     (typeOf "/started")
6 / 18

XML: everything is a string

xml.attr($x, "port")                 # "8080", a string
convert.toInt(" 8080 ")              # throws: not a valid integer
xml.findAll($x, "tag")               # 2 elements: the list
xml.text(xml.get($x, "started"))     # "2026-11-19T08:00:00Z"
xml.attr($x, "missing")              # throws; xml.hasAttr tests first
7 / 18

JSON numbers

def j as json.Value init json.decode(
    '{"a": 1, "b": 1.0, "c": 1e2, "big": 123456789012345678901}');
# typeOf: a int, b float, c float, big float
json.asInt($j, "/b")      # throws: node is a float, not an int
json.asFloat($j, "/a")    # 1.0: an int node promotes
json.asFloat($j, "/big")  # 1.2345678901234568e+20, digits lost
json.encode({"f": 2.0, "g": 1000000.0})   # {"f":2.0,"g":1000000.0}
8 / 18

YAML: version 1.2 and its traps

country: NO          # string: no "Norway problem" in YAML 1.2
answer: yes          # string, not true
version: 1.10        # float 1.1: the zero is gone
port: "8080"         # string: asInt throws
empty:               # null
zip: 01234           # decode error: 1.1 octal 668, 1.2 decimal 1234
big: 123456789012345678901    # kept as the string of its digits
9 / 18

TOML: dates, tables, no null

def t as toml.Value init toml.decode("d = 2026-11-19\nlt = 07:30:00\n"
    + "hex = 0xff\nn = 1_000\n[[item]]\nsku = \"A1\"\n[[item]]\nsku = \"B2\"\n");
time.iso(toml.asDatetime($t, "/d"))   # 2026-11-19T00:00:00Z
toml.asString($t, "/item/1/sku")      # B2: [[item]] is a list of tables
toml.encode({"a": null})              # throws: TOML has no null type
toml.encode([1, 2])                   # throws: root must be a table
10 / 18

Writing: a struct encodes directly

def c as Config init Config{name: "shop", port: 8080,
    tags: ["web", "eu"], db: Db{host: "localhost", pool: 4}};
json.encode($c)   # {"name":"shop","port":8080,"tags":["web","eu"],...}
yaml.encode($c)   # {name: shop, port: 8080, tags: [web, eu], db: {...}}
toml.encode($c)   # name = "shop" ... [db] host = "localhost" ...

def x as xml.Value init xml.element("app");
$x = xml.setAttr($x, "port", convert.toString($c.port));
$x = xml.append($x, xml.setText(xml.element("tag"), "web"));
xml.encode($x)    # <app port="8080"><tag>web</tag></app>
11 / 18

Round trips

toml.decode(toml.encode($t)) == $t          # true
json.decode(json.encode($j)) == $j          # true
yaml.decode(yaml.encodePretty($y)) == $y    # true
yaml.decode(yaml.encode($y)) == $y          # false!
{name: shop, ..., started: '2026-11-19T08:00:00Z', ...}
12 / 18

Converting between formats

toml.encode: cannot convert an opaque json.Value to a toml.Value
yaml.encodePretty: cannot convert an opaque toml.Value to a yaml.Value

Go through your own types instead:

def t as toml.Value init toml.decode(fs.readString("app.toml"));
def cfg as Config init readConfig($t);        # typed accessors
fs.writeString("app.json", json.encodePretty($cfg));
13 / 18

Malformed input throws

InputMessage (kind "runtime")
{"port": 80,}expected string key in object at line 1, column 13
{"a": 1, "a": 2}duplicate key "a" in object at line 1, column 10
port = 80 twicetoml: line 2: duplicate key "port"
name = hellotoml: line 1: unexpected value starting with "h"
a: [1, 2yaml: line 1: did not find expected ',' or ']'
<a><b></a>closing tag </a> does not match <b> at line 1, column 11
<a>&nbsp;</a>unknown entity   at line 1, column 4
14 / 18

Hostile input: kind "limit"

CapDefault (jennifer-tiny)Formats
Nesting depth1000 levels (64)all four
Value nodesabout 2 million (256K)all four
Input size4 MiB (512 KiB)YAML
Alias expansioncounted as nodesYAML
Entity expansionnone: only the five predefinedXML
limit: yaml.decode: document expands to too many nodes (limit 2097152); possible alias bomb
15 / 18

Telling "too big" from "malformed"

def o as json.DecodeOptions;
$o.maxDepth = 32;
$o.maxNodes = 10000;          # options only lower a cap
try {
    $doc = json.decodeWith($text, $o);
} catch (e) {
    if ($e.kind == "limit") { ... }   # too big
    else { ... }                      # malformed: kind "runtime"
}
16 / 18

Which one when

17 / 18

Read more

Based on Jennifer 0.27.0, November 2026.

18 / 18