bitser/examples/templates.lua
Jasmijn Wellner 71d2d7d73f Add an extension for #24
This is only a simplified system. A full implementation would need to add support for nested keys, and also support the same set of class libraries that bitser does.
2024-12-29 19:33:44 +01:00

39 lines
892 B
Lua

-- how to use the extension API to implement templates ala
-- https://github.com/bakpakin/binser#templates
local bitser = require 'bitser'
local bitser_templates = require 'examples.bitser-templates'
bitser.registerExtension('templates', bitser_templates)
local template = {
"name", "age", "salary", "email", "nested"
--nested = {'more', 'nested', 'keys'}
}
local Employee_MT = {
__name__ = 'Employee_MT',
name = "Employee",
_template = template
}
Employee_MT.__index = Employee_MT
local joe = setmetatable({
name = "Joe",
age = 11,
salary = "$1,000,000",
email = "joe@example.com",
nested = {
more = "blah",
nested = "FUBAR",
keys = "lost"
}
}, Employee_MT)
bitser.registerClass(Employee_MT)
print('without templates:', #bitser.dumps(joe))
bitser.unregisterClass('Employee_MT')
bitser_templates.registerClass(Employee_MT)
print('with templates:', #bitser.dumps(joe))