@@ -9,6 +9,8 @@ local escape_char_map = {
99 [" \t " ] = " \\ t"
1010}
1111
12+ local default_indent = " \t "
13+
1214local COMMONRANGE = " \127 -\159 " -- // TODO: add unicode escape sequences
1315
1416local 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