Skip to content

Commit

Permalink
Add JsonValue::Null to json example
Browse files Browse the repository at this point in the history
  • Loading branch information
wuaoxiang authored and Geal committed Oct 23, 2021
1 parent 337c218 commit 4a2f9af
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion examples/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use std::str;

#[derive(Debug, PartialEq)]
pub enum JsonValue {
Null,
Str(String),
Boolean(bool),
Num(f64),
Expand Down Expand Up @@ -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`).
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -315,4 +325,6 @@ fn main() {
}
_ => {}
}

assert!(root::<(&str, ErrorKind)>("null").is_ok());
}

0 comments on commit 4a2f9af

Please sign in to comment.