From dd6fd6f1988f1883289aa5517fc752f53e3d5d3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Blanchon?= Date: Wed, 25 Jun 2014 13:47:28 +0200 Subject: [PATCH] Added a tests of a string with a double quote in it --- JsonGeneratorTests/JsonArrayTests.cpp | 7 +++++++ JsonGeneratorTests/JsonObjectBase.cpp | 2 +- JsonGeneratorTests/StringBuilder.cpp | 19 +++++++++++++++++++ JsonGeneratorTests/StringBuilder.h | 1 + 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/JsonGeneratorTests/JsonArrayTests.cpp b/JsonGeneratorTests/JsonArrayTests.cpp index 8b25593a..6d1c9cbf 100644 --- a/JsonGeneratorTests/JsonArrayTests.cpp +++ b/JsonGeneratorTests/JsonArrayTests.cpp @@ -30,6 +30,13 @@ namespace JsonGeneratorTests AssertJsonIs("[\"hello\"]"); } + TEST_METHOD(AddOneStringContainingDoubleQuote) + { + arr.add("\""); + + AssertJsonIs("[\"\\\"\"]"); + } + TEST_METHOD(AddTwoStrings) { arr.add("hello"); diff --git a/JsonGeneratorTests/JsonObjectBase.cpp b/JsonGeneratorTests/JsonObjectBase.cpp index ef72ef39..98a26dbc 100644 --- a/JsonGeneratorTests/JsonObjectBase.cpp +++ b/JsonGeneratorTests/JsonObjectBase.cpp @@ -6,7 +6,7 @@ void JsonObjectBase::writeObjectTo(ObjectContainer& obj, StringBuilder& sb) { case JSON_STRING: if (obj.value.string) - sb.append("\"%s\"", obj.value.string); + sb.appendEscaped(obj.value.string); else sb.append("null"); break; diff --git a/JsonGeneratorTests/StringBuilder.cpp b/JsonGeneratorTests/StringBuilder.cpp index a8ec22f6..9698ce32 100644 --- a/JsonGeneratorTests/StringBuilder.cpp +++ b/JsonGeneratorTests/StringBuilder.cpp @@ -11,4 +11,23 @@ void StringBuilder::append(const char* format, ...) va_end(args); length += strlen(tail); +} + +void StringBuilder::appendEscaped(const char* s) +{ + if (length > capacity - 3) return; + + buffer[length++] = '"'; + + while (*s && length