data: data/json and data/xml (thin wrappers over codecs/Json / codecs/Xml; JSON Lines option; API tests)
This commit is contained in:
1 parent
a1edea0e44
commit
eeb62192b5
6 files changed
+291
No files matched your search
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
Reference in new issue
Block a user