to json -r not removing whitespaces fix (#11948)

fixes #11900  

# Description
Use `serde_json` instead.

# User-Facing Changes
The problem described in the issue now no longer persists.

No whitespace in the output of `to json --raw`
Output of unicode escape changed to consistent `\uffff`

# Tests + Formatting
I corrected all Tests that were affected by this change.
This commit is contained in:
dannou812 2024-03-20 22:14:31 +01:00 committed by GitHub
parent fdf7f28d07
commit 8237d15683
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 45 additions and 34 deletions

5
Cargo.lock generated
View file

@ -2994,6 +2994,7 @@ dependencies = [
"linked-hash-map",
"num-traits",
"serde",
"serde_json",
]
[[package]]
@ -4946,9 +4947,9 @@ dependencies = [
[[package]]
name = "serde_json"
version = "1.0.112"
version = "1.0.114"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4d1bd37ce2324cf3bf85e5a25f96eb4baf0d5aa6eba43e7ae8958870c4ec48ed"
checksum = "c5f09b1bd632ef549eaa9f60a1f8de742bdbc698e6cee2095fc84dde5f549ae0"
dependencies = [
"indexmap",
"itoa",

View file

@ -14,7 +14,7 @@ fn find_with_list_search_with_string() {
fn find_with_list_search_with_char() {
let actual = nu!("[moe larry curly] | find l | to json -r");
assert_eq!(actual.out, "[\"\u{1b}[37m\u{1b}[0m\u{1b}[41;37ml\u{1b}[0m\u{1b}[37marry\u{1b}[0m\",\"\u{1b}[37mcur\u{1b}[0m\u{1b}[41;37ml\u{1b}[0m\u{1b}[37my\u{1b}[0m\"]");
assert_eq!(actual.out, "[\"\\u001b[37m\\u001b[0m\\u001b[41;37ml\\u001b[0m\\u001b[37marry\\u001b[0m\",\"\\u001b[37mcur\\u001b[0m\\u001b[41;37ml\\u001b[0m\\u001b[37my\\u001b[0m\"]");
}
#[test]
@ -48,7 +48,7 @@ fn find_with_filepath_search_with_string() {
assert_eq!(
actual.out,
"[\"\u{1b}[37m\u{1b}[0m\u{1b}[41;37marep\u{1b}[0m\u{1b}[37mas.clu\u{1b}[0m\"]"
"[\"\\u001b[37m\\u001b[0m\\u001b[41;37marep\\u001b[0m\\u001b[37mas.clu\\u001b[0m\"]"
);
}
@ -57,7 +57,7 @@ fn find_with_filepath_search_with_multiple_patterns() {
let actual =
nu!(r#"["amigos.txt","arepas.clu","los.txt","tres.txt"] | find arep ami | to json -r"#);
assert_eq!(actual.out, "[\"\u{1b}[37m\u{1b}[0m\u{1b}[41;37mami\u{1b}[0m\u{1b}[37mgos.txt\u{1b}[0m\",\"\u{1b}[37m\u{1b}[0m\u{1b}[41;37marep\u{1b}[0m\u{1b}[37mas.clu\u{1b}[0m\"]");
assert_eq!(actual.out, "[\"\\u001b[37m\\u001b[0m\\u001b[41;37mami\\u001b[0m\\u001b[37mgos.txt\\u001b[0m\",\"\\u001b[37m\\u001b[0m\\u001b[41;37marep\\u001b[0m\\u001b[37mas.clu\\u001b[0m\"]");
}
#[test]

View file

@ -20,6 +20,7 @@ default = ["preserve_order"]
linked-hash-map = { version = "0.5", optional = true }
num-traits = "0.2"
serde = "1.0"
serde_json = "1.0.114"
[dev-dependencies]
# nu-path = { path="../nu-path", version = "0.91.1" }

View file

@ -1032,8 +1032,9 @@ pub fn to_string_raw<T>(value: &T) -> Result<String>
where
T: ser::Serialize,
{
let vec = to_vec(value)?;
let string = String::from_utf8(vec)?;
let output = string.lines().map(str::trim).collect();
Ok(output)
let result = serde_json::to_string(value);
match result {
Ok(result_string) => Ok(result_string),
Err(error) => Err(Error::Io(std::io::Error::from(error))),
}
}

View file

@ -45,3 +45,11 @@ fn to_json_escaped() -> TestResult {
r#"{"foo":{"bar":"[{\"a\":\"b\",\"c\": 2}]"}}"#,
)
}
#[test]
fn to_json_raw_backslash_in_quotes() -> TestResult {
run_test(
r#"{a: '\', b: 'some text'} | to json -r"#,
r#"{"a":"\\","b":"some text"}"#,
)
}