https://github.com/rochet2/luaserializer
LuaSerializer is a pure lua serializer that does not use loadstring or pcall for table deserialization
https://github.com/rochet2/luaserializer
lua lua-serializer serializer
Last synced: 20 days ago
JSON representation
LuaSerializer is a pure lua serializer that does not use loadstring or pcall for table deserialization
- Host: GitHub
- URL: https://github.com/rochet2/luaserializer
- Owner: Rochet2
- License: gpl-2.0
- Created: 2015-06-01T21:27:34.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2017-02-01T18:50:18.000Z (about 9 years ago)
- Last Synced: 2025-05-06T20:52:51.498Z (11 months ago)
- Topics: lua, lua-serializer, serializer
- Language: Lua
- Homepage: http://rochet2.github.io/
- Size: 16.6 KB
- Stars: 12
- Watchers: 2
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# LuaSerializer
It is recommended to use smallfolk instead. It is a lot faster and creates smaller and more readable output. It also has a C++ version for C++ compatibility needs: https://github.com/gvx/Smallfolk https://github.com/Rochet2/smallfolk_cpp
LuaSerializer is a pure lua serializer that does not use loadstring or pcall for table deserialization. Works with Lua 5.1 and 5.2.
Backlink: https://github.com/Rochet2/LuaSerializer
#Limitations
- Tables with cycles can not be serialized.
- Metatables are not serialized.
- Userdata can not be serialized
- Functions can not be serialized, but you can try serialize string.dump or the function contents as string
- Compression safety is questionable for unknown source data, use it only for server->client or otherwise safe assumed data
#Serializing
LuaSerializer serializes data into a string and is able to then deserialize the data without using loadstring or pcall (safely, not calling functions).
LuaSerializer is capable of safely serializing and deserializing:
- nil
- bool
- string
- number including nan and inf
- tables with no unserializable data and no cycles
#API
```lua
local LuaSerializer = LuaSerializer or require("LuaSerializer")
-- Some lua compatibility between 5.1 and 5.2
local unpack = unpack or table.unpack
-- Takes in values and returns a string with them serialized
-- Uses LZW compression, use LuaSerializer.serialize_nocompress if you dont want this
-- LuaSerializer.serialize(...)
-- Takes in a string of serialized data and returns a table with the values in it and the amount of values
-- The data must have been serialized with LuaSerializer.serialize_nocompress
-- LuaSerializer.unserialize(serializeddata)
local serialized = LuaSerializer.serialize(55, "test", {1,2, y = 66}, nil, true)
local data, n = LuaSerializer.unserialize(serialized)
print(unpack(data, 1, n))
-- prints:
-- 55 test table: 491A9920 nil true
-- Takes in values and returns a string with them serialized
-- Does not compress the result
-- LuaSerializer.serialize_nocompress(...)
-- Takes in a string of serialized data and returns a table with the values in it and the amount of values
-- The data must have been serialized with LuaSerializer.serialize
-- LuaSerializer.unserialize_nocompress(serializeddata)
local serialized = LuaSerializer.serialize_nocompress(55, "test", {1,2, y = 66}, nil, true)
local data, n = LuaSerializer.unserialize_nocompress(serialized)
print(unpack(data, 1, n))
-- prints:
-- 55 test table: 491A9920 nil true
```
#Included dependencies
You do not need to get these, they are already included
- Compression for string data: https://love2d.org/wiki/TLTools
#Special thanks
- Kenuvis < [Gate](http://www.ac-web.org/forums/showthread.php?148415-LUA-Gate-Project), [ElunaGate](https://github.com/ElunaLuaEngine/ElunaGate) >
- Laurea (alexeng) < https://github.com/Alexeng >
- Lua contributors < http://www.lua.org/ >