diff options
| author | ruki <[email protected]> | 2025-11-14 23:34:02 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2025-11-14 23:34:02 +0800 |
| commit | d066068a571d6e6515e56270374e96518d20eaf0 (patch) | |
| tree | 6d96a1dddf03ea86c897b9268db9366abf71868a | |
| parent | 8d9b5970ed3f44a84f5e7ed5f7f140e72a9f1e97 (diff) | |
fix test
| -rwxr-xr-x | tests/modules/json/test.lua | 14 | ||||
| -rw-r--r-- | xmake/core/base/json.lua | 8 | ||||
| -rw-r--r-- | xmake/plugins/show/info/basic.lua | 2 | ||||
| -rw-r--r-- | xmake/plugins/show/info/target.lua | 2 | ||||
| -rw-r--r-- | xmake/plugins/show/showlist.lua | 1 |
5 files changed, 19 insertions, 8 deletions
diff --git a/tests/modules/json/test.lua b/tests/modules/json/test.lua index b0e88112b..6bd89389c 100755 --- a/tests/modules/json/test.lua +++ b/tests/modules/json/test.lua @@ -11,12 +11,16 @@ function json_encode(luatable, opt) return json.encode(luatable, opt) end -function json_pure_decode(jsonstr) - return json.decode(jsonstr, {pure = true}) +function json_pure_decode(jsonstr, opt) + opt = opt or {} + opt.pure = true + return json.decode(jsonstr, opt) end -function json_pure_encode(luatable) - return json.encode(luatable, {pure = true}) +function json_pure_encode(luatable, opt) + opt = opt or {} + opt.pure = true + return json.encode(luatable, opt) end function test_json_decode(t) @@ -78,5 +82,5 @@ function test_pure_json_encode(t) " ]", "}" }, "\n") - t:are_equal(json.encode({name = "xmake", targets = {"foo", "bar"}}, {pure = true, pretty = true, indent = 4}), pretty_expected) + t:are_equal(json_pure_encode({name = "xmake", targets = {"foo", "bar"}}, {pretty = true, indent = 4}), pretty_expected) end diff --git a/xmake/core/base/json.lua b/xmake/core/base/json.lua index 2ed363461..760adefa0 100644 --- a/xmake/core/base/json.lua +++ b/xmake/core/base/json.lua @@ -24,6 +24,7 @@ local json = json or {} -- load modules local io = require("base/io") local os = require("base/os") +local table = require("base/table") local utils = require("base/utils") -- export null @@ -134,6 +135,10 @@ function json._pure_stringify(obj, level, as_key, opt) opt = opt or {} level = level or 0 local pretty = opt.pretty + local orderkeys = opt.orderkeys + if orderkeys == nil and pretty then + orderkeys = true + end local indent_step = pretty and (opt.indent or 4) or nil local newline = pretty and "\n" or "" local curr_indent = indent_step and string.rep(" ", indent_step * level) or nil @@ -172,7 +177,8 @@ function json._pure_stringify(obj, level, as_key, opt) end s[#s + 1] = '{' local first = true - for k, v in pairs(obj) do + local iter = orderkeys and table.orderpairs or pairs + for k, v in iter(obj) do if not first then s[#s + 1] = ',' end diff --git a/xmake/plugins/show/info/basic.lua b/xmake/plugins/show/info/basic.lua index 47b0f72e7..4ad14fe98 100644 --- a/xmake/plugins/show/info/basic.lua +++ b/xmake/plugins/show/info/basic.lua @@ -140,7 +140,7 @@ function main() if opt.json then local json_opt if opt.pretty then - json_opt = {pretty = true} + json_opt = {pretty = true, orderkeys = true} end print(json.encode(result or {}, json_opt)) end diff --git a/xmake/plugins/show/info/target.lua b/xmake/plugins/show/info/target.lua index 67f9c4a69..62d8b6ddd 100644 --- a/xmake/plugins/show/info/target.lua +++ b/xmake/plugins/show/info/target.lua @@ -373,7 +373,7 @@ function main(name) info.api_entries = nil local json_opt if opt.pretty then - json_opt = {pretty = true} + json_opt = {pretty = true, orderkeys = true} end print(json.encode(info or {}, json_opt)) else diff --git a/xmake/plugins/show/showlist.lua b/xmake/plugins/show/showlist.lua index e82a3ab52..205ae21ed 100644 --- a/xmake/plugins/show/showlist.lua +++ b/xmake/plugins/show/showlist.lua @@ -43,6 +43,7 @@ function _show_json(values) local opt = {} if option.get("pretty") then opt.pretty = true + opt.orderkeys = true end print(json.encode(values, opt)) end |
