我是靠谱客的博主 激动雨,这篇文章主要介绍JSON压缩:JSONMinifyJSON压缩:JSONMinify ,现在分享给大家,希望可以做个参考。

JSON压缩:JSONMinify

JSONMinify  2014-04-01 17:23:16 发布
您的评价:
         
3.5 
     0收藏

JSONMinify 移除了 JSON 或者 JSON+C(JSON+C = JSON with comments) 文档中的所有空白和注释,实现对 JSON 内容的最小化压缩。实现无开销和近乎完美的表现。这个包只有53行源码:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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(); } }

最后

以上就是激动雨最近收集整理的关于JSON压缩:JSONMinifyJSON压缩:JSONMinify 的全部内容,更多相关JSON压缩:JSONMinifyJSON压缩:JSONMinify内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(72)

评论列表共有 0 条评论

立即
投稿
返回
顶部