json_lib
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, TOML). Designed for small binaries, predictable behavior, and easy embedding.
- Core Node type representing JSON structures
- Parser to build Node trees from streams
- Stringifiers to JSON, YAML, XML, Bencode, and TOML
- File and in-memory buffer I/O abstractions
- Pretty-printing utilities
- Unicode-aware file helpers (BOM detection/handling)
Minimum supported Rust version: 1.88.0
Features at a glance
- Node and Numeric types for representing JSON values
- Parse and stringify with streaming-friendly traits
- FileSource/FileDestination and BufferSource/BufferDestination
- Pretty printer for human-friendly output
- Convert a Node to alternate formats (YAML, XML, Bencode, TOML)
- Unicode file format detection and read/write helpers
Installation
Add the library to a workspace member or use a path dependency:
# Cargo.toml
[]
= { = "library" }
If publishing to crates.io or using a Git dependency, adjust accordingly.
Embedding Guide
To embed this library in another Rust project:
- Add the path dependency as shown above.
- Import the required modules in your code:
use ; - See the EMBEDDING_GUIDE.md for detailed instructions and integration tips.
Quick start
Parse a file, then pretty-print to another file:
use ;
use print;
use Path;
Build a Node in memory and stringify to a buffer:
use ;
Convert a Node to YAML/XML/Bencode:
use ;
Read/write text files with Unicode BOM handling:
use ;
API overview
-
Version
- version() -> &'static str
-
I/O sources and destinations
- FileSource / FileDestination
- BufferSource / BufferDestination
-
Node model
- Node: Object, Array, Str, Number(Numeric), Boolean, None
- Numeric: Integer, UInteger, Float, Byte, Int8, Int16, UInt16, Int32, UInt32
-
Parsing and stringifying
- parse(&mut Source) -> Result<Node, String>
- stringify(&Node, &mut Destination)
- Pretty print: json_lib::misc::print(&Node, &mut Destination, indent, current_indent)
-
Alternative format encoders
- to_yaml(&Node, &mut Destination)
- to_xml(&Node, &mut Destination)
- to_bencode(&Node, &mut Destination)
- to_toml(&Node, &mut Destination)
-
Unicode-aware file helpers
- detect_format(path) -> Result<Format, String>
- read_file_to_string(path) -> Result<String, String>
- write_file_from_string(path, &str, Format) -> Result<(), String>
Example: maintaining a JSON-backed sequence
A typical workflow:
- Read from a file using FileSource
- Parse into a Node
- Mutate the Node in memory
- Write back to a file with stringify and FileDestination
This pattern works for configuration files, logs, and machine-generated data.
Design goals
- Small, explicit API focused on Node-based manipulation
- Works with streams and simple abstractions
- Deterministic and predictable output
- Easy to integrate for CLI tools and small services
Roadmap ideas
- Optional parse/stringify options (e.g., key sorting, non-ASCII escaping)
- Streaming parser and incremental writer
- JSON Pointer, Patch, and Merge Patch utilities
- Canonical JSON and stable hashing
- Optional serde interop and async I/O
If you’re interested in any of these, contributions are welcome.
Documentation
See the docs folder for:
- Development Guide
- Contributing Guidelines
- Code of Conduct
- Embedding Guide
License
This project is licensed under the terms of the LICENSE file included in the repository.
Contributing
- Open issues for bugs and feature requests
- PRs with tests are welcome
- Please format and lint your code before submitting
Support
If you run into issues or have questions, please open an issue in the repository.