mirror of
https://github.com/gvx/bitser.git
synced 2024-11-11 01:44:21 +00:00
Fix reference serialization inside classes
This commit is contained in:
parent
cf02122d22
commit
4fe34942c0
12
bitser.lua
12
bitser.lua
@ -151,14 +151,14 @@ local function write_number(value, _)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function write_string(value, seen)
|
local function write_string(value, _)
|
||||||
if #value < 32 then
|
if #value < 32 then
|
||||||
--short string
|
--short string
|
||||||
Buffer_write_byte(192 + #value)
|
Buffer_write_byte(192 + #value)
|
||||||
else
|
else
|
||||||
--long string
|
--long string
|
||||||
Buffer_write_byte(244)
|
Buffer_write_byte(244)
|
||||||
write_number(#value, seen)
|
write_number(#value)
|
||||||
end
|
end
|
||||||
Buffer_write_string(value)
|
Buffer_write_string(value)
|
||||||
end
|
end
|
||||||
@ -173,14 +173,14 @@ end
|
|||||||
|
|
||||||
local function write_table(value, seen)
|
local function write_table(value, seen)
|
||||||
local classkey
|
local classkey
|
||||||
local class = (class_name_registry[value.class] -- MiddleClass
|
local classname = (class_name_registry[value.class] -- MiddleClass
|
||||||
or class_name_registry[value.__baseclass] -- SECL
|
or class_name_registry[value.__baseclass] -- SECL
|
||||||
or class_name_registry[getmetatable(value)] -- hump.class
|
or class_name_registry[getmetatable(value)] -- hump.class
|
||||||
or class_name_registry[value.__class__]) -- Slither
|
or class_name_registry[value.__class__]) -- Slither
|
||||||
if class then
|
if classname then
|
||||||
classkey = classkey_registry[class]
|
classkey = classkey_registry[classname]
|
||||||
Buffer_write_byte(242)
|
Buffer_write_byte(242)
|
||||||
write_string(class)
|
serialize_value(classname, seen)
|
||||||
else
|
else
|
||||||
Buffer_write_byte(240)
|
Buffer_write_byte(240)
|
||||||
end
|
end
|
||||||
|
@ -163,6 +163,37 @@ describe("bitser", function()
|
|||||||
assert.is_true(class.isinstance(serdeser(bojack), Horse))
|
assert.is_true(class.isinstance(serdeser(bojack), Horse))
|
||||||
bitser.unregisterClass('Horse')
|
bitser.unregisterClass('Horse')
|
||||||
end)
|
end)
|
||||||
|
it("serializes custom class instances", function()
|
||||||
|
local Horse_mt = bitser.registerClass('Horse', {__index = {}}, nil, setmetatable)
|
||||||
|
local function Horse(name)
|
||||||
|
local self = {}
|
||||||
|
self.name = name
|
||||||
|
self[1] = 'instance can be sequence'
|
||||||
|
return setmetatable(self, Horse_mt)
|
||||||
|
end
|
||||||
|
local bojack = Horse('Bojack Horseman')
|
||||||
|
test_serdeser(bojack)
|
||||||
|
assert.are.equal(getmetatable(serdeser(bojack)), Horse_mt)
|
||||||
|
bitser.unregisterClass('Horse')
|
||||||
|
end)
|
||||||
|
it("serializes classes that repeat keys", function()
|
||||||
|
local my_mt = {"hi"}
|
||||||
|
local works = { foo = 'a', bar = {baz = 'b'}, }
|
||||||
|
local broken = { foo = 'a', bar = {foo = 'b'}, }
|
||||||
|
local more_broken = {
|
||||||
|
foo = 'a',
|
||||||
|
baz = {foo = 'b', bar = 'c'},
|
||||||
|
quz = {bar = 'd', bam = 'e'}
|
||||||
|
}
|
||||||
|
setmetatable(works, my_mt)
|
||||||
|
setmetatable(broken, my_mt)
|
||||||
|
setmetatable(more_broken, my_mt)
|
||||||
|
bitser.registerClass("Horse", my_mt, nil, setmetatable)
|
||||||
|
test_serdeser(works)
|
||||||
|
test_serdeser(broken)
|
||||||
|
test_serdeser(more_broken)
|
||||||
|
bitser.unregisterClass('Horse')
|
||||||
|
end)
|
||||||
it("serializes big data", function()
|
it("serializes big data", function()
|
||||||
local text = "this is a lot of nonsense, please disregard, we need a lot of data to get past 4 KiB (114 characters should do it)"
|
local text = "this is a lot of nonsense, please disregard, we need a lot of data to get past 4 KiB (114 characters should do it)"
|
||||||
local t = {}
|
local t = {}
|
||||||
|
Loading…
Reference in New Issue
Block a user