nushell/src/commands/to_json.rs

18 lines
618 B
Rust
Raw Normal View History

2019-05-28 06:00:00 +02:00
use crate::object::{Primitive, Value};
use crate::prelude::*;
pub fn to_json(args: CommandArgs) -> Result<OutputStream, ShellError> {
let out = args.input;
2019-06-16 01:03:49 +02:00
let span = args.name_span;
2019-05-28 06:00:00 +02:00
Ok(out
2019-06-16 01:03:49 +02: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 06:00:00 +02:00
.boxed())
}