Crates.io | json_lib |
lib.rs | json_lib |
version | 0.1.0 |
created_at | 2025-09-04 20:01:22.456953+00 |
updated_at | 2025-09-04 20:01:22.456953+00 |
description | JSON library implementation. |
homepage | |
repository | |
max_upload_size | |
id | 1824708 |
size | 108,043 |
A lightweight, modular JSON toolkit for Rust with pluggable I/O sources/destinations, a simple in-memory Node tree, and multiple serializers (JSON, YAML, XML, Bencode). Designed for small binaries, predictable behavior, and easy embedding.
Minimum supported Rust version: 1.88.0
Add the library to a workspace member or use a path dependency:
toml
# Cargo.toml
[dependencies]
json_lib = { path = "library" }
If publishing to crates.io or using a Git dependency, adjust accordingly.
Parse a file, then pretty-print to another file:
rust
use json_lib::{FileSource, FileDestination, parse};
use json_lib::json_lib::misc::print;
use std::path::Path;
fn main() -> Result<(), String> {
let input = "data.json";
let mut src = FileSource::new(input).map_err(|e| e.to_string())?;
let node = parse(&mut src).map_err(|e| e.to_string())?;
let output = Path::new(input).with_extension("pretty.json");
let mut dst = FileDestination::new(output.to_string_lossy().as_ref()).map_err(|e| e.to_string())?;
// 4-space indentation
print(&node, &mut dst, 4, 0);
Ok(())
}
Build a Node in memory and stringify to a buffer:
rust
use json_lib::{Node, Numeric, stringify, BufferDestination};
fn main() {
let node = Node::Object(vec![
("name".into(), Node::Str("example".into())),
("count".into(), Node::Number(Numeric::Integer(3))),
("items".into(), Node::Array(vec![
Node::Boolean(true),
Node::None,
Node::Str("text".into())
])),
].into_iter().collect());
let mut buf = BufferDestination::new();
stringify(&node, &mut buf);
let json = buf.to_string();
println!("{}", json);
}
Convert a Node to YAML/XML/Bencode:
rust
use json_lib::{Node, Numeric, to_yaml, to_xml, to_bencode, BufferDestination};
fn main() {
let node = Node::Array(vec![
Node::Number(Numeric::Integer(1)),
Node::Number(Numeric::Integer(2)),
Node::Number(Numeric::Integer(3)),
]);
let mut yaml = BufferDestination::new();
to_yaml(&node, &mut yaml);
println!("YAML:\n{}", yaml.to_string());
let mut xml = BufferDestination::new();
to_xml(&node, &mut xml);
println!("XML:\n{}", xml.to_string());
let mut bencode = BufferDestination::new();
to_bencode(&node, &mut bencode);
println!("Bencode:\n{}", bencode.to_string());
}
Read/write text files with Unicode BOM handling:
rust
use json_lib::{Format, detect_format, read_file_to_string, write_file_from_string};
fn main() -> Result<(), String> {
// Detect BOM/format
let fmt = detect_format("input.txt").unwrap_or(Format::UTF8);
let content = read_file_to_string("input.txt")?;
// Write as UTF-8 (no BOM) regardless of input
write_file_from_string("output.txt", &content, Format::UTF8)
}
Version
I/O sources and destinations
Node model
Parsing and stringifying
Alternative format encoders
Unicode-aware file helpers
A typical workflow:
This pattern works for configuration files, logs, and machine-generated data.
If you’re interested in any of these, contributions are welcome.
This project is licensed under the terms of the LICENSE file included in the repository.
If you run into issues or have questions, please open an issue in the repository.