nushell/crates/nu-cli/src/commands/from_json.rs

134 lines
4.5 KiB
Rust
Raw Normal View History

use crate::commands::WholeStreamCommand;
2019-05-28 06:00:00 +02:00
use crate::prelude::*;
use nu_errors::ShellError;
use nu_protocol::{Primitive, ReturnSuccess, Signature, TaggedDictBuilder, UntaggedValue, Value};
2019-05-28 06:00:00 +02:00
pub struct FromJSON;
2019-08-27 13:05:51 +02:00
#[derive(Deserialize)]
pub struct FromJSONArgs {
objects: bool,
}
2019-08-27 13:05:51 +02:00
impl WholeStreamCommand for FromJSON {
fn name(&self) -> &str {
"from-json"
}
fn signature(&self) -> Signature {
Signature::build("from-json").switch(
"objects",
"treat each line as a separate value",
Some('o'),
)
}
fn usage(&self) -> &str {
"Parse text as .json and create table."
2019-08-27 13:05:51 +02:00
}
fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
args.process(registry, from_json)?.run()
}
}
fn convert_json_value_to_nu_value(v: &serde_hjson::Value, tag: impl Into<Tag>) -> Value {
let tag = tag.into();
2019-07-09 06:31:26 +02:00
2019-05-28 06:00:00 +02:00
match v {
serde_hjson::Value::Null => UntaggedValue::Primitive(Primitive::Nothing).into_value(&tag),
serde_hjson::Value::Bool(b) => UntaggedValue::boolean(*b).into_value(&tag),
serde_hjson::Value::F64(n) => UntaggedValue::decimal(*n).into_value(&tag),
serde_hjson::Value::U64(n) => UntaggedValue::int(*n).into_value(&tag),
serde_hjson::Value::I64(n) => UntaggedValue::int(*n).into_value(&tag),
2019-07-09 06:31:26 +02:00
serde_hjson::Value::String(s) => {
UntaggedValue::Primitive(Primitive::String(String::from(s))).into_value(&tag)
2019-07-09 06:31:26 +02:00
}
serde_hjson::Value::Array(a) => UntaggedValue::Table(
2019-06-03 09:41:28 +02:00
a.iter()
.map(|x| convert_json_value_to_nu_value(x, &tag))
2019-06-03 09:41:28 +02:00
.collect(),
2019-07-09 06:31:26 +02:00
)
.into_value(tag),
2019-06-03 09:41:28 +02:00
serde_hjson::Value::Object(o) => {
let mut collected = TaggedDictBuilder::new(&tag);
2019-05-28 06:00:00 +02:00
for (k, v) in o.iter() {
collected.insert_value(k.clone(), convert_json_value_to_nu_value(v, &tag));
2019-05-28 06:00:00 +02:00
}
2019-07-09 06:31:26 +02:00
collected.into_value()
2019-05-28 06:00:00 +02:00
}
}
}
pub fn from_json_string_to_value(s: String, tag: impl Into<Tag>) -> serde_hjson::Result<Value> {
2019-06-16 01:03:49 +02:00
let v: serde_hjson::Value = serde_hjson::from_str(&s)?;
Ok(convert_json_value_to_nu_value(&v, tag))
2019-06-01 21:20:48 +02:00
}
2019-08-27 13:05:51 +02:00
fn from_json(
FromJSONArgs { objects }: FromJSONArgs,
RunnableContext { input, name, .. }: RunnableContext,
) -> Result<OutputStream, ShellError> {
let name_tag = name;
2019-07-24 00:22:11 +02:00
let stream = async_stream! {
let concat_string = input.collect_string(name_tag.clone()).await?;
2019-08-27 13:05:51 +02:00
if objects {
for json_str in concat_string.item.lines() {
2019-08-27 13:05:51 +02:00
if json_str.is_empty() {
continue;
}
match from_json_string_to_value(json_str.to_string(), &name_tag) {
2019-08-27 13:05:51 +02:00
Ok(x) =>
yield ReturnSuccess::value(x),
Err(e) => {
let mut message = "Could not parse as JSON (".to_string();
message.push_str(&e.to_string());
message.push_str(")");
yield Err(ShellError::labeled_error_with_secondary(
message,
"input cannot be parsed as JSON",
&name_tag,
"value originates from here",
concat_string.tag.clone()))
2019-08-24 09:38:38 +02:00
}
}
2019-08-27 13:05:51 +02:00
}
} else {
match from_json_string_to_value(concat_string.item, name_tag.clone()) {
2019-08-27 13:05:51 +02:00
Ok(x) =>
match x {
Value { value: UntaggedValue::Table(list), .. } => {
2019-08-27 13:05:51 +02:00
for l in list {
yield ReturnSuccess::value(l);
}
}
x => yield ReturnSuccess::value(x),
}
Err(e) => {
let mut message = "Could not parse as JSON (".to_string();
message.push_str(&e.to_string());
message.push_str(")");
yield Err(ShellError::labeled_error_with_secondary(
message,
"input cannot be parsed as JSON",
name_tag,
"value originates from here",
concat_string.tag))
2019-08-27 13:05:51 +02:00
}
}
2019-08-21 08:39:57 +02:00
}
};
Ok(stream.to_output_stream())
2019-05-28 06:00:00 +02:00
}