jzon/lib.rs
1//! **jzon** — purpose-built, zero-copy JSON serialization for specific structs.
2//!
3//! # Design
4//!
5//! Use `#[derive(ToJson, FromJson)]` to generate a **monomorphised** JSON
6//! (de)serializer for each of your types at compile time. No generic visitor
7//! indirection, no intermediate `Value` allocation, no format-string overhead.
8//!
9//! ## Cargo features
10//!
11//! | Feature | Default | Effect |
12//! |---------|---------|--------|
13//! | `derive` | ✓ | `#[derive(ToJson, FromJson)]` proc-macros |
14//! | `simd` | | u128 SWAR scanning (16 B/iter) |
15//! | `simd + unstable` | | `std::simd` portable SIMD (32–64 B/iter, nightly) |
16//! | `fast-float` | | `ryu` serialization, `fast_float2` parsing |
17//! | `stats` | | `ScannerStats` allocation/cache-hit counters |
18//!
19//! For serde integration see [`jzon-rs-serde`](https://crates.io/crates/jzon-rs-serde).
20//! For a `serde_json` drop-in see [`jzon-rs-compat`](https://crates.io/crates/jzon-rs-compat).
21//!
22//! ## Zero-copy deserialization
23//!
24//! Fields typed `&'de str` borrow **directly** from the input — no `String` is
25//! allocated unless the JSON string contains escape sequences.
26//!
27//! ## Field-hint cache
28//!
29//! The generated `FromJson` impl maintains a one-word *field-hint* variable
30//! that predicts which field key to expect next. For JSON whose field order
31//! matches the struct definition — the common case — almost every key dispatch
32//! is O(1) without hashing.
33//!
34//! ## Safe Rust only
35//!
36//! There are **no `unsafe` blocks** in this crate. All SIMD scanning uses
37//! `std::simd` (nightly) or pure u64/u128 arithmetic (SWAR).
38//!
39//! # Quick start
40//!
41//! ```rust,ignore
42//! use jzon::{ToJson, FromJson};
43//!
44//! #[derive(ToJson, FromJson, Debug, PartialEq)]
45//! #[serde(rename_all = "camelCase")]
46//! struct User<'a> {
47//! user_id: u64,
48//! name: &'a str,
49//! #[serde(skip_serializing_if = "Option::is_none")]
50//! email: Option<String>,
51//! #[serde(default)]
52//! score: f64,
53//! }
54//!
55//! let input = r#"{"userId":1,"name":"alice","score":9.5}"#;
56//! let user: User = User::from_json_str(input).unwrap();
57//! let out = user.to_json_string();
58//! ```
59
60// Enable `std::simd` portable SIMD on nightly when both features are set.
61#![cfg_attr(all(feature = "simd", feature = "unstable"), feature(portable_simd))]
62
63pub mod error;
64pub mod scanner;
65pub mod ser;
66pub mod de;
67pub mod simd;
68#[cfg(feature = "simd-intrinsics")]
69pub mod simd_arch;
70pub mod fixed;
71#[cfg(feature = "stats")]
72pub mod stats;
73
74pub use error::Error;
75pub use scanner::{JsonStr, Scanner};
76pub use ser::ToJson;
77pub use de::FromJson;
78pub use fixed::{FixedBuf, ToJsonExt, json_str_len};
79
80#[cfg(feature = "derive")]
81pub use jzon_derive::{FromJson, ToJson};