安全压缩 JSON 与 CSS:边界与工具链
哪些字节可以删、哪些必须逐字保留,构建期与部署期的分工。
Minification is deletion with a contract: remove what parsers ignore, never what they read. Breaking the contract ships broken assets — so it is worth knowing what the safe boundary looks like.
JSON: whitespace outside strings, nothing else
Valid minification removes indentation, line breaks and spaces around punctuation outside string values. Inside strings, every byte is data — a minifier that reformats them changes your payload. The JSON Minifier validates first and reports exact savings, so corrupt input never produces a plausible-looking file.
CSS: the exceptions list
- Strings and url() values: a space inside a font name or data URI is content.
- @media conditions: removing the space in
and (breaks parsing. - .calc() expressions:
calc(100% - 8px)needs those spaces — minus signs bind to numbers. - Custom properties and var(): valid, but values live in strings more than people expect.
The CSS Formatter & Minifier implements exactly that boundary and keeps colors and selectors intact while compacting the rest.
What savings to expect
| Input | Typical shrink |
|---|---|
| Pretty-printed JSON (2-space indent) | 30-60 percent |
| Hand-written CSS with comments | 20-40 percent |
| Already-compact CSS | 5-10 percent |
Note what minification is not: compression. Transport gzip or brotli on top — and measure with real files, since whitespace-heavy documents gain less once compressed.
The pre-flight checklist
- Validate the source first — the JSON Validator pinpoints syntax errors with line and column.
- Minify a copy, never your editing source.
- Keep the pretty version in version control and minify at deploy.
相关指南
文中提到的工具
JSON Minifier
Minify JSON to a single line by stripping all insignificant whitespace. See original vs minified size and exactly how many bytes you saved. Fully offline.
Open tool →CSS Formatter / Minifier
Beautify CSS with consistent indentation and one-declaration-per-line, or minify it by stripping comments and whitespace. Handles media queries and nested braces.
Open tool →JSON Validator
Validate JSON instantly and get the exact line, column and nearby snippet of any syntax error. Valid input shows structure stats: root type, keys, depth and size.
Open tool →