| Crates.io | batch-mode-json |
| lib.rs | batch-mode-json |
| version | 0.2.2 |
| created_at | 2025-02-27 02:24:47.610584+00 |
| updated_at | 2025-07-13 10:29:29.36541+00 |
| description | Utilities for extracting JSON and writing it to files with error handling. |
| homepage | |
| repository | https://github.com/klebs6/klebs-general |
| max_upload_size | |
| id | 1571165 |
| size | 109,648 |
The batch-mode-json crate provides utilities for extracting JSON content from text blocks, writing JSON data to files, and handling JSON parsing errors. This crate is useful for working with batch processing data that involves JSON-formatted content.
```json ... ```.The crate offers a function extract_json_from_possible_backticks_block that extracts JSON content from a string, handling surrounding backtick markers (```json ... ```) and whitespace.
pub fn extract_json_from_possible_backticks_block(content: &str) -> &str;
The write_to_file function asynchronously writes a given JSON string to a file. It handles file creation and ensures all data is flushed to disk.
pub async fn write_to_file(target_path: impl AsRef<Path>, serialized_json: &str) -> Result<(), io::Error>;
The crate also provides an error enum JsonParseError to capture various issues during JSON parsing and file handling.
error_tree!{
pub enum JsonParseError {
JsonRepairError(JsonRepairError),
InvalidJson,
SerdeError(serde_json::Error),
IoError(std::io::Error),
}
}
You can extract JSON content from a string that might be surrounded by backticks using the extract_json_from_possible_backticks_block function:
let content = "```json\n{\"key\": \"value\"}\n```";
let json_content = extract_json_from_possible_backticks_block(content);
pretty_assert_eq!(json_content, "{\"key\": \"value\"}");
To write JSON data to a file asynchronously, use the write_to_file function:
use batch_mode_json::write_to_file;
let json_data = r#"{"key": "value"}"#;
write_to_file("output.json", json_data).await.unwrap();
This crate is licensed under the MIT License. See LICENSE for details.