Skip to content

Commit

Permalink
Merge pull request #4 from azuqua/feature/parse-slashes
Browse files Browse the repository at this point in the history
[OKTA-251323] y u do dis
  • Loading branch information
alecembke-okta authored Oct 1, 2019
2 parents 6ff0d5c + e5f3d1c commit 815b1f4
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
target
Cargo.lock
.idea
.idea
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "json"
version = "0.11.18"
version = "0.11.19"
authors = ["Maciej Hirsz <[email protected]>", "Alec Embke <[email protected]>"]
description = "JSON implementation in Rust"
repository = "https://github.com/azuqua/json-rust"
Expand Down
44 changes: 40 additions & 4 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,18 +504,20 @@ 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] {
self.buffer.push(ch);
ch = expect_byte!(self);
continue;
}

match ch {
b'"' => break,
b'\\' => {
Expand All @@ -528,7 +530,7 @@ impl<'a> Parser<'a> {
},
b'"' |
b'\\' |
b'/' => escaped,
b'/' => escaped,
b'b' => 0x8,
b'f' => 0xC,
b't' => b'\t',
Expand All @@ -554,7 +556,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)
)
})
}
Expand Down Expand Up @@ -772,6 +774,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.
Expand All @@ -784,12 +787,45 @@ pub fn parse(source: &str) -> Result<JsonValue> {
#[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}";
Expand Down
1 change: 1 addition & 0 deletions tests/test_json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"a\\/b":"c\"d\"e","f":1}

0 comments on commit 815b1f4

Please sign in to comment.