mirror of
https://github.com/nushell/nushell.git
synced 2024-12-22 15:13:01 +01:00
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:
parent
061c822c5d
commit
5d3b63fa90
@ -13,7 +13,9 @@ impl Command for ToJson {
|
||||
}
|
||||
|
||||
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 {
|
||||
@ -27,7 +29,12 @@ impl Command for ToJson {
|
||||
call: &Call,
|
||||
input: PipelineData,
|
||||
) -> 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> {
|
||||
@ -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)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
@ -2,7 +2,7 @@ pub use self::de::{
|
||||
from_iter, from_reader, from_slice, from_str, Deserializer, StreamDeserializer,
|
||||
};
|
||||
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 mod builder;
|
||||
|
@ -1023,3 +1023,16 @@ where
|
||||
let string = String::from_utf8(vec)?;
|
||||
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)
|
||||
}
|
||||
|
@ -1324,3 +1324,11 @@ fn cjk_in_substrings() -> TestResult {
|
||||
"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}]"#,
|
||||
)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user