nushell/src/commands/to_json.rs

18 lines
618 B
Rust
Raw Normal View History

2019-05-28 04:00:00 +00:00
use crate::object::{Primitive, Value};
use crate::prelude::*;
pub fn to_json(args: CommandArgs) -> Result<OutputStream, ShellError> {
let out = args.input;
2019-06-15 23:03:49 +00:00
let span = args.name_span;
2019-05-28 04:00:00 +00:00
Ok(out
2019-06-15 23:03:49 +00:00
.map(move |a| match serde_json::to_string(&a) {
Ok(x) => ReturnValue::Value(Value::Primitive(Primitive::String(x))),
Err(_) => ReturnValue::Value(Value::Error(Box::new(ShellError::maybe_labeled_error(
"Can not convert to JSON string",
"can not convert piped data to JSON string",
span,
)))),
})
2019-05-28 04:00:00 +00:00
.boxed())
}