The data
library provides parsers and converts for various data forms.
local data = require("data");
Converts the Lua table to JSON.
tbl = { foo = 123, bar = "hello world" };
json = data.tojson(tbl);
Converts the Lua table to nicely formatted JSON.
tbl = { foo = 123, bar = "hello world" };
json = data.tojsonpretty(tbl);
json = "{ \"foo\" = 123, \"bar\" = \"hello world\" }";
tbl = data.fromjson(json);
xml = "<foo><bar>hello world</bar></foo>";
root = data.fromxml(xml);
An XML element is represented by an object that looks like this:
{ name = "bar",
text = "hello world",
attributes = { ... },
children = { ... } }
The number of child elements can be retreived like this:
#root.children;
A child element is retreived like this:
root.children[1];
Attributes are retreived like this:
root.attributes["foo"];
Converts the specified data
to a base-64 encoded string.
b64str = data.tobase64(...);
Converts str
from a base-64 encoded string.
data = data.frombase64("...");
Converts the specified data
to a base-32 encoded string.
b32str = data.tobase64(...);
Converts str
from a base-32 encoded string.
data = data.frombase32("...");
Converts seconds
to a time span string with the following format: [HH:]MM:SS.
print(data.sec2span(754)); -- 12:34
Converts a time span string to the number of seconds that it represents.
print(data.span2sec("12:34")); -- 754
Gets the current UNIX timestamp (milliseconds).
print(data.timestamp()); -- 1424096219
Generates an unsigned 64-bit integer nonce.
data.nonce();
Computes the binary hash of the data
using the specified digest engine.
Supported engines: MD5
, SHA1
, SHA256
, SHA512
, etc. See the OpenSSL documentation for a list of supported digest algorithms.
data.digest("sha1", "foobar");
Computes the hex hash of the data
using the specified digest engine.
Supported engines: MD5
, SHA1
, SHA256
, SHA512
, etc. See the OpenSSL documentation for a list of supported digest algorithms.
data.digesthex("sha1", "foobar");