Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

53
LINES

< > BotCompany Repo | #727 // JSONMinify.java - THE ORIGINAL (with package statement)

IOIOI

1  
package prophecy.common;
2  
3  
public class JSONMinify {
4  
  public static String minify(String jsonString) {
5  
    boolean in_string = false;
6  
    boolean in_multiline_comment = false;
7  
    boolean in_singleline_comment = false;
8  
    char string_opener = 'x'; // unused value, just something that makes compiler happy
9  
10  
    StringBuilder out = new StringBuilder();
11  
    for (int i = 0; i < jsonString.length(); i++) {
12  
      // get next (c) and next-next character (cc)
13  
14  
      char c = jsonString.charAt(i);
15  
      String cc = jsonString.substring(i, Math.min(i+2, jsonString.length()));
16  
17  
      // big switch is by what mode we're in (in_string etc.)
18  
      if (in_string) {
19  
        if (c == string_opener) {
20  
          in_string = false;
21  
          out.append(c);
22  
        } else if (c == '\\') { // no special treatment needed for \\u, it just works like this too
23  
          out.append(cc);
24  
          ++i;
25  
        } else
26  
          out.append(c);
27  
      } else if (in_singleline_comment) {
28  
        if (c == '\r' || c == '\n')
29  
          in_singleline_comment = false;
30  
      } else if (in_multiline_comment) {
31  
        if (cc.equals("*/")) {
32  
          in_multiline_comment = false;
33  
          ++i;
34  
        }
35  
      } else {
36  
        // we're outside of the special modes, so look for mode openers (comment start, string start)
37  
        if (cc.equals("/*")) {
38  
          in_multiline_comment = true;
39  
          ++i;
40  
        } else if (cc.equals("//")) {
41  
          in_singleline_comment = true;
42  
          ++i;
43  
        } else if (c == '"' || c == '\'') {
44  
          in_string = true;
45  
          string_opener = c;
46  
          out.append(c);
47  
        } else if (!Character.isWhitespace(c))
48  
          out.append(c);
49  
      }
50  
    }
51  
    return out.toString();
52  
  }
53  
}

Author comment

From http://jsonminify.tinybrain.de with greetings.

download  show line numbers   

Travelled to 12 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

Comments [hide]

ID Author/Program Comment Date
399 #1000610 Edit suggestion:
!636
!629

main {
static Object androidContext;
static String programID;

public static void main(String[] args) throws Exception {
package prophecy.common;

public class JSONMinify {
public static String minify(String jsonString) {
boolean in_string = false;
boolean in_multiline_comment = false;
boolean in_singleline_comment = false;
char string_opener = 'x'; // unused value, just something that makes compiler happy

StringBuilder out = new StringBuilder();
for (int i = 0; i < jsonString.length(); i++) {
// get next (c) and next-next character (cc)

char c = jsonString.charAt(i);
String cc = jsonString.substring(i, Math.min(i+2, jsonString.length()));

// big switch is by what mode we're in (in_string etc.)
if (in_string) {
if (c == string_opener) {
in_string = false;
out.append(c);
} else if (c == '\\') { // no special treatment needed for \\u, it just works like this too
out.append(cc);
++i;
} else
out.append(c);
} else if (in_singleline_comment) {
if (c == '\r' || c == '\n')
in_singleline_comment = false;
} else if (in_multiline_comment) {
if (cc.equals("*/")) {
in_multiline_comment = false;
++i;
}
} else {
// we're outside of the special modes, so look for mode openers (comment start, string start)
if (cc.equals("/*")) {
in_multiline_comment = true;
++i;
} else if (cc.equals("//")) {
in_singleline_comment = true;
++i;
} else if (c == '"' || c == '\'') {
in_string = true;
string_opener = c;
out.append(c);
} else if (!Character.isWhitespace(c))
out.append(c);
}
}
return out.toString();
}
}
}}
2015-08-18 11:38:21  delete 
397 #1000604 (pitcher) 2015-08-18 00:07:22

add comment

Snippet ID: #727
Snippet name: JSONMinify.java - THE ORIGINAL (with package statement)
Eternal ID of this version: #727/1
Text MD5: 3516d071e70d333f02d3ec6988827612
Author: stefan
Category:
Type: IOIOI
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-08-10 23:44:53
Source code size: 1775 bytes / 53 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 821 / 121
Referenced in: [show references]