Skip to content

Commit

Permalink
Permit empty arrays via OneOf. (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
jnaviask authored Oct 26, 2020
1 parent d01eaf2 commit bbabc6e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "lite-json"
version = "0.1.2"
version = "0.1.3"
authors = ["Bryan Chen <[email protected]>"]
description = "Simple JSON parser. Wasm / no_std ready."
license = "Apache-2.0"
Expand Down
17 changes: 13 additions & 4 deletions src/json_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,9 +329,14 @@ impl<I: Input> Parser<I> for Array {
) -> ResultOf<I, Self::Output> {
let context = &context.nest(input, current)?;
let (_, next) = <OpenSquareBracketChar as Parser<I>>::parse(input, current, context)?;
let (res, next) = <Elements as Parser<I>>::parse(input, next, context)?;
let (output, next) =
<OneOf<Elements, Whitespace> as Parser<I>>::parse(input, next, context)?;
let (_, next) = <CloseSquareBracketChar as Parser<I>>::parse(input, next, context)?;
Ok((res, next))
let output = match output {
Either::A(a) => a,
Either::B(_) => Vec::new(),
};
Ok((output, next))
}
}

Expand Down Expand Up @@ -375,7 +380,9 @@ mod tests {
#[test]
fn it_works() {
assert_eq!(
parse_json(&r#"{ "test": 1, "test2": [1e-4, 2.041e2, true, false, null, "\"1\n\""] }"#),
parse_json(
&r#"{ "test": 1, "test2": [1e-4, 2.041e2, true, false, null, "\"1\n\""], "test3": [], "test4": {} }"#
),
Ok(JsonValue::Object(vec![
(
vec!['t', 'e', 's', 't'],
Expand Down Expand Up @@ -406,7 +413,9 @@ mod tests {
JsonValue::Null,
JsonValue::String(vec!['\"', '1', 'n', '\"'])
])
)
),
(vec!['t', 'e', 's', 't', '3'], JsonValue::Array(vec![])),
(vec!['t', 'e', 's', 't', '4'], JsonValue::Object(vec![]))
]))
)
}
Expand Down

0 comments on commit bbabc6e

Please sign in to comment.