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
Translation of http://jsonminify.tinybrain.de
test run test run with input download show line numbers
Travelled to 12 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Recognizer | Recognition Result | Visualize | Recalc |
---|---|---|---|
#308 | 1612 | [visualize] |
Snippet ID: | #267 |
Snippet name: | JSON Minify in Lua |
Eternal ID of this version: | #267/1 |
Text MD5: | d07b53e5bcdbaa12bcbeb1184a49dde0 |
Author: | stefan |
Category: | tools |
Type: | Lua code |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2014-03-29 22:40:46 |
Source code size: | 1612 bytes / 52 lines |
Pitched / IR pitched: | Yes / Yes |
Views / Downloads: | 1216 / 319 |
Referenced in: | [show references] |