summaryrefslogtreecommitdiff
path: root/meta/3rd/OpenResty/library/cjson.lua
blob: aeef436a6d4368194fecdd985904f3095784f823 (plain)
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
---@meta

--- lua cjson
---
--- https://kyne.com.au/~mark/software/lua-cjson.php
--- https://kyne.com.au/~mark/software/lua-cjson-manual.html
--- https://openresty.org/en/lua-cjson-library.html
---
--- **NOTE:** This includes the additions in the OpenResty-maintained cjson fork: https://github.com/openresty/lua-cjson
---
--- ---
---
--- The cjson module will throw an error during JSON conversion if any invalid
--- data is encountered. Refer to `cjson.encode` and cjson.decode for details.
---
--- The cjson.safe module behaves identically to the cjson module, except when
--- errors are encountered during JSON conversion. On error, the cjson_safe.encode
--- and cjson_safe.decode functions will return nil followed by the error message.
---
--- cjson.new can be used to instantiate an independent copy of the Lua CJSON
--- module. The new module has a separate persistent encoding buffer, and default settings.
---
--- Lua CJSON can support Lua implementations using multiple preemptive threads
--- within a single Lua state provided the persistent encoding buffer is not
--- shared. This can be achieved by one of the following methods:
---
---  * Disabling the persistent encoding buffer with cjson.encode_keep_buffer
---  * Ensuring each thread calls cjson.encode separately (ie, treat cjson.encode as non-reentrant).
---  * Using a separate cjson module table per preemptive thread (cjson.new)
---
--- ## Note
---
--- Lua CJSON uses `strtod` and `snprintf` to perform numeric conversion as they
--- are usually well supported, fast and bug free. However, these functions
--- require a workaround for JSON encoding/parsing under locales using a comma
--- decimal separator. Lua CJSON detects the current locale during instantiation
--- to determine and automatically implement the workaround if required. Lua
--- CJSON should be reinitialised via cjson.new if the locale of the current
--- process changes. Using a different locale per thread is not supported.
---
---@class cjson
---@field _NAME          string
---@field _VERSION       string
---@field null           cjson.null
---@field empty_array    cjson.empty_array
---@field array_mt       cjson.array_mt
---@field empty_array_mt cjson.empty_array_mt
---
local cjson = {}


--- A metatable which can "tag" a table as a JSON Array in case it is empty (that
--- is, if the table has no elements, `cjson.encode()` will encode it as an empty
--- JSON Array).
---
--- Instead of:
---
---```lua
--- local function serialize(arr)
---     if #arr < 1 then
---         arr = cjson.empty_array
---     end
---
---     return cjson.encode({some_array = arr})
--- end
---```
---
--- This is more concise:
---
---```lua
--- local function serialize(arr)
---     setmetatable(arr, cjson.empty_array_mt)
---
---     return cjson.encode({some_array = arr})
--- end
---```
---
--- Both will generate:
---
---```json
--- {
---     "some_array": []
--- }
---```
---
--- **NOTE:** This field is specific to the OpenResty cjson fork.
---
---@alias cjson.empty_array_mt table


--- When lua-cjson encodes a table with this metatable, it will systematically
--- encode it as a JSON Array. The resulting, encoded Array will contain the
--- array part of the table, and will be of the same length as the `#` operator
--- on that table. Holes in the table will be encoded with the null JSON value.
---
--- ## Example
---
---```lua
--- local t = { "hello", "world" }
--- setmetatable(t, cjson.array_mt)
--- cjson.encode(t) -- ["hello","world"]
---```
---
--- Or:
---
---```lua
--- local t = {}
--- t[1] = "one"
--- t[2] = "two"
--- t[4] = "three"
--- t.foo = "bar"
--- setmetatable(t, cjson.array_mt)
--- cjson.encode(t) -- ["one","two",null,"three"]
---```
---
--- **NOTE:** This field is specific to the OpenResty cjson fork.
---
--- This value was introduced in the 2.1.0.5 release of this module.
---
---@alias cjson.array_mt table


--- Sentinel type that denotes a JSON `null` value
---@alias cjson.null lightuserdata


--- A lightuserdata, similar to `cjson.null`, which will be encoded as an empty
--- JSON Array by `cjson.encode()`.
---
--- For example, since encode_empty_table_as_object is true by default:
---
---```lua
--- local cjson = require "cjson"
---
--- local json = cjson.encode({
---     foo = "bar",
---     some_object = {},
---     some_array = cjson.empty_array
--- })
---```
---
--- This will generate:
---
---```json
--- {
---     "foo": "bar",
---     "some_object": {},
---     "some_array": []
--- }
---```
---
--- **NOTE:** This field is specific to the OpenResty cjson fork.
---
---@alias cjson.empty_array lightuserdata


--- unserialize a json string to a lua value
---
--- `cjson.decode` will deserialise any UTF-9 JSON string into a Lua value or table.
---
--- UTF-16 and UTF-32 JSON strings are not supported.
---
--- cjson.decode requires that any NULL (ASCII 0) and double quote (ASCII 34) characters are escaped within strings. All escape codes will be decoded and other bytes will be passed transparently. UTF-8 characters are not validated during decoding and should be checked elsewhere if required.
---
--- JSON null will be converted to a NULL lightuserdata value. This can be compared with `cjson.null` for convenience.
---
--- By default, numbers incompatible with the JSON specification (infinity, NaN, hexadecimal) can be decoded. This default can be changed with `cjson.decode_invalid_numbers()`.
---
--- ```lua
--- local json_text '[ true, { "foo": "bar" } ]'
--- cjson.decode(json_text) --> { true, { foo = "bar" } }
--- ```
---
---@param json string
---@return any
function cjson.decode(json) end


--- decode_invalid_numbers
---
--- Lua CJSON may generate an error when trying to decode numbers not supported
--- by the JSON specification. Invalid numbers are defined as:
---
---  * infinity
---  * not-a-number (NaN)
---  * hexadecimal
---
--- Available settings:
---
---  * `true`: Accept and decode invalid numbers. This is the default setting.
---  * `false`:  Throw an error when invalid numbers are encountered.
---
--- The current setting is always returned, and is only updated when an argument is provided.
---
---@param setting? boolean # (default: `true`)
---@return boolean setting # the value of the current setting
function cjson.decode_invalid_numbers(setting) end


--- decode_max_depth
---
--- Lua CJSON will generate an error when parsing deeply nested JSON once the
--- maximum array/object depth has been exceeded.  This check prevents
--- unnecessarily complicated JSON from slowing down the application, or crashing
--- the application due to lack of process stack space.
---
--- An error may be generated before the depth limit is hit if Lua is unable to
--- allocate more objects on the Lua stack.
---
--- By default, Lua CJSON will reject JSON with arrays and/or objects nested
--- more than 1000 levels deep.
---
--- The current setting is always returned, and is only updated when an argument is provided.
---
---@param depth? integer # must be positive (default `1000`)
---@return integer depth
function cjson.decode_max_depth(depth) end


--- decode_array_with_array_mt
---
--- If enabled, JSON Arrays decoded by cjson.decode will result in Lua tables
--- with the array_mt metatable. This can ensure a 1-to-1 relationship between
--- arrays upon multiple encoding/decoding of your JSON data with this module.
---
--- If disabled, JSON Arrays will be decoded to plain Lua tables, without the
--- `cjson.array_mt` metatable.
---
--- ## Example
---
---```lua
--- local cjson = require "cjson"
---
--- -- default behavior
--- local my_json = [[{"my_array":[]}]]
--- local t = cjson.decode(my_json)
--- cjson.encode(t) -- {"my_array":{}} back to an object
---
--- -- now, if this behavior is enabled
--- cjson.decode_array_with_array_mt(true)
---
--- local my_json = [[{"my_array":[]}]]
--- local t = cjson.decode(my_json)
--- cjson.encode(t) -- {"my_array":[]} properly re-encoded as an array
---```
---
--- **NOTE:** This function is specific to the OpenResty cjson fork.
---
---@param enabled boolean # (default: false)
function cjson.decode_array_with_array_mt(enabled) end


--- serialize a lua value to a json string
---
--- cjson.encode will serialise a Lua value into a string containing the JSON representation.
---
--- cjson.encode supports the following types:
---
---  * boolean
---  * lightuserdata (NULL value only)
---  * nil
---  * number
---  * string
---  * table
---
--- The remaining Lua types will generate an error:
---
---  * function
---  * lightuserdata (non-NULL values)
---  * thread
---  * userdata
---
--- By default, numbers are encoded with 14 significant digits. Refer to cjson.encode_number_precision for details.
---
--- Lua CJSON will escape the following characters within each UTF-8 string:
---
---  * Control characters (ASCII 0 - 31)
---  * Double quote (ASCII 34)
---  * Forward slash (ASCII 47)
---  * Blackslash (ASCII 92)
---  * Delete (ASCII 127)
---
--- All other bytes are passed transparently.
---
---
--- ## Caution
---
--- Lua CJSON will successfully encode/decode binary strings, but this is technically not supported by JSON and may not be compatible with other JSON libraries. To ensure the output is valid JSON, applications should ensure all Lua strings passed to cjson.encode are UTF-8.
---
--- ---
--- Base64 is commonly used to encode binary data as the most efficient encoding under UTF-8 can only reduce the encoded size by a further ~8%. Lua Base64 routines can be found in the LuaSocket and lbase64 packages.
---
--- Lua CJSON uses a heuristic to determine whether to encode a Lua table as a JSON array or an object. A Lua table with only positive integer keys of type number will be encoded as a JSON array. All other tables will be encoded as a JSON object.
---
--- Lua CJSON does not use metamethods when serialising tables.
---
---  * `rawget()` is used to iterate over Lua arrays
---  * `next()` is used to iterate over Lua objects
---
--- Lua arrays with missing entries (sparse arrays) may optionally be encoded in
--- several different ways. Refer to cjson.encode_sparse_array for details.
---
--- JSON object keys are always strings. Hence cjson.encode only supports table
--- keys which are type number or string. All other types will generate an error.
---
--- ## Note
--- 	Standards compliant JSON must be encapsulated in either an object ({}) or an array ([]). If strictly standards compliant JSON is desired, a table must be passed to cjson.encode.
---
--- ---
---
--- By default, encoding the following Lua values will generate errors:
---
---  * Numbers incompatible with the JSON specification (infinity, NaN)
---  * Tables nested more than 1000 levels deep
---  * Excessively sparse Lua arrays
---
--- These defaults can be changed with:
---
---  * cjson.encode_invalid_numbers
---  * cjson.encode_max_depth
---  * cjson.encode_sparse_array
---
---```lua
--- local value = { true, { foo = "bar" } }
--- cjson.encode(value) --> '[true,{"foo":"bar"}]'
---```
---
---@param value any
---@return string json
function cjson.encode(value) end


--- encode_invalid_numbers
---
--- Lua CJSON may generate an error when encoding floating point numbers not
--- supported by the JSON specification (invalid numbers):
---
---  * infinity
---  * not-a-number (NaN)
---
--- Available settings:
---
--- * `true`: Allow invalid numbers to be encoded. This will generate non-standard JSON, but this output is supported by some libraries.
--- * `false`: Throw an error when attempting to encode invalid numbers. This is the default setting.
--- * `"null"`: Encode invalid numbers as a JSON null value. This allows infinity and NaN to be encoded into valid JSON.
---
--- The current setting is always returned, and is only updated when an argument is provided.
---
---@param setting? boolean|'"null"'
---@return boolean|'"null"' setting
function cjson.encode_invalid_numbers(setting) end


--- encode_keep_buffer
---
--- Lua CJSON can reuse the JSON encoding buffer to improve performance.
---
--- Available settings:
---
--- * `true` - The buffer will grow to the largest size required and is not freed until the Lua CJSON module is garbage collected. This is the default setting.
--- * `false` - Free the encode buffer after each call to cjson.encode.
---
--- The current setting is always returned, and is only updated when an argument is provided.
---
---@param setting? boolean
---@return boolean setting
function cjson.encode_keep_buffer(setting) end


--- encode_max_depth
---
--- Once the maximum table depth has been exceeded Lua CJSON will generate an
--- error. This prevents a deeply nested or recursive data structure from
--- crashing the application.
---
--- By default, Lua CJSON will generate an error when trying to encode data
--- structures with more than 1000 nested tables.
---
--- The current setting is always returned, and is only updated when an argument is provided.
---
---@param depth? integer # must be positive (default `1000`)
---@return integer depth
function cjson.encode_max_depth(depth) end


--- encode_number_precision
---
--- The amount of significant digits returned by Lua CJSON when encoding numbers
--- can be changed to balance accuracy versus performance. For data structures
--- containing many numbers, setting cjson.encode_number_precision to a smaller
--- integer, for example 3, can improve encoding performance by up to 50%.
---
--- By default, Lua CJSON will output 14 significant digits when converting a number to text.
---
--- The current setting is always returned, and is only updated when an argument is provided.
---
--- **NOTE:** The maximum value of 16 is only supported by the OpenResty cjson
--- fork. The maximum in the upstream mpx/lua-cjson module is 14.
---
---@param precision? integer # must be between 1 and 16
---@return integer precision
function cjson.encode_number_precision(precision) end


--- encode_sparse_array
---
--- Lua CJSON classifies a Lua table into one of three kinds when encoding a JSON array. This is determined by the number of values missing from the Lua array as follows:
---
---  * Normal - All values are available.
---  * Sparse - At least 1 value is missing.
---  * Excessively sparse - The number of values missing exceeds the configured ratio.
---
--- Lua CJSON encodes sparse Lua arrays as JSON arrays using JSON null for the missing entries.
---
--- An array is excessively sparse when all the following conditions are met:
---
---  * ratio > 0
---  * maximum_index > safe
---  * maximum_index > item_count * ratio
---
--- Lua CJSON will never consider an array to be excessively sparse when ratio = 0.
--- The safe limit ensures that small Lua arrays are always encoded as sparse arrays.
---
--- By default, attempting to encode an excessively sparse array will generate
--- an error. If convert is set to true, excessively sparse arrays will be
--- converted to a JSON object.
---
--- The current settings are always returned. A particular setting is only
--- changed when the argument is provided (non-nil).
---
--- ## Example: Encoding a sparse array
---
---```lua
--- cjson.encode({ [3] = "data" })
--- -- Returns: '[null,null,"data"]'
---```
---
--- ## Example: Enabling conversion to a JSON object
---
---```lua
--- cjson.encode_sparse_array(true)
--- cjson.encode({ [1000] = "excessively sparse" })
--- -- Returns: '{"1000":"excessively sparse"}'
---```
---
---@param convert? boolean # (default: false)
---@param ratio? integer   # must be positive (default: 2)
---@param safe?  integer   # must be positive (default: 10)
---@return boolean convert
---@return integer ratio
---@return integer safe
function cjson.encode_sparse_array(convert, ratio, safe) end


--- encode_empty_table_as_object
---
--- Change the default behavior when encoding an empty Lua table.
---
--- By default, empty Lua tables are encoded as empty JSON Objects (`{}`). If
--- this is set to false, empty Lua tables will be encoded as empty JSON Arrays
--- instead (`[]`).
---
--- **NOTE:** This function is specific to the OpenResty cjson fork.
---
---@param setting boolean|'"on"'|'"off"'
function cjson.encode_empty_table_as_object(setting) end


--- encode_escape_forward_slash
---
--- If enabled, forward slash '/' will be encoded as '\/'.
---
--- If disabled, forward slash '/' will be encoded as '/' (no escape is applied).
---
--- **NOTE:** This function is specific to the OpenResty cjson fork.
---
---@param enabled boolean # (default: true)
function cjson.encode_escape_forward_slash(enabled) end


--- instantiate an independent copy of the Lua CJSON module
---
--- The new module has a separate persistent encoding buffer, and default settings
---@return cjson
function cjson.new() end


return cjson