add in a raw flag in the command to json (#555)

* add in the method to_string_raw

* add in a raw flag to json

* add in a test
This commit is contained in:
Michael Angerman 2021-12-22 11:56:49 -08:00 committed by GitHub
parent 061c822c5d
commit 5d3b63fa90
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 50 additions and 3 deletions

View File

@ -13,7 +13,9 @@ impl Command for ToJson {
} }
fn signature(&self) -> Signature { fn signature(&self) -> Signature {
Signature::build("to json").category(Category::Formats) Signature::build("to json")
.switch("raw", "remove all of the whitespace", Some('r'))
.category(Category::Formats)
} }
fn usage(&self) -> &str { fn usage(&self) -> &str {
@ -27,7 +29,12 @@ impl Command for ToJson {
call: &Call, call: &Call,
input: PipelineData, input: PipelineData,
) -> Result<nu_protocol::PipelineData, ShellError> { ) -> Result<nu_protocol::PipelineData, ShellError> {
to_json(call, input) let raw = call.has_flag("raw");
if raw {
to_json_raw(call, input)
} else {
to_json(call, input)
}
} }
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
@ -106,6 +113,25 @@ fn to_json(call: &Call, input: PipelineData) -> Result<PipelineData, ShellError>
} }
} }
fn to_json_raw(call: &Call, input: PipelineData) -> Result<PipelineData, ShellError> {
let span = call.head;
let value = input.into_value(span);
let json_value = value_to_json_value(&value)?;
match nu_json::to_string_raw(&json_value) {
Ok(serde_json_string) => Ok(Value::String {
val: serde_json_string,
span,
}
.into_pipeline_data()),
_ => Ok(Value::Error {
error: ShellError::CantConvert("JSON".into(), value.get_type().to_string(), span),
}
.into_pipeline_data()),
}
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;

View File

@ -2,7 +2,7 @@ pub use self::de::{
from_iter, from_reader, from_slice, from_str, Deserializer, StreamDeserializer, from_iter, from_reader, from_slice, from_str, Deserializer, StreamDeserializer,
}; };
pub use self::error::{Error, ErrorCode, Result}; pub use self::error::{Error, ErrorCode, Result};
pub use self::ser::{to_string, to_vec, to_writer, Serializer}; pub use self::ser::{to_string, to_string_raw, to_vec, to_writer, Serializer};
pub use self::value::{from_value, to_value, Map, Value}; pub use self::value::{from_value, to_value, Map, Value};
pub mod builder; pub mod builder;

View File

@ -1023,3 +1023,16 @@ where
let string = String::from_utf8(vec)?; let string = String::from_utf8(vec)?;
Ok(string) Ok(string)
} }
/// Encode the specified struct into a Hjson `String` buffer.
/// And remove all whitespace
#[inline]
pub fn to_string_raw<T>(value: &T) -> Result<String>
where
T: ser::Serialize,
{
let vec = to_vec(value)?;
let mut string = String::from_utf8(vec)?;
string.retain(|c| !c.is_whitespace());
Ok(string)
}

View File

@ -1324,3 +1324,11 @@ fn cjk_in_substrings() -> TestResult {
"title-page.md", "title-page.md",
) )
} }
#[test]
fn to_json_raw_flag() -> TestResult {
run_test(
"[[a b]; [jim susie] [3 4]] | to json -r",
r#"[{"a":"jim","b":"susie"},{"a":3,"b":4}]"#,
)
}