Commit 1d14748b authored by Milo Yip's avatar Milo Yip

Added overloaded functions for default parseFlags

Can write d.Parse(...) instead of d.Parse<0>(...)
Hope to reduce strangeness and confusion for beginner.
parent 6f306755
...@@ -22,7 +22,7 @@ int main(int, char*[]) { ...@@ -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;
} }
......
...@@ -12,7 +12,7 @@ int main() { ...@@ -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"];
......
...@@ -19,14 +19,14 @@ int main(int, char*[]) { ...@@ -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
......
...@@ -818,6 +818,11 @@ public: ...@@ -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: ...@@ -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: ...@@ -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; }
......
...@@ -257,6 +257,11 @@ public: ...@@ -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_; }
......
...@@ -65,7 +65,7 @@ int main() { ...@@ -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"];
......
...@@ -28,7 +28,7 @@ public: ...@@ -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)) { ...@@ -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)) { ...@@ -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)) { ...@@ -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)) { ...@@ -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) { ...@@ -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)) { ...@@ -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);
} }
} }
......
...@@ -8,7 +8,7 @@ using namespace rapidjson; ...@@ -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 { ...@@ -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);
......
...@@ -42,7 +42,7 @@ TEST(JsonChecker, Reader) { ...@@ -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) { ...@@ -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);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment