Skip to content

Commit 122e475

Browse files
committed
allowed one way encoding of non json types with invalidObjectsAsType
these types will be encoded as string in format0 __lua_<type> e.g. __lua_function
1 parent 01289bd commit 122e475

3 files changed

Lines changed: 49 additions & 29 deletions

File tree

hjson.lua

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
11
-- MIT License - Copyright (c) 2019 Void (cryon.io)
2+
local decoder = require "hjson.decoder"
3+
local encoder = require "hjson.encoder"
4+
local encoderH = require "hjson.encoderH"
25

3-
local decoder = require"hjson.decoder"
4-
local encoder = require"hjson.encoder"
5-
local encoderH = require"hjson.encoderH"
6-
7-
local function decode(str, strict, object_hook, object_pairs_hook)
6+
local function decode(str, strict, object_hook, object_pairs_hook)
87
local _decoder = decoder:new(strict, object_hook, object_pairs_hoo)
98
return _decoder:decode(str)
109
end
1110

12-
local function encode_json(obj, indent, skipkeys)
13-
local _encoder = encoder:new(indent, skipkeys)
11+
-- options = {indent, skipkeys, sort_keys, item_sort_key, invalidObjectsAsType}
12+
local function encode_json(obj, options)
13+
local _encoder = encoder:new(options)
1414
return _encoder:encode(obj)
1515
end
1616

17-
local function encode(obj, indent, skipkeys, sort_keys, item_sort_key)
18-
local forward = false
19-
if indent == "" or indent == false or indent == 0 then
20-
return encode_json(obj, indent, skipkeys)
17+
-- options = {indent, skipkeys, sort_keys, item_sort_key, invalidObjectsAsType}
18+
local function encode(obj, options)
19+
if type(options) ~= "table" then
20+
options = {}
21+
end
22+
23+
if options.indent == "" or options.indent == false or options.indent == 0 then
24+
return encode_json(obj, options)
2125
end
22-
local _encoderH = encoderH:new(indent, skipkeys, sort_keys, item_sort_key)
26+
local _encoderH = encoderH:new(options)
2327
return _encoderH:encode(obj)
2428
end
2529

@@ -28,8 +32,8 @@ local hjson = {
2832
stringify = encode,
2933
encode_to_json = encode_json,
3034
stringify_to_json = encode_json,
31-
decode = decode,
35+
decode = decode,
3236
parse = decode
3337
}
3438

35-
return hjson
39+
return hjson

hjson/encoder.lua

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,17 @@ end
4343

4444
local JsonEncoder = {}
4545

46-
function JsonEncoder:new(indent, skipkeys, sort_keys, item_sort_key)
46+
function JsonEncoder:new(options)
47+
if type(options) ~= "table" then
48+
options = {}
49+
end
50+
local indent, skipkeys, sort_keys, item_sort_key, invalidObjectsAsType =
51+
options.indent,
52+
options.skipkeys,
53+
options.sort_keys,
54+
options.item_sort_key,
55+
options.invalidObjectsAsType
56+
4757
if skipkeys == nil then
4858
skipkeys = true
4959
end
@@ -57,7 +67,7 @@ function JsonEncoder:new(indent, skipkeys, sort_keys, item_sort_key)
5767
indent = string.rep(" ", indent)
5868
end
5969

60-
if type(indent) == "boolean" and indent then
70+
if type(indent) == "boolean" and indent then
6171
indent = " "
6272
end
6373

@@ -195,14 +205,12 @@ function JsonEncoder:new(indent, skipkeys, sort_keys, item_sort_key)
195205
if type(func) == "function" then
196206
return func(o, encode)
197207
end
208+
if invalidObjectsAsType then
209+
return encodeFunctionMap["string"]('__lua_' ..type(o))
210+
end
198211
error("Unexpected type '" .. _type .. "'")
199212
end
200213

201-
local function json_encode(o)
202-
stack = {}
203-
return encode(o)
204-
end
205-
206214
local je = {
207215
_encode = encode
208216
}
@@ -212,8 +220,8 @@ function JsonEncoder:new(indent, skipkeys, sort_keys, item_sort_key)
212220
return je
213221
end
214222

215-
function JsonEncoder:encode(o)
216-
return self._encode(o)
223+
function JsonEncoder:encode(o, allowNonJsonTypes)
224+
return self._encode(o, allowNonJsonTypes)
217225
end
218226

219227
return JsonEncoder

hjson/encoderH.lua

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,17 @@ end
110110

111111
local HjsonEncoder = {}
112112

113-
function HjsonEncoder:new(indent, skipkeys, sort_keys, item_sort_key)
113+
function HjsonEncoder:new(options)
114+
if type(options) ~= "table" then
115+
options = {}
116+
end
117+
local indent, skipkeys, sort_keys, item_sort_key, invalidObjectsAsType =
118+
options.indent,
119+
options.skipkeys,
120+
options.sort_keys,
121+
options.item_sort_key,
122+
options.invalidObjectsAsType
123+
114124
if skipkeys == nil then
115125
skipkeys = true
116126
end
@@ -291,14 +301,12 @@ function HjsonEncoder:new(indent, skipkeys, sort_keys, item_sort_key)
291301
if type(func) == "function" then
292302
return func(o, _encode)
293303
end
304+
if invalidObjectsAsType then
305+
return encodeFunctionMap["string"]('__lua_' ..type(o))
306+
end
294307
error("Unexpected type '" .. _type .. "'")
295308
end
296309

297-
local function json_encode(o)
298-
stack = {}
299-
return _encode(o)
300-
end
301-
302310
local je = {
303311
_encode = _encode
304312
}

0 commit comments

Comments
 (0)