-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjson.d.lua
More file actions
148 lines (143 loc) · 3.47 KB
/
Copy pathjson.d.lua
File metadata and controls
148 lines (143 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
---@meta _
---@class mods.json.null
---
---JSON encoding and decoding helpers.
---
---> [!NOTE]
--->
--->This module aims to implement strict
--->[RFC 8259](https://www.rfc-editor.org/rfc/rfc8259) JSON behavior.
---
---## Usage
---
---```lua
---local json = mods.json
---
---local encoded = json.encode({ ok = true, value = 42 })
---local decoded = json.decode(encoded)
---
---print(encoded) --> {"ok":true,"value":42}
---print(decoded.ok) --> true
---print(decoded.value) --> 42
---```
---
---## Behavior
---
---* booleans, strings, and finite numbers map directly to JSON values
---
---```lua
---print(json.encode(true)) --> true
---print(json.encode("hi")) --> "hi"
---print(json.encode(12.5)) --> 12.5
---```
---
---* `json.null` encodes to `null`, and decoded `null` becomes `json.null`
---
---```lua
---local value = json.decode('{"a":null}')
---print(value.a == json.null) --> true
---print(json.encode(json.null)) --> null
---```
---
---* tables encode as arrays or objects based on their keys
---
---```lua
---print(json.encode({})) --> []
---print(json.encode({ "a", "b" })) --> ["a","b"]
---print(json.encode({ a = 1 })) --> {"a":1}
---```
---
---* standalone JSON values like booleans, strings, and numbers are valid
---
---```lua
---print(json.decode("true")) --> true
---print(json.decode('"text"')) --> text
---print(json.decode("42")) --> 42
---```
---
---* encoding rejects unsupported Lua types, cyclic tables, mixed tables, sparse arrays, `NaN`, and infinities
---
---```lua
---local t = {}
---t.self = t
---
---assert(json.encode(function() end))
-----> unsupported type: function
---
---assert(json.encode(t))
-----> cannot encode cyclic table
---
---assert(json.encode({ [1] = "a", b = true }))
-----> cannot encode mixed table
---
---assert(json.encode({ [1] = "a", [3] = "c" }))
-----> cannot encode sparse array
---
---assert(json.encode(0 / 0))
-----> cannot encode NaN
---
---assert(json.encode(math.huge))
-----> cannot encode infinity
---```
---
---- decoding rejects comments, trailing commas, single-quoted strings, and invalid escapes
---
---```lua
---assert(json.decode('{"a":1,}'))
-----> expected string key at line 1, column 8
---
---assert(json.decode('{"a":1}// comment'))
-----> unexpected trailing content at line 1, column 8
---
---assert(json.decode("{'a':1}"))
-----> expected string key at line 1, column 2
---
---assert(json.decode('["\\x"]'))
-----> invalid escape sequence at line 1, column 3
---```
---
---@class mods.json
---@field null mods.json.null Sentinel used to represent JSON `null` in Lua.
local M = {}
---
---Encode a Lua value as JSON.
---
---```lua
---local s = json.encode({
--- ok = true,
--- items = { 1, 2, 3 },
---}, {
--- indent = " ",
---})
---
---print(s)
----- {
----- "ok": true,
----- "items": [
----- 1,
----- 2,
----- 3
----- ]
----- }
---```
---
---@param value any Lua value to encode.
---@param opts? {sort_keys?:boolean, indent?:string} Encoding options.
---@return string? json JSON string.
---@return string? err Error message when encoding fails.
function M.encode(value, opts) end
---
---Decode a JSON string into Lua values.
---
---```lua
---local value = json.decode('{"user":"Ada","active":true,"note":null}')
---print(value.user) --> Ada
---print(value.active) --> true
---print(value.note == json.null) --> true
---```
---
---@param s string JSON string.
---@return any value Decoded Lua value.
---@return string? err Error message when decoding fails.
function M.decode(s) end
return M