binser + middleclass, I think I'm confused...

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
mutantant
Prole
Posts: 6
Joined: Tue Feb 05, 2013 12:02 am

binser + middleclass, I think I'm confused...

Post by mutantant »

Hey all.

I'm hoping someone who has used a serializer and middleclass together can help me. I seem to be confused.

Tried, as a test, to serialize a middleclass class, and then deserialize it. Obviously, in the docs for binser, middleclass is a supported "custom" mapping for binser to use.

(binser URL: https://github.com/bakpakin/binser)

And yet, I can't seem to figure it out.

Basically, I made a junk test class "Testclass" with nothing but a initialize function and a "print some crap" function that printed "Yo momma!". Yep, I'm a full grown adult. Then I spawned an actual member of said class:

Code: Select all

testclass = Testclass()
Then I tried what I'm supposed to do based off the binser docs:

Code: Select all

local class = binser.registerClass(class[, name])
So, basically I'm confused at that point. See, I understand how to declare this custom class for binser:

Code: Select all

somevariable = binser.registerClass(testclass)
But have no idea how to proceed. I tried serializing testclass after I registered the class, which "works" kinda, but I don't think it's taking into account the middleclass class at all, since when I deserialize it, the member functions don't work (nil value, etc.). So, I'm not sure how to tell binser to use that registered way it knows how to deal with middleclass.

And the docs for it sort of skim over this aspect. I tried and tried, and feel I'm missing some simple step.

So, in short, can someone who has used this before point me in the right direction? How does one serialize and deserialize a middleclass class via binser? I registered the class, and then serialized it, but when deserialized, the class isn't really a class at all.

I hope that makes sense, as I'm quite tired and hope I didn't ramble on!
MrFariator
Party member
Posts: 548
Joined: Wed Oct 05, 2016 11:53 am

Re: binser + middleclass, I think I'm confused...

Post by MrFariator »

I've only used Bitser (https://github.com/gvx/bitser) myself, but I think the issue is that you're registering an instance of a class, as opposed to the class itself.

ie. from your example code instead of

Code: Select all

somevariable = binser.registerClass(testclass) -- passing an instance of Testclass
do

Code: Select all

somevariable = binser.registerClass(Testclass) -- passing Testclass
instead.
User avatar
mutantant
Prole
Posts: 6
Joined: Tue Feb 05, 2013 12:02 am

[Solved by changing libs] Re: binser + middleclass, I think I'm confused...

Post by mutantant »

MrFariator wrote: Sat Mar 23, 2019 8:31 pm I've only used Bitser (https://github.com/gvx/bitser) myself, but I think the issue is that you're registering an instance of a class, as opposed to the class itself.

ie. from your example code instead of

Code: Select all

somevariable = binser.registerClass(testclass) -- passing an instance of Testclass
do

Code: Select all

somevariable = binser.registerClass(Testclass) -- passing Testclass
instead.
Thanks for the answer. Yes, I actually tried both the instance and the class itself, both with the same results. And just for giggles, I tried to call binser.registerClass without assigning it to a variable at all. No joy.

But, since I saw it in your reply, I tried bitser instead. And lo and behold, doing nearly the exact same thing (with the mere change of the serialize and deserialize functions being named different in the two libs) it worked first time.

This isn't to say binser isn't great, it's more likely I'm just an idiot and missed a step.

But for now, I think bitser is what I'll use as it worked first try for me.

And just for completeness, following is the test code I used to check it (forgive the obvious poor coding quality, as it's just junk code to be sure I know what the hell I'm doing before using it in production code).

Code: Select all

bitser = require 'libs.bitser'

local Testclass = class('Testclass')
function Testclass:print()
    print("Yo momma!")
end
local testclass = Testclass()
print(testclass:print())
bitser.registerClass(Testclass)
local testclass2 = bitser.loads(bitser.dumps(testclass))
print(testclass2:print())
Thanks again!

p.s. Obviously, the code above ends up with both objects referring to one table. testclass2 ends up pointing to testclass, and changing one changes the other, etc. Only a mad(wo)man would do as I did in the test code, but it worked well enough to be sure the two-way street of serializing and deserializing worked as expected!
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 4 guests