🚀 JSON KiT
JSON KiT is a high-performance, lightweight Rust utility library engineered for robust JSON data processing.
Its primary mission is to bridge the gap between "JSON with Comments" (JSONC) and standard JSON parsers by efficiently sanitizing input strings, as well as providing tools for formatting JSON data.
📑 Table of Contents
✨ Features
- 🛡️ Comment Stripping: Seamlessly removes standard C-style comments.
- Single-line:
// comment - Multi-line:
/* comment */
- Single-line:
- 📦 JSON Minification: Efficiently removes whitespace to reduce payload size.
- ✅ JSON Validation: Validates JSON strings to ensure they are well-formed.
- 🎨 JSON Formatting: Prettify minified JSON with customizable indentation (spaces or tabs).
- 🧠 Context-Aware Parsing: Intelligently distinguishes between actual comments and comment-like patterns within string literals.
- ⚡ High Performance: Built on top of the
regexcrate withOnceLockfor thread-safe, lazy initialization.
📦 Installation
Integrate jsonkit into your project by adding it to your Cargo.toml:
[]
= { = "https://gitlab.com/lazylib/rs/jsonkit" }
Note: As this crate is hosted in a private/custom repository, please ensure your git configuration allows access if necessary.
🚀 Usage
Strip Comments
Sanitizing JSON content is straightforward.
use strip_comments;
Minify JSON
Reduce the size of your JSON by removing unnecessary whitespace.
use minify;
Format JSON
You can format (prettify) JSON strings using prettify.
use ;
Validate JSON
Check if a string is a valid JSON.
use validate;
📚 API Reference
strip_comments
Transforms a JSON string containing comments into a standard JSON string.
| Parameter | Type | Description |
|---|---|---|
raw_json_with_comments |
&str |
The input string containing JSONC data. |
| Returns | String |
A new, sanitized string ready for parsing. |
minify
Minifies a JSON string by removing unnecessary whitespace.
| Parameter | Type | Description |
|---|---|---|
json |
&str |
The input JSON string. |
| Returns | String |
The minified JSON string. |
prettify
Prettifies a JSON string with the specified indentation style.
| Parameter | Type | Description |
|---|---|---|
raw_json |
&str |
The input JSON string. |
style |
IndentStyle |
The indentation style (IndentStyle::Tabs or IndentStyle::Spaces(usize)). |
| Returns | Result<String, String> |
The formatted JSON string or an error message. |
validate
Validates a JSON string.
| Parameter | Type | Description |
|---|---|---|
json |
&str |
The input JSON string. |
| Returns | Result<(), String> |
Ok(()) if valid, or an error message. |
IndentStyle
Enum for selecting indentation style.
🛠️ Development
We welcome contributions! To get started with local development:
-
Clone the repository:
-
Run tests: Ensure all functionality works as expected.
📄 License
This project is proudly licensed under the MIT License. See the LICENSE file for details.
Made with ❤️ in Rust.