How cyruspyre/flexon works
1. What this repo is
- flexon is a single Rust library crate with two companion crates: benchmark for performance measurement and fuzz for parser robustness testing.
- The core architecture is centered on fast JSON ingestion, optional JSONC-style comments, optional span tracking, lazy value representation, and serde interoperability.
2. Top-level data flow
- Input enters through Parser from either in-memory text or a streaming reader.
- The parser validates and tokenizes JSON, then produces either a concrete Value, a lazy value tree, or a serde-deserialized Rust type.
- The reverse path goes through serde serialization modules to emit JSON text.
3. Input/source boundary
- src/source isolates how bytes are obtained and prepared for parsing.
- The main source forms are a reader-backed source and a null-padded in-memory source, which support safe fast scanning and in-situ parsing optimizations.
- This boundary is important because some APIs operate on borrowed buffers, while others stream from Read.
4. Parsing core
- src/parser is the structural JSON engine.
- It contains normal parsing plus specialized paths for skipping, skipping to a target location, and unchecked parsing variants.
- The checked vs. unchecked split is a major architectural boundary: checked APIs validate as they go, while unchecked paths trade safety checks for speed, especially in partial-parse scenarios.
5. Partial extraction / path-based access
- The crate supports parsing only a subtree of a document, not necessarily the whole JSON payload.
- This is surfaced through path-oriented APIs such as “get from” and lazy access by pointer-like queries.
- Architecturally, this means the parser can stop early once the requested substructure is found, rather than materializing the full document.
6. Value model
- src/value defines the JSON value layer and is split into borrowed, owned, and lazy representations.
- Borrowed values avoid allocations where possible; owned values own string/storage data; lazy values validate while deferring materialization.
- Arrays, objects, numbers, strings, and a builder are separated into dedicated modules, showing that the value layer is not a monolith but a family of storage strategies.
7. Serde integration
- src/serde is the bridge to standard Rust serialization/deserialization.
- It contains dedicated deserialize and serialize adapters, plus span/value/unchecked support around those flows.
- This is the main interop surface for typed Rust structs and enums, and it is distinct from the crate’s native untyped Value APIs.
8. Configuration and feature gating
- src/config, src/comment, and src/span define optional behavior that changes the parser’s contract.
- Comments and trailing commas are compile-time or runtime configuration choices depending on the feature set; span tracking adds source-location metadata to parsed output.
- The README makes clear that these features are intentionally optional so the fast path stays lean for strict JSON use cases.
9. SIMD and numeric parsing
- src/simd and src/fast_float are the performance-heavy subsystems.
- SIMD is used for broad scanning/tokenization acceleration, while fast_float handles high-performance number parsing across decimal/binary representations.
- The architecture treats numeric conversion as a distinct optimized stage rather than folding it into generic parsing logic.
10. Error, pointer, and span metadata
- src/error, src/pointer, and src/span provide the supporting metadata and diagnostics layer.
- These modules allow the parser and higher-level APIs to report structured failures, locate values in the input, and express JSON-pointer-style navigation.
- This is what makes the checked APIs and comment/span features usable in real applications.
11. Auxiliary crates
- benchmark contains representative JSON fixtures and benchmark targets for struct deserialization, value deserialization, field extraction, lazy access, and serialization.
- fuzz contains multiple parser fuzz targets, including checked and unchecked parse paths, which is a strong signal that parser correctness and safety boundaries are a first-class concern.
12. How to draw the architecture
- A good diagram would show input source -> parser -> {value tree | serde deserializer | lazy tree} with optional branches for comments/spans and checked/unchecked validation.
- Add a parallel optimized path for SIMD/fast-float underneath parsing.
- Show serde as an adapter boundary to user structs, and benchmark/fuzz as external verification/measurement crates around the core library.
Main entry points: parser/mod.rs Parser · lib.rs flexon · serde/mod.rs Serde bridge · value/mod.rs Value
Components 25
Core crate
Main JSON parsing, value, and serde integration layers.
- flexon · Top-level library entry and public API surface.
src/lib.rs - Source · Abstracts in-memory and streaming input preparation.
src/source/mod.rs - Reader src · Reader-backed input for streaming parsing.
src/source/reader.rs - Padded src · Null-padded in-memory source for fast scanning and in-situ parsing.
src/source/null_padded.rs - Parser · Structural JSON parsing, skipping, partial extraction, and unchecked variants.
src/parser/mod.rs - Skip path · Early-exit traversal for skipping values and locating subtrees.
src/parser/skip.rs - Get from · Targeted parsing that can stop once a requested subtree is found.
src/parser/skip_to.rs - Unchecked parse · Parsing variants that trade validation for speed.
src/parser/unchecked.rs - Value · Native JSON value layer spanning borrowed, owned, and lazy representations.
src/value/mod.rs - Borrowed · Borrowed JSON values that avoid allocations when possible.
src/value/borrowed/mod.rs - Owned · Owned JSON values with self-contained string and buffer storage.
src/value/owned/mod.rs - Lazy · Lazy value representation that validates before materializing fully.
src/value/lazy/mod.rs - Serde bridge · Adapters between flexon values and Rust serde types.
src/serde/mod.rs - Deserialize · Typed deserialization from JSON into Rust data structures.
src/serde/de.rs - Serialize · Serialization from Rust values back to JSON text.
src/serde/ser.rs - Serde value · Integration glue for serde and flexon value representations.
src/serde/value.rs - SIMD · Vectorized scanning and tokenization acceleration.
src/simd.rs - Fast float · High-performance numeric conversion for decimal and binary forms.
src/fast_float/mod.rs
Optional features
Configuration, comments, spans, pointer navigation, and diagnostics.
- Config · Parse-time configuration and feature-gated behavior.
src/config.rs - Comments · Optional comment and trailing-comma support.
src/comment.rs - Spans · Source-location tracking for parsed output and diagnostics.
src/span.rs - Errors · Structured parse and conversion failures.
src/error.rs - Pointer · JSON-pointer-style path addressing and lookup.
src/pointer.rs
Connections 35
- flexon → Source: uses · Public APIs begin with input sources.
- flexon → Parser: exposes · Parser is the central JSON ingestion engine.
- flexon → Value: exposes · Untyped JSON values are a core output form.
- flexon → Serde bridge: bridges · Serde integration is a primary interop surface.
- flexon → Config: configures · Feature-gated behavior shapes parsing contracts.
- flexon → Errors: reports · Structured errors are part of the public API.
- flexon → Pointer: navigates · Pointer-style lookups support partial access.
- flexon → Spans: annotates · Span metadata can be carried through parsing and serde flows.
- flexon → SIMD: accelerates · SIMD underpins fast scanning paths.
- flexon → Fast float: parses numbers · Dedicated numeric conversion is a performance-sensitive stage.
- Source → Reader src: stream · Reader-backed inputs support streaming parsing.
- Source → Padded src: buffer · Null-padded memory inputs support fast in-situ parsing.
- Source → Parser: feeds · Prepared input is handed to the parser.
- Parser → Skip path: contains · Skipping and partial traversal are parser subpaths.
- Parser → Get from: contains · Path-based early exit is a dedicated parser mode.
- Parser → Unchecked parse: contains · Unchecked variants form the speed-oriented boundary.
- Parser → Value: produces · Parsing can materialize JSON values.
- Parser → Deserialize: deserializes · Parser output can flow directly into typed Rust data.
- Parser → Comments: optionally allows · Comment/trailing-comma handling changes the accepted syntax.
- Parser → Spans: optionally tracks · Span tracking can be attached during parsing.
- Value → Borrowed: variants · Borrowed storage is one value strategy.
- Value → Owned: variants · Owned storage is another value strategy.
- Value → Lazy: variants · Lazy representation defers full materialization.
- Serde bridge → Deserialize: adapts · Deserialize adapters connect JSON to Rust types.
- Serde bridge → Serialize: adapts · Serialize adapters emit JSON text from Rust data.
- Serde bridge → Serde value: supports · Value-specific serde glue sits alongside typed adapters.
- Serde bridge → Spans: propagates · Span-aware serde flows preserve source metadata.
- Serde bridge → Unchecked parse: can use · Serde interop includes unchecked fast paths.
- Fast float → Parser: supports · Numeric conversion is an optimized parsing stage.
- SIMD → Parser: supports · SIMD accelerates scanning and token recognition.
- Benchmark → Deserialize: measures · Benchmarks cover typed deserialization.
- Benchmark → Value: measures · Benchmarks cover value parsing and extraction.
- Benchmark → Serialize: measures · Benchmarks cover serialization.
- Fuzz → Parser: tests · Fuzz targets exercise checked parsing paths.
- Fuzz → Unchecked parse: tests · Fuzz targets also cover unchecked parsing paths.