在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):bakpakin/binser开源软件地址(OpenSource Url):https://github.com/bakpakin/binser开源编程语言(OpenSource Language):Lua 100.0%开源软件介绍(OpenSource Introduction):binser - Customizable Lua SerializerThere already exists a number of serializers for Lua, each with their own uses, limitations, and quirks. binser is yet another robust, pure Lua serializer that specializes in serializing Lua data with lots of userdata and custom classes and types. binser is a binary serializer and does not serialize data into human readable representation or use the Lua parser to read expressions. This makes it safe and moderately fast, especially on LuaJIT. binser also handles cycles, self-references, and metatables. How to UseExamplelocal binser = require "binser"
local mydata = binser.serialize(45, {4, 8, 12, 16}, "Hello, World!")
print(binser.deserializeN(mydata, 3))
-- 45 table: 0x7fa60054bdb0 Hello, World! Serializing and Deserializinglocal str = binser.serialize(...) Serialize (almost) any Lua data into a Lua string. Numbers, strings, tables,
booleans, and nil are all fully supported by default. Custom userdata and custom
types, both identified by metatables, can also be supported by specifying a
custom serialization function. Unserializable data should throw an error. Aliased to local results, len = binser.deserialize(str[, index]) Deserialize any string previously serialized by binser. Can optionally start at
an index in the string (to drop leading characters). Index is 1 by default. Unrecognized data should
throw an error. Results is a list of length len. Aliased to local ... = binser.deserializeN(str, n[, index]) Deserializes at most n values from str. The default value for n is one,
so Custom typeslocal metatable = binser.register(metatable, name, serialize, deserialize) Registers a custom type, identified by its metatable, to be serialized. Registering types has two main purposes. First, it allows custom serialization and deserialization for userdata and tables that contain userdata, which can't otherwise be serialized in a uniform way. Second, it allows efficient serialization of small tables with large metatables, as registered metatables are not serialized. The If local class = binser.registerClass(class[, name]) Registers a class as a custom type. binser currently supports 30log and
middleclass. local metatable = binser.unregister(name) Users should seldom need this, but to explicitly unregister a type, call this. TemplatesIf binser's already compact serialization isn't enough, and you don't want to write
complex and error prone custom serializers, binser has a functionality called templating.
Templates specify the layout of a custom type, so that table keys don't need to be serialized
many times. To specify a template, add the An example: local template = {
"name", "age", "salary", "email",
nested = {"more", "nested", "keys"}
}
local Employee_MT = {
name = "Employee",
}
local joe = setmetatable({
name = "Joe",
age = 11,
salary = "$1,000,000",
email = "[email protected]",
nested = {
more = "blah",
nested = "FUBAR",
keys = "lost"
}
}, Employee_MT)
-- Print length of serialized employee without templating
-- 117
binser.registerClass(Employee_MT)
print(#binser.s(joe))
binser.unregister(Employee_MT)
-- Print length of serialized employee with templating
-- 72
Employee_MT._template = template
binser.registerClass(Employee_MT)
print(#binser.s(joe)) In the above example, the resulting serialized value with templating is nearly half of the size of the default table serialization. ResourcesIf there are certain objects that don't need to be serialized at all, like images, audio, or any system resource, binser can mark them as such to only serialize a reference to them. Resources must be registered in a similar way to custom types and given a unique name. local resource = binser.registerResource(resource, name) Registers a resource. local resource = binser.unregisterResource(name) Resources can be unregistered in a similar manner as custom types. File IOMostly for convenience, binser has functions for writing and reading to files. These work through Lua's built in IO. binser.writeFile(filepath, ...) Serializes Lua objects and writes them to a file. Overwrites the previous file. binser.appendFile(filepath, ...) Same as writing to a file, but doesn't overwrite the old file. local results, len = binser.readFile(filepath) Reads and deserializes a file. The trio of file convenience function have shortened aliases as well.
WhyMost Lua serializers serialize into valid Lua code, which while very useful, makes it impossible to do things like custom serialization and deserialization. binser was originally written as a way to save game levels with images and other native resources, but is extremely general. LuaRocksbinser is available as a rock on LuaRocks. Install via:
Testingbinser uses busted for testing. Install and
run Notes
BugsPull requests are welcome, please help me squash bugs! |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论