How to serialize ANY table?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
MrFariator
Party member
Posts: 548
Joined: Wed Oct 05, 2016 11:53 am

Re: How to serialize ANY table?

Post by MrFariator »

You can not comment out the line, because then bitser will effectively stop writing out stuff. Instead you need to modify the "types" table on line 246, which defines the serializer function to use for each data type, so something like this:

Code: Select all

local types = {
  number = write_number, 
  string = write_string, 
  table = write_table, 
  boolean = write_boolean, 
  ["nil"] = write_nil, 
  cdata = write_cdata, 
  userdata = write_nil -- use write_nil, or create your custom serializer
}
20_NickName_20
Prole
Posts: 7
Joined: Thu Apr 21, 2022 3:16 pm

Re: How to serialize ANY table?

Post by 20_NickName_20 »

MrFariator wrote: Mon Apr 25, 2022 7:11 pm You can not comment out the line, because then bitser will effectively stop writing out stuff. Instead you need to modify the "types" table on line 246, which defines the serializer function to use for each data type, so something like this:

Code: Select all

local types = {
  number = write_number, 
  string = write_string, 
  table = write_table, 
  boolean = write_boolean, 
  ["nil"] = write_nil, 
  cdata = write_cdata, 
  userdata = write_nil -- use write_nil, or create your custom serializer
}

This works, but when I try to deserialize the data, an error appears:

Error

bitser.lua:324: table index is nil

Traceback

[love "callbacks.lua"]:228: in function 'handler'
bitser.lua:324: in function 'deserialize_value'
bitser.lua:324: in function 'loads'
main.lua:50: in function 'draw'
[love "callbacks.lua"]:168: in function <[love "callbacks.lua"]:144>
[C]: in function 'xpcall'
User avatar
EngineerSmith
Prole
Posts: 38
Joined: Thu Dec 02, 2021 11:38 am
Contact:

Re: How to serialize ANY table?

Post by EngineerSmith »

Not sure if this question is still open, but with love11.4 luajit was updated to include `string.buffer` library. So you can now use that to serialize lua tables. If you're using Ubuntu, some program managers don't install the correct version of luajit with love11.4 so they don't have `string.buffer`. So you can get around this by using the app image from the official site

Code: Select all

local buffer = require("string.buffer")
local tbl= {
	"hello",
	value = "world"
}
print(tbl[0], tbl.value) -- hello   world
local str = buffer.encode(tbl)
local decodedTbl = buffer.decode(str)
print(decodedTbl[0], decodedTbl.value) -- hello   world
print(love.data.encode("base64", "string", str)) -- you can just print the encoded string, but it can have new line values as it is binary data so I prefer to encode it
If you know what values are going to be in your lua table, you can optimize it (just note that these string keys must always be in the same order for encoding or decoding otherwise it won't know what key to use in the correct place)

Code: Select all

local buffer = require("string.buffer")
local options = {
  dict = { "commonly", "used", "string", "keys" },
}
local buf_enc = buffer.new(options)
local buf_dec = buffer.new(options)

local function encode(obj)
  return buf_enc:reset():encode(obj):get()
end

local function decode(str)
  return buf_dec:set(str):decode()
end
Full docs are here: https://repo.or.cz/luajit-2.0.git/blob_ ... uffer.html
Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests