An unofficial, read-only Hacker News reader.

Lua for data representation (2025) (opens external site)

104 points by vinhnx 4 days ago | citrons.xyz | 61 comments on HN

View full thread · View on Hacker News

Comment thread (16)

  1. Most developers don’t care about Roblox. Lua is cute but awkward.

    Let’s see, no native json support. This day and age it should be built in. No array support: is {} an empty json object or empty json array? Just add the dang arrays already.

    LuaJIT is very awesome! Amazingly fast. Does it support the latest Lua? Nope it doesn’t. Lua is at 5.5 or something, LuaJIT at 5.2 maybe.

    Default global vars? Nope. More awkward than cute. Maybe introduce some profile / pragma to invert that. Everything should be local by default not global.

    Speaking of Roblox, they liked Lua so much they forked it and created a new language altogether! Luau which is only fully compatible with 5.1

    I mean it’s still a nice language for a configuration format but otherwise I think that summarizes why people might not be reaching for it as much, in other cases.

    Replies (4)
    1. Wanting things like native JSON support goes against the Lua spirit. Something like that belongs in a library, and lo and behold, there are several:

      https://luarocks.org/modules/luarocks/json4lua - https://luarocks.org/modules/winterstream/wjson - https://luarocks.org/modules/xpol/rapidjson - etc.

      Lua supports arrays; it has optimizations when all of a table’s keys are integers. Keep in mind that AWK is the same way: There’s no numeric array in AWK per se either, just AWK’s version of a “table”, but it works just fine when all of the keys are integers.

      Keep in mind that I have a fork of Lua which is 116,224 bytes in size (security hardened Lua 5.1 with `lfs` support); Lua is tiny and that’s why it does not have a built in JSON library nor will it wash the dishes for you.

      Versions of Lua after Lua 5.1 introduced changes which broke backward compatibility. Lua 5.1 is smaller than newer versions of Lua, runs great, and is incredibly fast. I see Lua 5.5 as a different language than Lua 5.1, even though one can make a polyglot program which can run in both. Lua 5.1 is also the point from which a bunch of offshoots were made: luau (Roblox Lua), LuaJIT, etc.

      Replies (1)
      1. > Wanting things like native JSON support goes against the Lua spirit. Something like that belongs in a library, and lo and behold, there are several:

        I guess my point is that why it's not picked as large of a support. I can see that being a good reason for Lua in 2006 when JSON was still not as popular but for better or worser, it's taken over so having built in JSON support would help, especially for a language geared for configuration and data representation.

        > Keep in mind that I have a fork of Lua which is 116,224 bytes in size (security hardened Lua 5.1 with `lfs` support); Lua is tiny and that’s why it does not have a built in JSON library nor will it wash the dishes for you.

        Agree, that's where it really shines, it's small and embeddable.

        > Lua 5.1 is also the point from which a bunch of offshoots were made: luau (Roblox Lua), LuaJIT, etc.

        But you see how the official site doesn't seem to issue bug fixes for anything lower than 5.4 yet everyone says 5.1 is the version to stick with. And there is luau and LuaJIT as well, that I think explains a bit why people are embracing it as eagerly any longer.

        Replies (1)
        1. There is a fork of Lua which is “Lua with a bunch of other stuff” called Pluto. It’s a superset of Lua 5.5, and it includes a bunch of other things such as JSON support.

          https://pluto-lang.org/

          The reason I like Lua is because I use it like a tiny Perl, but, unlike Perl, I already include the entire language with my open source project (it’s embedded in another program I wrote) so my Lua scripts in said project will run on any POSIX compliant system: The Lua fork I use and include also compiles as a standalone scripting language.

    2. luajit and luau supporting 5.1 only is a feature: it freeze language so no more backward compatibility breaks

      Lua has many quirks (global by default etc.) but with a language server like https://luals.github.io/, you can detect most of them easily plus optional type annotations.

      Replies (1)
      1. Also, it’s possible to force global variables to be explicitly declared.

          function declare(name)
            rawset(_G,name,false)
          end
          function exists(name)
            if rawget(_G,name) ~= nil then 
              return true 
            else return false end
          end
          setmetatable(_G, {
            __newindex = function(self,x) error("Undeclared global " .. x) end,
            __index = function(self,x) error("Undeclared global " .. x) end
          })
        
        And now

          declare("foo")
          foo = 1
          -- Create name if it hasn’t 
          -- been declared yet
          if not exists("name") then 
            declare("name")
            name = 1
          end
        
        More discussion: https://lua.org/pil/14.2.html
        Replies (1)
        1. That is neat how that's possible to do! It would be nice if it would just do local by default as well. Basically swap the defaults: everything is local and only what's explicitly global is global? I realize it's not an easy thing to change at this point in the game. Maybe some compile pragma or another extension could help? But it should be a new default in a modern language.
          Replies (1)
          1. The reason I put up with things like global by default, no “continue”, “--” instead of “#” for comments, and so on is because Lua is widely implemented, to the point it’s an informal standard for embedded scripting. There’s PUC Lua (the original), LuaJIT, Lua in Rust, Lua in Go, Lua in C#, Lua in Javascript, Lua that compiles to native code, and so on. All of these variants have agreed on having Lua, flaws and all.

            I would love to see a POSIX standard for Lua (probably based on Lua 5.1); it’s widely enough deployed that a standard for it would make sense. Right now the standard is either PUC Lua 5.1 or whatever the latest release of PUA Lua is at a given time.

            I prefer Lua over Perl/Python/Ruby because I know I can get a Lua runtime going in whatever environment I’m in—e.g. when writing my Lua book, I used my personal fork of Lua 5.1, but the person I was writing the book with was using Moonsharp (Lua in C#) since they were a C# developer. We could easily share code with each other.

            Replies (1)
            1. Interested in your lua book, is it available now?

              Continue this thread (1 replies) →

    3. LuaJIT borrowed from 5.2 and 5.3 (PUC-Rio's Lua) what it could without breaking compatibility, as I understand it. LuaJIT 3.0 adds some feature of its own.

      All the Lua 5.x versions that I have seen have small compatibility issues, and that's why many projects stick with what it's compatible with 5.1 only.

      C++ doesn't have any "native json support" either, and that's fine. I don't think JSON is that critical. You can use a library for the kind of serialization that you want. Neovim uses JSON and Lua extensively, and it just uses a C library inside.

      It's true that `{}` doubles down as an empty array or an empty map. It is not a problem that often, IME. You also can't signal that a certain key in a map is `nil`, but you can use a sentinel for it. You can do the same to disambiguate between these two.

      Different features hit differently to different people. I agree that global by default seems so wrong, but I just go used to it, and I don't care anymore. To me, the main deal is not that Lua doesn't have this features by default, is more that, when I want them, there is no generally agreed library/framework that can plug all the features in a unified, coherent way. Penlight should be that, but I don't like it that much.

      This is something that personally hits me the most, because I'm used to Qt. With Qt, I have tons and tons of functionality and a well established community and practices.

      Replies (2)
      1. Pluto is a fork of Lua 5.5 which is “Lua with a bunch of standard libraries” and other features like more mainstream classes. Yes, Pluto does have built-in JSON support.

        https://pluto-lang.org

      2. > All the Lua 5.x versions that I have seen have small compatibility issues, and that's why many projects stick with what it's compatible with 5.1 only.

        But you see how "why isn't Lua more popular" is answered with that? Do I start with 5.5? 5.3 and below seems is EoL already per the official site, they only have 5.4 bug fixes. It's a bit like C++ I guess, use only a small subset of features perhaps?

        > C++ doesn't have any "native json support" either, and that's fine.

        Unlike C++ Lua posits itself as an easy data representation and configuration language but it doesn't have a built-in way to load one of the most popular serialization formats out there these day.

        > Different features hit differently to different people.

        Definitely, I am just explaining why it doesn't appeal to me and the reason I heard other people didn't start or continue using it.

        Replies (1)
        1. Oh, I definitely agree that the issues of version compatibility are a problem for wider adoption. I have written a few small Lua projects, and I always struggle to decide which versions to support.

          If it had a few changes and perhaps some optional additions (while keeping a "classic" or "standalone" version), it would probably be more popular.

          > Unlike C++ Lua posits itself as an easy data representation and configuration language but it doesn't have a built-in way to load one of the most popular serialization formats out there these day.

          But that's the thing: if you want to represent data or configure something, wouldn't you prefer to do it in Lua? Seems the history of the language was using Lua syntax for the data representation, which is a bit more convenient than JSON for a few schemas. JSON is useful for interoperability, but it's far from my favorite for making a configuration file. The next project I'm working on is a small Lua script that will let the user configure itself via Lua, because that's more convenient than a static config format.

    4. Rust doesn't have native or stdlib JSON support either, and it is relatively modern (and quite popular).