-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhjson.lua
113 lines (100 loc) · 3.03 KB
/
hjson.lua
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
-- MIT License - Copyright (c) 2023 V (alis.is)
local decoder = require "hjson.decoder"
local encoder = require "hjson.encoder"
local encoderH = require "hjson.encoderH"
---@class HJsonKeyValuePair
---@field key any
---@field value any
---#DES 'hjson.decode'
---
---decodes h/json
---@param str string
---@param strict boolean?
---@param object_hook (fun(obj: table): table)?
---@param object_pairs_hook (fun(pairs: HJsonKeyValuePair[]): HJsonKeyValuePair[])?
---@return any
local function decode(str, strict, object_hook, object_pairs_hook)
local _decoder = decoder:new(strict, object_hook, object_pairs_hook)
return _decoder:decode(str)
end
---@class HJsonEncodeOptions
---@field indent string|boolean|nil
---@field skip_keys boolean?
---@field sort_keys boolean?
---@field item_sort_key (fun(k1:any, k2:any): boolean)?
---@field invalid_objects_as_type boolean?
---@param options HJsonEncodeOptions?
---@return HJsonEncodeOptions
local function preprocess_encode_options(options)
if type(options) ~= "table" then
local result = {} --[[@as HJsonEncodeOptions]]
return result
end
if options.skipkeys == true then
print("skipkeys is deprecated, use skip_keys instead")
options.skip_keys = true
end
if options.sortKeys == true then
print("sortKeys is deprecated, use sort_keys instead")
options.sort_keys = true
end
if options.invalidObjectsAsType == true then
print("invalidObjectsAsType is deprecated, use invalid_objects_as_type instead")
options.invalid_objects_as_type = true
end
return options
end
---#DES 'hjson.encode_to_json'
---
---encodes json
---@param obj any
---@param options HJsonEncodeOptions?
---@return any
local function encode_json(obj, options)
options = preprocess_encode_options(options)
local _encoder = encoder:new(options)
return _encoder:encode(obj)
end
---#DES 'hjson.encode'
---
---encodes hjson
---@param obj any
---@param options HJsonEncodeOptions?
---@return any
local function encode(obj, options)
options = preprocess_encode_options(options) --[[@as HJsonEncodeOptions]]
if options.indent == "" or options.indent == false or options.indent == 0 then
return encode_json(obj, options)
end
local _encoderH = encoderH:new(options)
return _encoderH:encode(obj)
end
local hjson = {
encode = encode,
---#DES 'hjson.stringify'
---
---encodes hjson
---@param obj any
---@param options HJsonEncodeOptions?
---@return any
stringify = encode,
encode_to_json = encode_json,
---#DES 'hjson.stringify_to_json'
---
---encodes json
---@param obj any
---@param options HJsonEncodeOptions?
---@return any
stringify_to_json = encode_json,
decode = decode,
---#DES 'hjson.parse'
---
---decodes h/json
---@param str string
---@param strict boolean?
---@param object_hook (fun(obj: table): table)?
---@param object_pairs_hook (fun(pairs: HJsonKeyValuePair[]): HJsonKeyValuePair[])?
---@return any
parse = decode
}
return hjson