Reading and writing JSON, YAML, TOML, XML
The other four codecs — and what happens when a value doesn't fit.
Doc.from_* reads a format into a Document; Doc.to_* writes one back out. JSON, YAML, TOML, and XML all read into the exact same Document that OML does — converting between any of the five is just read one, write another:
from omnist import Doc
Doc.from_json('{"name": "Ann", "tags": ["x", "y"]}').to_toml()
Doc.from_yaml("name: Ann\n").to_json()to_toml():
name = "Ann"
tags = [
"x",
"y",
]
to_json(): {"name": "Ann"}Writing is lenient by default — a value a format can't hold the way it was typed (JSON/XML have no native date type) gets adjusted, and the adjustment is recorded, not silently lost:
from omnist import doc, WriteReport, WriteError
import datetime
d = doc({"d": datetime.date(2024, 1, 1)})
d.to_json() # stringified, still succeeds
rep = WriteReport()
d.to_json(report=rep) # inspect what changed
[(a.code, a.severity) for a in rep]
d.to_json(strict=True) # raises instead of adjustingto_json(): {"d": "2024-01-01"}
report: [('temporal.stringified', 'warning')]
strict raised WriteError: warning: $.d: temporal value written as an ISO-8601 stringA value with no legal representation at all (TOML has no null, and there's no safe substitute) fails unconditionally instead of guessing — check_json/check_yaml/check_toml/check_xml (and the matching Doc methods) simulate a write and return the report without producing output, so you can ask "would this be lossy?" without writing anything:
d.check_json()warning: $.d: temporal value written as an ISO-8601 string