From e5bf0fc688fe2951e70ba1be377073d9da704985 Mon Sep 17 00:00:00 2001 From: Alec Embke Date: Mon, 30 Sep 2019 18:41:11 -0700 Subject: [PATCH 1/2] [OKTA-251323] y u do dis --- .gitignore | 2 +- Cargo.toml | 2 +- src/parser.rs | 45 +++++++++++++++++++++++++++++++++++++++++---- tests/test_json | 1 + 4 files changed, 44 insertions(+), 6 deletions(-) create mode 100644 tests/test_json diff --git a/.gitignore b/.gitignore index 2da6cde..64f40ab 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ target Cargo.lock -.idea \ No newline at end of file +.idea diff --git a/Cargo.toml b/Cargo.toml index 100764f..6de589e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "json" -version = "0.11.18" +version = "0.11.19" authors = ["Maciej Hirsz ", "Alec Embke "] description = "JSON implementation in Rust" repository = "https://github.com/azuqua/json-rust" diff --git a/src/parser.rs b/src/parser.rs index 75c3875..4a15737 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -418,6 +418,7 @@ impl<'a> Parser<'a> { let colno = col.chars().count(); + println!("buffer {:?}", self.buffer.iter().map(|c| *c as char).collect::>()); Err(Error::UnexpectedCharacter { ch: ch, line: lineno + 1, @@ -504,11 +505,12 @@ impl<'a> Parser<'a> { // having to be read from source to a buffer and then from a buffer to // our target string. Nothing to be done about this, really. fn read_complex_string<'b>(&mut self, start: usize) -> Result<&'b str> { - self.buffer.clear(); + let len = self.buffer.len(); + //self.buffer.clear(); let mut ch = b'\\'; // TODO: Use fastwrite here as well - self.buffer.extend_from_slice(self.source[start .. self.index - 1].as_bytes()); + self.buffer.extend_from_slice(&self.source.as_bytes()[start .. self.index - 1]); loop { if ALLOWED[ch as usize] { @@ -516,6 +518,7 @@ impl<'a> Parser<'a> { ch = expect_byte!(self); continue; } + match ch { b'"' => break, b'\\' => { @@ -528,7 +531,7 @@ impl<'a> Parser<'a> { }, b'"' | b'\\' | - b'/' => escaped, + b'/' => escaped, b'b' => 0x8, b'f' => 0xC, b't' => b'\t', @@ -554,7 +557,7 @@ impl<'a> Parser<'a> { // issues here, we construct a new slice from raw parts, which // then has lifetime bound to the outer function scope instead // of the parser itself. - slice::from_raw_parts(self.buffer.as_ptr(), self.buffer.len()) + slice::from_raw_parts(self.buffer[len..].as_ptr(), self.buffer.len() - len) ) }) } @@ -772,6 +775,7 @@ impl<'a> Parser<'a> { } } +#[derive(Debug)] struct StackBlock<'a>(JsonValue, &'a str); // All that hard work, and in the end it's just a single function in the API. @@ -784,12 +788,45 @@ pub fn parse(source: &str) -> Result { #[cfg(test)] mod tests { use super::*; + use ::stringify; + use ::stringify_pretty; use ::value::JsonValue; #[macro_use] use crate::object; use crate::array; + use std::fs::File; + use std::io::prelude::*; + + #[test] + fn it_should_parse_escaped_forward_slashes() { + let mut file = File::open("tests/test_json").unwrap(); + let mut contents = String::new(); + file.read_to_string(&mut contents).unwrap(); + + let actual = parse(&contents).unwrap(); + let serialized = stringify(actual.clone()); + + let serialized_pretty = stringify_pretty(actual.clone(), 2); + println!("serialized: {}", serialized_pretty); + + assert_eq!(serialized, contents); + } + + #[test] + fn it_should_parse_escaped_forward_slashes_2() { + let contents = String::from("{\"ab\":\"c\\\"d\\\"e\",\"f\":1}"); + + let actual = parse(&contents).unwrap(); + let serialized = stringify(actual.clone()); + + let serialized_pretty = stringify_pretty(actual.clone(), 2); + println!("serialized: {}", serialized_pretty); + + assert_eq!(serialized, contents); + } + #[test] fn it_should_parse_basic_json_values() { let s = "{\"a\":1,\"b\":true,\"c\":false,\"d\":null,\"e\":2}"; diff --git a/tests/test_json b/tests/test_json new file mode 100644 index 0000000..aa8978e --- /dev/null +++ b/tests/test_json @@ -0,0 +1 @@ +{"a\\/b":"c\"d\"e","f":1} \ No newline at end of file From e5f3d1cc6eb86358db64478779a4fa241ca566d6 Mon Sep 17 00:00:00 2001 From: Alec Embke Date: Mon, 30 Sep 2019 18:42:16 -0700 Subject: [PATCH 2/2] clean up --- src/parser.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/parser.rs b/src/parser.rs index 4a15737..807288c 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -418,7 +418,6 @@ impl<'a> Parser<'a> { let colno = col.chars().count(); - println!("buffer {:?}", self.buffer.iter().map(|c| *c as char).collect::>()); Err(Error::UnexpectedCharacter { ch: ch, line: lineno + 1,