nushell/src/commands/from_json.rs

151 lines
4.8 KiB
Rust
Raw Normal View History

use crate::commands::WholeStreamCommand;
use crate::data::{Primitive, TaggedDictBuilder, Value};
2019-05-28 06:00:00 +02:00
use crate::prelude::*;
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")
}
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>) -> Tagged<Value> {
let tag = tag.into();
2019-07-09 06:31:26 +02:00
2019-05-28 06:00:00 +02:00
match v {
2019-08-30 23:34:35 +02:00
serde_hjson::Value::Null => Value::Primitive(Primitive::Nothing).tagged(tag),
serde_hjson::Value::Bool(b) => Value::boolean(*b).tagged(tag),
serde_hjson::Value::F64(n) => Value::number(n).tagged(tag),
serde_hjson::Value::U64(n) => Value::number(n).tagged(tag),
serde_hjson::Value::I64(n) => Value::number(n).tagged(tag),
2019-07-09 06:31:26 +02:00
serde_hjson::Value::String(s) => {
Value::Primitive(Primitive::String(String::from(s))).tagged(tag)
2019-07-09 06:31:26 +02:00
}
serde_hjson::Value::Array(a) => Value::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
)
.tagged(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_tagged(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
2019-08-01 03:58:42 +02:00
collected.into_tagged_value()
2019-05-28 06:00:00 +02:00
}
}
}
2019-07-09 06:31:26 +02:00
pub fn from_json_string_to_value(
s: String,
tag: impl Into<Tag>,
2019-08-01 03:58:42 +02:00
) -> serde_hjson::Result<Tagged<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
2019-08-21 08:39:57 +02:00
let stream = async_stream_block! {
let values: Vec<Tagged<Value>> = input.values.collect().await;
let mut concat_string = String::new();
let mut latest_tag: Option<Tag> = None;
for value in values {
let value_tag = value.tag();
latest_tag = Some(value_tag);
match value.item {
2019-08-01 03:58:42 +02:00
Value::Primitive(Primitive::String(s)) => {
2019-08-21 08:39:57 +02:00
concat_string.push_str(&s);
concat_string.push_str("\n");
2019-08-01 03:58:42 +02:00
}
2019-08-21 08:39:57 +02:00
_ => yield Err(ShellError::labeled_error_with_secondary(
"Expected a string from pipeline",
"requires string input",
name_tag,
"value originates from here",
value_tag,
)),
2019-08-21 08:39:57 +02:00
2019-08-01 03:58:42 +02:00
}
2019-08-21 08:39:57 +02:00
}
2019-08-27 13:05:51 +02:00
if objects {
for json_str in concat_string.lines() {
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(_) => {
if let Some(last_tag) = latest_tag {
yield Err(ShellError::labeled_error_with_secondary(
"Could nnot parse as JSON",
"input cannot be parsed as JSON",
name_tag,
2019-08-27 13:05:51 +02:00
"value originates from here",
last_tag))
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, name_tag) {
2019-08-27 13:05:51 +02:00
Ok(x) =>
match x {
Tagged { item: Value::Table(list), .. } => {
2019-08-27 13:05:51 +02:00
for l in list {
yield ReturnSuccess::value(l);
}
}
x => yield ReturnSuccess::value(x),
}
Err(_) => {
if let Some(last_tag) = latest_tag {
yield Err(ShellError::labeled_error_with_secondary(
"Could not parse as JSON",
"input cannot be parsed as JSON",
name_tag,
2019-08-27 13:05:51 +02:00
"value originates from here",
last_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
}