Module explicit

Module explicit 

Source
Expand description

More explicit and fine-grained iterators for JSON escaping and unescaping.

This module provides an alternative API to the one in the crate root. While the root API yields slices (&str or &[u8]) that represent the final output, this module’s iterators yield “chunk” structs. These structs distinguish between parts of the input that were processed literally and the specific characters that were escaped or unescaped.

This approach offers several advantages:

  • Greater Control: You can inspect each component of the transformation, which can be useful for debugging, logging, or more complex data processing.
  • Potential Performance: By avoiding the need to look up single-byte escape sequences in a table on every iteration, some workflows may see a minor performance improvement.
  • Clarity: The structure of the output more closely reflects the transformation process, which can make the logic easier to follow.

§Example: Escaping

use json_escape::explicit::escape_str;

let mut escaper = escape_str("a\nb");

// The first chunk contains the literal "a" and the escaped newline.
let chunk1 = escaper.next().unwrap();
assert_eq!("a", chunk1.literal());
assert_eq!(Some(r#"\n"#), chunk1.escaped());

// The second chunk contains the literal "b" and no escaped sequence.
let chunk2 = escaper.next().unwrap();
assert_eq!("b", chunk2.literal());
assert_eq!(None, chunk2.escaped());

// The iterator is now exhausted.
assert!(escaper.next().is_none());

§Example: Unescaping

use json_escape::explicit::unescape;

let mut unescaper = unescape(br"hello\tworld");

// The first chunk contains the literal "hello" and the unescaped tab.
let chunk1 = unescaper.next().unwrap().unwrap();
assert_eq!(b"hello", chunk1.literal());
assert_eq!(Some('\t'), chunk1.unescaped());

// The second chunk contains the literal "world" and no unescaped character.
let chunk2 = unescaper.next().unwrap().unwrap();
assert_eq!(b"world", chunk2.literal());
assert_eq!(None, chunk2.unescaped());

// The iterator is now exhausted.
assert!(unescaper.next().is_none());

Both Escape and Unescape iterators provide display helpers for easy integration with Rust’s formatting system, preserving the zero-allocation benefits of the main API.

Structs§

DisplayUnescape
A wrapper struct for implementing fmt::Display on an Unescape iterator.
DisplayUnescapedChunk
Helper struct for safely displaying an UnescapedChunk.
Escape
An iterator over a string that yields EscapedChunks.
EscapedChunk
A chunk of a JSON-escaped string, separating the literal part from the escaped sequence.
Unescape
An iterator over a byte slice that yields UnescapedChunks.
UnescapedChunk
A chunk of a JSON-unescaped byte slice, separating the literal part from the unescaped character.

Functions§

escape_str
Creates an iterator that yields chunks of an escaped JSON string.
unescape
Creates an iterator that yields chunks of an unescaped JSON string.
unescape_quoted
Creates a streaming JSON string unescaper that handles enclosing quotes.