json object
This commit is contained in:
parent
c5f971f7ff
commit
74b6a1e410
45
src/lib.rs
45
src/lib.rs
@ -15,7 +15,6 @@ mod tests {
|
|||||||
use crate::combinators::repeated;
|
use crate::combinators::repeated;
|
||||||
use crate::primitives::{any_char, literal, literal_char, one_of, pred};
|
use crate::primitives::{any_char, literal, literal_char, one_of, pred};
|
||||||
use crate::sequence::seq;
|
use crate::sequence::seq;
|
||||||
use std::collections::HashMap;
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_parsing() {
|
fn test_parsing() {
|
||||||
@ -38,7 +37,7 @@ mod tests {
|
|||||||
Str(String),
|
Str(String),
|
||||||
Num(f64),
|
Num(f64),
|
||||||
Array(Vec<JsonValue>),
|
Array(Vec<JsonValue>),
|
||||||
Object(HashMap<String, JsonValue>),
|
Object(Vec<(String, JsonValue)>),
|
||||||
}
|
}
|
||||||
|
|
||||||
trait JsonParser<'a, T>: Parser<&'a str, T, &'a str> {}
|
trait JsonParser<'a, T>: Parser<&'a str, T, &'a str> {}
|
||||||
@ -87,13 +86,17 @@ mod tests {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn json_string() -> impl JsonParser<'static, JsonValue> {
|
fn json_string_raw() -> impl JsonParser<'static, String> {
|
||||||
seq((
|
seq((
|
||||||
literal_char('"'),
|
literal_char('"'),
|
||||||
repeated(pred(any_char, |ch| *ch != '"')),
|
repeated(pred(any_char, |ch| *ch != '"')),
|
||||||
literal_char('"'),
|
literal_char('"'),
|
||||||
))
|
))
|
||||||
.map(|(_, s, _)| JsonValue::Str(s.iter().cloned().collect::<String>()))
|
.map(|(_, s, _)| s.iter().cloned().collect::<String>())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn json_string() -> impl JsonParser<'static, JsonValue> {
|
||||||
|
json_string_raw().map(JsonValue::Str)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn whitespace() -> impl JsonParser<'static, ()> {
|
fn whitespace() -> impl JsonParser<'static, ()> {
|
||||||
@ -112,7 +115,20 @@ mod tests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//fn json_object() -> impl JsonParser<'static, JsonValue> {}
|
fn json_object() -> impl JsonParser<'static, JsonValue> {
|
||||||
|
move |input| {
|
||||||
|
let kv = json_string_raw()
|
||||||
|
.surrounded_by(whitespace())
|
||||||
|
.then_ignore(literal_char(':'))
|
||||||
|
.then(json_value().surrounded_by(whitespace()));
|
||||||
|
|
||||||
|
repeated(kv)
|
||||||
|
.separated_by(literal_char(','), false)
|
||||||
|
.delimited(literal_char('{'), literal_char('}'))
|
||||||
|
.map(JsonValue::Object)
|
||||||
|
.parse(input)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn json_value() -> impl JsonParser<'static, JsonValue> {
|
fn json_value() -> impl JsonParser<'static, JsonValue> {
|
||||||
choice((
|
choice((
|
||||||
@ -121,6 +137,7 @@ mod tests {
|
|||||||
json_number(),
|
json_number(),
|
||||||
json_string(),
|
json_string(),
|
||||||
json_array(),
|
json_array(),
|
||||||
|
json_object(),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -168,17 +185,29 @@ mod tests {
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
json_array().parse(r#"[8,null,[],5]"#).unwrap(),
|
json_array().parse(r#"[8,null,[],5],{}"#).unwrap(),
|
||||||
(
|
(
|
||||||
JsonValue::Array(vec![
|
JsonValue::Array(vec![
|
||||||
JsonValue::Num(8.),
|
JsonValue::Num(8.),
|
||||||
JsonValue::Null,
|
JsonValue::Null,
|
||||||
JsonValue::Array(vec![]),
|
JsonValue::Array(vec![]),
|
||||||
JsonValue::Num(5.)
|
JsonValue::Num(5.),
|
||||||
]),
|
]),
|
||||||
""
|
",{}"
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
assert_eq!(json_value().parse("true"), Ok((JsonValue::Bool(true), "")));
|
assert_eq!(json_value().parse("true"), Ok((JsonValue::Bool(true), "")));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_json() {
|
||||||
|
assert_eq!(
|
||||||
|
json_object().parse(r#"{ "a": 23}"#).unwrap().0,
|
||||||
|
JsonValue::Object(vec![("a".into(), JsonValue::Num(23.))])
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
json_object().parse(r#"{}"#).unwrap().0,
|
||||||
|
JsonValue::Object(vec![])
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user