From 9fe18f71c1177050ce2d25c4fbe74cca8a1c34a4 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Mon, 15 Feb 2016 17:15:27 +0800 Subject: [PATCH] Add Document::Parse() overloads --- include/rapidjson/document.h | 37 ++++++++++++++++++++++ test/unittest/documenttest.cpp | 58 ++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h index b59cc69..d418a61 100644 --- a/include/rapidjson/document.h +++ b/include/rapidjson/document.h @@ -20,6 +20,8 @@ #include "reader.h" #include "internal/meta.h" #include "internal/strfunc.h" +#include "memorystream.h" +#include "encodedstream.h" #include // placement new #ifdef _MSC_VER @@ -2025,6 +2027,41 @@ public: GenericDocument& Parse(const Ch* str) { return Parse(str); } + + template + GenericDocument& Parse(const typename SourceEncoding::Ch* str, size_t length) { + RAPIDJSON_ASSERT(!(parseFlags & kParseInsituFlag)); + MemoryStream ms(static_cast(str), length * sizeof(typename SourceEncoding::Ch)); + EncodedInputStream is(ms); + ParseStream(is); + return *this; + } + + template + GenericDocument& Parse(const Ch* str, size_t length) { + return Parse(str, length); + } + + GenericDocument& Parse(const Ch* str, size_t length) { + return Parse(str, length); + } + +#if RAPIDJSON_HAS_STDSTRING + template + GenericDocument& Parse(const std::basic_string& str) { + return Parse(str.data(), str.size()); + } + + template + GenericDocument& Parse(const std::basic_string& str) { + return Parse(str); + } + + GenericDocument& Parse(const std::basic_string& str) { + return Parse(str); + } +#endif // RAPIDJSON_HAS_STDSTRING + //!@} //!@name Handling parse errors diff --git a/test/unittest/documenttest.cpp b/test/unittest/documenttest.cpp index 1b1c469..e6e312f 100644 --- a/test/unittest/documenttest.cpp +++ b/test/unittest/documenttest.cpp @@ -34,6 +34,8 @@ void ParseCheck(DocumentType& doc) { typedef typename DocumentType::ValueType ValueType; EXPECT_FALSE(doc.HasParseError()); + if (doc.HasParseError()) + printf("Error: %d at %zu\n", static_cast(doc.GetParseError()), doc.GetErrorOffset()); EXPECT_TRUE(static_cast(doc)); EXPECT_TRUE(doc.IsObject()); @@ -93,6 +95,26 @@ void ParseTest() { doc.ParseInsitu(buffer); ParseCheck(doc); free(buffer); + + // Parse(const Ch*, size_t) + size_t length = strlen(json); + buffer = reinterpret_cast(malloc(length * 2)); + memcpy(buffer, json, length); + memset(buffer + length, 'X', length); +#if RAPIDJSON_HAS_STDSTRING + std::string s2(buffer, length); // backup buffer +#endif + doc.SetNull(); + doc.Parse(buffer, length); + free(buffer); + ParseCheck(doc); + +#if RAPIDJSON_HAS_STDSTRING + // Parse(std::string) + doc.SetNull(); + doc.Parse(s2); + ParseCheck(doc); +#endif } TEST(Document, Parse) { @@ -140,6 +162,42 @@ static FILE* OpenEncodedFile(const char* filename) { return 0; } +TEST(Document, Parse_Encoding) { + const char* json = " { \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3, 4] } "; + + typedef GenericDocument > DocumentType; + DocumentType doc; + + // Parse(const SourceEncoding::Ch*) + // doc.Parse >(json); + // EXPECT_FALSE(doc.HasParseError()); + // EXPECT_EQ(0, StrCmp(doc[L"hello"].GetString(), L"world")); + + // Parse(const SourceEncoding::Ch*, size_t) + size_t length = strlen(json); + char* buffer = reinterpret_cast(malloc(length * 2)); + memcpy(buffer, json, length); + memset(buffer + length, 'X', length); +#if RAPIDJSON_HAS_STDSTRING + std::string s2(buffer, length); // backup buffer +#endif + doc.SetNull(); + doc.Parse >(buffer, length); + free(buffer); + EXPECT_FALSE(doc.HasParseError()); + if (doc.HasParseError()) + printf("Error: %d at %zu\n", static_cast(doc.GetParseError()), doc.GetErrorOffset()); + EXPECT_EQ(0, StrCmp(doc[L"hello"].GetString(), L"world")); + +#if RAPIDJSON_HAS_STDSTRING + // Parse(std::string) + doc.SetNull(); + doc.Parse >(s2); + EXPECT_FALSE(doc.HasParseError()); + EXPECT_EQ(0, StrCmp(doc[L"hello"].GetString(), L"world")); +#endif +} + TEST(Document, ParseStream_EncodedInputStream) { // UTF8 -> UTF16 FILE* fp = OpenEncodedFile("utf8.json");