Skip to content

Commit 3ad1094

Browse files
author
Roman Atachiants
authored
Wrap recursively arrays for json.array() (#18)
1 parent f53bc7b commit 3ad1094

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

fixtures/array.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ function main()
99
["str"] = json.array("hello"),
1010
["int"] = json.array(12),
1111
["bool"] = json.array(true),
12-
["float"] = json.array(12.34)
12+
["float"] = json.array(12.34),
13+
["empties"] = json.array(json.array())
1314
}
1415
end

json/json.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ func apiArray(state *lua.LState) int {
8484
}
8585
}
8686

87+
// Wrap user data in an array
88+
if custom, ok := state.CheckAny(1).(*lua.LUserData); ok {
89+
state.Push(&lua.LUserData{Value: []any{custom.Value}})
90+
return 1
91+
}
92+
8793
// Otherwise, return an array
8894
fallthrough
8995
default:

script_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ func TestEmptyArray(t *testing.T) {
307307
"str": ["hello"],
308308
"int": [12],
309309
"bool": [true],
310-
"float": [12.34]
310+
"float": [12.34],
311+
"empties": [[]]
311312
}`, string(b))
312-
313313
}

0 commit comments

Comments
 (0)