data: codecs/Json (RFC 8259 parse / serialize: ordered keys, Types.null, bigInts as strings, pretty / ascii, events pull tokenizer, decodeAt / lines, streaming writer); JSONTestSuite + Python json fixtures; bench lines
Types (backward compatible additions in the Data section): Types.object / isObject / set / keys (ordered objects and empty-object marking, shared by every data format), Types.Date (RFC 3339 typed dates for TOML / plist / CBOR / msgpack: new / is / valid / parse / toUnix / fromUnix), XmlNode gains optional text / target / ns fields. tools/bench.sh: also generates the json fixtures. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
ccb06775c5
commit
b6f40e0a74
10 files changed
+1818
-3
No files matched your search
@@ -0,0 +1,86 @@
|
||||
-- tests/data/Canon: the canonical text of a decoded data value, the same as tools/fixtures/_data_common.py canon() on
|
||||
-- Python's parse: null n, true t, false f, numbers #%d (integral under 2^53) or #%.17g, strings s<len>:<bytes>,
|
||||
-- buffers b<len>:<bytes>, arrays [x,...], objects {k=v,...} sorted by key text, dates d<rfc3339>. A module the data
|
||||
-- tests require (running it alone does nothing).
|
||||
local Types = require("@formats/Types")
|
||||
|
||||
local Canon = {}
|
||||
|
||||
local function num(x: number): string
|
||||
if x ~= x then
|
||||
return "#nan"
|
||||
elseif x == math.huge then
|
||||
return "#inf"
|
||||
elseif x == -math.huge then
|
||||
return "#-inf"
|
||||
elseif x == math.floor(x) and math.abs(x) < 2 ^ 53 then
|
||||
return string.format("#%d", x)
|
||||
end
|
||||
return "#" .. string.format("%.17g", x)
|
||||
end
|
||||
|
||||
local function isArray(t: { [any]: any }): boolean
|
||||
if Types.isObject(t) then
|
||||
return false
|
||||
end
|
||||
local n = #t
|
||||
local c = 0
|
||||
for _ in next, t do
|
||||
c += 1
|
||||
end
|
||||
return c == n
|
||||
end
|
||||
|
||||
local function canon(v: any, out: { string })
|
||||
local tv = type(v)
|
||||
if v == Types.null then
|
||||
table.insert(out, "n")
|
||||
elseif tv == "boolean" then
|
||||
table.insert(out, if v then "t" else "f")
|
||||
elseif tv == "number" then
|
||||
table.insert(out, num(v))
|
||||
elseif tv == "string" then
|
||||
table.insert(out, "s" .. #v .. ":" .. v)
|
||||
elseif tv == "buffer" then
|
||||
table.insert(out, "b" .. buffer.len(v) .. ":" .. buffer.tostring(v))
|
||||
elseif tv == "table" and Types.Date.is(v) then
|
||||
table.insert(out, "d" .. tostring(v))
|
||||
elseif tv == "table" then
|
||||
if isArray(v) then
|
||||
table.insert(out, "[")
|
||||
for i = 1, #v do
|
||||
canon(v[i], out)
|
||||
table.insert(out, ",")
|
||||
end
|
||||
table.insert(out, "]")
|
||||
else
|
||||
local items = {}
|
||||
for k, x in next, v do
|
||||
local ko = {}
|
||||
canon(k, ko)
|
||||
table.insert(items, { table.concat(ko), x })
|
||||
end
|
||||
table.sort(items, function(a, b)
|
||||
return a[1] < b[1]
|
||||
end)
|
||||
table.insert(out, "{")
|
||||
for _, it in items do
|
||||
table.insert(out, it[1] .. "=")
|
||||
canon(it[2], out)
|
||||
table.insert(out, ",")
|
||||
end
|
||||
table.insert(out, "}")
|
||||
end
|
||||
else
|
||||
table.insert(out, "?" .. tv)
|
||||
end
|
||||
end
|
||||
|
||||
-- of(v) -> the canonical text
|
||||
function Canon.of(v: any): string
|
||||
local out = {}
|
||||
canon(v, out)
|
||||
return table.concat(out)
|
||||
end
|
||||
|
||||
return Canon
|
||||
Reference in new issue
Block a user