Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort keys when encoding tables as JSON #11124

Merged
merged 5 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions news/changelog-1.6.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ All changes included in 1.6:
- ([#10858](https://github.com/quarto-dev/quarto-cli/issues/10858)): Don't crash in `gfm` when `content` of a `FloatRefTarget` is of type `Blocks`.
- ([#10894](https://github.com/quarto-dev/quarto-cli/issues/10894)): Fix configuration of title and prefix in callouts for `html`, `revealjs`, `pdf`, and `typst`.
- ([#10999](https://github.com/quarto-dev/quarto-cli/issues/10999)): New API entry point: `quarto.paths.rscript()` to resolve `Rscript` path in Lua filters and extensions consistently with Quarto itself.
- ([#11124](https://github.com/quarto-dev/quarto-cli/pull/11124)): Sort keys when encoding tables as JSON

## `dashboard` Format

Expand All @@ -29,6 +30,7 @@ All changes included in 1.6:
- Fix `kbd` element styling on dark themes.
- ([#10761](https://github.com/quarto-dev/quarto-cli/issues/10761)): Add support for `licence: CC0` to automatically link to Creative Commons licence [CC0 1.0](https://creativecommons.org/publicdomain/zero/1.0/).
- ([#10817](https://github.com/quarto-dev/quarto-cli/issues/10817)): Ensure that user provided SCSS has precedent over quarto generated scss also for dark theme.
- ([#11124](https://github.com/quarto-dev/quarto-cli/pull/11124)): Use stable order of GLightbox options
cscheid marked this conversation as resolved.
Show resolved Hide resolved

## `revealjs` Format

Expand Down
48 changes: 32 additions & 16 deletions src/resources/pandoc/datadir/_json.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
-- Copyright (c) 2020 rxi
-- https://github.com/rxi/json.lua
--
-- 2023-02-08: Modified by RStudio, PBC to make encoding more robust under the
-- following example: encode(decode("[null, 'test']"))
-- includes unreleased upstream changes: https://github.com/rxi/json.lua/blob/dbf4b2dd2eb7c23be2773c89eb059dadd6436f94/json.lua
-- includes unmerged upstream pull request: https://github.com/rxi/json.lua/pull/51
-- includes unmerged upstream pull request: https://github.com/rxi/json.lua/pull/52
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy of
-- this software and associated documentation files (the "Software"), to deal in
Expand All @@ -26,7 +27,22 @@
-- SOFTWARE.
--

local json = { _version = "0.1.2" }
local json = { _version = "0.1.2-quarto" }
cscheid marked this conversation as resolved.
Show resolved Hide resolved

-- taken from https://www.lua.org/pil/19.3.html
function pairsByKeys (t, f)
local a = {}
for n in pairs(t) do table.insert(a, n) end
table.sort(a, f)
local i = 0 -- iterator variable
local iter = function () -- iterator function
i = i + 1
if a[i] == nil then return nil
else return a[i], t[a[i]]
end
end
return iter
end

-------------------------------------------------------------------------------
-- Encode
Expand Down Expand Up @@ -72,7 +88,10 @@ local function encode_table(val, stack)

stack[val] = true

local n = 0
cscheid marked this conversation as resolved.
Show resolved Hide resolved
if next(val) == nil then
return '[]'
cscheid marked this conversation as resolved.
Show resolved Hide resolved
end

local types = {}

for k in pairs(val) do
Expand Down Expand Up @@ -101,13 +120,13 @@ local function encode_table(val, stack)
return "[" .. table.concat(res, ",") .. "]"
elseif types["string"] then
-- Treat as object
for k, v in pairs(val) do
for k, v in pairsByKeys(val) do
table.insert(res, encode_string(k) .. ":" .. encode(v, stack))
end
stack[val] = nil
return "{" .. table.concat(res, ",") .. "}"
else
return "[]"
error( string.format("invalid table: unsupported key type %s", types[1]) )
cscheid marked this conversation as resolved.
Show resolved Hide resolved
end
end

Expand Down Expand Up @@ -139,7 +158,7 @@ encode = function(val, stack)
end


local function jsonEncode(val)
function json.encode(val)
return ( encode(val) )
end

Expand Down Expand Up @@ -205,7 +224,7 @@ local function codepoint_to_utf8(n)
return string.char(f(n / 4096) + 224, f(n % 4096 / 64) + 128, n % 64 + 128)
elseif n <= 0x10ffff then
return string.char(f(n / 262144) + 240, f(n % 262144 / 4096) + 128,
f(n % 4096 / 64) + 128, n % 64 + 128)
f(n % 4096 / 64) + 128, n % 64 + 128)
end
error( string.format("invalid unicode codepoint '%x'", n) )
end
Expand All @@ -214,7 +233,7 @@ end
local function parse_unicode_escape(s)
local n1 = tonumber( s:sub(1, 4), 16 )
local n2 = tonumber( s:sub(7, 10), 16 )
-- Surrogate pair?
-- Surrogate pair?
if n2 then
return codepoint_to_utf8((n1 - 0xd800) * 0x400 + (n2 - 0xdc00) + 0x10000)
else
Expand All @@ -240,8 +259,8 @@ local function parse_string(str, i)
local c = str:sub(j, j)
if c == "u" then
local hex = str:match("^[dD][89aAbB]%x%x\\u%x%x%x%x", j + 1)
or str:match("^%x%x%x%x", j + 1)
or decode_error(str, j - 1, "invalid unicode escape in string")
or str:match("^%x%x%x%x", j + 1)
or decode_error(str, j - 1, "invalid unicode escape in string")
res = res .. parse_unicode_escape(hex)
j = j + #hex
else
Expand Down Expand Up @@ -380,7 +399,7 @@ parse = function(str, idx)
end


local function jsonDecode(str)
function json.decode(str)
if type(str) ~= "string" then
error("expected argument of type string, got " .. type(str))
end
Expand All @@ -393,7 +412,4 @@ local function jsonDecode(str)
end


return {
encode = jsonEncode,
decode = jsonDecode
}
return json
Loading