Merge branch 'master' of git.timshomepage.net:timw4mail/json-parser
This commit is contained in:
commit
a3b16acb13
5
README.md
Normal file
5
README.md
Normal 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/)
|
32
src/lib.rs
32
src/lib.rs
@ -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")));
|
||||
|
Loading…
Reference in New Issue
Block a user