| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
jopen
11年前发布

JSON压缩:JSONMinify

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

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();    }  }

项目主页:http://www.open-open.com/lib/view/home/1396341282450

 本文由用户 jopen 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
 转载本站原创文章,请注明出处,并保留原始链接、图片水印。
 本站是一个以用户分享为主的开源技术平台,欢迎各类分享!
 本文地址:https://www.open-open.com/lib/view/open1396341282450.html
JSON开发包 JSONMinify