Skip to content

Commit 1345be7

Browse files
committed
default indent if not defined - empty for json, '\t' for hjson
1 parent 395db8d commit 1345be7

File tree

3 files changed

+25
-23
lines changed

3 files changed

+25
-23
lines changed

hjson.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ local function decode(str, options)
2020
end
2121

2222
---@class HJsonEncodeOptions
23-
---@field indent string|boolean|nil
24-
---@field skip_keys boolean?
23+
---@field indent string|boolean|integer|nil
24+
---@field skip_keys boolean? skip invalid keys
2525
---@field sort_keys boolean?
2626
---@field item_sort_key (fun(k1:any, k2:any): boolean)?
2727
---@field invalid_objects_as_type boolean?

hjson/encoder.lua

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ local escape_char_map = {
88
["\r"] = "\\r",
99
["\t"] = "\\t"
1010
}
11+
local default_indent = false
1112

1213
local function isArray(t)
1314
local i = 0
@@ -45,19 +46,18 @@ function JsonEncoder:new(options)
4546
options.item_sort_key, options.invalid_objects_as_type
4647

4748
if skip_invalid_keys == nil then skip_invalid_keys = true end
48-
if indent == nil then indent = " " end
49-
if type(indent) ~= "number" and type(indent) ~= "string" and type(indent) ~=
50-
"boolean" then
51-
error(
52-
"indent (#1 parameter) has to be of type string, number or boolean")
49+
local indent_type = type(indent)
50+
assert(indent_type == "string" or indent_type == "number" or indent_type == "boolean" or indent == nil,
51+
"indent has to be of type string, number or boolean, got " .. indent_type)
52+
assert(indent_type ~= "string" or indent:match("^%s*$"),
53+
"indent has to be a string consisting of whitespace characters only")
54+
55+
if type(indent) == "number" then
56+
indent = math.floor(indent)
57+
indent = string.rep(" ", indent)
5358
end
54-
if type(indent) == "number" then indent = string.rep(" ", indent) end
55-
56-
if type(indent) == "boolean" and indent then indent = " " end
57-
58-
assert(indent and indent:match("%s*"),
59-
"indent (#1 parameter) has to be of type string with at least 2 spaces or integer greater than 1")
60-
59+
if indent == true then indent = "\t" end
60+
if not indent or indent == "" then indent = default_indent end
6161

6262
local stack = {}
6363
local currentIndentLevel = 0

hjson/encoderH.lua

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ local escape_char_map = {
99
["\t"] = "\\t"
1010
}
1111

12+
local default_indent = "\t"
13+
1214
local COMMONRANGE = "\127-\159" -- // TODO: add unicode escape sequences
1315

1416
local function containsSequences(s, sequences)
@@ -102,19 +104,19 @@ function HjsonEncoder:new(options)
102104
options.item_sort_key, options.invalid_objects_as_type
103105

104106
if skip_invalid_keys == nil then skip_invalid_keys = true end
105-
if indent == nil then indent = " " end
106-
if (type(indent) ~= "number" or indent < 2) and
107-
(type(indent) ~= "string" or not indent:find("%s%s*")) then
108-
error(
109-
"indent (#1 parameter) has to be of type string with at least 2 spaces or integer greater than 1")
110-
end
107+
local indent_type = type(indent)
108+
assert(indent_type == "string" or indent_type == "number" or indent_type == "boolean" or indent == nil,
109+
"indent has to be of type string, number or boolean, got " .. indent_type)
110+
assert(indent_type ~= "string" or indent:match("^%s*$"),
111+
"indent has to be a string consisting of whitespace characters only")
112+
assert(type(indent) ~= "boolean" or indent == true,
113+
"if indent is a boolean, it has to be true, got " .. tostring(indent))
114+
111115
if type(indent) == "number" then
112116
indent = math.floor(indent)
113117
indent = string.rep(" ", indent)
114118
end
115-
116-
assert(indent and indent:match("%s*"),
117-
"indent (#1 parameter) has to be of type string with at least 2 spaces or integer greater than 1")
119+
if not indent or indent == true or indent == "" then indent = default_indent end
118120

119121
local stack = {}
120122
local currentIndentLevel = 0

0 commit comments

Comments
 (0)