json-escape
A high-performance, no_std
compatible library for streaming JSON string escaping and unescaping. Process large JSON strings with zero-copy slicing and no intermediate allocations, ideal for parsers and memory-constrained environments. ⚡
The core of the library is two iterator-based structs:
Escape
: Lazily yields escaped string slices from an input&str
.Unescape
: Lazily yields unescaped byte slices from an input&[u8]
.
This streaming approach avoids allocating a single large String
for the result, making it incredibly efficient for large data and I/O operations.
Key Features
- 🚀 Streaming & Iterator-based: Process data in chunks without buffering the entire result in memory.
- ✨ Zero-Copy Slicing: For sequences of characters that don't need modification, the iterators yield slices borrowed directly from the input.
- 🧩
no_std
Compatible: Usable in embedded systems and other memory-constrained environments (withalloc
for owned conversions). - Unicode Excellence: Correctly decodes
\uXXXX
sequences, including full support for UTF-16 surrogate pairs (e.g.,\uD83D\uDE00
for 😀). - 🔎 Robust Error Handling: The
Unescape
iterator returns descriptive errors for invalid or truncated escape sequences. std::io
Integration: With thestd
feature,Unescape
implementsstd::io::Read
, allowing it to be plugged directly into APIs that consume readers (likeserde_json
).
Quick Start
Escaping a String
use escape_str;
use Cow;
let input = "Hello, \"world\"!\nThis is a backslash: \\";
let expected = r#"Hello, \"world\"!\nThis is a backslash: \\"#;
// escape_str returns an iterator. Collect it into a String.
let escaped_string: String = escape_str.collect;
assert_eq!;
// For efficiency, convert to a Cow<str>. This avoids allocation
// if the input string requires no escaping.
let cow: = escape_str.into;
assert!;
Unescaping a String
use unescape;
use Cow;
let input = r#"Emoji: \uD83D\uDE00 and a tab\t!"#;
let expected = "Emoji: 😀 and a tab\t!";
// unescape returns an iterator over Result<&[u8], _>.
// The `decode_utf8` helper collects and validates the output.
let decoded_cow: = unescape.decode_utf8.unwrap;
assert_eq!;
Advanced Usage: Zero-Allocation REST API Parsing
A common scenario in web services is receiving a JSON payload where one of the fields is another JSON object, escaped as a string.
The standard approach requires allocating a new String
to hold the unescaped payload
before parsing it. This is inefficient, especially for large payloads.
json-escape
avoids this entirely by plugging its streaming Unescape
reader directly into serde_json
.
The json-escape
Solution: No Intermediate Allocation!
use unescape_quoted;
use Deserialize;
use RawValue;
// The inner payload we want to extract and parse.
// The outer structure. We use `&RawValue` for a zero-copy view.
Installation
Add this to your Cargo.toml
:
[]
= "0.1.1"
Feature Flags
alloc
(enabled by default): ProvidesCow
,String
, andVec
conversions.std
(enabled by default): Providesstd::io::Read
andstd::error::Error
implementations.
For no_std
environments without an allocator, use:
[]
= { = "*", = false }
License
This project is licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.