diff options
author | Ryan Leckey <ryan@launchbadge.com> | 2017-06-13 15:57:21 -0700 |
---|---|---|
committer | Ryan Leckey <ryan@launchbadge.com> | 2017-06-13 15:57:21 -0700 |
commit | 3c3d1e860eded0027f1efe3bb25e0fa6fa8b8940 (patch) | |
tree | dc27dc47ebf2537b3f21fe10ae23de2efe824b53 /src/file | |
parent | ab0d8cb9aa107c8d561f3c188e6cbf472a7df23b (diff) |
Add JSON
Diffstat (limited to 'src/file')
-rw-r--r-- | src/file/format/json.rs | 77 | ||||
-rw-r--r-- | src/file/format/mod.rs | 18 |
2 files changed, 86 insertions, 9 deletions
diff --git a/src/file/format/json.rs b/src/file/format/json.rs new file mode 100644 index 0000000..a3d80a7 --- /dev/null +++ b/src/file/format/json.rs @@ -0,0 +1,77 @@ +use serde_json; +use source::Source; +use std::collections::HashMap; +use std::error::Error; +use value::{Value, ValueKind}; + +pub fn parse(uri: Option<&String>, text: &str, namespace: Option<&String>) -> Result<HashMap<String, Value>, Box<Error>> { + // Parse a JSON object value from the text + let mut root: serde_json::Value = serde_json::from_str(text)?; + + // Limit to namespace + if let Some(namespace) = namespace { + root = serde_json::Value::Object(match root { + serde_json::Value::Object(ref mut table) => { + if let Some(serde_json::Value::Object(table)) = table.remove(namespace) { + table + } else { + serde_json::Map::new() + } + } + + _ => { + serde_json::Map::new() + } + }); + }; + + // TODO: Have a proper error fire if the root of a file is ever not a Table + let value = from_json_value(uri, &root); + match value.kind { + ValueKind::Table(map) => Ok(map), + + _ => Ok(HashMap::new()), + } +} + +fn from_json_value(uri: Option<&String>, value: &serde_json::Value) -> Value { + match *value { + serde_json::Value::String(ref value) => Value::new(uri, ValueKind::String(value.clone())), + + serde_json::Value::Number(ref value) => { + if let Some(value) = value.as_i64() { + Value::new(uri, ValueKind::Integer(value)) + } else if let Some(value) = value.as_f64() { + Value::new(uri, ValueKind::Float(value)) + } else { + unreachable!(); + } + } + + serde_json::Value::Bool(value) => Value::new(uri, ValueKind::Boolean(value)), + + serde_json::Value::Object(ref table) => { + let mut m = HashMap::new(); + + for (key, value) in table { + m.insert(key.clone(), from_json_value(uri, value)); + } + + Value::new(uri, ValueKind::Table(m)) + } + + serde_json::Value::Array(ref array) => { + let mut l = Vec::new(); + + for value in array { + l.push(from_json_value(uri, value)); + } + + Value::new(uri, ValueKind::Array(l)) + } + + serde_json::Value::Null => { + Value::new(uri, ValueKind::Nil) + } + } +} diff --git a/src/file/format/mod.rs b/src/file/format/mod.rs index 5c36815..c9b1012 100644 --- a/src/file/format/mod.rs +++ b/src/file/format/mod.rs @@ -6,8 +6,8 @@ use std::collections::HashMap; #[cfg(feature = "toml")] mod toml; -// #[cfg(feature = "json")] -// mod json; +#[cfg(feature = "json")] +mod json; // #[cfg(feature = "yaml")] // mod yaml; @@ -18,9 +18,9 @@ pub enum FileFormat { #[cfg(feature = "toml")] Toml, - // /// JSON (parsed with serde_json) - // #[cfg(feature = "json")] - // Json, + /// JSON (parsed with serde_json) + #[cfg(feature = "json")] + Json, // /// YAML (parsed with yaml_rust) // #[cfg(feature = "yaml")] @@ -35,8 +35,8 @@ impl FileFormat { #[cfg(feature = "toml")] FileFormat::Toml => vec!["toml"], - // #[cfg(feature = "json")] - // FileFormat::Json => vec!["json"], + #[cfg(feature = "json")] + FileFormat::Json => vec!["json"], // #[cfg(feature = "yaml")] // FileFormat::Yaml => vec!["yaml", "yml"], @@ -51,8 +51,8 @@ impl FileFormat { #[cfg(feature = "toml")] FileFormat::Toml => toml::parse(uri, text, namespace), - // #[cfg(feature = "json")] - // FileFormat::Json => json::Content::parse(text, namespace), + #[cfg(feature = "json")] + FileFormat::Json => json::parse(uri, text, namespace), // #[cfg(feature = "yaml")] // FileFormat::Yaml => yaml::Content::parse(text, namespace), |