Skip to content

AceSerializer-3.0

embeddable This library can be embedded into your addon as a mixin; list it when you create the addon and its methods become available directly on your addon object (via self).
Creating an addon object →

AceSerializer-3.0 can serialize any variable (except functions or userdata) into a string format that can be sent over the addon communication channel.

AceSerializer was designed to keep all data intact, especially very large numbers or floating point numbers, and table structures. The only caveat currently is, that multiple references to the same table will be send individually.

Usage

Mix it in when creating your addon object:

lua
MyAddon = LibStub("AceAddon-3.0"):NewAddon("MyAddon", "AceSerializer-3.0")

Serialize any values into a string:

lua
local MyVal1 = 23
local MyVal2 = "some text"
local MyVal3 = { "foo", 42, "bar" }

local serializedData = MyAddon:Serialize(MyVal1, MyVal2, MyVal3)

Deserialize. The first return is a success boolean; on success the original values follow, on failure an error message:

lua
local success, v1, v2, v3 = MyAddon:Deserialize(serializedData)
if not success then
    -- handle error (v1 is the error message)
end

Combine with AceComm-3.0 to send structured data between clients.

API Reference

Embed

method#
lua
AceSerializer:Embed(target)

Copies AceSerializer's methods onto target so you can call them on it directly. See The :Embed method.

Parameters

ParameterTypeDefaultDescription
targettableThe target object to embed AceSerializer into.

Returns

TypeDescription
tableThe target object.

Serialize

method#
lua
AceSerializer:Serialize(...)

Serialize the data passed into the function.

Takes a list of values (strings, numbers, booleans, nils, tables) and returns it in serialized form (a string). May throw errors on invalid data types.

Parameters

ParameterTypeDefaultDescription
...anyList of values to serialize (strings, numbers, booleans, nils, tables).

Returns

TypeDescription
stringThe data in its serialized form.

Deserialize

method#
lua
AceSerializer:Deserialize(str)

Deserializes the data into its original values.

Accepts serialized data, ignoring all control characters and whitespace.

Parameters

ParameterTypeDefaultDescription
strstringThe serialized data (from :Serialize).

Returns

TypeDescription
booleansuccess: true on success, false on failure.
any...: On success, the deserialized list of values; on failure, the error message (string).

Example

lua
local ok, value = self:Deserialize(payload)
if ok then
    -- use value
end

Ace3, a World of Warcraft addon framework.