function minify(jsonString) local in_string = false local in_multiline_comment = false local in_singleline_comment = false local string_opener local out = {} for i=1, #jsonString do -- get next (c) and next-next character (cc) local c = jsonString:sub(i, i) local cc = jsonString:sub(i, i+2) -- big switch is by what mode we're in (in_string etc.) if in_string then if c == string_opener then in_string = false table.insert(out, c) elseif c == '\\' then -- no special treatment needed for \\u, it just works like this too table.insert(out, cc) i=i+1 else table.insert(out, c) end elseif in_singleline_comment then if c == '\r' or c == '\n' then in_singleline_comment = false end elseif in_multiline_comment then if cc == "*/" then in_multiline_comment = false i=i+1 end else -- we're outside of the special modes, so look for mode openers (comment start, string start) if cc == "/*" then in_multiline_comment = true i=i+1 elseif cc == "//" then in_singleline_comment = true i=i+1 elseif c == '"' or c == '\'' then in_string = true string_opener = c table.insert(out, c) elseif c ~= ' ' and c ~= '\t' and c ~= '\r' and c ~= '\n' then table.insert(out, c) end end end return table.concat(out) end