| Crates.io | jsonkit |
| lib.rs | jsonkit |
| version | 0.2.2 |
| created_at | 2025-12-16 19:17:55.437556+00 |
| updated_at | 2025-12-17 19:00:49.144608+00 |
| description | A high-performance, lightweight Rust utility library engineered for robust JSON data processing. |
| homepage | |
| repository | https://gitlab.com/lazylib/rs/jsonkit |
| max_upload_size | |
| id | 1988491 |
| size | 27,687 |
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.
// comment/* comment */regex crate with OnceLock for thread-safe, lazy initialization.Integrate jsonkit into your project by adding it to your Cargo.toml:
[dependencies]
jsonkit = { git = "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.
Sanitizing JSON content is straightforward.
use jsonkit::strip_comments;
fn main() {
// Input: JSON with comments (JSONC)
let json_with_comments = r#"
{
// User profile configuration
"name": "Alice",
"age": 30, /* Age verified */
"website": "http://example.com" // URL preserved
}
"#;
// Process: Strip comments
let clean_json = strip_comments(json_with_comments);
// Output: Standard JSON
println!("{}", clean_json);
}
Reduce the size of your JSON by removing unnecessary whitespace.
use jsonkit::minify;
fn main() {
let json = r#"
{
"name": "Alice",
"age": 30
}
"#;
let minified = minify(json);
println!("{}", minified); // Output: {"name":"Alice","age":30}
}
You can format (prettify) JSON strings using prettify.
use jsonkit::{prettify, IndentStyle};
fn main() {
let raw_json = r#"{"name":"Alice","age":30,"skills":["Rust","JSON"]}"#;
// Format with 4 spaces
match prettify(raw_json, IndentStyle::Spaces(4)) {
Ok(formatted) => println!("{}", formatted),
Err(e) => eprintln!("Error formatting JSON: {}", e),
}
// Format with Tabs
// let formatted = prettify(raw_json, IndentStyle::Tabs).unwrap();
}
Check if a string is a valid JSON.
use jsonkit::validate;
fn main() {
let valid_json = r#"{"name": "Alice", "age": 30}"#;
match validate(valid_json) {
Ok(_) => println!("Valid JSON"),
Err(e) => println!("Invalid JSON: {}", e),
}
let invalid_json = r#"{"name": "Alice", "age": 30,}"#; // Trailing comma
match validate(invalid_json) {
Ok(_) => println!("Valid JSON"),
Err(e) => println!("Invalid JSON: {}", e),
}
}
strip_commentspub fn strip_comments(raw_json_with_comments: &str) -> String
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. |
minifypub fn minify(json: &str) -> String
Minifies a JSON string by removing unnecessary whitespace.
| Parameter | Type | Description |
|---|---|---|
json |
&str |
The input JSON string. |
| Returns | String |
The minified JSON string. |
prettifypub fn prettify(raw_json: &str, style: IndentStyle) -> Result<String, String>
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. |
validatepub fn validate(json: &str) -> Result<(), String>
Validates a JSON string.
| Parameter | Type | Description |
|---|---|---|
json |
&str |
The input JSON string. |
| Returns | Result<(), String> |
Ok(()) if valid, or an error message. |
IndentStyleEnum for selecting indentation style.
pub enum IndentStyle {
Tabs,
Spaces(usize),
}
We welcome contributions! To get started with local development:
Clone the repository:
git clone https://gitlab.com/lazylib/rs/jsonkit.git
cd jsonkit
Run tests: Ensure all functionality works as expected.
cargo test
This project is proudly licensed under the MIT License. See the LICENSE file for details.
Made with ❤️ in Rust.