0

EDIT: Take a lua table as a string, and use javascript to convert it to a javascript array. No programming in lua.

So a lua table is just an associative array in a different format.

    --LUA TABLE EXAMPLE
    {
    ["glow"] = true,
    ["xOffset"] = -287.99981689453,
    ["yOffset"] = -227.55575561523,
    ["anchorPoint"] = "CENTER",
    ["cooldownSwipe"] = true,
    ["customTextUpdate"] = "update",
    ["cooldownEdge"] = false,
    ["icon"] = true,
    ["useglowColor"] = false,
    ["internalVersion"] = 24,
    ["keepAspectRatio"] = false,
    ["animation"] = {
        ["start"] = {
            ["duration_type"] = "seconds",
            ["type"] = "none",
        },
        ["main"] = {
            ["duration_type"] = "seconds",
            ["type"] = "none",
        },
        ["finish"] = {
            ["duration_type"] = "seconds",
            ["type"] = "none",
        },
    }

I've looked around for some javascript functions or libraries to do this though I've only come across some lua libraries to convert json to a lua table. The lua table will always be in a string. Does there exist a way of doing this?

Amelia Magee
  • 452
  • 5
  • 14
  • Possible duplicate of [Convert JSON String to Lua Table?](https://stackoverflow.com/questions/24908199/convert-json-string-to-lua-table) – VLAZ Sep 25 '19 at 20:13
  • @VLAZ no i've already read that one, i'm looking to convert using JAVASCRIPT. – Amelia Magee Sep 25 '19 at 20:21
  • I've written a NPM package that does this. It's called [Luon](https://github.com/appgurueu/luon) (for "Lua object notation"). – Luatic Sep 14 '22 at 13:39

4 Answers4

1

You can to do it manually to make it a valid JSON then parse it :

const luaStr = `{
    ["glow"] = true,
    ["xOffset"] = -287.99981689453,
    ["yOffset"] = -227.55575561523,
    ["anchorPoint"] = "CENTER",
    ["cooldownSwipe"] = true,
    ["customTextUpdate"] = "update",
    ["cooldownEdge"] = false,
    ["icon"] = true,
    ["useglowColor"] = false,
    ["internalVersion"] = 24,
    ["keepAspectRatio"] = false,
    ["animation"] = {
        ["start"] = {
            ["duration_type"] = "seconds",
            ["type"] = "none",
        },
        ["main"] = {
            ["duration_type"] = "seconds",
            ["type"] = "none",
        },
        ["finish"] = {
            ["duration_type"] = "seconds",
            ["type"] = "none",
        },
    }}`;

const result = luaStr  
  .replace(/\[|\]/g, '') // remove the brackets
  .replace(/=/g, ':') // replace the = with :
  .replace(/(\,)(?=\s*})/g, ''); // remove trailing commas
  
const parsed = JSON.parse(result);

console.log(result);
Taki
  • 17,320
  • 4
  • 26
  • 47
  • 2
    What happens if the white space is in the middle of a string?' – Scott Sauyet Sep 25 '19 at 20:28
  • @ScottSauyet oh, didn't see that, i removed them to make it easier to remove trailing commas, i'll rethink this – Taki Sep 25 '19 at 20:35
  • 1
    I only noticed because I was working on a somewhat similar solution (now an answer) that has a different failure mode inside a string! – Scott Sauyet Sep 25 '19 at 20:46
  • @ScottSauyet i updated my answer, and yeah yours looks good but i guess there always will be tricky if the string had a value like `"aaa", }` for some reason .. – Taki Sep 25 '19 at 20:51
  • 1
    Yes, I think any regex-based solution is going to have such issues. This actually needs a proper parser. I think mine has a less-likely failure mode than this answer, but nothing like this will solve the general case. – Scott Sauyet Sep 25 '19 at 23:49
1

This is only a partial solution. It could fail in some cases where the text inside a string matches the key syntax. But that may not be a concern for you.

const lua = `
{
    ["glow"] = true,
    ["xOffset"] = -287.99981689453,
    ["yOffset"] = -227.55575561523,
    ["anchorPoint"] = "CENTER",
    ["cooldownSwipe"] = true,
    ["customTextUpdate"] = "update",
    ["cooldownEdge"] = false,
    ["icon"] = true,
    ["useglowColor"] = false,
    ["internalVersion"] = 24,
    ["keepAspectRatio"] = false,
    ["animation"] = {
        ["start"] = {
            ["duration_type"] = "seconds",
            ["type"] = "none",
        },
        ["main"] = {
            ["duration_type"] = "seconds",
            ["type"] = "none",
        },
        ["finish"] = {
            ["duration_type"] = "seconds",
            ["type"] = "none",
        },
    }
}`

const lua2json = lua => 
  JSON .parse (lua .replace (
    /\[([^\[\]]+)\]\s*=/g, 
    (s, k) => `${k} :`
  ) 
  .replace (/,(\s*)\}/gm, (s, k) => `${k}}`))

console .log (
  lua2json (lua)
)

I didn't know if you were looking to create JSON or an object. I chose the latter, but you could always remove the JSON.parse wrapper.

Scott Sauyet
  • 49,207
  • 4
  • 49
  • 103
  • Would not be better to include double quotes in regexp too ? Suppose it should be part of syntax and can give better reliability(?) – Jan Sep 26 '19 at 17:42
  • @Tom: Quite possibly. The problem is that this technique is inherently fragile. Any plain-regex based solution is almost certainly going to have failure modes, so the question is whether you want to use a full-fledged parser (I really recommend [Peg.js](https://pegjs.org/)) and if not, what compromises do you make. Since it looks like the quotes are required syntax for Lua (is this true?), and I was only focused on interpreting a well-formed Lua table string, I didn't worry about it. But that might help catch ill-formed input. – Scott Sauyet Sep 26 '19 at 18:11
0

Here is something that you might not know, { ["someString"]: 2 } is valid javascript and will evaluate to {someString: 2} which is valid json. The only issue is it needs to be evaluated, which means using eval (which you really never should, if you can avoid it).

const luaStr = `{
    ["glow"] = true,
    ["xOffset"] = -287.99981689453,
    ["yOffset"] = -227.55575561523,
    ["anchorPoint"] = "CENTER",
    ["cooldownSwipe"] = true,
    ["customTextUpdate"] = "update",
    ["cooldownEdge"] = false,
    ["icon"] = true,
    ["useglowColor"] = false,
    ["internalVersion"] = 24,
    ["keepAspectRatio"] = false,
    ["animation"] = {
        ["start"] = {
            ["duration_type"] = "seconds",
            ["type"] = "none",
        },
        ["main"] = {
            ["duration_type"] = "seconds",
            ["type"] = "none",
        },
        ["finish"] = {
            ["duration_type"] = "seconds",
            ["type"] = "none",
        },
    }}`;
    
const jsonString = luaStr.replace(/\] = /g, ']: ');
const jsonObj = eval(`(${jsonString})`);

console.log(jsonObj);
Olian04
  • 6,480
  • 2
  • 27
  • 54
0

Added array converting

const luaStr = `{
    ["glow"] = true,
    ["xOffset"] = -287.99981689453,
    ["yOffset"] = -227.55575561523,
    ["anchorPoint"] = "CENTER",
    ["cooldownSwipe"] = true,
    ["customTextUpdate"] = "update",
    ["cooldownEdge"] = false,
    ["icon"] = true,
    ["useglowColor"] = false,
    ["internalVersion"] = 24,
    ["keepAspectRatio"] = false,
    ["animation"] = {
        ["start"] = {
            ["duration_type"] = "seconds",
            ["type"] = "none",
        },
        ["main"] = {
            ["duration_type"] = "seconds",
            ["type"] = "none",
        },
        ["finish"] = {
            ["duration_type"] = "seconds",
            ["type"] = "none",
        },
        ["test1"] = { "test1"
     },

        ["test2"] = { "test1",
         "test2" },

        ["test3"] = { "test1", "test2", "test2" },
    }}`;

const result = luaStr  
.replace(/\[|\]/g, '') // remove the brackets
.replace(/=/g, ':') // replace the = with :
.replace(/(\,)(?=\s*})/g, '') // remove trailing commas
.replace(/\{\s*(\"[^".]*\"(\s*\,\s*\"[^".]*\")*)\s*\}/g, '[$1]') // to array

console.log (result)

const parsed = JSON.parse(result);
console.log(parsed);
Dexif
  • 1