bitser icon indicating copy to clipboard operation
bitser copied to clipboard

Deserialize only used once

Open nyankers opened this issue 2 years ago • 0 comments

Bitser only deserializes the first instance of a class instance, because future instances access the seen table, which stores the value passed to the deserializer rather than the value resulting from it.

It's not trivial to wait until the deserializer function runs to store it because of the possibility of self-referential tables. Some kind of translation will have to happen if the deserializer returns something other than the created instance.

Here's a somewhat contrived example script to show the problem:

local bitser = require("bitser")

local class = {}

local instance = setmetatable({}, class)

local deserialize = function(data, class)
	return instance
end

bitser.registerClass("class", class, nil, deserialize)

local serialized = bitser.dumps{instance, instance}
local array = bitser.loads(serialized)

print(instance == array[1]) -- returns true
print(instance == array[2]) -- returns false, expected true

nyankers avatar Jul 17 '23 03:07 nyankers