1 | function minify(jsonString) |
2 | local in_string = false |
3 | local in_multiline_comment = false |
4 | local in_singleline_comment = false |
5 | local string_opener |
6 | |
7 | local out = {} |
8 | for i=1, #jsonString do |
9 | -- get next (c) and next-next character (cc) |
10 | |
11 | local c = jsonString:sub(i, i) |
12 | local cc = jsonString:sub(i, i+2) |
13 | |
14 | -- big switch is by what mode we're in (in_string etc.) |
15 | if in_string then |
16 | if c == string_opener then |
17 | in_string = false |
18 | table.insert(out, c) |
19 | elseif c == '\\' then -- no special treatment needed for \\u, it just works like this too |
20 | table.insert(out, cc) |
21 | i=i+1 |
22 | else |
23 | table.insert(out, c) |
24 | end |
25 | elseif in_singleline_comment then |
26 | if c == '\r' or c == '\n' then |
27 | in_singleline_comment = false |
28 | end |
29 | elseif in_multiline_comment then |
30 | if cc == "*/" then |
31 | in_multiline_comment = false |
32 | i=i+1 |
33 | end |
34 | else |
35 | -- we're outside of the special modes, so look for mode openers (comment start, string start) |
36 | if cc == "/*" then |
37 | in_multiline_comment = true |
38 | i=i+1 |
39 | elseif cc == "//" then |
40 | in_singleline_comment = true |
41 | i=i+1 |
42 | elseif c == '"' or c == '\'' then |
43 | in_string = true |
44 | string_opener = c |
45 | table.insert(out, c) |
46 | elseif c ~= ' ' and c ~= '\t' and c ~= '\r' and c ~= '\n' then |
47 | table.insert(out, c) |
48 | end |
49 | end |
50 | end |
51 | return table.concat(out) |
52 | 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: | 1218 / 320 |
Referenced in: | [show references] |