Skip to content

Commit

Permalink
Merge pull request #890 from StilesCrisis/issue-889
Browse files Browse the repository at this point in the history
Issue 889
  • Loading branch information
miloyip authored Mar 13, 2017
2 parents 5de06bf + 55f8a32 commit 266870d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 20 deletions.
24 changes: 4 additions & 20 deletions include/rapidjson/writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,7 @@ class Writer {
return EndValue(WriteString(str, length));
}

template <typename T>
RAPIDJSON_ENABLEIF_RETURN((internal::IsSame<Ch, T>), (bool)) String(const T* str, SizeType length, bool copy = false) {
bool String(const Ch* str, SizeType length, bool copy = false) {
RAPIDJSON_ASSERT(str != 0);
(void)copy;
Prefix(kStringType);
Expand All @@ -219,8 +218,7 @@ class Writer {
return WriteStartObject();
}

template <typename T>
RAPIDJSON_ENABLEIF_RETURN((internal::IsSame<Ch, T>), (bool)) Key(const T* str, SizeType length, bool copy = false) { return String(str, length, copy); }
bool Key(const Ch* str, SizeType length, bool copy = false) { return String(str, length, copy); }

bool EndObject(SizeType memberCount = 0) {
(void)memberCount;
Expand Down Expand Up @@ -250,23 +248,9 @@ class Writer {
//@{

//! Simpler but slower overload.
template <typename T>
RAPIDJSON_ENABLEIF_RETURN((internal::IsSame<Ch, T>), (bool)) String(const T* const& str) { return String(str, internal::StrLen(str)); }
template <typename T>
RAPIDJSON_ENABLEIF_RETURN((internal::IsSame<Ch, T>), (bool)) Key(const T* const& str) { return Key(str, internal::StrLen(str)); }
bool String(const Ch* const& str) { return String(str, internal::StrLen(str)); }
bool Key(const Ch* const& str) { return Key(str, internal::StrLen(str)); }

//! The compiler can give us the length of quoted strings for free.
template <typename T, size_t N>
RAPIDJSON_ENABLEIF_RETURN((internal::IsSame<Ch, T>), (bool)) String(const T (&str)[N]) {
RAPIDJSON_ASSERT(str[N-1] == '\0'); // you must pass in a null-terminated string (quoted constant strings are always null-terminated)
return String(str, N-1);
}
template <typename T, size_t N>
RAPIDJSON_ENABLEIF_RETURN((internal::IsSame<Ch, T>), (bool)) Key(const T (&str)[N]) {
RAPIDJSON_ASSERT(str[N-1] == '\0'); // you must pass in a null-terminated string (quoted constant strings are always null-terminated)
return Key(str, N-1);
}

//@}

//! Write a raw JSON value.
Expand Down
13 changes: 13 additions & 0 deletions test/unittest/writertest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,19 @@ TEST(Writer, String) {
#endif
}

TEST(Writer, Issue_889) {
char buf[100] = "Hello";

StringBuffer buffer;
Writer<StringBuffer> writer(buffer);
writer.StartArray();
writer.String(buf);
writer.EndArray();

EXPECT_STREQ("[\"Hello\"]", buffer.GetString());
EXPECT_TRUE(writer.IsComplete()); \
}

TEST(Writer, ScanWriteUnescapedString) {
const char json[] = "[\" \\\"0123456789ABCDEF\"]";
// ^ scanning stops here.
Expand Down

0 comments on commit 266870d

Please sign in to comment.