Merge pull request #5 from Alloyed/fix-classes

Fix reference serialization inside classes
This commit is contained in:
Robin Wellner 2016-10-31 11:33:30 +01:00 committed by GitHub
commit 714a51faed
2 changed files with 38 additions and 7 deletions

View File

@ -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
@ -421,4 +421,4 @@ end, unregisterClass = function(name)
classkey_registry[name] = nil classkey_registry[name] = nil
class_deserialize_registry[name] = nil class_deserialize_registry[name] = nil
class_registry[name] = nil class_registry[name] = nil
end, reserveBuffer = Buffer_prereserve, clearBuffer = Buffer_clear} end, reserveBuffer = Buffer_prereserve, clearBuffer = Buffer_clear}

View File

@ -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 = {}