Skip to content

Commit 815b1f4

Browse files
Merge pull request #4 from azuqua/feature/parse-slashes
[OKTA-251323] y u do dis
2 parents 6ff0d5c + e5f3d1c commit 815b1f4

File tree

4 files changed

+43
-6
lines changed

4 files changed

+43
-6
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
target
22
Cargo.lock
3-
.idea
3+
.idea

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "json"
3-
version = "0.11.18"
3+
version = "0.11.19"
44
authors = ["Maciej Hirsz <[email protected]>", "Alec Embke <[email protected]>"]
55
description = "JSON implementation in Rust"
66
repository = "https://github.com/azuqua/json-rust"

src/parser.rs

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -504,18 +504,20 @@ impl<'a> Parser<'a> {
504504
// having to be read from source to a buffer and then from a buffer to
505505
// our target string. Nothing to be done about this, really.
506506
fn read_complex_string<'b>(&mut self, start: usize) -> Result<&'b str> {
507-
self.buffer.clear();
507+
let len = self.buffer.len();
508+
//self.buffer.clear();
508509
let mut ch = b'\\';
509510

510511
// TODO: Use fastwrite here as well
511-
self.buffer.extend_from_slice(self.source[start .. self.index - 1].as_bytes());
512+
self.buffer.extend_from_slice(&self.source.as_bytes()[start .. self.index - 1]);
512513

513514
loop {
514515
if ALLOWED[ch as usize] {
515516
self.buffer.push(ch);
516517
ch = expect_byte!(self);
517518
continue;
518519
}
520+
519521
match ch {
520522
b'"' => break,
521523
b'\\' => {
@@ -528,7 +530,7 @@ impl<'a> Parser<'a> {
528530
},
529531
b'"' |
530532
b'\\' |
531-
b'/' => escaped,
533+
b'/' => escaped,
532534
b'b' => 0x8,
533535
b'f' => 0xC,
534536
b't' => b'\t',
@@ -554,7 +556,7 @@ impl<'a> Parser<'a> {
554556
// issues here, we construct a new slice from raw parts, which
555557
// then has lifetime bound to the outer function scope instead
556558
// of the parser itself.
557-
slice::from_raw_parts(self.buffer.as_ptr(), self.buffer.len())
559+
slice::from_raw_parts(self.buffer[len..].as_ptr(), self.buffer.len() - len)
558560
)
559561
})
560562
}
@@ -772,6 +774,7 @@ impl<'a> Parser<'a> {
772774
}
773775
}
774776

777+
#[derive(Debug)]
775778
struct StackBlock<'a>(JsonValue, &'a str);
776779

777780
// All that hard work, and in the end it's just a single function in the API.
@@ -784,12 +787,45 @@ pub fn parse(source: &str) -> Result<JsonValue> {
784787
#[cfg(test)]
785788
mod tests {
786789
use super::*;
790+
use ::stringify;
791+
use ::stringify_pretty;
787792
use ::value::JsonValue;
788793

789794
#[macro_use]
790795
use crate::object;
791796
use crate::array;
792797

798+
use std::fs::File;
799+
use std::io::prelude::*;
800+
801+
#[test]
802+
fn it_should_parse_escaped_forward_slashes() {
803+
let mut file = File::open("tests/test_json").unwrap();
804+
let mut contents = String::new();
805+
file.read_to_string(&mut contents).unwrap();
806+
807+
let actual = parse(&contents).unwrap();
808+
let serialized = stringify(actual.clone());
809+
810+
let serialized_pretty = stringify_pretty(actual.clone(), 2);
811+
println!("serialized: {}", serialized_pretty);
812+
813+
assert_eq!(serialized, contents);
814+
}
815+
816+
#[test]
817+
fn it_should_parse_escaped_forward_slashes_2() {
818+
let contents = String::from("{\"ab\":\"c\\\"d\\\"e\",\"f\":1}");
819+
820+
let actual = parse(&contents).unwrap();
821+
let serialized = stringify(actual.clone());
822+
823+
let serialized_pretty = stringify_pretty(actual.clone(), 2);
824+
println!("serialized: {}", serialized_pretty);
825+
826+
assert_eq!(serialized, contents);
827+
}
828+
793829
#[test]
794830
fn it_should_parse_basic_json_values() {
795831
let s = "{\"a\":1,\"b\":true,\"c\":false,\"d\":null,\"e\":2}";

tests/test_json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"a\\/b":"c\"d\"e","f":1}

0 commit comments

Comments
 (0)