diff --git a/src/de.rs b/src/de.rs index 88d0f2624..21580f65b 100644 --- a/src/de.rs +++ b/src/de.rs @@ -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")] diff --git a/src/read.rs b/src/read.rs index fc3a3ca74..0106387c2 100644 --- a/src/read.rs +++ b/src/read.rs @@ -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] { @@ -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> {} diff --git a/tests/test.rs b/tests/test.rs index 6c08cc8d2..ec71555e0 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -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"); +}