Merge branch 'master' of git.timshomepage.net:timw4mail/json-parser

This commit is contained in:
Timothy Warren 2020-02-11 10:58:03 -05:00
commit a3b16acb13
3 changed files with 17 additions and 36 deletions

5
README.md Normal file
View File

@ -0,0 +1,5 @@
# Naive JSON Parser
A Rust JSON parser with no dependencies
Based on a JavaScript [implementation](https://lihautan.com/json-parser-with-javascript/)

View File

@ -390,12 +390,10 @@ mod tests {
#[test] #[test]
fn parse_keyword() { fn parse_keyword() {
let mut parser = JSON::new(r#""foobarbaz""#); let res = JSON::new(r#""foobarbaz""#).parse_keyword("true", JSONValue::True);
let res = JSON::parse_keyword(&mut parser, "true", JSONValue::True);
assert_eq!(res, Ok(None)); assert_eq!(res, Ok(None));
let mut parser = JSON::new("true"); let res = JSON::new("true").parse_keyword("true", JSONValue::True);
let res = JSON::parse_keyword(&mut parser, "true", JSONValue::True);
assert_eq!(res, Ok(Some(JSONValue::True))); assert_eq!(res, Ok(Some(JSONValue::True)));
} }
@ -408,41 +406,34 @@ mod tests {
#[test] #[test]
fn parse_string() { fn parse_string() {
let mut parser = JSON::new(r#""\t""#); let res = JSON::new(r#""\t""#).parse_string();
let res = JSON::parse_string(&mut parser);
assert_eq!(res, Ok(Some(JSONValue::String(String::from("\t"))))); assert_eq!(res, Ok(Some(JSONValue::String(String::from("\t")))));
let mut parser = JSON::new(r#""\u203d""#); let res = JSON::new(r#""\u203d""#).parse_string();
let res = JSON::parse_string(&mut parser);
assert_eq!(res, Ok(Some(JSONValue::String(String::from(""))))); assert_eq!(res, Ok(Some(JSONValue::String(String::from("")))));
} }
#[test] #[test]
fn parse_empty_array() { fn parse_empty_array() {
let mut parser = JSON::new("[]"); let res = JSON::new("[]").parse_value();
let res = JSON::parse_value(&mut parser);
assert_eq!(res, Ok(JSONValue::Array(vec![]))); assert_eq!(res, Ok(JSONValue::Array(vec![])));
} }
#[test] #[test]
fn parse_number() { fn parse_number() {
// This function works like I think, right? // This function works like I think, right?
assert_ne!(','.is_ascii_digit(), true); assert_eq!(','.is_ascii_digit(), false);
let mut parser = JSON::new(r#""foo""#); let res = JSON::new(r#""foo""#).parse_number();
let res = JSON::parse_number(&mut parser);
assert_eq!(res, Ok(None)); assert_eq!(res, Ok(None));
let mut parser = JSON::new("3.14159"); let res = JSON::new("3.14159").parse_number();
let res = JSON::parse_number(&mut parser);
assert_eq!(res, Ok(Some(JSONValue::Number(3.14159f64)))); assert_eq!(res, Ok(Some(JSONValue::Number(3.14159f64))));
let mut parser = JSON::new("3e4"); let res = JSON::new("3e4").parse_number();
let res = JSON::parse_number(&mut parser);
assert_eq!(res, Ok(Some(JSONValue::Number(3e4f64)))); assert_eq!(res, Ok(Some(JSONValue::Number(3e4f64))));
let mut parser = JSON::new("1.234,"); let res = JSON::new("1.234,").parse_number();
let res = JSON::parse_number(&mut parser);
assert_eq!(res, Ok(Some(JSONValue::Number(1.234f64)))); assert_eq!(res, Ok(Some(JSONValue::Number(1.234f64))));
} }
@ -462,8 +453,7 @@ mod tests {
#[test] #[test]
fn parse_object() { fn parse_object() {
let mut parser = JSON::new(r#"{"foo": "bar"}"#); let result = JSON::new(r#"{"foo": "bar"}"#).parse_object();
let result = JSON::parse_object(&mut parser);
let mut hash_map: HashMap<String, JSONValue> = HashMap::new(); let mut hash_map: HashMap<String, JSONValue> = HashMap::new();
hash_map.insert(String::from("foo"), JSONValue::String(String::from("bar"))); hash_map.insert(String::from("foo"), JSONValue::String(String::from("bar")));
@ -531,7 +521,7 @@ mod tests {
); );
let result = JSON::parse( let result = JSON::parse(
r#"[{ r#"[{
"a": 9.38083151965, "a": 9.38083151965,
"b": 4e3, "b": 4e3,
"c": [1, 2, 3], "c": [1, 2, 3],

View File

@ -1,14 +0,0 @@
[{
"a": 1,
"b": 4e3,
"c": [1, 2, 3],
"d": "foo",
"e": {
"f": {
"g": {
"h": null
}
}
},
"i": ["\"", "\\", "/", "\b", "\f", "\n", "\r", "\t", "\u0001", "\uface"]
}]