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]
fn parse_keyword() {
let mut parser = JSON::new(r#""foobarbaz""#);
let res = JSON::parse_keyword(&mut parser, "true", JSONValue::True);
let res = JSON::new(r#""foobarbaz""#).parse_keyword("true", JSONValue::True);
assert_eq!(res, Ok(None));
let mut parser = JSON::new("true");
let res = JSON::parse_keyword(&mut parser, "true", JSONValue::True);
let res = JSON::new("true").parse_keyword("true", JSONValue::True);
assert_eq!(res, Ok(Some(JSONValue::True)));
}
@ -408,41 +406,34 @@ mod tests {
#[test]
fn parse_string() {
let mut parser = JSON::new(r#""\t""#);
let res = JSON::parse_string(&mut parser);
let res = JSON::new(r#""\t""#).parse_string();
assert_eq!(res, Ok(Some(JSONValue::String(String::from("\t")))));
let mut parser = JSON::new(r#""\u203d""#);
let res = JSON::parse_string(&mut parser);
let res = JSON::new(r#""\u203d""#).parse_string();
assert_eq!(res, Ok(Some(JSONValue::String(String::from("")))));
}
#[test]
fn parse_empty_array() {
let mut parser = JSON::new("[]");
let res = JSON::parse_value(&mut parser);
let res = JSON::new("[]").parse_value();
assert_eq!(res, Ok(JSONValue::Array(vec![])));
}
#[test]
fn parse_number() {
// 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::parse_number(&mut parser);
let res = JSON::new(r#""foo""#).parse_number();
assert_eq!(res, Ok(None));
let mut parser = JSON::new("3.14159");
let res = JSON::parse_number(&mut parser);
let res = JSON::new("3.14159").parse_number();
assert_eq!(res, Ok(Some(JSONValue::Number(3.14159f64))));
let mut parser = JSON::new("3e4");
let res = JSON::parse_number(&mut parser);
let res = JSON::new("3e4").parse_number();
assert_eq!(res, Ok(Some(JSONValue::Number(3e4f64))));
let mut parser = JSON::new("1.234,");
let res = JSON::parse_number(&mut parser);
let res = JSON::new("1.234,").parse_number();
assert_eq!(res, Ok(Some(JSONValue::Number(1.234f64))));
}
@ -462,8 +453,7 @@ mod tests {
#[test]
fn parse_object() {
let mut parser = JSON::new(r#"{"foo": "bar"}"#);
let result = JSON::parse_object(&mut parser);
let result = JSON::new(r#"{"foo": "bar"}"#).parse_object();
let mut hash_map: HashMap<String, JSONValue> = HashMap::new();
hash_map.insert(String::from("foo"), JSONValue::String(String::from("bar")));

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"]
}]