diff --git a/docs/FORMATS.md b/docs/FORMATS.md
index 5da5c20..d9372b9 100644
--- a/docs/FORMATS.md
+++ b/docs/FORMATS.md
@@ -6,6 +6,8 @@ is tested under both runtimes (luau `--codegen -O2` and LuaJIT through the build
| Category | Format | Extensions | Read | Write | Capabilities | Test |
|---|---|---|---|---|---|---|
| image | png | .png .apng | yes | yes | animation: false, alpha: true, depths: 1, 2, 4, 8, 16 | `tests/image/png.luau` |
+| data | json | .json .jsonl .ndjson .geojson | yes | yes | ordered: true, streaming: true | `tests/data/json.luau` |
+| data | xml | .xml .xsd .xsl .xslt .rss .atom .xhtml | yes | yes | namespaces: true, streaming: true | `tests/data/xml.luau` |
Codecs (layer 1, shared by formats; not loadable on their own):
diff --git a/src/Formats/Registry.luau b/src/Formats/Registry.luau
index ba09f70..dafa3ee 100644
--- a/src/Formats/Registry.luau
+++ b/src/Formats/Registry.luau
@@ -26,6 +26,30 @@ local list: { Entry } = {
return require("@formats/image/png")
end,
},
+ {
+ name = "json",
+ category = "data",
+ path = "data/json",
+ extensions = { "json", "jsonl", "ndjson", "geojson" },
+ mime = { "application/json", "application/x-ndjson" },
+ deps = { "core/Err", "core/Bytes", "codecs/Json" },
+ caps = { decode = true, encode = true, ordered = true, streaming = true },
+ load = function()
+ return require("@formats/data/json")
+ end,
+ },
+ {
+ name = "xml",
+ category = "data",
+ path = "data/xml",
+ extensions = { "xml", "xsd", "xsl", "xslt", "rss", "atom", "xhtml" },
+ mime = { "application/xml", "text/xml" },
+ deps = { "core/Err", "core/Bytes", "Types", "codecs/Xml" },
+ caps = { decode = true, encode = true, namespaces = true, streaming = true },
+ load = function()
+ return require("@formats/data/xml")
+ end,
+ },
}
return list
diff --git a/src/Formats/data/json/init.luau b/src/Formats/data/json/init.luau
new file mode 100644
index 0000000..6b3e2fd
--- /dev/null
+++ b/src/Formats/data/json/init.luau
@@ -0,0 +1,88 @@
+--!strict
+-- data/json: JSON (RFC 8259) files, a thin wrapper over codecs/Json (which see for the value mapping and options).
+-- Supports: decode to plain Luau values (Types.null, ordered keys and bigInts as strings by option), encode compact or
+-- pretty. JSON Lines through codecs/Json.lines. Not supported: JSON5 / comments / trailing commas.
+
+-- ------------------------------------------------------------------------------------------------ requires
+local Err = require("@formats/core/Err")
+local Bytes = require("@formats/core/Bytes")
+local Json = require("@formats/codecs/Json")
+
+-- ------------------------------------------------------------------------------------------------ contract
+local JsonFormat = {
+ name = "json",
+ category = "data",
+ extensions = { "json", "jsonl", "ndjson", "geojson" },
+ mime = { "application/json", "application/x-ndjson" },
+ deps = { "core/Err", "core/Bytes", "codecs/Json" },
+ caps = { decode = true, encode = true, ordered = true, streaming = true },
+}
+
+export type DecodeOptions = Json.DecodeOptions & {
+ lines: boolean?, -- JSON Lines: decode every value into an array (default false: one value)
+}
+export type EncodeOptions = Json.EncodeOptions & {
+ lines: boolean?, -- JSON Lines: the value must be an array, written one compact value per line
+}
+
+-- ------------------------------------------------------------------------------------------------ sniff
+function JsonFormat.sniff(r: Bytes.Reader): number
+ local head = r:peek(64)
+ if string.sub(head, 1, 3) == "\239\187\191" then
+ head = string.sub(head, 4)
+ end
+ local first = string.match(head, "^[ \t\r\n]*(.)")
+ if first == "{" then
+ local nxt = string.match(head, "^[^{]*{[ \t\r\n]*(.)")
+ return if nxt == nil or nxt == '"' or nxt == "}" then 0.6 else 0.1
+ elseif first == "[" then
+ return 0.3
+ end
+ return 0
+end
+
+-- ------------------------------------------------------------------------------------------------ decode
+function JsonFormat.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
+ local s = buffer.readstring(r.buf, r.base, r.limit - r.base)
+ if opts and opts.lines then
+ local out = {}
+ for _, v, err in Json.lines(s, opts) do
+ if err then
+ return nil, err
+ end
+ table.insert(out, v)
+ end
+ return out, nil
+ end
+ return Json.decode(s, opts)
+end
+
+-- ------------------------------------------------------------------------------------------------ encode
+function JsonFormat.encode(value: any, opts: EncodeOptions?): (buffer?, Err.Err?)
+ if opts and opts.lines then
+ if type(value) ~= "table" then
+ return nil, Err.new(Err.ARG, "lines: expected an array of values")
+ end
+ local o = table.clone(opts) :: any
+ o.indent = nil
+ local parts = {}
+ for i, v in value do
+ local s, err = Json.encode(v, o)
+ if not s then
+ return nil, err
+ end
+ parts[i] = s .. "\n"
+ end
+ return buffer.fromstring(table.concat(parts)), nil
+ end
+ local s, err = Json.encode(value, opts)
+ if not s then
+ return nil, err
+ end
+ if opts and opts.indent then
+ s ..= "\n"
+ end
+ return buffer.fromstring(s), nil
+end
+
+return JsonFormat
diff --git a/src/Formats/data/xml/init.luau b/src/Formats/data/xml/init.luau
new file mode 100644
index 0000000..c992418
--- /dev/null
+++ b/src/Formats/data/xml/init.luau
@@ -0,0 +1,57 @@
+--!strict
+-- data/xml: XML 1.0 files, a thin wrapper over codecs/Xml (which see for the node shape and options). Supports:
+-- decode to a "#document" XmlNode tree (doc.root = the root element), encode a document or an element. Not
+-- supported: external entities / DTDs, validation.
+
+-- ------------------------------------------------------------------------------------------------ requires
+local Err = require("@formats/core/Err")
+local Bytes = require("@formats/core/Bytes")
+local Types = require("@formats/Types")
+local Xml = require("@formats/codecs/Xml")
+
+-- ------------------------------------------------------------------------------------------------ contract
+local XmlFormat = {
+ name = "xml",
+ category = "data",
+ extensions = { "xml", "xsd", "xsl", "xslt", "rss", "atom", "xhtml" },
+ mime = { "application/xml", "text/xml" },
+ deps = { "core/Err", "core/Bytes", "Types", "codecs/Xml" },
+ caps = { decode = true, encode = true, namespaces = true, streaming = true },
+}
+
+export type DecodeOptions = Xml.DecodeOptions
+export type EncodeOptions = Xml.EncodeOptions
+
+-- ------------------------------------------------------------------------------------------------ sniff
+function XmlFormat.sniff(r: Bytes.Reader): number
+ local head = r:peek(64)
+ if string.sub(head, 1, 3) == "\239\187\191" then
+ head = string.sub(head, 4)
+ end
+ if string.match(head, "^<%?xml[ \t\r\n]") or string.sub(head, 1, 4) == "\255\254<\0" or string.sub(head, 1, 4) == "\254\255\0<" then
+ return 0.5
+ end
+ if string.match(head, "^[ \t\r\n]*<[%a!]") then
+ return 0.3
+ end
+ return 0
+end
+
+-- ------------------------------------------------------------------------------------------------ decode
+function XmlFormat.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.XmlNode?, Err.Err?)
+ return Xml.decode(buffer.readstring(r.buf, r.base, r.limit - r.base), opts)
+end
+
+-- ------------------------------------------------------------------------------------------------ encode
+function XmlFormat.encode(node: Types.XmlNode, opts: EncodeOptions?): (buffer?, Err.Err?)
+ if type(node) ~= "table" or type(node.name) ~= "string" then
+ return nil, Err.new(Err.ARG, "expected an XmlNode")
+ end
+ local s, err = Xml.encode(node, opts)
+ if not s then
+ return nil, err
+ end
+ return buffer.fromstring(s), nil
+end
+
+return XmlFormat
diff --git a/tests/data/json.luau b/tests/data/json.luau
new file mode 100644
index 0000000..b313221
--- /dev/null
+++ b/tests/data/json.luau
@@ -0,0 +1,71 @@
+-- data/json tests: the contract, the format through the API (F.load / F.save / sniffing), JSON Lines, Python's
+-- json.dumps corpus through the format, and the fuzz pass. The codec itself is tested in tests/codecs/json.luau.
+-- Fixtures: tools/fixtures/json.py.
+local T = require("@tests/T")
+local F = require("@formats")
+local JsonF = require("@formats/data/json")
+local Bytes = require("@formats/core/Bytes")
+local Err = require("@formats/core/Err")
+local Canon = require("@tests/data/Canon")
+local Corpus = require("@tests/gen/json_corpus")
+
+local t = T.suite("data/json")
+
+t:eq(JsonF.name, "json", "name")
+t:eq(JsonF.category, "data", "category")
+t:check(JsonF.caps.decode and JsonF.caps.encode, "caps")
+
+local function dec(s: string, opts: any?): (any, any)
+ return JsonF.decode(Bytes.reader(buffer.fromstring(s)), opts)
+end
+
+for i, c in Corpus do
+ local v, err = dec(c.pretty)
+ t:check(v ~= nil, "corpus " .. i .. ": " .. tostring(err))
+ t:eq(Canon.of(v), c.canon, "corpus " .. i)
+ local b = JsonF.encode(v, { indent = 2 }) :: buffer
+ t:eq(buffer.tostring(b), c.pretty .. "\n", "corpus " .. i .. ": encode")
+end
+
+-- through the API: sniffed, by name, saved
+do
+ local v, err = F.load(buffer.fromstring('{"a": [1, 2], "b": null}'))
+ t:check(v ~= nil, "F.load sniffs json: " .. tostring(err))
+ t:eq(v.b, F.null, "F.null")
+ local v2 = F.load(buffer.fromstring("[1, 2]"), { format = "json" })
+ t:eq(v2[2], 2, "F.load format = json")
+ local s = F.saveString({ x = 1, y = { true, false } }, { format = "json" })
+ t:eq(s, '{"x":1,"y":[true,false]}', "F.saveString json")
+ local back = F.data.load(buffer.fromstring(s :: string), { format = "json" })
+ t:eq(back.y[1], true, "F.data.load")
+end
+
+-- JSON Lines
+do
+ local v = dec('{"a":1}\n{"a":2}\n\n[3]\n', { lines = true })
+ t:eq(#v, 3, "lines decode count")
+ t:eq(v[2].a, 2, "lines decode value")
+ local b = JsonF.encode({ { a = 1 }, { 2, 3 } }, { lines = true }) :: buffer
+ t:eq(buffer.tostring(b), '{"a":1}\n[2,3]\n', "lines encode")
+ local _, e = dec("1\n[", { lines = true })
+ t:eq(e and e.code, Err.TRUNCATED, "lines error")
+end
+
+-- reader range: the data inside a bigger buffer
+do
+ local big = buffer.fromstring("xx[1,2,3]yy")
+ local v = JsonF.decode(Bytes.reader(big, 2, 7))
+ t:eq(#v, 3, "reader range")
+end
+
+t:eq(JsonF.sniff(Bytes.reader(buffer.fromstring(' {"a":1}'))), 0.6, "sniff object")
+t:eq(JsonF.sniff(Bytes.reader(buffer.fromstring("hello"))), 0, "sniff text")
+
+local samples = {}
+for i = 1, 30 do
+ table.insert(samples, Corpus[i].pretty)
+end
+t:fuzz("decode", function(b: buffer)
+ return JsonF.decode(Bytes.reader(b))
+end, samples)
+t:done()
diff --git a/tests/data/xml.luau b/tests/data/xml.luau
new file mode 100644
index 0000000..6edbe02
--- /dev/null
+++ b/tests/data/xml.luau
@@ -0,0 +1,49 @@
+-- data/xml tests: the contract, the format through the API (F.load / F.save / sniffing), documents from Python's
+-- ElementTree fixtures decoded and re-encoded through the format, and the fuzz pass. The codec itself is tested in
+-- tests/codecs/xml.luau. Fixtures: tools/fixtures/xml.py.
+local T = require("@tests/T")
+local F = require("@formats")
+local XmlF = require("@formats/data/xml")
+local Xml = require("@formats/codecs/Xml")
+local Bytes = require("@formats/core/Bytes")
+local Err = require("@formats/core/Err")
+local Docs = require("@tests/gen/xml_docs")
+
+local t = T.suite("data/xml")
+
+t:eq(XmlF.name, "xml", "name")
+t:eq(XmlF.category, "data", "category")
+t:check(XmlF.caps.decode and XmlF.caps.encode, "caps")
+
+for i = 1, 60 do
+ local d = Docs[i]
+ local doc, err = XmlF.decode(Bytes.reader(buffer.fromstring(d.data)), { comments = true, cdata = true, whitespace = true })
+ t:check(doc ~= nil, "doc " .. i .. ": " .. tostring(err))
+ local b, eerr = XmlF.encode(doc :: any)
+ t:check(b ~= nil, "doc " .. i .. ": encode " .. tostring(eerr))
+ local again = XmlF.decode(Bytes.reader(b :: buffer), { comments = true, cdata = true, whitespace = true })
+ t:check(again ~= nil, "doc " .. i .. ": re-decode")
+ t:eq(Xml.text((again :: any).root), Xml.text((doc :: any).root), "doc " .. i .. ": same text")
+end
+
+do
+ local doc, err = F.load(buffer.fromstring('t'))
+ t:check(doc ~= nil, "F.load sniffs xml: " .. tostring(err))
+ t:eq(doc.root.name, "r", "root")
+ local s = F.saveString(doc, { format = "xml" })
+ t:eq(s, '\nt', "F.saveString")
+ local _, e = XmlF.encode(5 :: any)
+ t:eq(e and e.code, Err.ARG, "encode argument check")
+ local _, e2 = F.load(buffer.fromstring(""), { format = "xml" })
+ t:eq(e2 and e2.code, Err.CORRUPT, "error through the API")
+ t:eq(e2 and e2.format, "xml", "error tagged with the format")
+end
+
+local samples = {}
+for i = 1, 30 do
+ table.insert(samples, Docs[i].data)
+end
+t:fuzz("decode", function(b: buffer)
+ return XmlF.decode(Bytes.reader(b))
+end, samples)
+t:done()