Added overloaded functions for default parseFlags

Can write d.Parse(...) instead of d.Parse<0>(...)
Hope to reduce strangeness and confusion for beginner.
This commit is contained in:
Milo Yip 2014-06-29 15:03:38 +08:00
parent 6f306755d5
commit 1d14748bc9
9 changed files with 34 additions and 16 deletions

View File

@ -22,7 +22,7 @@ int main(int, char*[]) {
Writer<FileWriteStream> writer(os); Writer<FileWriteStream> writer(os);
// JSON reader parse from the input stream and let writer generate the output. // JSON reader parse from the input stream and let writer generate the output.
if (!reader.Parse<0>(is, writer)) { if (!reader.Parse(is, writer)) {
fprintf(stderr, "\nError(%u): %s\n", (unsigned)reader.GetErrorOffset(), reader.GetParseError()); fprintf(stderr, "\nError(%u): %s\n", (unsigned)reader.GetErrorOffset(), reader.GetParseError());
return 1; return 1;
} }

View File

@ -12,7 +12,7 @@ int main() {
// 1. Parse a JSON string into DOM. // 1. Parse a JSON string into DOM.
const char* json = "{\"project\":\"rapidjson\",\"stars\":10}"; const char* json = "{\"project\":\"rapidjson\",\"stars\":10}";
Document d; Document d;
d.Parse<0>(json); d.Parse(json);
// 2. Modify it by DOM. // 2. Modify it by DOM.
Value& s = d["stars"]; Value& s = d["stars"];

View File

@ -19,14 +19,14 @@ int main(int, char*[]) {
#if 0 #if 0
// "normal" parsing, decode strings to new buffers. Can use other input stream via ParseStream(). // "normal" parsing, decode strings to new buffers. Can use other input stream via ParseStream().
if (document.Parse<0>(json).HasParseError()) if (document.Parse(json).HasParseError())
return 1; return 1;
#else #else
// In-situ parsing, decode strings directly in the source string. Source must be string. // In-situ parsing, decode strings directly in the source string. Source must be string.
{ {
char buffer[sizeof(json)]; char buffer[sizeof(json)];
memcpy(buffer, json, sizeof(json)); memcpy(buffer, json, sizeof(json));
if (document.ParseInsitu<0>(buffer).HasParseError()) if (document.ParseInsitu(buffer).HasParseError())
return 1; return 1;
} }
#endif #endif

View File

@ -818,6 +818,11 @@ public:
return ParseStream<parseFlags,Encoding,InputStream>(is); return ParseStream<parseFlags,Encoding,InputStream>(is);
} }
template <typename InputStream>
GenericDocument& ParseStream(InputStream& is) {
return ParseStream<0, Encoding, InputStream>(is);
}
//! Parse JSON text from a mutable string. //! Parse JSON text from a mutable string.
/*! \tparam parseFlags Combination of ParseFlag. /*! \tparam parseFlags Combination of ParseFlag.
\param str Mutable zero-terminated string to be parsed. \param str Mutable zero-terminated string to be parsed.
@ -834,6 +839,10 @@ public:
return ParseInsitu<parseFlags, Encoding>(str); return ParseInsitu<parseFlags, Encoding>(str);
} }
GenericDocument& ParseInsitu(Ch* str) {
return ParseInsitu<0, Encoding>(str);
}
//! Parse JSON text from a read-only string. //! Parse JSON text from a read-only string.
/*! \tparam parseFlags Combination of ParseFlag (must not contain kParseInsituFlag). /*! \tparam parseFlags Combination of ParseFlag (must not contain kParseInsituFlag).
\param str Read-only zero-terminated string to be parsed. \param str Read-only zero-terminated string to be parsed.
@ -850,6 +859,10 @@ public:
return Parse<parseFlags, Encoding>(str); return Parse<parseFlags, Encoding>(str);
} }
GenericDocument& Parse(const Ch* str) {
return Parse<0>(str);
}
//! Whether a parse error was occured in the last parsing. //! Whether a parse error was occured in the last parsing.
bool HasParseError() const { return parseError_ != 0; } bool HasParseError() const { return parseError_ != 0; }

View File

@ -257,6 +257,11 @@ public:
return !HasParseError(); return !HasParseError();
} }
template <typename InputStream, typename Handler>
bool Parse(InputStream& is, Handler& handler) {
return Parse<0>(is, handler);
}
bool HasParseError() const { return parseError_ != 0; } bool HasParseError() const { return parseError_ != 0; }
const char* GetParseError() const { return parseError_; } const char* GetParseError() const { return parseError_; }
size_t GetErrorOffset() const { return errorOffset_; } size_t GetErrorOffset() const { return errorOffset_; }

View File

@ -65,7 +65,7 @@ int main() {
// 1. Parse a JSON string into DOM. // 1. Parse a JSON string into DOM.
const char* json = "{\"project\":\"rapidjson\",\"stars\":10}"; const char* json = "{\"project\":\"rapidjson\",\"stars\":10}";
Document d; Document d;
d.Parse<0>(json); d.Parse(json);
// 2. Modify it by DOM. // 2. Modify it by DOM.
Value& s = d["stars"]; Value& s = d["stars"];

View File

@ -28,7 +28,7 @@ public:
temp_ = (char *)malloc(length_ + 1); temp_ = (char *)malloc(length_ + 1);
// Parse as a document // Parse as a document
EXPECT_FALSE(doc_.Parse<0>(json_).IsNull()); EXPECT_FALSE(doc_.Parse(json_).IsNull());
} }
virtual void TearDown() { virtual void TearDown() {
@ -66,7 +66,7 @@ TEST_F(RapidJson, SIMD_SUFFIX(ReaderParse_DummyHandler)) {
StringStream s(json_); StringStream s(json_);
BaseReaderHandler<> h; BaseReaderHandler<> h;
Reader reader; Reader reader;
EXPECT_TRUE(reader.Parse<0>(s, h)); EXPECT_TRUE(reader.Parse(s, h));
} }
} }
@ -88,7 +88,7 @@ TEST_F(RapidJson, SIMD_SUFFIX(DoucmentParseInsitu_MemoryPoolAllocator)) {
//MemoryPoolAllocator<> allocator(userBuffer, userBufferSize); //MemoryPoolAllocator<> allocator(userBuffer, userBufferSize);
//Document doc(&allocator); //Document doc(&allocator);
Document doc; Document doc;
doc.ParseInsitu<0>(temp_); doc.ParseInsitu(temp_);
ASSERT_TRUE(doc.IsObject()); ASSERT_TRUE(doc.IsObject());
//if (i == 0) { //if (i == 0) {
// size_t size = doc.GetAllocator().Size(); // size_t size = doc.GetAllocator().Size();
@ -110,7 +110,7 @@ TEST_F(RapidJson, SIMD_SUFFIX(DoucmentParse_MemoryPoolAllocator)) {
//MemoryPoolAllocator<> allocator(userBuffer, userBufferSize); //MemoryPoolAllocator<> allocator(userBuffer, userBufferSize);
//Document doc(&allocator); //Document doc(&allocator);
Document doc; Document doc;
doc.Parse<0>(json_); doc.Parse(json_);
ASSERT_TRUE(doc.IsObject()); ASSERT_TRUE(doc.IsObject());
//if (i == 0) { //if (i == 0) {
// size_t size = doc.GetAllocator().Size(); // size_t size = doc.GetAllocator().Size();
@ -128,7 +128,7 @@ TEST_F(RapidJson, SIMD_SUFFIX(DoucmentParse_CrtAllocator)) {
for (size_t i = 0; i < kTrialCount; i++) { for (size_t i = 0; i < kTrialCount; i++) {
memcpy(temp_, json_, length_ + 1); memcpy(temp_, json_, length_ + 1);
GenericDocument<UTF8<>, CrtAllocator> doc; GenericDocument<UTF8<>, CrtAllocator> doc;
doc.Parse<0>(temp_); doc.Parse(temp_);
ASSERT_TRUE(doc.IsObject()); ASSERT_TRUE(doc.IsObject());
} }
} }
@ -234,7 +234,7 @@ TEST_F(RapidJson, internal_Pow10) {
TEST_F(RapidJson, SIMD_SUFFIX(Whitespace)) { TEST_F(RapidJson, SIMD_SUFFIX(Whitespace)) {
for (size_t i = 0; i < kTrialCount; i++) { for (size_t i = 0; i < kTrialCount; i++) {
Document doc; Document doc;
ASSERT_TRUE(doc.Parse<0>(whitespace_).IsArray()); ASSERT_TRUE(doc.Parse(whitespace_).IsArray());
} }
} }
@ -279,7 +279,7 @@ TEST_F(RapidJson, SIMD_SUFFIX(ReaderParse_DummyHandler_FileReadStream)) {
FileReadStream s(fp, buffer, sizeof(buffer)); FileReadStream s(fp, buffer, sizeof(buffer));
BaseReaderHandler<> h; BaseReaderHandler<> h;
Reader reader; Reader reader;
reader.Parse<0>(s, h); reader.Parse(s, h);
fclose(fp); fclose(fp);
} }
} }

View File

@ -8,7 +8,7 @@ using namespace rapidjson;
TEST(Document, Parse) { TEST(Document, Parse) {
Document doc; Document doc;
doc.Parse<0>(" { \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3, 4] } "); doc.Parse(" { \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3, 4] } ");
EXPECT_TRUE(doc.IsObject()); EXPECT_TRUE(doc.IsObject());
@ -59,7 +59,7 @@ struct OutputStringStream : public std::ostringstream {
TEST(Document, AcceptWriter) { TEST(Document, AcceptWriter) {
Document doc; Document doc;
doc.Parse<0>(" { \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3, 4] } "); doc.Parse(" { \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3, 4] } ");
OutputStringStream os; OutputStringStream os;
Writer<OutputStringStream> writer(os); Writer<OutputStringStream> writer(os);

View File

@ -42,7 +42,7 @@ TEST(JsonChecker, Reader) {
} }
GenericDocument<UTF8<>, CrtAllocator> document; // Use Crt allocator to check exception-safety (no memory leak) GenericDocument<UTF8<>, CrtAllocator> document; // Use Crt allocator to check exception-safety (no memory leak)
if (!document.Parse<0>((const char*)json).HasParseError()) if (!document.Parse((const char*)json).HasParseError())
FAIL(); FAIL();
//printf("%s(%u):%s\n", filename, (unsigned)document.GetErrorOffset(), document.GetParseError()); //printf("%s(%u):%s\n", filename, (unsigned)document.GetErrorOffset(), document.GetParseError());
free(json); free(json);
@ -63,7 +63,7 @@ TEST(JsonChecker, Reader) {
} }
GenericDocument<UTF8<>, CrtAllocator> document; // Use Crt allocator to check exception-safety (no memory leak) GenericDocument<UTF8<>, CrtAllocator> document; // Use Crt allocator to check exception-safety (no memory leak)
document.Parse<0>((const char*)json); document.Parse((const char*)json);
EXPECT_TRUE(!document.HasParseError()); EXPECT_TRUE(!document.HasParseError());
free(json); free(json);
} }