Add JsonValue::Null to json example
@@ -16,6 +16,7 @@ use std::str;
#[derive(Debug, PartialEq)]
pub enum JsonValue {
+ Null,
Str(String),
Boolean(bool),
Num(f64),
@@ -75,6 +76,10 @@ fn boolean<'a, E: ParseError<&'a str>>(input: &'a str) -> IResult<&'a str, bool,
alt((parse_true, parse_false))(input)
}
+fn null<'a, E: ParseError<&'a str>>(input: &'a str) -> IResult<&'a str, (), E> {
+ value((), tag("null"))(input)
+}
+
/// this parser combines the previous `parse_str` parser, that recognizes the
/// interior of a string, with a parse to recognize the double quote character,
/// before the string (using `preceded`) and after the string (using `terminated`).
@@ -159,6 +164,7 @@ fn json_value<'a, E: ParseError<&'a str> + ContextError<&'a str>>(
map(string, |s| JsonValue::Str(String::from(s))),
map(double, JsonValue::Num),
map(boolean, JsonValue::Boolean),
+ map(null, |_| JsonValue::Null),
)),
)(i)
}
@@ -169,7 +175,11 @@ fn root<'a, E: ParseError<&'a str> + ContextError<&'a str>>(
) -> IResult<&'a str, JsonValue, E> {
delimited(
sp,
- alt((map(hash, JsonValue::Object), map(array, JsonValue::Array))),
+ alt((
+ map(hash, JsonValue::Object),
+ map(array, JsonValue::Array),
+ map(null, |_| JsonValue::Null),
+ )),
opt(sp),
)(i)
}
@@ -315,4 +325,6 @@ fn main() {
}
_ => {}
}
+
+ assert!(root::<(&str, ErrorKind)>("null").is_ok());
}
