mirror of
https://github.com/gvx/bitser.git
synced 2024-11-24 06:54:20 +00:00
Update USAGE.md
This commit is contained in:
parent
b0e1be678f
commit
6403e4cc6b
80
USAGE.md
80
USAGE.md
@ -2,6 +2,17 @@
|
|||||||
* [Serializing class instances](#serializing-class-instances)
|
* [Serializing class instances](#serializing-class-instances)
|
||||||
* [Advanced usage](#advanced-usage)
|
* [Advanced usage](#advanced-usage)
|
||||||
* [Reference](#reference)
|
* [Reference](#reference)
|
||||||
|
* [`bitser.dumps`](#dumps)
|
||||||
|
* [`bitser.dumpLoveFile`](#dumplovefile)
|
||||||
|
* [`bitser.loads`](#loads)
|
||||||
|
* [`bitser.loadData`](#loaddata)
|
||||||
|
* [`bitser.loadLoveFile`](#loadlovefile)
|
||||||
|
* [`bitser.register`](#register)
|
||||||
|
* [`bitser.registerClass`](#registerclass)
|
||||||
|
* [`bitser.unregister`](#unregister)
|
||||||
|
* [`bitser.unregisterClass`](#unregisterclass)
|
||||||
|
* [`bitser.reserveBuffer`](#reservebuffer)
|
||||||
|
* [`bitser.clearBuffer`](#clearbuffer)
|
||||||
|
|
||||||
# Basic usage
|
# Basic usage
|
||||||
|
|
||||||
@ -20,7 +31,35 @@ contains one of those. If you need to, look into [`bitser.register`](#register).
|
|||||||
|
|
||||||
# Serializing class instances
|
# Serializing class instances
|
||||||
|
|
||||||
(I need to finish this section.)
|
All you need to make bitser correctly serialize your class instances is register that class:
|
||||||
|
|
||||||
|
```lua
|
||||||
|
-- this is usually enough
|
||||||
|
bitser.registerClass(MyClass)
|
||||||
|
|
||||||
|
-- if you use Slither, you can add it to __attributes__
|
||||||
|
class 'MyClass' {
|
||||||
|
__attributes__ = {bitser.registerClass},
|
||||||
|
-- insert rest of class here
|
||||||
|
}
|
||||||
|
|
||||||
|
local data = bitser.dumps(MyClass(42))
|
||||||
|
local instance = bitser.loads(data)
|
||||||
|
```
|
||||||
|
|
||||||
|
Note that classnames need to be unique to avoid confusion, so if you have two different classes named `Foo` you'll need to do
|
||||||
|
something like:
|
||||||
|
|
||||||
|
```lua
|
||||||
|
-- in module_a.lua
|
||||||
|
bitser.registerClass('module_a.Foo', Foo)
|
||||||
|
|
||||||
|
-- in module_b.lua
|
||||||
|
bitser.registerClass('module_b.Foo', Foo)
|
||||||
|
```
|
||||||
|
|
||||||
|
See the reference sections on [`bitser.registerClass`](#registerclass) and
|
||||||
|
[`bitser.unregisterClass`](#unregisterclass) for more information.
|
||||||
|
|
||||||
## Supported class libraries
|
## Supported class libraries
|
||||||
|
|
||||||
@ -32,14 +71,29 @@ contains one of those. If you need to, look into [`bitser.register`](#register).
|
|||||||
|
|
||||||
# Advanced usage
|
# Advanced usage
|
||||||
|
|
||||||
(I need to finish this section.)
|
If you use [LÖVE](https://love2d.org/), you'll want to use [`bitser.dumpLoveFile`](#dumplovefile) and [`bitser.loadLoveFile`](#loadlovefile) if you want to serialize to the save directory. You also might have images and other resources that you'll need to register, like follows:
|
||||||
|
|
||||||
|
```lua
|
||||||
|
function love.load()
|
||||||
|
bad_guy_img = bitser.register('bad_guy_img', love.graphics.newImage('img/bad_guy.png'))
|
||||||
|
if love.filesystem.exists('save_point.dat') then
|
||||||
|
level_data = bitser.loadLoveFile('save_point.dat')
|
||||||
|
else
|
||||||
|
level_data = create_level_data()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function save_point_reached()
|
||||||
|
bitser.dumpLoveFile('save_point.dat', level_data)
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
# Reference
|
# Reference
|
||||||
|
|
||||||
## dumps
|
## dumps
|
||||||
|
|
||||||
```lua
|
```lua
|
||||||
local string = bitser.dumps(value)
|
string = bitser.dumps(value)
|
||||||
```
|
```
|
||||||
|
|
||||||
Basic serialization of `value` into a Lua string.
|
Basic serialization of `value` into a Lua string.
|
||||||
@ -60,7 +114,7 @@ See also: [`bitser.loadLoveFile`](#loadlovefile).
|
|||||||
## loads
|
## loads
|
||||||
|
|
||||||
```lua
|
```lua
|
||||||
local value = bitser.loads(string)
|
value = bitser.loads(string)
|
||||||
```
|
```
|
||||||
|
|
||||||
Deserializes `value` from `string`.
|
Deserializes `value` from `string`.
|
||||||
@ -70,7 +124,7 @@ See also: [`bitser.dumps`](#dumps).
|
|||||||
## loadData
|
## loadData
|
||||||
|
|
||||||
```lua
|
```lua
|
||||||
local value = bitser.loadData(light_userdata, size)
|
value = bitser.loadData(light_userdata, size)
|
||||||
```
|
```
|
||||||
|
|
||||||
Deserializes `value` from raw data. You probably won't need to use this function ever.
|
Deserializes `value` from raw data. You probably won't need to use this function ever.
|
||||||
@ -78,7 +132,7 @@ Deserializes `value` from raw data. You probably won't need to use this function
|
|||||||
## loadLoveFile
|
## loadLoveFile
|
||||||
|
|
||||||
```lua
|
```lua
|
||||||
local value = bitser.loadLoveFile(file_name)
|
value = bitser.loadLoveFile(file_name)
|
||||||
```
|
```
|
||||||
|
|
||||||
Reads from `file_name` and deserializes `value` more efficiently than reading the file and then deserializing that string.
|
Reads from `file_name` and deserializes `value` more efficiently than reading the file and then deserializing that string.
|
||||||
@ -89,21 +143,23 @@ See also: [`bitser.dumpLoveFile`](#dumplovefile).
|
|||||||
## register
|
## register
|
||||||
|
|
||||||
```lua
|
```lua
|
||||||
bitser.register(name, resource)
|
resource = bitser.register(name, resource)
|
||||||
```
|
```
|
||||||
|
|
||||||
Registers the value `resource` with the name `name`, which has to be a unique string. Registering static resources like images,
|
Registers the value `resource` with the name `name`, which has to be a unique string. Registering static resources like images,
|
||||||
functions, classes and huge strings, makes sure bitser doesn't attempt to serialize them, but only stores a named
|
functions, classes and huge strings, makes sure bitser doesn't attempt to serialize them, but only stores a named
|
||||||
reference to them.
|
reference to them.
|
||||||
|
|
||||||
|
Returns the registered resource as a convenience.
|
||||||
|
|
||||||
See also: [`bitser.unregister`](#unregister).
|
See also: [`bitser.unregister`](#unregister).
|
||||||
|
|
||||||
## registerClass
|
## registerClass
|
||||||
|
|
||||||
```lua
|
```lua
|
||||||
bitser.registerClass(class)
|
class = bitser.registerClass(class)
|
||||||
bitser.registerClass(name, class)
|
class = bitser.registerClass(name, class)
|
||||||
bitser.registerClass(name, class, classkey, deserializer)
|
class = bitser.registerClass(name, class, classkey, deserializer)
|
||||||
```
|
```
|
||||||
|
|
||||||
Registers the class `class`, so that bitser can correctly serialize and deserialize instances of `class`.
|
Registers the class `class`, so that bitser can correctly serialize and deserialize instances of `class`.
|
||||||
@ -117,7 +173,7 @@ Class names also have to be unique, so if you use multiple classes with the same
|
|||||||
variant as well to give them different names.
|
variant as well to give them different names.
|
||||||
|
|
||||||
The arguments `classkey` and `deserializer` exist so you can hook in unsupported class libraries without needing
|
The arguments `classkey` and `deserializer` exist so you can hook in unsupported class libraries without needing
|
||||||
to patch bitser. [See the list of supported class libraries](#supported-class-libraries)
|
to patch bitser. [See the list of supported class libraries](#supported-class-libraries).
|
||||||
|
|
||||||
If not nil, the argument `classkey` should be a string such that
|
If not nil, the argument `classkey` should be a string such that
|
||||||
`rawget(obj, classkey) == class` for any `obj` whose type is `class`. This is done so that key is skipped for serialization.
|
`rawget(obj, classkey) == class` for any `obj` whose type is `class`. This is done so that key is skipped for serialization.
|
||||||
@ -125,6 +181,8 @@ If not nil, the argument `classkey` should be a string such that
|
|||||||
If not nil, the argument `deserializer` should be a function such that `deserializer(obj, class)` returns a valid
|
If not nil, the argument `deserializer` should be a function such that `deserializer(obj, class)` returns a valid
|
||||||
instance of `class` with the properties of `obj`. `deserializer` is allowed to mutate `obj`.
|
instance of `class` with the properties of `obj`. `deserializer` is allowed to mutate `obj`.
|
||||||
|
|
||||||
|
Returns the registered resource as a convenience.
|
||||||
|
|
||||||
See also: [`bitser.unregisterClass`](#unregisterclass).
|
See also: [`bitser.unregisterClass`](#unregisterclass).
|
||||||
|
|
||||||
## unregister
|
## unregister
|
||||||
|
Loading…
Reference in New Issue
Block a user