Last active
November 18, 2022 13:02
-
-
Save Tim-Machine/55209200f105b11fe839 to your computer and use it in GitHub Desktop.
Little script to convert lua tables into json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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