Last active
April 27, 2020 02:34
-
-
Save TaizWeb/8489b72d474aeb8dbd432b10ae372cc0 to your computer and use it in GitHub Desktop.
A function to fix escape sequences in lua strings. Useful for preparing JSON from an API for conversion.
This file contains 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
function escapeFix(data) | |
local newData = "" | |
for i=1,string.len(data) do | |
local char = string.sub(data, i, i) | |
local nextChar = string.sub(data, i+1, i+1) | |
local prevChar = string.sub(data, i-1, i-1) | |
if (char == '"' and (prevChar ~= "{" and nextChar ~= ":" and prevChar ~= ":" and nextChar ~= "," and prevChar ~= "," and nextChar ~= "}")) then | |
newData = newData .. "\\" -- Append another, non-escaped, '\' | |
end | |
newData = newData .. string.sub(data, i, i) | |
end | |
newData = string.gsub(newData, "\n", "\\n") -- Strip out newlines as well | |
return newData | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment