diff --git a/crates/nu-command/src/formats/to/nuon.rs b/crates/nu-command/src/formats/to/nuon.rs index 1d0ea435a8..b57f2f8cd7 100644 --- a/crates/nu-command/src/formats/to/nuon.rs +++ b/crates/nu-command/src/formats/to/nuon.rs @@ -173,12 +173,11 @@ fn value_to_string(v: &Value, span: Span) -> Result { fn value_to_string_without_quotes(v: &Value, span: Span) -> Result { match v { Value::String { val, .. } => Ok({ - let mut quoted = escape_quote_string(val); - if !needs_quotes(val) { - quoted.remove(0); - quoted.pop(); + if needs_quotes(val) { + escape_quote_string(val) + } else { + val.clone() } - quoted }), _ => value_to_string(v, span), } @@ -209,6 +208,7 @@ fn needs_quotes(string: &str) -> bool { || string.contains('\t') || string.contains('\n') || string.contains('\r') + || string.contains('\"') } #[cfg(test)] diff --git a/crates/nu-command/tests/format_conversions/nuon.rs b/crates/nu-command/tests/format_conversions/nuon.rs index 77f7b71517..94069acc2f 100644 --- a/crates/nu-command/tests/format_conversions/nuon.rs +++ b/crates/nu-command/tests/format_conversions/nuon.rs @@ -103,6 +103,51 @@ fn to_nuon_escaping2() { assert_eq!(actual.out, "hello\\world"); } +#[test] +fn to_nuon_escaping3() { + let actual = nu!( + cwd: "tests/fixtures/formats", pipeline( + r#" + ["hello\\world"] + | to nuon + | from nuon + | $in == [hello\world] + "# + )); + + assert_eq!(actual.out, "true"); +} + +#[test] +fn to_nuon_escaping4() { + let actual = nu!( + cwd: "tests/fixtures/formats", pipeline( + r#" + ["hello\"world"] + | to nuon + | from nuon + | $in == ["hello\"world"] + "# + )); + + assert_eq!(actual.out, "true"); +} + +#[test] +fn to_nuon_escaping5() { + let actual = nu!( + cwd: "tests/fixtures/formats", pipeline( + r#" + {s: "hello\"world"} + | to nuon + | from nuon + | $in == {s: "hello\"world"} + "# + )); + + assert_eq!(actual.out, "true"); +} + #[test] fn to_nuon_negative_int() { let actual = nu!(