diff --git a/bitser.lua b/bitser.lua index c04d0ad..e91d341 100644 --- a/bitser.lua +++ b/bitser.lua @@ -151,14 +151,14 @@ local function write_number(value, _) end end -local function write_string(value, seen) +local function write_string(value, _) if #value < 32 then --short string Buffer_write_byte(192 + #value) else --long string Buffer_write_byte(244) - write_number(#value, seen) + write_number(#value) end Buffer_write_string(value) end @@ -173,14 +173,14 @@ end local function write_table(value, seen) 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[getmetatable(value)] -- hump.class or class_name_registry[value.__class__]) -- Slither - if class then - classkey = classkey_registry[class] + if classname then + classkey = classkey_registry[classname] Buffer_write_byte(242) - write_string(class) + serialize_value(classname, seen) else Buffer_write_byte(240) end @@ -421,4 +421,4 @@ end, unregisterClass = function(name) classkey_registry[name] = nil class_deserialize_registry[name] = nil class_registry[name] = nil -end, reserveBuffer = Buffer_prereserve, clearBuffer = Buffer_clear} \ No newline at end of file +end, reserveBuffer = Buffer_prereserve, clearBuffer = Buffer_clear} diff --git a/spec/bitser_spec.lua b/spec/bitser_spec.lua index 1b7a616..08eeb12 100644 --- a/spec/bitser_spec.lua +++ b/spec/bitser_spec.lua @@ -163,6 +163,37 @@ describe("bitser", function() assert.is_true(class.isinstance(serdeser(bojack), Horse)) bitser.unregisterClass('Horse') 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() 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 = {}