diff --git a/src/emitterutils.cpp b/src/emitterutils.cpp index 42c2a3988..93c2f9b1d 100644 --- a/src/emitterutils.cpp +++ b/src/emitterutils.cpp @@ -380,6 +380,8 @@ bool WriteChar(ostream_wrapper& out, char ch) { out << "\"\\n\""; } else if (ch == '\b') { out << "\"\\b\""; + } else if (ch == '\\') { + out << "\"\\\\\""; } else if ((0x20 <= ch && ch <= 0x7e) || ch == ' ') { out << "\"" << ch << "\""; } else { diff --git a/test/integration/emitter_test.cpp b/test/integration/emitter_test.cpp index e65ed85c6..6ce166772 100644 --- a/test/integration/emitter_test.cpp +++ b/test/integration/emitter_test.cpp @@ -970,6 +970,14 @@ TEST_F(EmitterTest, ValueOfDoubleQuote) { ExpectEmit("foo: \"\\\"\""); } +TEST_F(EmitterTest, ValueOfBackslash) { + out << YAML::BeginMap; + out << YAML::Key << "foo" << YAML::Value << '\\'; + out << YAML::EndMap; + + ExpectEmit("foo: \"\\\\\""); +} + class EmitterErrorTest : public ::testing::Test { protected: void ExpectEmitError(const std::string& expectedError) {