diff --git a/crates/nu-command/src/formats/from/json.rs b/crates/nu-command/src/formats/from/json.rs index e8b5d58e9e..d9d5e808d2 100644 --- a/crates/nu-command/src/formats/from/json.rs +++ b/crates/nu-command/src/formats/from/json.rs @@ -4,7 +4,7 @@ use std::{ }; use nu_engine::command_prelude::*; -use nu_protocol::ListStream; +use nu_protocol::{ListStream, PipelineMetadata}; #[derive(Clone)] pub struct FromJson; @@ -81,7 +81,7 @@ impl Command for FromJson { PipelineData::Value(Value::String { val, .. }, metadata) => { Ok(PipelineData::ListStream( read_json_lines(Cursor::new(val), span, strict, engine_state.ctrlc.clone()), - metadata, + update_metadata(metadata), )) } PipelineData::ByteStream(stream, metadata) @@ -90,7 +90,7 @@ impl Command for FromJson { if let Some(reader) = stream.reader() { Ok(PipelineData::ListStream( read_json_lines(reader, span, strict, None), - metadata, + update_metadata(metadata), )) } else { Ok(PipelineData::Empty) @@ -113,10 +113,10 @@ impl Command for FromJson { if strict { Ok(convert_string_to_value_strict(&string_input, span)? - .into_pipeline_data_with_metadata(metadata)) + .into_pipeline_data_with_metadata(update_metadata(metadata))) } else { Ok(convert_string_to_value(&string_input, span)? - .into_pipeline_data_with_metadata(metadata)) + .into_pipeline_data_with_metadata(update_metadata(metadata))) } } } @@ -263,6 +263,12 @@ fn convert_string_to_value_strict(string_input: &str, span: Span) -> Result) -> Option { + metadata + .map(|md| md.with_content_type(Some("application/json".into()))) + .or_else(|| Some(PipelineMetadata::default().with_content_type(Some("application/json".into())))) +} + #[cfg(test)] mod test { use super::*; diff --git a/crates/nu-protocol/src/pipeline/metadata.rs b/crates/nu-protocol/src/pipeline/metadata.rs index f5a4d5098e..1ed9222f46 100644 --- a/crates/nu-protocol/src/pipeline/metadata.rs +++ b/crates/nu-protocol/src/pipeline/metadata.rs @@ -7,6 +7,22 @@ pub struct PipelineMetadata { pub content_type: Option, } +impl PipelineMetadata { + pub fn with_data_source(self, data_source: DataSource) -> Self { + Self { + data_source, + ..self + } + } + + pub fn with_content_type(self, content_type: Option) -> Self { + Self { + content_type, + ..self + } + } +} + /// Describes where the particular [`PipelineMetadata`] originates. /// /// This can either be a particular family of commands (useful so downstream commands can adjust