2019-06-03 09:41:28 +02:00
|
|
|
use crate::object::base::OF64;
|
|
|
|
use crate::object::{DataDescriptor, Dictionary, Primitive, Value};
|
2019-05-28 06:00:00 +02:00
|
|
|
use crate::prelude::*;
|
|
|
|
|
2019-06-03 09:41:28 +02:00
|
|
|
fn convert_json_value_to_nu_value(v: &serde_hjson::Value) -> Value {
|
2019-05-28 06:00:00 +02:00
|
|
|
match v {
|
2019-06-03 09:41:28 +02:00
|
|
|
serde_hjson::Value::Null => Value::Primitive(Primitive::String("".to_string())),
|
|
|
|
serde_hjson::Value::Bool(b) => Value::Primitive(Primitive::Boolean(*b)),
|
|
|
|
serde_hjson::Value::F64(n) => Value::Primitive(Primitive::Float(OF64::from(*n))),
|
|
|
|
serde_hjson::Value::U64(n) => Value::Primitive(Primitive::Int(*n as i64)),
|
|
|
|
serde_hjson::Value::I64(n) => Value::Primitive(Primitive::Int(*n as i64)),
|
|
|
|
serde_hjson::Value::String(s) => Value::Primitive(Primitive::String(s.clone())),
|
|
|
|
serde_hjson::Value::Array(a) => Value::List(
|
|
|
|
a.iter()
|
|
|
|
.map(|x| convert_json_value_to_nu_value(x))
|
|
|
|
.collect(),
|
|
|
|
),
|
|
|
|
serde_hjson::Value::Object(o) => {
|
2019-05-28 06:00:00 +02:00
|
|
|
let mut collected = Dictionary::default();
|
|
|
|
for (k, v) in o.iter() {
|
2019-06-03 09:41:28 +02:00
|
|
|
collected.add(
|
|
|
|
DataDescriptor::from(k.clone()),
|
|
|
|
convert_json_value_to_nu_value(v),
|
|
|
|
);
|
2019-05-28 06:00:00 +02:00
|
|
|
}
|
|
|
|
Value::Object(collected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-01 21:20:48 +02:00
|
|
|
pub fn from_json_string_to_value(s: String) -> Value {
|
2019-06-03 09:41:28 +02:00
|
|
|
let v: serde_hjson::Value = serde_hjson::from_str(&s).unwrap();
|
2019-06-01 21:20:48 +02:00
|
|
|
convert_json_value_to_nu_value(&v)
|
|
|
|
}
|
|
|
|
|
2019-05-28 06:00:00 +02:00
|
|
|
pub fn from_json(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
|
|
|
let out = args.input;
|
|
|
|
Ok(out
|
|
|
|
.map(|a| match a {
|
|
|
|
Value::Primitive(Primitive::String(s)) => {
|
2019-06-01 21:20:48 +02:00
|
|
|
ReturnValue::Value(from_json_string_to_value(s))
|
2019-05-28 06:00:00 +02:00
|
|
|
}
|
|
|
|
_ => ReturnValue::Value(Value::Primitive(Primitive::String("".to_string()))),
|
|
|
|
})
|
|
|
|
.boxed())
|
|
|
|
}
|