Skip to content

Commit d429563

Browse files
committed
docs & minor fixes
1 parent b7ebd6b commit d429563

7 files changed

+263
-309
lines changed

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2020 Void (cryon.io)
3+
Copyright (c) 2020 V (cryon.io)
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

hjson-lua-0.1.2-2.rockspec hjson-lua-0.1.3-1.rockspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package = "hjson-lua"
2-
version = "0.1.2-2"
2+
version = "0.1.3-1"
33
source = {
44
url = "git://github.com/cryi/hjson-lua.git",
55
}

hjson.lua

+34-3
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,50 @@
1-
-- MIT License - Copyright (c) 2019 Void (cryon.io)
1+
-- MIT License - Copyright (c) 2021 V (cryon.io)
22
local decoder = require "hjson.decoder"
33
local encoder = require "hjson.encoder"
44
local encoderH = require "hjson.encoderH"
55

6+
---@class HjsonKeyValuePair
7+
---@field key any
8+
---@field value any
9+
10+
---#DES 'decode'
11+
---
12+
---decodes h/json
13+
---@param str string
14+
---@param strict boolean
15+
---@param object_hook fun(obj: table): table
16+
---@param object_pairs_hook fun(pairs: HjsonKeyValuePair[]): HjsonKeyValuePair[]
17+
---@return any
618
local function decode(str, strict, object_hook, object_pairs_hook)
719
local _decoder = decoder:new(strict, object_hook, object_pairs_hook)
820
return _decoder:decode(str)
921
end
1022

11-
-- options = {indent, skipkeys, sort_keys, item_sort_key, invalidObjectsAsType}
23+
---@class HjsonEncodeOptions
24+
---@field indent string|boolean
25+
---@field skipkeys boolean
26+
---@field sortKeys boolean
27+
---@field item_sort_key fun(k1:any, k2:any): boolean
28+
---@field invalidObjectsAsType boolean
29+
30+
31+
---#DES 'encode_json'
32+
---
33+
---encodes json
34+
---@param obj any
35+
---@param options HjsonEncodeOptions
36+
---@return any
1237
local function encode_json(obj, options)
1338
local _encoder = encoder:new(options)
1439
return _encoder:encode(obj)
1540
end
1641

17-
-- options = {indent, skipkeys, sort_keys, item_sort_key, invalidObjectsAsType}
42+
---#DES 'encode_json'
43+
---
44+
---encodes hjson
45+
---@param obj any
46+
---@param options HjsonEncodeOptions
47+
---@return any
1848
local function encode(obj, options)
1949
if type(options) ~= "table" then
2050
options = {}
@@ -27,6 +57,7 @@ local function encode(obj, options)
2757
return _encoderH:encode(obj)
2858
end
2959

60+
3061
local hjson = {
3162
encode = encode,
3263
stringify = encode,

hjson/decoder.lua

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
-- MIT License - Copyright (c) 2019 Void (cryon.io)
1+
-- MIT License - Copyright (c) 2021 V (cryon.io)
22

33
local WHITESPACE = " \t\n\r"
44
local PUNCTUATOR = "{}[],:"
@@ -169,7 +169,7 @@ function HjsonDecoder:new(strict, object_hook, object_pairs_hook)
169169
goto scan_string_loop_start
170170
elseif terminator ~= "\\" then
171171
if strict then
172-
decodeError(s, begin, "Invalid control character" .. ch)
172+
decodeError(s, begin, "Invalid control character" .. terminator)
173173
else
174174
chunks = chunks .. terminator
175175
goto scan_string_loop_start
@@ -292,7 +292,7 @@ function HjsonDecoder:new(strict, object_hook, object_pairs_hook)
292292
local frac = nil
293293
local exp = nil
294294

295-
trimmed_range = trim(s:sub(begin, _end - 1))
295+
local trimmed_range = trim(s:sub(begin, _end - 1))
296296
if chf == "n" and trimmed_range == "null" then
297297
return nil, _end
298298
elseif chf == "t" and trimmed_range == "true" then
@@ -305,12 +305,12 @@ function HjsonDecoder:new(strict, object_hook, object_pairs_hook)
305305
if integer then
306306
frac = s:match("^(%.%d+)", begin + #integer) or ""
307307
exp = s:match("^([eE][-+]?%d+)", begin + #integer + #frac) or ""
308-
ending = s:match("^([\t ]*)", begin + #integer + #frac + #exp) or ""
308+
local ending = s:match("^([\t ]*)", begin + #integer + #frac + #exp) or ""
309309
m = integer .. frac .. exp .. ending
310310
end
311311
end
312312
if m and begin + #m == _end then
313-
res = tonumber(integer .. frac .. exp)
313+
local res = tonumber(integer .. frac .. exp)
314314
return res, _end
315315
end
316316

@@ -397,7 +397,7 @@ function HjsonDecoder:new(strict, object_hook, object_pairs_hook)
397397
-- Trivial empty object
398398
if not objectWithoutBraces and ch == "}" then
399399
if type(object_pairs_hook) == "function" then
400-
result = object_pairs_hook(pairs)
400+
local result = object_pairs_hook(pairs)
401401
return result, _end + 1
402402
end
403403
pairs = {}
@@ -440,15 +440,15 @@ function HjsonDecoder:new(strict, object_hook, object_pairs_hook)
440440
ch, _end = getNext(s, _end)
441441
end
442442
if type(object_pairs_hook) == "function" then
443-
result = object_pairs_hook(pairs)
443+
local result = object_pairs_hook(pairs)
444444
return result, _end
445445
end
446446

447-
pairs = dict(pairs)
447+
local obj = dict(pairs)
448448
if type(object_hook) == "function" then
449-
pairs = object_hook(pairs)
449+
obj = object_hook(obj)
450450
end
451-
return pairs, _end
451+
return obj, _end
452452
end
453453

454454
local function parseArray(state, scanOnce)

0 commit comments

Comments
 (0)