/du-serializer

A Lua library to serialize table in string in Dual Universe to improve performances and reduce size to store data in databanks or for transmissions.

Primary LanguageLuaGNU Lesser General Public License v2.1LGPL-2.1

du-serializer

Tests

A Lua library to serialize table in string in Dual Universe to improve performances and reduce size to store data in databanks or for transmissions.

Documentation

serialize(table t)

Serialize a table t and return a string. Tables and arrays are supported, not mixed table. See serializer_spec.lua for supported and unsupported cases.

deserialize(string s)

Deserialize a serialized table as string s and return the table.

Benchmark

Following graphs are the results of the benchmark, encoding and decoding a simple test table in bench.lua multiple times. Dkjson is a popular JSON encoding/decoding library in Lua, which is embedded by default in Dual Universe. Pure Lua Json, is a compact pure-Lua alternative library for JSON encoding/decoding, can be found at json.lua 0.1.2, created by Rxi.

This benchmark has been done in Dual Universe, on a clean programming board. The missing data for the dkjson and pure lua json is due to the fact it trigger the CPU OVERLOAD error in the game.

*Benchmark updated the 09/25/2021.*

How to use

To use this library in Dual Universe, you can simply copy the lua code and paste it in a Library slot. It can be used to compute transmissions for the du-socket library. See below an example :

local player = {
  id = 999,
  name = "Username",
  pos = { 1, 2, 3},
  org = "Org name",
  relation = 0
}

local s = serialize(player) -->  {relation=0,org="Org name",pos={1,2,3},id=999,name="Username"}
local t = deserialize(s)
print(t.name) --> Username

Keep in mind that for Lua-keyed tables, they are not ordered. However, ordinary arrays using integer indexes are ordered. (As seen in the above example, the pos data within that field will be ordered, but the position that the pos key takes will not.)

Credits

Thanks to Arialia for her collaboration from the game organization Silentium