| Crates.io | yyjson-rs |
| lib.rs | yyjson-rs |
| version | 0.1.0 |
| created_at | 2025-03-12 12:25:21.22435+00 |
| updated_at | 2025-03-12 12:25:21.22435+00 |
| description | Rust bindings for yyjson |
| homepage | |
| repository | https://github.com/bnmoch3/yyjson-rs |
| max_upload_size | |
| id | 1589714 |
| size | 55,431 |
A Rust wrapper for yyjson, a high-performance JSON library written in ANSI C.
Add the following to your Cargo.toml:
[dependencies]
yyjson-rs = "0.1.0" # Check crates.io for latest version
use yyjson_rs::DocContext;
fn main() -> anyhow::Result<()> {
let json = r#"
{
"name": "Alice",
"age": 30,
"scores": [95.5, 89.2, 92.8],
"metadata": {
"active": true,
"tags": ["rust", "c", "json"]
}
}"#;
let ctx = DocContext::default();
let doc = ctx.parse(json.as_bytes())?;
let root = doc.root();
// Access primitive values
let name: Option<&str> = root.at_key("name").and_then(|v| v.str());
let age: Option<u64> = root.at_key("age").and_then(|v| v.u64());
// Navigate nested structures
let scores = root.at_key("scores").and_then(|v| v.list()).unwrap();
let first_score = scores.get(0).and_then(|v| v.f64()).unwrap();
let active: Option<bool> = root
.at_key("metadata")
.and_then(|m| m.at_key("active"))
.unwrap()
.bool();
Ok(())
}
Iterate over lists and objects:
// Array iteration
let scores = root.at_key("scores").and_then(|v| v.list()).unwrap();
for score in scores.iter() {
println!("Score: {}", score.f64().unwrap());
}
// Object iteration
let metadata = root.at_key("metadata").and_then(|v| v.obj()).unwrap();
for (key, value) in metadata.iter() {
println!("{}: {}", key, value);
}
use yyjson_rs::{
BasicAllocProvider, DocContext, ReadOptions, WriteOptions, Writer, YyjsonAllocProvider,
};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// parse doc
let json = r#"{"name": "John", /* age is invalid */ "age": NaN,}"#;
let read_opts = ReadOptions {
allow_trailing_commas: true,
allow_comments: true,
allow_inf_and_nan: true,
..Default::default()
};
let doc_context = DocContext::new(BasicAllocProvider::default(), read_opts);
let doc = doc_context.parse(json.as_bytes())?;
// write
let alloc_provider = BasicAllocProvider::default();
let allocator = alloc_provider.get_allocator();
let write_opts = WriteOptions {
pretty: true,
allow_inf_and_nan: true,
..Default::default()
};
let mut writer = Writer::new(allocator, Some(&write_opts));
let output = doc.write(&mut writer)?;
println!("{}", output.as_str());
Ok(())
}
The library supports multiple allocation strategies:
BasicAllocProvider: Default, libc allocatorPoolAllocProvider: Pre-allocates a fixed-length buffer, suitable for single
use per JSON document rather than across multiple JSON documentsDynamicAllocProvider: Similar to pool allocator, however, when there is not
enough memory, it dynamically requests more memory using malloc and frees all
allocations when it is destroyedFor non-standard JSON, you can customize parsing with ReadOptions:
allow_comments: allow c-style single line and multiple line commentsallow_trailing_commas: allow trailing comma at end of an object or arraystop_when_done: if there is additional content after a JSON document, such
as a newline, stop once parsing of the JSON document is done rather than
erroring outallow_inf_and_nan: Allow inf/nan valuesbignums_as_raw_strings: read big numbers that cannot be represented as u64
and i64, and floats that cannot be represented by finite f64 as raw
stringsControl JSON output with WriteOptions:
pretty: Write JSON pretty with 4 space indentpretty_with_two_spaces: Write JSON pretty with 2 space indent, overrides
prettyescape_unicode: escape unicode as uXXXX, make output ASCII onlyescape_slashes: escape '/' as '/'.allow_inf_and_nan: (non-standard) write inf and nan number as 'Infinity' and
'NaN' literal.inf_and_nan_as_null: write inf and nan number as null literal. Overrides
allow_inf_and_nanadd_newline_at_end: adds newline character at the end of JSON, helpful for
NDJSONThis library provides a thin wrapper around yyjson, maintaining the original library's high-performance.
While using FFI internally, the public API ensures:
MIT
Contributions are welcome! Please submit pull requests or open issues on the repository.