diff --git a/crates/nu-command/src/formats/to/nuon.rs b/crates/nu-command/src/formats/to/nuon.rs index 65af8174e2..74b37175a2 100644 --- a/crates/nu-command/src/formats/to/nuon.rs +++ b/crates/nu-command/src/formats/to/nuon.rs @@ -137,10 +137,15 @@ fn value_to_string(v: &Value, span: Span) -> Result { } Ok(format!("{{{}}}", collection.join(", "))) } - Value::String { val, .. } => Ok(format!("\"{}\"", val)), + Value::String { val, .. } => Ok(format!("\"{}\"", escape(val))), } } +fn escape(input: &str) -> String { + let output = input.replace('\\', "\\\\"); + output.replace('"', "\\\"") +} + fn to_nuon(call: &Call, input: PipelineData) -> Result { let v = input.into_value(call.head); diff --git a/crates/nu-command/tests/format_conversions/nuon.rs b/crates/nu-command/tests/format_conversions/nuon.rs index 37dcce7a4c..d1fe9d23f1 100644 --- a/crates/nu-command/tests/format_conversions/nuon.rs +++ b/crates/nu-command/tests/format_conversions/nuon.rs @@ -74,6 +74,34 @@ fn to_nuon_bool() { assert_eq!(actual.out, "false"); } +#[test] +fn to_nuon_escaping() { + let actual = nu!( + cwd: "tests/fixtures/formats", pipeline( + r#" + "hello\"world" + | to nuon + | from nuon + "# + )); + + assert_eq!(actual.out, "hello\"world"); +} + +#[test] +fn to_nuon_escaping2() { + let actual = nu!( + cwd: "tests/fixtures/formats", pipeline( + r#" + "hello\\world" + | to nuon + | from nuon + "# + )); + + assert_eq!(actual.out, "hello\\world"); +} + #[test] fn to_nuon_negative_int() { let actual = nu!(