This is a npm package that can serialize array and object to Lua table, or unserialize Lua table to array and object.
$ npm install
$ npm run dev
$ npm run build
Serialize
javascript
variable tolua
data string.
import * as luadata from 'luadata';
const v = {
some: 'luadata',
};
luadata.serializer.serialize(v); // '{some="luadata"}'
Control if stringified data should be human-read formatted.
import * as luadata from 'luadata';
const v = {
some: 'luadata',
};
luadata.serializer.serialize(v, { indent: " " });
Output
{
some = "luadata",
}
Control stringified data should be human-read formatted at which level, notice that first line will not be automatic indented.
import * as luadata from 'luadata';
const v = {
some: 'luadata',
};
luadata.serializer.serialize(v, { indent: " ", indentLevel: 1 });
Output
{
some = "luadata",
}
Control if the stringified data is a multi-value luadata.
import * as luadata from 'luadata';
const v = [
'This is a tuple',
{ a: 1 },
];
luadata.serializer.serialize(v, { tuple: true }); // 'This is a tuple',{a=1}
Unserialize
lua
data string tojavascript
variable.
import * as luadata from 'luadata';
const luadata_str = "{a=1,b=2,3}";
luadata.serializer.unserialize(luadata_str); // new Map([["a", 1], ["b", 2], [1, 3]])
luadata.serializer.unserialize(luadata_str, { dictType: 'object' }); // { a: 1, b: 2, 3: 3 }
Control if the
lua
data string is a tuple variable.
import * as luadata from 'luadata';
const luadata_str = "'This is a tuple',1,false";
luadata.serializer.unserialize(luadata_str, { tuple: true }); // ['This is a tuple', 1, false]
Control how will the luadata table will be transformed into javascript variable. Due to javascript limitation that javascript object key must be string or symbol,
object
mode will cause data/typing loss.
import * as luadata from 'luadata';
const luadata_str = "{a=1,b=2,['3']='three',[3]=3}";
luadata.serializer.unserialize(luadata_str, { dictType: 'map' }); // new Map([["a", 1], ["b", 2], ["3", "three"], [3, 3]])
luadata.serializer.unserialize(luadata_str, { dictType: 'object' }); // { a: 1, b: 2, 3: 3 }
Provide luadata _G environment, supports both object like or map like. Due to javascript limitation that javascript object key must be string or symbol,
object
mode will cause data/typing loss.
import * as luadata from 'luadata';
luadata.serializer.unserialize("a", { global: { a: 1 } }); // 1
luadata.serializer.unserialize("a['b'].c", { global: { a: { b: { c: 1 } } } }); // 1
luadata.serializer.unserialize("a.b", { global: { a: { b: { c: 1 } } } }); // new Map([["c": 1]])
luadata.serializer.unserialize("a.b", { global: { a: { b: { c: [1] } } } }); // new Map([["c": [1]]])
luadata.serializer.unserialize("a.b", { global: { a: { b: { c: 1 } } }, dictType: 'object' }); // { c: 1 }
luadata.serializer.unserialize("a.b", { global: { a: { b: { c: [1] } } }, dictType: 'object' }); // { c: [1] }
Control if non-exists global variable is allowed, default value is
true
.
import * as luadata from 'luadata';
luadata.serializer.unserialize("b", { global: { a: 1 } }); // Error: attempt to refer a non-exists global variable.
luadata.serializer.unserialize("b", { global: { a: 1 }, strictGlobal: false }); // undefined
BSD