Skip to content

Instantly share code, notes, and snippets.

@Tim-Machine
Last active November 18, 2022 13:02
Show Gist options
  • Select an option

  • Save Tim-Machine/55209200f105b11fe839 to your computer and use it in GitHub Desktop.

Select an option

Save Tim-Machine/55209200f105b11fe839 to your computer and use it in GitHub Desktop.
Little script to convert lua tables into json
pako = require('pako') ## https://www.npmjs.com/package/pako
convertDataURIToBinary = (dataURI) ->
raw = window.atob(dataURI)
rawLength = raw.length
array = new Uint8Array(new ArrayBuffer(rawLength))
i = 0
while i < rawLength
array[i] = raw.charCodeAt(i)
i++
array
luaTableToJson = (string)->
data = convertDataURIToBinary(string)
rawData = pako.ungzip(data)
str = String.fromCharCode.apply(null, rawData);
## this may or may not be required, it was for my applicaiton
str = str.replace("do local _=", '')
str = str.replace(/\=/g, ':');
string = str.replace(";return _;end", '')
string = string.replace(/\{([a-zA-z]*)\:/g, "\{\"$1\":");
string = string.replace(/\,([a-zA-z]*)\:/g, "\,\"$1\":");
string = string.replace(/\[([0-9]*)\]\:/g, "\"$1\":");
string = string.replace(/{{/g, "[{");
string = string.replace(/}}/g, "}]");
string = string.replace(/}],{/g, "}},{");
string = string.replace(/}]}/g, "}}}");
JSON.parse(string)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment