Skip to content

Commit 0794096

Browse files
committed
0.2.0
1 parent 8b1df23 commit 0794096

5 files changed

Lines changed: 59 additions & 35 deletions

File tree

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,23 @@ Library exports json.lua like and JS like api.
2929
- `obj` - Lua object - `table`, `string`, `number`, `nil`, `boolean`
3030
- `options` table with following values:
3131
- `indent` - default `" "`. Accepts string of whitespace characters or a number representing number of spaces (non indented HJSON is JSON, automatically forwards to `_to_json` version)
32-
- `skipkeys` - default `true` Skips invalid keys. If false throws error on invalid key.
32+
- `skip_keys` - default `true` Skips invalid keys. If false throws error on invalid key.
3333
- Valid key types: `boolean`, `nil`, `string`
34-
- `sortKeys` - whether to sort keys in objects
34+
- `sort_keys` - whether to sort keys in objects
3535
- `item_sort_key` - sort function which is passed to `table.sort` sorting object keys
36-
- `invalidObjectsAsType` if true functions and others objects are replaced with their type name in format `__lua_<type>` e.g. `__lua_function`
36+
- `invalid_objects_as_type` if true functions and others objects are replaced with their type name in format `__lua_<type>` e.g. `__lua_function`
3737
- Lua object to JSON - returns JSON string
3838
- `encode(obj, options)`
3939
- `stringify(obj, options)`
4040
- Parameters:
4141
- `obj` - Lua object - `table`, `string`, `number`, `nil`, `boolean`
4242
- `options` table with following values:
4343
- `indent` - default `" "`. Accepts string of whitespace characters or a number representing number of spaces (non indented HJSON is JSON, automatically forwards to `_to_json` version)
44-
- `skipkeys` - default `true` Skips invalid keys. If false throws error on invalid key.
44+
- `skip_keys` - default `true` Skips invalid keys. If false throws error on invalid key.
4545
- Valid key types: `boolean`, `nil`, `string`
46-
- `sortKeys` - whether to sort keys in objects
46+
- `sort_keys` - whether to sort keys in objects
4747
- `item_sort_key` - sort function which is passed to `table.sort` sorting object keys
48-
- `invalidObjectsAsType` if true functions and others objects are replaced with their type name in format `__lua_<type>` e.g. `__lua_function`
48+
- `invalid_objects_as_type` if true functions and others objects are replaced with their type name in format `__lua_<type>` e.g. `__lua_function`
4949
- H/JSON to Lua object - returns Lua object
5050
- `decode(str, strict, object_hook, object_pairs_hook)`
5151
- `parse(str, strict, object_hook, object_pairs_hook)`
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package = "hjson-lua"
2-
version = "0.1.6-1"
2+
version = "0.2.0-1"
33
source = {
44
url = "git://github.com/hjson/hjson-lua.git",
55
}

hjson.lua

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ local decoder = require "hjson.decoder"
33
local encoder = require "hjson.encoder"
44
local encoderH = require "hjson.encoderH"
55

6-
---@class HjsonKeyValuePair
6+
---@class HJsonKeyValuePair
77
---@field key any
88
---@field value any
99

@@ -13,28 +13,54 @@ local encoderH = require "hjson.encoderH"
1313
---@param str string
1414
---@param strict boolean?
1515
---@param object_hook (fun(obj: table): table)?
16-
---@param object_pairs_hook (fun(pairs: HjsonKeyValuePair[]): HjsonKeyValuePair[])?
16+
---@param object_pairs_hook (fun(pairs: HJsonKeyValuePair[]): HJsonKeyValuePair[])?
1717
---@return any
1818
local function decode(str, strict, object_hook, object_pairs_hook)
1919
local _decoder = decoder:new(strict, object_hook, object_pairs_hook)
2020
return _decoder:decode(str)
2121
end
2222

23-
---@class HjsonEncodeOptions
23+
---@class HJsonEncodeOptions
2424
---@field indent string|boolean|nil
25-
---@field skipkeys boolean?
26-
---@field sortKeys boolean?
25+
---@field skip_keys boolean?
26+
---@field sort_keys boolean?
2727
---@field item_sort_key (fun(k1:any, k2:any): boolean)?
28-
---@field invalidObjectsAsType boolean?
28+
---@field invalid_objects_as_type boolean?
2929

30+
---@param options HJsonEncodeOptions?
31+
---@return HJsonEncodeOptions
32+
local function preprocess_encode_options(options)
33+
if type(options) ~= "table" then
34+
local result = {} --[[@as HJsonEncodeOptions]]
35+
return result
36+
end
37+
38+
if options.skipkeys == true then
39+
print("skipkeys is deprecated, use skip_keys instead")
40+
options.skip_keys = true
41+
end
42+
43+
if options.sortKeys == true then
44+
print("sortKeys is deprecated, use sort_keys instead")
45+
options.sort_keys = true
46+
end
47+
48+
if options.invalidObjectsAsType == true then
49+
print("invalidObjectsAsType is deprecated, use invalid_objects_as_type instead")
50+
options.invalid_objects_as_type = true
51+
end
52+
return options
53+
end
3054

3155
---#DES 'hjson.encode_to_json'
3256
---
3357
---encodes json
3458
---@param obj any
35-
---@param options HjsonEncodeOptions?
59+
---@param options HJsonEncodeOptions?
3660
---@return any
3761
local function encode_json(obj, options)
62+
options = preprocess_encode_options(options)
63+
3864
local _encoder = encoder:new(options)
3965
return _encoder:encode(obj)
4066
end
@@ -43,12 +69,10 @@ end
4369
---
4470
---encodes hjson
4571
---@param obj any
46-
---@param options HjsonEncodeOptions?
72+
---@param options HJsonEncodeOptions?
4773
---@return any
4874
local function encode(obj, options)
49-
if type(options) ~= "table" then
50-
options = {}
51-
end
75+
options = preprocess_encode_options(options) --[[@as HJsonEncodeOptions]]
5276

5377
if options.indent == "" or options.indent == false or options.indent == 0 then
5478
return encode_json(obj, options)
@@ -63,15 +87,15 @@ local hjson = {
6387
---
6488
---encodes hjson
6589
---@param obj any
66-
---@param options HjsonEncodeOptions?
90+
---@param options HJsonEncodeOptions?
6791
---@return any
6892
stringify = encode,
6993
encode_to_json = encode_json,
7094
---#DES 'hjson.stringify_to_json'
7195
---
7296
---encodes json
7397
---@param obj any
74-
---@param options HjsonEncodeOptions?
98+
---@param options HJsonEncodeOptions?
7599
---@return any
76100
stringify_to_json = encode_json,
77101
decode = decode,
@@ -81,7 +105,7 @@ local hjson = {
81105
---@param str string
82106
---@param strict boolean?
83107
---@param object_hook (fun(obj: table): table)?
84-
---@param object_pairs_hook (fun(pairs: HjsonKeyValuePair[]): HjsonKeyValuePair[])?
108+
---@param object_pairs_hook (fun(pairs: HJsonKeyValuePair[]): HJsonKeyValuePair[])?
85109
---@return any
86110
parse = decode
87111
}

hjson/encoder.lua

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ local JsonEncoder = {}
4040

4141
function JsonEncoder:new(options)
4242
if type(options) ~= "table" then options = {} end
43-
local indent, skipkeys, sortKeys, item_sort_key, invalidObjectsAsType =
44-
options.indent, options.skipkeys, options.sortKeys,
45-
options.item_sort_key, options.invalidObjectsAsType
43+
local indent, skip_keys, sort_keys, item_sort_key, invalid_objects_as_type =
44+
options.indent, options.skip_keys, options.sort_keys,
45+
options.item_sort_key, options.invalid_objects_as_type
4646

47-
if skipkeys == nil then skipkeys = true end
47+
if skip_keys == nil then skip_keys = true end
4848
if indent == nil then indent = " " end
4949
if type(indent) ~= "number" and type(indent) ~= "string" and type(indent) ~=
5050
"boolean" then
@@ -71,7 +71,7 @@ function JsonEncoder:new(options)
7171
elseif _type == "string" then
7272
return encodeString(key)
7373
end
74-
if skipkeys then return nil end
74+
if skip_keys then return nil end
7575
error(string.format("Invalid key type - %s (%s) ", _type, key))
7676
end
7777

@@ -125,7 +125,7 @@ function JsonEncoder:new(options)
125125
keysetMap[key] = k
126126
end
127127
end
128-
if sortKeys then
128+
if sort_keys then
129129
if type(item_sort_key) == "function" then
130130
table.sort(keyset, item_sort_key)
131131
else
@@ -171,7 +171,7 @@ function JsonEncoder:new(options)
171171
end
172172
local func = encodeFunctionMap[_type]
173173
if type(func) == "function" then return func(o, encode) end
174-
if invalidObjectsAsType then
174+
if invalid_objects_as_type then
175175
return encodeFunctionMap["string"]('__lua_' .. type(o))
176176
end
177177
error("Unexpected type '" .. _type .. "'")

hjson/encoderH.lua

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,11 @@ local HjsonEncoder = {}
9696

9797
function HjsonEncoder:new(options)
9898
if type(options) ~= "table" then options = {} end
99-
local indent, skipkeys, sortKeys, item_sort_key, invalidObjectsAsType =
100-
options.indent, options.skipkeys, options.sortKeys,
101-
options.item_sort_key, options.invalidObjectsAsType
99+
local indent, skip_keys, sort_keys, item_sort_key, invalid_objects_as_type =
100+
options.indent, options.skip_keys, options.sort_keys,
101+
options.item_sort_key, options.invalid_objects_as_type
102102

103-
if skipkeys == nil then skipkeys = true end
103+
if skip_keys == nil then skip_keys = true end
104104
if indent == nil then indent = " " end
105105
if (type(indent) ~= "number" or indent < 2) and
106106
(type(indent) ~= "string" or not indent:find("%s%s*")) then
@@ -165,7 +165,7 @@ function HjsonEncoder:new(options)
165165
return key
166166
end
167167
end
168-
if skipkeys then return nil end
168+
if skip_keys then return nil end
169169
error(string.format("Invalid key type - %s (%s) ", _type, key))
170170
end
171171

@@ -211,7 +211,7 @@ function HjsonEncoder:new(options)
211211
keysetMap[key] = k
212212
end
213213
end
214-
if sortKeys then
214+
if sort_keys then
215215
if type(item_sort_key) == "function" then
216216
table.sort(keyset, item_sort_key)
217217
else
@@ -257,7 +257,7 @@ function HjsonEncoder:new(options)
257257
end
258258
local func = encodeFunctionMap[_type]
259259
if type(func) == "function" then return func(o, _encode) end
260-
if invalidObjectsAsType then
260+
if invalid_objects_as_type then
261261
return encodeFunctionMap["string"]('__lua_' .. type(o))
262262
end
263263
error("Unexpected type '" .. _type .. "'")

0 commit comments

Comments
 (0)