rename: src/Formats -> src/Rosetta, @formats -> @rosetta requires, build/luajit/rosetta + rosetta._compat, build/roblox/Rosetta, ROSETTA_* env/globals, writer vendor strings
This commit is contained in:
1 parent
2956576d7f
commit
22ddcd78fa
941 files changed
+3364
-3364
No files matched your search
@@ -0,0 +1,91 @@
|
||||
--!strict
|
||||
-- data/json: JSON (RFC 8259) files, a thin wrapper over codecs/text/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/text/Json.lines. Not supported: JSON5 / comments / trailing commas.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
local Sniff = require("@rosetta/core/Sniff")
|
||||
local Bytes = require("@rosetta/core/Bytes")
|
||||
local Json = require("@rosetta/codecs/text/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/text/Json", "core/Sniff" },
|
||||
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 = Sniff.text(r, 64)
|
||||
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
|
||||
-- past any nested '[': a JSON value must start (TOML / INI "[section]" and "[[table]]" do not)
|
||||
local nxt = string.match(head, "^[ \t\r\n]*[%[ \t\r\n]*(.)")
|
||||
if nxt == nil or string.find('{"-0123456789tfn]', nxt, 1, true) then
|
||||
return 0.3
|
||||
end
|
||||
return 0
|
||||
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
|
||||
Reference in new issue
Block a user