Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Support string prefix parsing #995

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/de.rs
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
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
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");
}