Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ where
disable_recursion_limit: false,
}
}

/// Consume the deserializer and return its inner reader.
pub fn into_reader(self) -> R {
self.read
}
}

#[cfg(feature = "std")]
Expand Down
10 changes: 10 additions & 0 deletions src/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,11 @@ impl<'a> SliceRead<'a> {
}
}

/// Get the byte index of the data that will be read next
pub fn index(&self) -> usize {
self.index
}

fn position_of_index(&self, i: usize) -> Position {
let mut position = Position { line: 1, column: 0 };
for ch in &self.slice[..i] {
Expand Down Expand Up @@ -623,6 +628,11 @@ impl<'a> StrRead<'a> {
data: s,
}
}

/// Get the byte index of the data that will be read next
pub fn index(&self) -> usize {
self.delegate.index()
}
}

impl<'a> private::Sealed for StrRead<'a> {}
Expand Down
19 changes: 19 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2407,3 +2407,22 @@ fn hash_positive_and_negative_zero() {
assert_eq!(hash(k1), hash(k2));
}
}

#[test]
fn parse_string_prefix() {
#[derive(PartialEq, Deserialize, Debug)]
struct S {
a: u32
}

let data = "{\"a\": 42} tail";

let mut de = serde_json::Deserializer::from_str(data);
let val = S::deserialize(&mut de).unwrap();

assert_eq!(val, S { a: 42 });

let str_read = de.into_reader();
let tail = &data[str_read.index()..];
assert_eq!(tail, " tail");
}