Skip to content

Commit 1881efa

Browse files
committed
updated readme
1 parent 122e475 commit 1881efa

File tree

3 files changed

+36
-36
lines changed

3 files changed

+36
-36
lines changed

README.md

+16-10
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,27 @@ Ported from [hjson-py](https://github.com/hjson/hjson-py). Inspired by rxi - [js
1616
Library exports json.lua like and JS like api.
1717

1818
- Lua object to HJSON - returns HJSON string
19-
- `encode(obj, indent, skipkeys)`
20-
- `stringify(obj, indent, skipkeys)`
19+
- `encode(obj, options)`
20+
- `stringify(obj, options)`
2121
- Parameters:
2222
- `obj` - Lua object - `table`, `string`, `number`, `nil`, `boolean`
23-
- `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)
24-
- `skipkeys` - default `true` Skips invalid keys. If false throws error on invalid key.
25-
- Valid key types: `boolean`, `nil`, `string`
23+
- `options` table with following values:
24+
- `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)
25+
- `skipkeys` - default `true` Skips invalid keys. If false throws error on invalid key.
26+
- Valid key types: `boolean`, `nil`, `string`
27+
- `item_sort_key` - sort function which is passed to `table.sort` sorting object keys
28+
- `invalidObjectsAsType` if true functions and others objects are replaced with their type name in format `__lua_<type>` e.g. `__lua_function`
2629
- Lua object to JSON - returns JSON string
27-
- `encode(obj, indent, skipkeys)`
28-
- `stringify(obj, indent, skipkeys)`
30+
- `encode(obj, options)`
31+
- `stringify(obj, options)`
2932
- Parameters:
3033
- `obj` - Lua object - `table`, `string`, `number`, `nil`, `boolean`
31-
- `indent` - default `" "`. Accepts string of whitespace characters or a number representing number of spaces
32-
- `skipkeys` - default `true` Skips invalid keys. If false throws error on invalid key.
33-
- Valid key types: `boolean`, `nil`, `string`
34+
- `options` table with following values:
35+
- `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)
36+
- `skipkeys` - default `true` Skips invalid keys. If false throws error on invalid key.
37+
- Valid key types: `boolean`, `nil`, `string`
38+
- `item_sort_key` - sort function which is passed to `table.sort` sorting object keys
39+
- `invalidObjectsAsType` if true functions and others objects are replaced with their type name in format `__lua_<type>` e.g. `__lua_function`
3440
- H/JSON to Lua object - returns Lua object
3541
- `decode(str, strict, object_hook, object_pairs_hook)`
3642
- `parse(str, strict, object_hook, object_pairs_hook)`

hjson/encoder.lua

+10-13
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,9 @@ function JsonEncoder:new(options)
4747
if type(options) ~= "table" then
4848
options = {}
4949
end
50-
local indent, skipkeys, sort_keys, item_sort_key, invalidObjectsAsType =
50+
local indent, skipkeys, item_sort_key, invalidObjectsAsType =
5151
options.indent,
5252
options.skipkeys,
53-
options.sort_keys,
5453
options.item_sort_key,
5554
options.invalidObjectsAsType
5655

@@ -153,17 +152,15 @@ function JsonEncoder:new(options)
153152
keysetMap[key] = k
154153
end
155154
end
156-
if sort_keys then
157-
if type(item_sort_key) == "function" then
158-
table.sort(keyset, item_sort_key)
159-
else
160-
table.sort(
161-
keyset,
162-
function(a, b)
163-
return a:upper() < b:upper()
164-
end
165-
)
166-
end
155+
if type(item_sort_key) == "function" then
156+
table.sort(keyset, item_sort_key)
157+
else
158+
table.sort(
159+
keyset,
160+
function(a, b)
161+
return a:upper() < b:upper()
162+
end
163+
)
167164
end
168165
local buf = "{" .. newlineIndent
169166
for i, sk in ipairs(keyset) do

hjson/encoderH.lua

+10-13
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,9 @@ function HjsonEncoder:new(options)
114114
if type(options) ~= "table" then
115115
options = {}
116116
end
117-
local indent, skipkeys, sort_keys, item_sort_key, invalidObjectsAsType =
117+
local indent, skipkeys, item_sort_key, invalidObjectsAsType =
118118
options.indent,
119119
options.skipkeys,
120-
options.sort_keys,
121120
options.item_sort_key,
122121
options.invalidObjectsAsType
123122

@@ -249,17 +248,15 @@ function HjsonEncoder:new(options)
249248
keysetMap[key] = k
250249
end
251250
end
252-
if sort_keys then
253-
if type(item_sort_key) == "function" then
254-
table.sort(keyset, item_sort_key)
255-
else
256-
table.sort(
257-
keyset,
258-
function(a, b)
259-
return a:upper() < b:upper()
260-
end
261-
)
262-
end
251+
if type(item_sort_key) == "function" then
252+
table.sort(keyset, item_sort_key)
253+
else
254+
table.sort(
255+
keyset,
256+
function(a, b)
257+
return a:upper() < b:upper()
258+
end
259+
)
263260
end
264261
local buf = "{" .. newlineIndent
265262
for i, sk in ipairs(keyset) do

0 commit comments

Comments
 (0)