89 lines
3.0 KiB
Luau
89 lines
3.0 KiB
Luau
--!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
|