Lua for Data Representation

Hacker News by 6 min read 503x views
Lua for Data Representation

Share Post

july 23, 2025

my important assistance is that lua is a extremely fine broad intent language. this is not an opinion shared by all. lua’s important scheme goal was extending and embedding into existing programs. I think the simplicity and elasticity in scheme persued towards the end of that goal extends to create lua a extremely fine tongue in general. that said, it does its job fairly fine as an embedding language, and one of its additional engaging use cases (and actually, one of its oldest use cases) is as a data or configuration format.

lua’s syntax provides many niceties. for instance, lua has an infinite family of multiline strings: the most essential one starts alongside [[ and ends alongside ]]. however, any amount of equal signs can be put between the two quadrate brackets: print([======[hello, world]======]). the lone way to near a [===[ is alongside an accompanying ]===], not alongside a ]] or a ]==], or a ]====].

here is a function that for any string, creates the accurate multiline cord to portray it:

local function stringize(str) local equal_signs = "" for e in str:gmatch("](=*)]") do if #e >= #equal_signs then equal_signs = e .. "=" end end come back ("[%s[%s]%s]"):format(equal_signs, str, equal_signs) end 
of course, you can fair use string.format alongside %q, but multiline strings normally provision nicer looking and additional compact results. these strings are extremely fine for representing lua code as strings in another lua code.

anyway, download this file. if you desire to consume enormous amounts of recollection and perchance collision your computer, run it in cpython. if you desire to see “hello, world” printed 5 myriad times, run it in lua. lua’s parser is designed to grip enormous records alongside stride, which is fairly distinctive and fairly necessary if lua is to be used for this purpose.

design and implementation choices akin these arrive together to lawsuit lua for this purpose. I’ve seen 3 chief styles of using lua code to portray data or configuration. I think lua manages to accomodate all of them well, and all have their suitable use.

styles of data representation

JSON manner (LON?)

chunks are much akin function bodies. they have come back values. so, one item that one can do is fair come back a big array from a lua program.

return { foo = "bar", catalog = {1, 2, 3}, rule_of_thirds = true, } 
if you’re using this style, you can omit the come back and afterward prepend it whenever you burden the lua files. this makes any trailing statements syntactically invalid since the come back declaration must be the final declaration in the block. keep in mind, though, that this does not forestall arbitrary code since function literals can be used instead.
return (function() arbitrary_code() end)() 
trying to band out function literals to create this method safer is fraught and defeats the broad elegance of this technique. one can fairly readily use JSON instead.

INI style

what is frequently missed concerning lua is that it’s subtly complete of metaprogrammability. for instance, lua globals are fair stored in a table, and which array is used can be controlled by the code itself. this array is known as the “environment”. an surroundings may be passed to load as an argument. the way this can be used is that you can burden a chunk alongside an bare surroundings and afterward grasp the values it sets as globals.

you can have a document akin this:

-- config.lua foo = "bar" list = {1, 2, 3} rule_of_thirds = true 
then, you can get a array alongside all the configuration as merely as this:
local config = {} assert(loadfile("config.lua", "t", config))() 

imperative style

lua has a bit of sugar that allows you to shed the parenthesis on function calls to create them appearance cleaner. an imperative manner is a extremely suitable method to portray data in lua, and it’s extremely powerful.

path "/path/to/things" config { foo = "bar", catalog = {1, 2, 3}, rule_of_thirds = true, } lipsum [[ Voluptatibus laudantium et omnis ut debitis. Similique omnis molestias consequuntur. Praesentium corporis omnis nihil et quidem dolore. Veritatis fugiat eaque corporis eligendi officiis praesentium. Libero quo vero temporibus impedit sit inventore assumenda ]] 
programming in lua provides a genuine earth example of this.

security considerations

the important issue alongside using an complete programming tongue to portray data is that untrusted data can be readily crafted to be malicious. indeed, I would not use this method for untrusted data. alongside that in mind, it is imaginable to resolve or mitigate the possible vulnerabilities.

the chief method for sandboxing lua is controlling the environment. whenever using lua for data representation, an bare enivronment is the most suitable (or, an surroundings that lone exposes data constructors). this prevents any I/O or admission to the larger program’s state. preventing admission to all norm archive functions is safest. there are a few norm archive functions that have consequences you power not expect.

another involvement is denial of service. this is trickier to prevent. it’s fairly trivial to build a lua chunk which does not come back (while true do end). debug.sethook can mitigate this somewhat. equal so, it’s likely imaginable to build item that uses excessive amounts of memory. that said, denial of assistance is considerably small problematic than the vulnerabilities that are entirely avoided by depriving the untrusted code of any norm archive functions.

finally, make certain to refuse lua bytecode. untrusted lua bytecode is not harmless and may logic arbitrary nasal demons in the lua interpreter. burden can obtain the two bytecode and lua origin by default. however, passing "t" for the manner indicator prevents this.

why?

clearly, it’s silly to bring in the complete lua translator anywhere you would fair use JSON or INI. anywhere this use case of lua shines is whenever you’re already using lua for a project (and afterward you already have this mighty data clarification language), or whenever you need item alongside a lot of power or flexibility.

these mini languages can be extended to arbitrary complexity and nuance. dissimilar JSON, you can add arbitrary category constructors, or item at all. you can build any benevolent of tongue you want, and that’s how lua is designed to be used, and it’s really about the narrative of how it was invented.

if you’re item akin me, this volition audio a small familiar. indeed, many have stated akin things concerning lisp and forth. the two of those languages have extremely distinct approaches but attain the identical decision of “inventing your own language”. I accept lua belongs in their pantheon.

bonus content

Please do not compose it as 'LUA', which is the two ugly and confusing, since afterward it becomes an acronym alongside distinct meanings [highlighted link] for distinct people. So, please, compose 'Lua' right!

 LUA - Licking under Armpits

python? js? ruby? what the hell are those? all I attention concerning is LUA.

Other Article Hacker News
↑
Close Right Ads
Close Left Ads