Skip to content

Commit

Permalink
feat: add support for JSON5
Browse files Browse the repository at this point in the history
Should allow for easier copying and pasting
  • Loading branch information
hibachrach committed Mar 9, 2020
1 parent c881bb6 commit 1c987b9
Show file tree
Hide file tree
Showing 6 changed files with 247 additions and 18 deletions.
199 changes: 191 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ exitcode = "1.1.2"
serde_json = "1.0"
atty = "0.2"
itertools= "0.8.2"
json5 = "0.2.5"
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ optional.
Only key names are really relevant here. Note that entities other than objects
and empty objects are ignored.

Note that all [JSON5][json5] syntax is accepted. JSON5 is a superset of JSON
with support for different types of quotes, comments, etc., so you can much more
easily copy from an actual JavaScript environment. See the [JSON5
website][json5] for more details.

### JSON with properties (`-f jsonprop`)

```json
Expand Down Expand Up @@ -180,6 +185,11 @@ names are ignored:
}
```

Note that all [JSON5][json5] syntax is accepted. JSON5 is a superset of JSON
with support for different types of quotes, comments, etc., so you can much more
easily copy from an actual JavaScript environment. See the [JSON5
website][json5] for more details.

By default, this format looks for the properties `name` for what to print for
each item and `children` for what items are immediate descendants. To change
this, you can use the `--template` and `--children` options, respectively.
Expand Down Expand Up @@ -214,4 +224,5 @@ This project respects [semantic versioning][semver].
[lisp-parser-python]: https://norvig.com/lispy.html
[rustup-instructions]: https://rustup.rs/
[ruut-releases]: https://github.com/HarrisonB/ruut/releases
[json5]: https://json5.org/
[semver]: https://semver.org/
45 changes: 36 additions & 9 deletions src/json.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use super::{Error, Node};
use serde_json;
use json5;
use serde_json::Value as JsonValue;
use std::convert::From;

pub fn deserialize(serialized: String) -> Result<Node, Error> {
if serialized.trim().is_empty() {
return Err(Error::EmptyInputError);
}
let root_value: JsonValue = serde_json::from_str(&serialized)?;
let root_value: JsonValue = json5::from_str(&serialized)?;
match root_value {
JsonValue::Object(map) => {
if map.len() > 1 {
Expand Down Expand Up @@ -38,12 +37,6 @@ fn json_value_to_node(name: String, value: &JsonValue) -> Node {
}
}

impl From<serde_json::error::Error> for Error {
fn from(serde_error: serde_json::error::Error) -> Error {
Error::FormatSpecificError(format!("{}", serde_error))
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -134,4 +127,38 @@ mod tests {
}
);
}
#[test]
fn good_json5() {
let json = r#"
{
cool: {
// A comment
beans: {
man: null,
},
wow: null,
}
}
"#;
let root_node = deserialize(json.to_string()).unwrap();
assert_eq!(
root_node,
Node {
name: "cool".to_string(),
children: vec![
Node {
name: "beans".to_string(),
children: vec![Node {
name: "man".to_string(),
children: Vec::new()
}]
},
Node {
name: "wow".to_string(),
children: Vec::new()
}
]
}
);
}
}
Loading

0 comments on commit 1c987b9

Please sign in to comment.