This directory contains small, self-contained examples that demonstrate how to use
json_escape in different ways — from the simplest string unescape to advanced
streaming and zero-copy integration with serde_json.
Each example is runnable with:
cargo run --example <name>-
What it shows: The basics of unescaping a string slice.
-
Key API:
unescape+.decode_utf8() -
Run:
cargo run --example simple_unescape
-
What it shows: Manual streaming unescape from a file using
UnescapeStream. -
How it works:
- Reads a file in fixed-size chunks (
[u8; 8]buffer). - Feeds each chunk into
try_unescape_next. - Handles “boundary characters” and tokens manually.
- Reads a file in fixed-size chunks (
-
Why it matters: Teaches how streaming works under the hood and how to deal with chunked I/O.
-
Run:
cargo run --example stream_file
-
What it shows: A higher-level version of the previous example using
UnescapeStream::unescape_from_fn. -
How it works:
srcclosure pulls chunks from the file.dstclosure consumes eachUnescapedToken.unescape_from_fndrives the loop for you.
-
Why it matters: Much simpler if you don’t need low-level control.
-
Run:
cargo run --example stream_file_fn
-
What it shows: Advanced integration with [
serde_json] usingRawValue. -
How it works:
- Parse outer JSON with a raw, escaped payload.
- Use
unescape_quotedto create a streaming reader. - Deserialize the inner payload directly without allocating an intermediate string.
-
Why it matters: Demonstrates zero-copy JSON parsing for maximum efficiency.
-
Run:
cargo run --example zero_copy_serde
-
What it shows: How to work directly with the low-level token API.
-
How it works:
- Iterate over
EscapedTokenwhen escaping. - Iterate over
UnescapedTokenwhen unescaping. - Custom processing logic (e.g. transforming or filtering tokens).
- Iterate over
-
Why it matters: Gives you maximum control if you need custom escape/unescape behavior.
-
Run:
cargo run --example custom_processor_tokens
- Start with
simple_unescape.rsto understand the basics. - Move to
stream_file.rsto see manual streaming. - Then try
stream_file_fn.rsto learn the ergonomic driver. - Explore
zero_copy_serde.rsfor advanced serde integration. - Finally, dive into
custom_processor_tokens.rsfor low-level control.
escape_str→ Iterator ofEscapedToken.unescape→ Iterator ofUnescapedTokenresults.UnescapeStream→ Incremental unescaper for chunked input.UnescapeStream::unescape_from_fn→ Ergonomic streaming driver.unescape_quoted→ Wraps a JSON string (with quotes + escapes) as aReadforserde_json.
cargo run --example simple_unescape
cargo run --example stream_file
cargo run --example stream_file_fn
cargo run --example zero_copy_serde
cargo run --example custom_processor_tokens