From 01b2d463f745e6019ad32ea2e0e8e3d51def0d38 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Sat, 5 Mar 2016 10:34:00 +0800 Subject: [PATCH 001/305] Fix #573 --- include/rapidjson/document.h | 1 - test/unittest/valuetest.cpp | 4 ++++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h index d1f1b6f..855543e 100644 --- a/include/rapidjson/document.h +++ b/include/rapidjson/document.h @@ -1649,7 +1649,6 @@ public: /*! \note If the value is 64-bit integer type, it may lose precision. Use \c IsLosslessFloat() to check whether the converison is lossless. */ float GetFloat() const { - RAPIDJSON_ASSERT(IsFloat()); return static_cast(GetDouble()); } diff --git a/test/unittest/valuetest.cpp b/test/unittest/valuetest.cpp index 9d3609d..d6c7492 100644 --- a/test/unittest/valuetest.cpp +++ b/test/unittest/valuetest.cpp @@ -652,6 +652,10 @@ TEST(Value, Float) { z.SetFloat(12.34f); EXPECT_NEAR(12.34f, z.GetFloat(), 0.0f); + // Issue 573 + z.SetInt(0); + EXPECT_EQ(0.0f, z.GetFloat()); + z = 56.78f; EXPECT_NEAR(56.78f, z.GetFloat(), 0.0f); From 6e70e3521a8a615c5a01dd566a8443fa6b6de514 Mon Sep 17 00:00:00 2001 From: Sergey Kosarevsky Date: Sat, 5 Mar 2016 13:47:32 +0100 Subject: [PATCH 002/305] Removed commented code and added an explanatory comment instead --- include/rapidjson/reader.h | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/include/rapidjson/reader.h b/include/rapidjson/reader.h index a143c41..3d7bb63 100644 --- a/include/rapidjson/reader.h +++ b/include/rapidjson/reader.h @@ -1287,13 +1287,12 @@ private: bool cont = true; if (parseFlags & kParseNumbersAsStringsFlag) { - if (parseFlags & kParseInsituFlag) { s.Pop(); // Pop stack no matter if it will be used or not. typename InputStream::Ch* head = is.PutBegin(); const size_t length = s.Tell() - startOffset; RAPIDJSON_ASSERT(length <= 0xFFFFFFFF); -// *(head + length) = '\0'; + // unable to insert the \0 character here, it will erase the comma after this number const typename TargetEncoding::Ch* const str = reinterpret_cast(head); cont = handler.RawNumber(str, SizeType(length), false); } @@ -1308,7 +1307,6 @@ private: const SizeType length = static_cast(stackStream.Length()) - 1; cont = handler.RawNumber(str, SizeType(length), true); } - } else { size_t length = s.Length(); From d175915c5517685884a4ab5d2e8653b4616a27aa Mon Sep 17 00:00:00 2001 From: Chris Lundquist Date: Sat, 5 Mar 2016 18:14:20 -0800 Subject: [PATCH 003/305] [simplewriter.cpp] show generated output This makes it painfully obvious that writer expects subsequent key/value pairs. --- example/simplewriter/simplewriter.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/example/simplewriter/simplewriter.cpp b/example/simplewriter/simplewriter.cpp index f889150..70d18f4 100644 --- a/example/simplewriter/simplewriter.cpp +++ b/example/simplewriter/simplewriter.cpp @@ -9,12 +9,12 @@ int main() { StringBuffer s; Writer writer(s); - writer.StartObject(); - writer.String("hello"); - writer.String("world"); - writer.String("t"); - writer.Bool(true); - writer.String("f"); + writer.StartObject(); // writer expects subsequent key/value pairs. + writer.String("hello"); // key + writer.String("world"); // value + writer.String("t"); // key + writer.Bool(true); // value + writer.String("f"); // etc... writer.Bool(false); writer.String("n"); writer.Null(); @@ -29,6 +29,7 @@ int main() { writer.EndArray(); writer.EndObject(); + // {"hello":"world","t":true,"f":false,"n":null,"i":123,"pi":3.1416,"a":[0,1,2,3]} cout << s.GetString() << endl; return 0; From 7886965e344b29e32da789e284658fd066ad5e4e Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Tue, 8 Mar 2016 10:03:31 +0800 Subject: [PATCH 004/305] Fix a bug in dtoa This previously affects Writer:: SetMaxDecimalPlaces() --- include/rapidjson/internal/dtoa.h | 2 +- test/unittest/dtoatest.cpp | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/include/rapidjson/internal/dtoa.h b/include/rapidjson/internal/dtoa.h index d458284..bc45496 100644 --- a/include/rapidjson/internal/dtoa.h +++ b/include/rapidjson/internal/dtoa.h @@ -180,7 +180,7 @@ inline char* Prettify(char* buffer, int length, int k, int maxDecimalPlaces) { buffer[1] = '.'; for (int i = 2; i < offset; i++) buffer[i] = '0'; - if (length + offset > maxDecimalPlaces) { + if (length - kk > maxDecimalPlaces) { // When maxDecimalPlaces = 2, 0.123 -> 0.12, 0.102 -> 0.1 // Remove extra trailing zeros (at least one) after truncation. for (int i = maxDecimalPlaces + 1; i > 2; i--) diff --git a/test/unittest/dtoatest.cpp b/test/unittest/dtoatest.cpp index da02095..fe28271 100644 --- a/test/unittest/dtoatest.cpp +++ b/test/unittest/dtoatest.cpp @@ -81,6 +81,12 @@ TEST(dtoa, maxDecimalPlaces) { TEST_DTOA(3, 2.225073858507201e-308, "0.0"); // Max subnormal positive double TEST_DTOA(3, 2.2250738585072014e-308, "0.0"); // Min normal positive double TEST_DTOA(3, 1.7976931348623157e308, "1.7976931348623157e308"); // Max double + TEST_DTOA(5, -0.14000000000000001, "-0.14"); + TEST_DTOA(4, -0.14000000000000001, "-0.14"); + TEST_DTOA(3, -0.14000000000000001, "-0.14"); + TEST_DTOA(3, -0.10000000000000001, "-0.1"); + TEST_DTOA(2, -0.10000000000000001, "-0.1"); + TEST_DTOA(1, -0.10000000000000001, "-0.1"); #undef TEST_DTOA } From 1623ef2a96be9f5b6bcd638ad8ac815428b22e57 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Tue, 8 Mar 2016 14:35:10 +0800 Subject: [PATCH 005/305] Update simplewriter example with Writer::Key() --- example/simplewriter/simplewriter.cpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/example/simplewriter/simplewriter.cpp b/example/simplewriter/simplewriter.cpp index 70d18f4..8d1275c 100644 --- a/example/simplewriter/simplewriter.cpp +++ b/example/simplewriter/simplewriter.cpp @@ -9,23 +9,23 @@ int main() { StringBuffer s; Writer writer(s); - writer.StartObject(); // writer expects subsequent key/value pairs. - writer.String("hello"); // key - writer.String("world"); // value - writer.String("t"); // key - writer.Bool(true); // value - writer.String("f"); // etc... + writer.StartObject(); // Between StartObject()/EndObject(), + writer.Key("hello"); // output a key, + writer.String("world"); // follow by a value. + writer.Key("t"); + writer.Bool(true); + writer.Key("f"); writer.Bool(false); - writer.String("n"); + writer.Key("n"); writer.Null(); - writer.String("i"); + writer.Key("i"); writer.Uint(123); - writer.String("pi"); + writer.Key("pi"); writer.Double(3.1416); - writer.String("a"); - writer.StartArray(); + writer.Key("a"); + writer.StartArray(); // Between StartArray()/EndArray(), for (unsigned i = 0; i < 4; i++) - writer.Uint(i); + writer.Uint(i); // all values are elements of the array. writer.EndArray(); writer.EndObject(); From 7a79e91ecd67a5e17fb2fc12a5d19ec70f457f63 Mon Sep 17 00:00:00 2001 From: Cory Omand Date: Tue, 8 Mar 2016 15:33:04 -0800 Subject: [PATCH 006/305] PrettyWriter formatting options. This change adds PrettyWriter::SetFormatOptions with a corresponding bitfield enum PrettyFormatOptions. This allows options affecting the format of the PrettyWriter to be set. The first option to be provided is kFormatSingleLineArray, which instructs the PrettyWriter to write arrays on a single line, rather than breaking them up onto a line per element. --- include/rapidjson/prettywriter.h | 30 +++++++++++++++++++++++++----- test/unittest/prettywritertest.cpp | 23 +++++++++++++++++++++++ 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/include/rapidjson/prettywriter.h b/include/rapidjson/prettywriter.h index 5ec4ccc..75dc474 100644 --- a/include/rapidjson/prettywriter.h +++ b/include/rapidjson/prettywriter.h @@ -24,6 +24,14 @@ RAPIDJSON_DIAG_OFF(effc++) RAPIDJSON_NAMESPACE_BEGIN +//! Combination of PrettyWriter format flags. +/*! \see PrettyWriter::SetFormatOptions + */ +enum PrettyFormatOptions { + kFormatDefault = 0, //!< Default pretty formatting. + kFormatSingleLineArray = 1 //!< Format arrays on a single line. +}; + //! Writer with indentation and spacing. /*! \tparam OutputStream Type of ouptut os. @@ -43,7 +51,7 @@ public: \param levelDepth Initial capacity of stack. */ explicit PrettyWriter(OutputStream& os, StackAllocator* allocator = 0, size_t levelDepth = Base::kDefaultLevelDepth) : - Base(os, allocator, levelDepth), indentChar_(' '), indentCharCount_(4) {} + Base(os, allocator, levelDepth), indentChar_(' '), indentCharCount_(4), formatOptions_(kFormatDefault) {} explicit PrettyWriter(StackAllocator* allocator = 0, size_t levelDepth = Base::kDefaultLevelDepth) : @@ -61,6 +69,14 @@ public: return *this; } + //! Set pretty writer formatting options. + /*! \param options Formatting options. + */ + PrettyWriter& SetFormatOptions(PrettyFormatOptions options) { + formatOptions_ = options; + return *this; + } + /*! @name Implementation of Handler \see Handler */ @@ -130,7 +146,7 @@ public: RAPIDJSON_ASSERT(Base::level_stack_.template Top()->inArray); bool empty = Base::level_stack_.template Pop(1)->valueCount == 0; - if (!empty) { + if (!empty && !(formatOptions_ & kFormatSingleLineArray)) { Base::os_->Put('\n'); WriteIndent(); } @@ -173,11 +189,14 @@ protected: if (level->inArray) { if (level->valueCount > 0) { Base::os_->Put(','); // add comma if it is not the first element in array - Base::os_->Put('\n'); + if (formatOptions_ & kFormatSingleLineArray) + Base::os_->Put(' '); } - else + + if (!(formatOptions_ & kFormatSingleLineArray)) { Base::os_->Put('\n'); - WriteIndent(); + WriteIndent(); + } } else { // in object if (level->valueCount > 0) { @@ -213,6 +232,7 @@ protected: Ch indentChar_; unsigned indentCharCount_; + PrettyFormatOptions formatOptions_; private: // Prohibit copy constructor & assignment operator. diff --git a/test/unittest/prettywritertest.cpp b/test/unittest/prettywritertest.cpp index e05d710..a372f79 100644 --- a/test/unittest/prettywritertest.cpp +++ b/test/unittest/prettywritertest.cpp @@ -39,6 +39,19 @@ static const char kPrettyJson[] = " \"i64\": -1234567890123456789\n" "}"; +static const char kPrettyJson_FormatOptions_SLA[] = +"{\n" +" \"hello\": \"world\",\n" +" \"t\": true,\n" +" \"f\": false,\n" +" \"n\": null,\n" +" \"i\": 123,\n" +" \"pi\": 3.1416,\n" +" \"a\": [1, 2, 3, -1],\n" +" \"u64\": 1234567890123456789,\n" +" \"i64\": -1234567890123456789\n" +"}"; + TEST(PrettyWriter, Basic) { StringBuffer buffer; PrettyWriter writer(buffer); @@ -48,6 +61,16 @@ TEST(PrettyWriter, Basic) { EXPECT_STREQ(kPrettyJson, buffer.GetString()); } +TEST(PrettyWriter, FormatOptions) { + StringBuffer buffer; + PrettyWriter writer(buffer); + writer.SetFormatOptions(kFormatSingleLineArray); + Reader reader; + StringStream s(kJson); + reader.Parse(s, writer); + EXPECT_STREQ(kPrettyJson_FormatOptions_SLA, buffer.GetString()); +} + TEST(PrettyWriter, SetIndent) { StringBuffer buffer; PrettyWriter writer(buffer); From 305882489c1d59de91b4abf311a3324abbcad972 Mon Sep 17 00:00:00 2001 From: Konstantin Trushin Date: Sun, 13 Mar 2016 14:07:39 +0300 Subject: [PATCH 007/305] do potentially precision-losing conversions explicitly --- include/rapidjson/pointer.h | 8 ++++---- test/unittest/itoatest.cpp | 4 ++-- test/unittest/strtodtest.cpp | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/include/rapidjson/pointer.h b/include/rapidjson/pointer.h index eddeab4..9444938 100644 --- a/include/rapidjson/pointer.h +++ b/include/rapidjson/pointer.h @@ -987,11 +987,11 @@ private: src_++; Ch c = 0; for (int j = 0; j < 2; j++) { - c <<= 4; + c = static_cast(c << 4); Ch h = *src_; - if (h >= '0' && h <= '9') c += h - '0'; - else if (h >= 'A' && h <= 'F') c += h - 'A' + 10; - else if (h >= 'a' && h <= 'f') c += h - 'a' + 10; + if (h >= '0' && h <= '9') c = static_cast(c + h - '0'); + else if (h >= 'A' && h <= 'F') c = static_cast(c + h - 'A' + 10); + else if (h >= 'a' && h <= 'f') c = static_cast(c + h - 'a' + 10); else { valid_ = false; return 0; diff --git a/test/unittest/itoatest.cpp b/test/unittest/itoatest.cpp index 9c3107d..79db1c7 100644 --- a/test/unittest/itoatest.cpp +++ b/test/unittest/itoatest.cpp @@ -93,7 +93,7 @@ static void u32toa_naive(uint32_t value, char* buffer) { char temp[10]; char *p = temp; do { - *p++ = char(value % 10) + '0'; + *p++ = static_cast(char(value % 10) + '0'); value /= 10; } while (value > 0); @@ -117,7 +117,7 @@ static void u64toa_naive(uint64_t value, char* buffer) { char temp[20]; char *p = temp; do { - *p++ = char(value % 10) + '0'; + *p++ = static_cast(char(value % 10) + '0'); value /= 10; } while (value > 0); diff --git a/test/unittest/strtodtest.cpp b/test/unittest/strtodtest.cpp index a42d96e..cde836c 100644 --- a/test/unittest/strtodtest.cpp +++ b/test/unittest/strtodtest.cpp @@ -42,7 +42,7 @@ TEST(Strtod, CheckApproximationCase) { u.u = 0x465a72e467d88 | ((static_cast(-149 + kExponentBias)) << kSignificandSize); const double b = u.d; const uint64_t bInt = (u.u & kSignificandMask) | kHiddenBit; - const int bExp = ((u.u & kExponentMask) >> kSignificandSize) - kExponentBias - kSignificandSize; + const int bExp = static_cast(((u.u & kExponentMask) >> kSignificandSize) - kExponentBias - kSignificandSize); EXPECT_DOUBLE_EQ(1.7864e-45, b); EXPECT_EQ(RAPIDJSON_UINT64_C2(0x001465a7, 0x2e467d88), bInt); EXPECT_EQ(-201, bExp); From 3e21bb429d492206c9ce2f3fd44264a5220913c4 Mon Sep 17 00:00:00 2001 From: Nicholas Fraser Date: Sun, 20 Mar 2016 01:10:33 -0400 Subject: [PATCH 008/305] Added optional support for trailing commas This adds kParseTrailingCommasFlag to allow a trailing comma at the end of maps and arrays. This is part of issue #36, adding optional support for relaxed JSON syntax. --- doc/dom.md | 1 + include/rapidjson/reader.h | 19 ++++++++++++++++++ test/unittest/readertest.cpp | 37 ++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+) diff --git a/doc/dom.md b/doc/dom.md index cb25fc4..79b6817 100644 --- a/doc/dom.md +++ b/doc/dom.md @@ -116,6 +116,7 @@ Parse flags | Meaning `kParseStopWhenDoneFlag` | After parsing a complete JSON root from stream, stop further processing the rest of stream. When this flag is used, parser will not generate `kParseErrorDocumentRootNotSingular` error. Using this flag for parsing multiple JSONs in the same stream. `kParseFullPrecisionFlag` | Parse number in full precision (slower). If this flag is not set, the normal precision (faster) is used. Normal precision has maximum 3 [ULP](http://en.wikipedia.org/wiki/Unit_in_the_last_place) error. `kParseCommentsFlag` | Allow one-line `// ...` and multi-line `/* ... */` comments (relaxed JSON syntax). +`kParseTrailingCommasFlag` | Allow trailing commas at the end of objects and arrays (relaxed JSON syntax). By using a non-type template parameter, instead of a function parameter, C++ compiler can generate code which is optimized for specified combinations, improving speed, and reducing code size (if only using a single specialization). The downside is the flags needed to be determined in compile-time. diff --git a/include/rapidjson/reader.h b/include/rapidjson/reader.h index 3d7bb63..4f09018 100644 --- a/include/rapidjson/reader.h +++ b/include/rapidjson/reader.h @@ -149,6 +149,7 @@ enum ParseFlag { kParseFullPrecisionFlag = 16, //!< Parse number in full precision (but slower). kParseCommentsFlag = 32, //!< Allow one-line (//) and multi-line (/**/) comments. kParseNumbersAsStringsFlag = 64, //!< Parse all numbers (ints/doubles) as strings. + kParseTrailingCommasFlag = 128, //!< Allow trailing commas at the end of objects and arrays. kParseDefaultFlags = RAPIDJSON_PARSE_DEFAULT_FLAGS //!< Default parse flags. Can be customized by defining RAPIDJSON_PARSE_DEFAULT_FLAGS }; @@ -636,6 +637,15 @@ private: RAPIDJSON_PARSE_ERROR(kParseErrorObjectMissCommaOrCurlyBracket, is.Tell()); break; } + + if (parseFlags & kParseTrailingCommasFlag) { + if (is.Peek() == '}') { + is.Take(); + if (RAPIDJSON_UNLIKELY(!handler.EndObject(memberCount))) + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); + return; + } + } } } @@ -676,6 +686,15 @@ private: } else RAPIDJSON_PARSE_ERROR(kParseErrorArrayMissCommaOrSquareBracket, is.Tell()); + + if (parseFlags & kParseTrailingCommasFlag) { + if (is.Peek() == ']') { + is.Take(); + if (RAPIDJSON_UNLIKELY(!handler.EndArray(elementCount))) + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); + return; + } + } } } diff --git a/test/unittest/readertest.cpp b/test/unittest/readertest.cpp index 32af8a8..df3b403 100644 --- a/test/unittest/readertest.cpp +++ b/test/unittest/readertest.cpp @@ -778,6 +778,10 @@ TEST(Reader, ParseArray_Error) { TEST_ARRAY_ERROR(kParseErrorArrayMissCommaOrSquareBracket, "[1}", 2); TEST_ARRAY_ERROR(kParseErrorArrayMissCommaOrSquareBracket, "[1 2]", 3); + // Array cannot have a trailing comma (without kParseTrailingCommasFlag); + // a value must follow a comma + TEST_ARRAY_ERROR(kParseErrorValueInvalid, "[1,]", 3); + #undef TEST_ARRAY_ERROR } @@ -978,6 +982,10 @@ TEST(Reader, ParseObject_Error) { // Must be a comma or '}' after an object member TEST_ERROR(kParseErrorObjectMissCommaOrCurlyBracket, "{\"a\":1]", 6); + // Object cannot have a trailing comma (without kParseTrailingCommasFlag); + // an object member name must follow a comma + TEST_ERROR(kParseErrorObjectMissName, "{\"a\":1,}", 7); + // This tests that MemoryStream is checking the length in Peek(). { MemoryStream ms("{\"a\"", 1); @@ -1552,6 +1560,35 @@ TEST(Reader, NumbersAsStrings) { } } +TEST(Reader, TrailingCommas) { + { + // trailing array comma + StringStream s("[1,2,3,]"); + ParseArrayHandler<3> h; + Reader reader; + EXPECT_TRUE(reader.Parse(s, h)); + EXPECT_EQ(5u, h.step_); + } + { + // trailing object comma + const char* json = "{ \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3],}"; + StringStream s(json); + ParseObjectHandler h; + Reader reader; + EXPECT_TRUE(reader.Parse(s, h)); + EXPECT_EQ(20u, h.step_); + } + { + // trailing object and array commas with whitespace + const char* json = "{ \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3\n,\n]\n,\n } "; + StringStream s(json); + ParseObjectHandler h; + Reader reader; + EXPECT_TRUE(reader.Parse(s, h)); + EXPECT_EQ(20u, h.step_); + } +} + #ifdef __GNUC__ RAPIDJSON_DIAG_POP #endif From 7c0e9d941d657c0e155b4018dc2a2867d2a1ff6e Mon Sep 17 00:00:00 2001 From: Nicholas Fraser Date: Sun, 20 Mar 2016 11:39:00 -0400 Subject: [PATCH 009/305] Added additional tests for trailing commas --- test/unittest/readertest.cpp | 88 ++++++++++++++++++++++++++++++++++-- 1 file changed, 83 insertions(+), 5 deletions(-) diff --git a/test/unittest/readertest.cpp b/test/unittest/readertest.cpp index df3b403..7c72f68 100644 --- a/test/unittest/readertest.cpp +++ b/test/unittest/readertest.cpp @@ -1562,7 +1562,6 @@ TEST(Reader, NumbersAsStrings) { TEST(Reader, TrailingCommas) { { - // trailing array comma StringStream s("[1,2,3,]"); ParseArrayHandler<3> h; Reader reader; @@ -1570,8 +1569,8 @@ TEST(Reader, TrailingCommas) { EXPECT_EQ(5u, h.step_); } { - // trailing object comma - const char* json = "{ \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3],}"; + const char* json = "{ \"hello\" : \"world\", \"t\" : true , \"f\" : false," + "\"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3],}"; StringStream s(json); ParseObjectHandler h; Reader reader; @@ -1579,14 +1578,93 @@ TEST(Reader, TrailingCommas) { EXPECT_EQ(20u, h.step_); } { - // trailing object and array commas with whitespace - const char* json = "{ \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3\n,\n]\n,\n } "; + // whitespace around trailing commas + const char* json = "{ \"hello\" : \"world\", \"t\" : true , \"f\" : false," + "\"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3\n,\n]\n,\n} "; StringStream s(json); ParseObjectHandler h; Reader reader; EXPECT_TRUE(reader.Parse(s, h)); EXPECT_EQ(20u, h.step_); } + { + // comments around trailing commas + const char* json = "{ \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null," + "\"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3/*test*/,/*test*/]/*test*/,/*test*/}"; + StringStream s(json); + ParseObjectHandler h; + Reader reader; + EXPECT_TRUE(reader.Parse(s, h)); + EXPECT_EQ(20u, h.step_); + } +} + +TEST(Reader, MultipleTrailingCommaErrors) { + // only a single trailing comma is allowed. + { + StringStream s("[1,2,3,,]"); + ParseArrayHandler<3> h; + Reader reader; + ParseResult r = reader.Parse(s, h); + EXPECT_TRUE(reader.HasParseError()); + EXPECT_EQ(kParseErrorValueInvalid, r.Code()); + EXPECT_EQ(7u, r.Offset()); + } + { + const char* json = "{ \"hello\" : \"world\", \"t\" : true , \"f\" : false," + "\"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3,],,}"; + StringStream s(json); + ParseObjectHandler h; + Reader reader; + ParseResult r = reader.Parse(s, h); + EXPECT_TRUE(reader.HasParseError()); + EXPECT_EQ(kParseErrorObjectMissName, r.Code()); + EXPECT_EQ(95u, r.Offset()); + } +} + +TEST(Reader, EmptyExceptForCommaErrors) { + // not allowed even with trailing commas enabled; the + // trailing comma must follow a value. + { + StringStream s("[,]"); + ParseArrayHandler<3> h; + Reader reader; + ParseResult r = reader.Parse(s, h); + EXPECT_TRUE(reader.HasParseError()); + EXPECT_EQ(kParseErrorValueInvalid, r.Code()); + EXPECT_EQ(1u, r.Offset()); + } + { + StringStream s("{,}"); + ParseObjectHandler h; + Reader reader; + ParseResult r = reader.Parse(s, h); + EXPECT_TRUE(reader.HasParseError()); + EXPECT_EQ(kParseErrorObjectMissName, r.Code()); + EXPECT_EQ(1u, r.Offset()); + } +} + +TEST(Reader, TrailingCommaHandlerTermination) { + { + HandlerTerminateAtEndArray h; + Reader reader; + StringStream s("[1,2,3,]"); + ParseResult r = reader.Parse(s, h); + EXPECT_TRUE(reader.HasParseError()); + EXPECT_EQ(kParseErrorTermination, r.Code()); + EXPECT_EQ(8u, r.Offset()); + } + { + HandlerTerminateAtEndObject h; + Reader reader; + StringStream s("{\"t\": true, \"f\": false,}"); + ParseResult r = reader.Parse(s, h); + EXPECT_TRUE(reader.HasParseError()); + EXPECT_EQ(kParseErrorTermination, r.Code()); + EXPECT_EQ(24u, r.Offset()); + } } #ifdef __GNUC__ From 68217548f338af3bd38a2f51cb683b0bab26298d Mon Sep 17 00:00:00 2001 From: Nicholas Fraser Date: Sun, 20 Mar 2016 12:52:48 -0400 Subject: [PATCH 010/305] Added trailing comma support to iterative parser This also fixes cases where the iterative parser should have produced kParseErrorValueInvalid rather than kParseErrorUnspecifiedSyntaxError when expecting a value (after a colon in an object, after a comma in an array, and at the start of an array.) --- include/rapidjson/reader.h | 21 ++++++++-- test/unittest/readertest.cpp | 78 ++++++++++++++++++++++++++++-------- 2 files changed, 79 insertions(+), 20 deletions(-) diff --git a/include/rapidjson/reader.h b/include/rapidjson/reader.h index 4f09018..30251fa 100644 --- a/include/rapidjson/reader.h +++ b/include/rapidjson/reader.h @@ -640,9 +640,9 @@ private: if (parseFlags & kParseTrailingCommasFlag) { if (is.Peek() == '}') { - is.Take(); if (RAPIDJSON_UNLIKELY(!handler.EndObject(memberCount))) RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); + is.Take(); return; } } @@ -689,9 +689,9 @@ private: if (parseFlags & kParseTrailingCommasFlag) { if (is.Peek() == ']') { - is.Take(); if (RAPIDJSON_UNLIKELY(!handler.EndArray(elementCount))) RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); + is.Take(); return; } } @@ -1541,7 +1541,7 @@ private: IterativeParsingErrorState, // Left bracket IterativeParsingErrorState, // Right bracket IterativeParsingErrorState, // Left curly bracket - IterativeParsingErrorState, // Right curly bracket + IterativeParsingObjectFinishState, // Right curly bracket IterativeParsingErrorState, // Comma IterativeParsingErrorState, // Colon IterativeParsingMemberKeyState, // String @@ -1587,7 +1587,7 @@ private: // ElementDelimiter { IterativeParsingArrayInitialState, // Left bracket(push Element state) - IterativeParsingErrorState, // Right bracket + IterativeParsingArrayFinishState, // Right bracket IterativeParsingObjectInitialState, // Left curly bracket(push Element state) IterativeParsingErrorState, // Right curly bracket IterativeParsingErrorState, // Comma @@ -1689,6 +1689,11 @@ private: case IterativeParsingObjectFinishState: { + // Transit from delimiter is only allowed when trailing commas are enabled + if (!(parseFlags & kParseTrailingCommasFlag) && src == IterativeParsingMemberDelimiterState) { + RAPIDJSON_PARSE_ERROR_NORETURN(kParseErrorObjectMissName, is.Tell()); + return IterativeParsingErrorState; + } // Get member count. SizeType c = *stack_.template Pop(1); // If the object is not empty, count the last member. @@ -1714,6 +1719,11 @@ private: case IterativeParsingArrayFinishState: { + // Transit from delimiter is only allowed when trailing commas are enabled + if (!(parseFlags & kParseTrailingCommasFlag) && src == IterativeParsingElementDelimiterState) { + RAPIDJSON_PARSE_ERROR_NORETURN(kParseErrorValueInvalid, is.Tell()); + return IterativeParsingErrorState; + } // Get element count. SizeType c = *stack_.template Pop(1); // If the array is not empty, count the last element. @@ -1773,6 +1783,9 @@ private: case IterativeParsingMemberDelimiterState: RAPIDJSON_PARSE_ERROR(kParseErrorObjectMissName, is.Tell()); return; case IterativeParsingMemberKeyState: RAPIDJSON_PARSE_ERROR(kParseErrorObjectMissColon, is.Tell()); return; case IterativeParsingMemberValueState: RAPIDJSON_PARSE_ERROR(kParseErrorObjectMissCommaOrCurlyBracket, is.Tell()); return; + case IterativeParsingKeyValueDelimiterState: + case IterativeParsingArrayInitialState: + case IterativeParsingElementDelimiterState: RAPIDJSON_PARSE_ERROR(kParseErrorValueInvalid, is.Tell()); return; case IterativeParsingElementState: RAPIDJSON_PARSE_ERROR(kParseErrorArrayMissCommaOrSquareBracket, is.Tell()); return; default: RAPIDJSON_PARSE_ERROR(kParseErrorUnspecificSyntaxError, is.Tell()); return; } diff --git a/test/unittest/readertest.cpp b/test/unittest/readertest.cpp index 7c72f68..83c1802 100644 --- a/test/unittest/readertest.cpp +++ b/test/unittest/readertest.cpp @@ -1127,6 +1127,16 @@ TEST(Reader, IterativeParsing_ErrorHandling) { TESTERRORHANDLING("{\"a\": 1", kParseErrorObjectMissCommaOrCurlyBracket, 7u); TESTERRORHANDLING("[1 2 3]", kParseErrorArrayMissCommaOrSquareBracket, 3u); TESTERRORHANDLING("{\"a: 1", kParseErrorStringMissQuotationMark, 6u); + TESTERRORHANDLING("{\"a\":}", kParseErrorValueInvalid, 5u); + TESTERRORHANDLING("{\"a\":]", kParseErrorValueInvalid, 5u); + TESTERRORHANDLING("[1,2,}", kParseErrorValueInvalid, 5u); + TESTERRORHANDLING("[}]", kParseErrorValueInvalid, 1u); + TESTERRORHANDLING("[,]", kParseErrorValueInvalid, 1u); + TESTERRORHANDLING("[1,,]", kParseErrorValueInvalid, 3u); + + // Trailing commas are not allowed without kParseTrailingCommasFlag + TESTERRORHANDLING("{\"a\": 1,}", kParseErrorObjectMissName, 8u); + TESTERRORHANDLING("[1,2,3,]", kParseErrorValueInvalid, 7u); // Any JSON value can be a valid root element in RFC7159. TESTERRORHANDLING("\"ab", kParseErrorStringMissQuotationMark, 3u); @@ -1560,12 +1570,13 @@ TEST(Reader, NumbersAsStrings) { } } -TEST(Reader, TrailingCommas) { +template +void TestTrailingCommas() { { StringStream s("[1,2,3,]"); ParseArrayHandler<3> h; Reader reader; - EXPECT_TRUE(reader.Parse(s, h)); + EXPECT_TRUE(reader.Parse(s, h)); EXPECT_EQ(5u, h.step_); } { @@ -1574,7 +1585,7 @@ TEST(Reader, TrailingCommas) { StringStream s(json); ParseObjectHandler h; Reader reader; - EXPECT_TRUE(reader.Parse(s, h)); + EXPECT_TRUE(reader.Parse(s, h)); EXPECT_EQ(20u, h.step_); } { @@ -1584,7 +1595,7 @@ TEST(Reader, TrailingCommas) { StringStream s(json); ParseObjectHandler h; Reader reader; - EXPECT_TRUE(reader.Parse(s, h)); + EXPECT_TRUE(reader.Parse(s, h)); EXPECT_EQ(20u, h.step_); } { @@ -1594,18 +1605,27 @@ TEST(Reader, TrailingCommas) { StringStream s(json); ParseObjectHandler h; Reader reader; - EXPECT_TRUE(reader.Parse(s, h)); + EXPECT_TRUE(reader.Parse(s, h)); EXPECT_EQ(20u, h.step_); } } -TEST(Reader, MultipleTrailingCommaErrors) { +TEST(Reader, TrailingCommas) { + TestTrailingCommas(); +} + +TEST(Reader, TrailingCommasIterative) { + TestTrailingCommas(); +} + +template +void TestMultipleTrailingCommaErrors() { // only a single trailing comma is allowed. { StringStream s("[1,2,3,,]"); ParseArrayHandler<3> h; Reader reader; - ParseResult r = reader.Parse(s, h); + ParseResult r = reader.Parse(s, h); EXPECT_TRUE(reader.HasParseError()); EXPECT_EQ(kParseErrorValueInvalid, r.Code()); EXPECT_EQ(7u, r.Offset()); @@ -1616,21 +1636,30 @@ TEST(Reader, MultipleTrailingCommaErrors) { StringStream s(json); ParseObjectHandler h; Reader reader; - ParseResult r = reader.Parse(s, h); + ParseResult r = reader.Parse(s, h); EXPECT_TRUE(reader.HasParseError()); EXPECT_EQ(kParseErrorObjectMissName, r.Code()); EXPECT_EQ(95u, r.Offset()); } } -TEST(Reader, EmptyExceptForCommaErrors) { +TEST(Reader, MultipleTrailingCommaErrors) { + TestMultipleTrailingCommaErrors(); +} + +TEST(Reader, MultipleTrailingCommaErrorsIterative) { + TestMultipleTrailingCommaErrors(); +} + +template +void TestEmptyExceptForCommaErrors() { // not allowed even with trailing commas enabled; the // trailing comma must follow a value. { StringStream s("[,]"); ParseArrayHandler<3> h; Reader reader; - ParseResult r = reader.Parse(s, h); + ParseResult r = reader.Parse(s, h); EXPECT_TRUE(reader.HasParseError()); EXPECT_EQ(kParseErrorValueInvalid, r.Code()); EXPECT_EQ(1u, r.Offset()); @@ -1639,34 +1668,51 @@ TEST(Reader, EmptyExceptForCommaErrors) { StringStream s("{,}"); ParseObjectHandler h; Reader reader; - ParseResult r = reader.Parse(s, h); + ParseResult r = reader.Parse(s, h); EXPECT_TRUE(reader.HasParseError()); EXPECT_EQ(kParseErrorObjectMissName, r.Code()); EXPECT_EQ(1u, r.Offset()); } } -TEST(Reader, TrailingCommaHandlerTermination) { +TEST(Reader, EmptyExceptForCommaErrors) { + TestEmptyExceptForCommaErrors(); +} + +TEST(Reader, EmptyExceptForCommaErrorsIterative) { + TestEmptyExceptForCommaErrors(); +} + +template +void TestTrailingCommaHandlerTermination() { { HandlerTerminateAtEndArray h; Reader reader; StringStream s("[1,2,3,]"); - ParseResult r = reader.Parse(s, h); + ParseResult r = reader.Parse(s, h); EXPECT_TRUE(reader.HasParseError()); EXPECT_EQ(kParseErrorTermination, r.Code()); - EXPECT_EQ(8u, r.Offset()); + EXPECT_EQ(7u, r.Offset()); } { HandlerTerminateAtEndObject h; Reader reader; StringStream s("{\"t\": true, \"f\": false,}"); - ParseResult r = reader.Parse(s, h); + ParseResult r = reader.Parse(s, h); EXPECT_TRUE(reader.HasParseError()); EXPECT_EQ(kParseErrorTermination, r.Code()); - EXPECT_EQ(24u, r.Offset()); + EXPECT_EQ(23u, r.Offset()); } } +TEST(Reader, TrailingCommaHandlerTermination) { + TestTrailingCommaHandlerTermination(); +} + +TEST(Reader, TrailingCommaHandlerTerminationIterative) { + TestTrailingCommaHandlerTermination(); +} + #ifdef __GNUC__ RAPIDJSON_DIAG_POP #endif From 4fdcb10c3ec2439cd263cb3ca1a98ea8f0b63fef Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Sat, 26 Mar 2016 22:47:07 +0800 Subject: [PATCH 011/305] Fix #587 --- include/rapidjson/document.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h index 855543e..e1b1fbc 100644 --- a/include/rapidjson/document.h +++ b/include/rapidjson/document.h @@ -2509,8 +2509,8 @@ public: bool HasMember(const std::basic_string& name) const { return value_.HasMember(name); } #endif template bool HasMember(const GenericValue& name) const { return value_.HasMember(name); } - MemberIterator FindMember(const Ch* name) const { value_.FindMember(name); } - template MemberIterator FindMember(const GenericValue& name) const { value_.FindMember(name); } + MemberIterator FindMember(const Ch* name) const { return value_.FindMember(name); } + template MemberIterator FindMember(const GenericValue& name) const { return value_.FindMember(name); } #if RAPIDJSON_HAS_STDSTRING MemberIterator FindMember(const std::basic_string& name) const { return value_.FindMember(name); } #endif From 926d7ffcc893d4078a89cc4e2dc6e2bc0c425be8 Mon Sep 17 00:00:00 2001 From: Jarred Nicholls Date: Mon, 28 Mar 2016 14:31:36 -0400 Subject: [PATCH 012/305] =?UTF-8?q?Later=20clang=20compilers=20will=20warn?= =?UTF-8?q?=20on=20float=20->=20double=20promotion=20because=20it=20can=20?= =?UTF-8?q?add=20precision.=20In=20the=20context=20of=20RapidJSON=20?= =?UTF-8?q?=E2=80=93=20especially=20with=20its=20float=20methods=20on=20Ge?= =?UTF-8?q?nericValue=20=E2=80=93=C2=A0I=20think=20this=20warning=20holds?= =?UTF-8?q?=20no=20water=20and=20should=20be=20ignored.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Trim whitespace off the end of various lines. Added an additional NumberStream specialization that will always perform a TakePush() even when just Take() is called. This supports RawNumber parsing by pushing onto our StackStream particular parts of the number that currently aren't captured because of full precision double parsing, such as the negative sign, scientific number exponents, etc. RawNumber parsing fails with input streams that don't have copy optimization, such as the BasicIStreamWrapper stream. To work around this, instead do the Transcode copy operation by reading from a UTF8 StringStream instead of the original InputStream. Since the NumberStream downcasts all input Ch into chars, we know we're dealing with UTF8/ASCII compatible stack characters during the Transcoding. --- include/rapidjson/reader.h | 116 ++++++++++++++-------------- test/unittest/CMakeLists.txt | 5 ++ test/unittest/readertest.cpp | 143 +++++++++++++++++++++++++---------- 3 files changed, 168 insertions(+), 96 deletions(-) diff --git a/include/rapidjson/reader.h b/include/rapidjson/reader.h index 30251fa..6f45571 100644 --- a/include/rapidjson/reader.h +++ b/include/rapidjson/reader.h @@ -1,5 +1,5 @@ // Tencent is pleased to support the open source community by making RapidJSON available. -// +// // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. // // Licensed under the MIT License (the "License"); you may not use this file except @@ -7,9 +7,9 @@ // // http://opensource.org/licenses/MIT // -// Unless required by applicable law or agreed to in writing, software distributed -// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR -// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the // specific language governing permissions and limitations under the License. #ifndef RAPIDJSON_READER_H_ @@ -127,7 +127,7 @@ RAPIDJSON_NAMESPACE_BEGIN /////////////////////////////////////////////////////////////////////////////// // ParseFlag -/*! \def RAPIDJSON_PARSE_DEFAULT_FLAGS +/*! \def RAPIDJSON_PARSE_DEFAULT_FLAGS \ingroup RAPIDJSON_CONFIG \brief User-defined kParseDefaultFlags definition. @@ -158,7 +158,7 @@ enum ParseFlag { /*! \class rapidjson::Handler \brief Concept for receiving events from GenericReader upon parsing. - The functions return true if no error occurs. If they return false, + The functions return true if no error occurs. If they return false, the event publisher should terminate the process. \code concept Handler { @@ -425,7 +425,7 @@ inline const char *SkipWhitespace_SIMD(const char* p, const char* end) { #ifdef RAPIDJSON_SIMD //! Template function specialization for InsituStringStream -template<> inline void SkipWhitespace(InsituStringStream& is) { +template<> inline void SkipWhitespace(InsituStringStream& is) { is.src_ = const_cast(SkipWhitespace_SIMD(is.src_)); } @@ -443,17 +443,17 @@ template<> inline void SkipWhitespace(EncodedInputStream, MemoryStream>& // GenericReader //! SAX-style JSON parser. Use \ref Reader for UTF8 encoding and default allocator. -/*! GenericReader parses JSON text from a stream, and send events synchronously to an +/*! GenericReader parses JSON text from a stream, and send events synchronously to an object implementing Handler concept. - It needs to allocate a stack for storing a single decoded string during + It needs to allocate a stack for storing a single decoded string during non-destructive parsing. - For in-situ parsing, the decoded string is directly written to the source + For in-situ parsing, the decoded string is directly written to the source text string, no temporary buffer is required. A GenericReader object can be reused for parsing multiple JSON text. - + \tparam SourceEncoding Encoding of the input stream. \tparam TargetEncoding Encoding of the parse output. \tparam StackAllocator Allocator type for stack. @@ -525,7 +525,7 @@ public: //! Whether a parse error has occured in the last parsing. bool HasParseError() const { return parseResult_.IsError(); } - + //! Get the \ref ParseErrorCode of last parsing. ParseErrorCode GetParseErrorCode() const { return parseResult_.Code(); } @@ -585,7 +585,7 @@ private: void ParseObject(InputStream& is, Handler& handler) { RAPIDJSON_ASSERT(is.Peek() == '{'); is.Take(); // Skip '{' - + if (RAPIDJSON_UNLIKELY(!handler.StartObject())) RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); @@ -628,12 +628,12 @@ private: SkipWhitespaceAndComments(is); RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; break; - case '}': + case '}': is.Take(); if (RAPIDJSON_UNLIKELY(!handler.EndObject(memberCount))) RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); return; - default: + default: RAPIDJSON_PARSE_ERROR(kParseErrorObjectMissCommaOrCurlyBracket, is.Tell()); break; } @@ -654,10 +654,10 @@ private: void ParseArray(InputStream& is, Handler& handler) { RAPIDJSON_ASSERT(is.Peek() == '['); is.Take(); // Skip '[' - + if (RAPIDJSON_UNLIKELY(!handler.StartArray())) RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); - + SkipWhitespaceAndComments(is); RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; @@ -780,7 +780,7 @@ private: *stack_.template Push() = c; ++length_; } - + RAPIDJSON_FORCEINLINE void* Push(SizeType count) { length_ += count; return stack_.template Push(count); @@ -838,10 +838,10 @@ private: //!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN #define Z16 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 static const char escape[256] = { - Z16, Z16, 0, 0,'\"', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,'/', - Z16, Z16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,'\\', 0, 0, 0, - 0, 0,'\b', 0, 0, 0,'\f', 0, 0, 0, 0, 0, 0, 0,'\n', 0, - 0, 0,'\r', 0,'\t', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + Z16, Z16, 0, 0,'\"', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,'/', + Z16, Z16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,'\\', 0, 0, 0, + 0, 0,'\b', 0, 0, 0,'\f', 0, 0, 0, 0, 0, 0, 0,'\n', 0, + 0, 0,'\r', 0,'\t', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Z16, Z16, Z16, Z16, Z16, Z16, Z16, Z16 }; #undef Z16 @@ -893,8 +893,8 @@ private: } else { size_t offset = is.Tell(); - if (RAPIDJSON_UNLIKELY((parseFlags & kParseValidateEncodingFlag ? - !Transcoder::Validate(is, os) : + if (RAPIDJSON_UNLIKELY((parseFlags & kParseValidateEncodingFlag ? + !Transcoder::Validate(is, os) : !Transcoder::Transcode(is, os)))) RAPIDJSON_PARSE_ERROR(kParseErrorStringInvalidEncoding, offset); } @@ -954,7 +954,7 @@ private: } _mm_storeu_si128(reinterpret_cast<__m128i *>(os.Push(16)), s); } - + is.src_ = p; } @@ -977,7 +977,7 @@ private: if (RAPIDJSON_UNLIKELY(*p == '\"') || RAPIDJSON_UNLIKELY(*p == '\\') || RAPIDJSON_UNLIKELY(static_cast(*p) < 0x20)) { is.src_ = p; is.dst_ = q; - return; + return; } else *q++ = *p++; @@ -1063,11 +1063,11 @@ private: } #endif - template + template class NumberStream; template - class NumberStream { + class NumberStream { public: typedef typename InputStream::Ch Ch; @@ -1090,10 +1090,10 @@ private: }; template - class NumberStream : public NumberStream { - typedef NumberStream Base; + class NumberStream : public NumberStream { + typedef NumberStream Base; public: - NumberStream(GenericReader& reader, InputStream& is) : NumberStream(reader, is), stackStream(reader.stack_) {} + NumberStream(GenericReader& reader, InputStream& is) : Base(reader, is), stackStream(reader.stack_) {} ~NumberStream() {} RAPIDJSON_FORCEINLINE Ch TakePush() { @@ -1101,9 +1101,9 @@ private: return Base::is.Take(); } - RAPIDJSON_FORCEINLINE void Push(char c) { - stackStream.Put(c); - } + RAPIDJSON_FORCEINLINE void Push(char c) { + stackStream.Put(c); + } size_t Length() { return stackStream.Length(); } @@ -1116,13 +1116,25 @@ private: StackStream stackStream; }; + template + class NumberStream : public NumberStream { + typedef NumberStream Base; + public: + NumberStream(GenericReader& reader, InputStream& is) : Base(reader, is) {} + ~NumberStream() {} + + RAPIDJSON_FORCEINLINE Ch Take() { return Base::TakePush(); } + }; + template void ParseNumber(InputStream& is, Handler& handler) { internal::StreamLocalCopy copy(is); NumberStream s(*this, copy.s); + ((parseFlags & kParseFullPrecisionFlag) != 0), + (parseFlags & kParseNumbersAsStringsFlag) != 0 && + (parseFlags & kParseInsituFlag) == 0> s(*this, copy.s); size_t startOffset = s.Tell(); @@ -1173,7 +1185,7 @@ private: bool useDouble = false; double d = 0.0; if (use64bit) { - if (minus) + if (minus) while (RAPIDJSON_LIKELY(s.Peek() >= '0' && s.Peek() <= '9')) { if (RAPIDJSON_UNLIKELY(i64 >= RAPIDJSON_UINT64_C2(0x0CCCCCCC, 0xCCCCCCCC))) // 2^63 = 9223372036854775808 if (RAPIDJSON_LIKELY(i64 != RAPIDJSON_UINT64_C2(0x0CCCCCCC, 0xCCCCCCCC) || s.Peek() > '8')) { @@ -1210,9 +1222,6 @@ private: int expFrac = 0; size_t decimalPosition; if (Consume(s, '.')) { - if (((parseFlags & kParseNumbersAsStringsFlag) != 0) && ((parseFlags & kParseInsituFlag) == 0)) { - s.Push('.'); - } decimalPosition = s.Length(); if (RAPIDJSON_UNLIKELY(!(s.Peek() >= '0' && s.Peek() <= '9'))) @@ -1223,7 +1232,7 @@ private: // Use i64 to store significand in 64-bit architecture if (!use64bit) i64 = i; - + while (RAPIDJSON_LIKELY(s.Peek() >= '0' && s.Peek() <= '9')) { if (i64 > RAPIDJSON_UINT64_C2(0x1FFFFF, 0xFFFFFFFF)) // 2^53 - 1 for fast path break; @@ -1260,11 +1269,7 @@ private: // Parse exp = e [ minus / plus ] 1*DIGIT int exp = 0; if (Consume(s, 'e') || Consume(s, 'E')) { - if ( ((parseFlags & kParseNumbersAsStringsFlag) != 0) && ((parseFlags & kParseInsituFlag) == 0) ) { - s.Push( 'e' ); - } - - if (!useDouble) { + if (!useDouble) { d = static_cast(use64bit ? i64 : i); useDouble = true; } @@ -1316,14 +1321,15 @@ private: cont = handler.RawNumber(str, SizeType(length), false); } else { - StackStream stackStream(stack_); SizeType numCharsToCopy = static_cast(s.Length()); + StringStream srcStream(s.Pop()); + StackStream dstStream(stack_); while (numCharsToCopy--) { - Transcoder::Transcode(is, stackStream); + Transcoder, TargetEncoding>::Transcode(srcStream, dstStream); } - stackStream.Put('\0'); - const typename TargetEncoding::Ch* str = stackStream.Pop(); - const SizeType length = static_cast(stackStream.Length()) - 1; + dstStream.Put('\0'); + const typename TargetEncoding::Ch* str = dstStream.Pop(); + const SizeType length = static_cast(dstStream.Length()) - 1; cont = handler.RawNumber(str, SizeType(length), true); } } @@ -1369,10 +1375,10 @@ private: case '"': ParseString(is, handler); break; case '{': ParseObject(is, handler); break; case '[': ParseArray (is, handler); break; - default : + default : ParseNumber(is, handler); break; - + } } @@ -1444,7 +1450,7 @@ private: #undef N #undef N16 //!@endcond - + if (sizeof(Ch) == 1 || static_cast(c) < 256) return static_cast(tokenMap[static_cast(c)]); else @@ -1775,7 +1781,7 @@ private: // Error flag has been set. return; } - + switch (src) { case IterativeParsingStartState: RAPIDJSON_PARSE_ERROR(kParseErrorDocumentEmpty, is.Tell()); return; case IterativeParsingFinishState: RAPIDJSON_PARSE_ERROR(kParseErrorDocumentRootNotSingular, is.Tell()); return; @@ -1788,7 +1794,7 @@ private: case IterativeParsingElementDelimiterState: RAPIDJSON_PARSE_ERROR(kParseErrorValueInvalid, is.Tell()); return; case IterativeParsingElementState: RAPIDJSON_PARSE_ERROR(kParseErrorArrayMissCommaOrSquareBracket, is.Tell()); return; default: RAPIDJSON_PARSE_ERROR(kParseErrorUnspecificSyntaxError, is.Tell()); return; - } + } } template diff --git a/test/unittest/CMakeLists.txt b/test/unittest/CMakeLists.txt index 02c1532..3f76a4f 100644 --- a/test/unittest/CMakeLists.txt +++ b/test/unittest/CMakeLists.txt @@ -38,6 +38,11 @@ if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror -Wall -Wextra -Weffc++ -Wswitch-default -Wfloat-equal") elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror -Wall -Wextra -Weffc++ -Wswitch-default -Wfloat-equal -Wimplicit-fallthrough -Weverything") + # If the user is running a newer version of Clang that includes the + # -Wdouble-promotion, we will ignore that warning. + if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 3.7) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-double-promotion") + endif() elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") add_definitions(-D_CRT_SECURE_NO_WARNINGS=1) endif() diff --git a/test/unittest/readertest.cpp b/test/unittest/readertest.cpp index 83c1802..3f11fec 100644 --- a/test/unittest/readertest.cpp +++ b/test/unittest/readertest.cpp @@ -1,5 +1,5 @@ // Tencent is pleased to support the open source community by making RapidJSON available. -// +// // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. // // Licensed under the MIT License (the "License"); you may not use this file except @@ -7,9 +7,9 @@ // // http://opensource.org/licenses/MIT // -// Unless required by applicable law or agreed to in writing, software distributed -// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR -// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the // specific language governing permissions and limitations under the License. #include "unittest.h" @@ -241,13 +241,13 @@ static void TestParseDouble() { TEST_DOUBLE(fullPrecision, "0.017976931348623157e+310", 1.7976931348623157e+308); // Max double in another form // Since - // abs((2^-1022 - 2^-1074) - 2.2250738585072012e-308) = 3.109754131239141401123495768877590405345064751974375599... 10^-324 - // abs((2^-1022) - 2.2250738585072012e-308) = 1.830902327173324040642192159804623318305533274168872044... 10 ^ -324 + // abs((2^-1022 - 2^-1074) - 2.2250738585072012e-308) = 3.109754131239141401123495768877590405345064751974375599... �� 10^-324 + // abs((2^-1022) - 2.2250738585072012e-308) = 1.830902327173324040642192159804623318305533274168872044... �� 10 ^ -324 // So 2.2250738585072012e-308 should round to 2^-1022 = 2.2250738585072014e-308 TEST_DOUBLE(fullPrecision, "2.2250738585072012e-308", 2.2250738585072014e-308); // http://www.exploringbinary.com/java-hangs-when-converting-2-2250738585072012e-308/ // More closer to normal/subnormal boundary - // boundary = 2^-1022 - 2^-1075 = 2.225073858507201136057409796709131975934819546351645648... 10^-308 + // boundary = 2^-1022 - 2^-1075 = 2.225073858507201136057409796709131975934819546351645648... �� 10^-308 TEST_DOUBLE(fullPrecision, "2.22507385850720113605740979670913197593481954635164564e-308", 2.2250738585072009e-308); TEST_DOUBLE(fullPrecision, "2.22507385850720113605740979670913197593481954635164565e-308", 2.2250738585072014e-308); @@ -297,7 +297,7 @@ static void TestParseDouble() { } // Cover trimming - TEST_DOUBLE(fullPrecision, + TEST_DOUBLE(fullPrecision, "2.22507385850720113605740979670913197593481954635164564802342610972482222202107694551652952390813508" "7914149158913039621106870086438694594645527657207407820621743379988141063267329253552286881372149012" "9811224514518898490572223072852551331557550159143974763979834118019993239625482890171070818506906306" @@ -306,7 +306,7 @@ static void TestParseDouble() { "5722898802581825451803257070188608721131280795122334262883686223215037756666225039825343359745688844" "2390026549819838548794829220689472168983109969836584681402285424333066033985088644580400103493397042" "7567186443383770486037861622771738545623065874679014086723327636718751234567890123456789012345678901" -"e-308", +"e-308", 2.2250738585072014e-308); { @@ -457,12 +457,12 @@ template struct ParseStringHandler : BaseReaderHandler > { ParseStringHandler() : str_(0), length_(0), copy_() {} ~ParseStringHandler() { EXPECT_TRUE(str_ != 0); if (copy_) free(const_cast(str_)); } - + ParseStringHandler(const ParseStringHandler&); ParseStringHandler& operator=(const ParseStringHandler&); bool Default() { ADD_FAILURE(); return false; } - bool String(const typename Encoding::Ch* str, size_t length, bool copy) { + bool String(const typename Encoding::Ch* str, size_t length, bool copy) { EXPECT_EQ(0, str_); if (copy) { str_ = static_cast(malloc((length + 1) * sizeof(typename Encoding::Ch))); @@ -470,7 +470,7 @@ struct ParseStringHandler : BaseReaderHandler= 128 are assigned to signed integer types. // Therefore, utype is added for declaring unsigned array, and then cast it to Encoding::Ch. @@ -650,7 +650,7 @@ TEST(Reader, ParseString_Error) { // http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt - // 3 Malformed sequences + // 3 Malformed sequences // 3.1 Unexpected continuation bytes { @@ -684,19 +684,19 @@ TEST(Reader, ParseString_Error) { } } - // 4 Overlong sequences + // 4 Overlong sequences // 4.1 Examples of an overlong ASCII character TEST_STRINGENCODING_ERROR(UTF8<>, UTF16<>, unsigned char, ARRAY('[', '\"', 0xC0u, 0xAFu, '\"', ']', '\0')); TEST_STRINGENCODING_ERROR(UTF8<>, UTF16<>, unsigned char, ARRAY('[', '\"', 0xE0u, 0x80u, 0xAFu, '\"', ']', '\0')); TEST_STRINGENCODING_ERROR(UTF8<>, UTF16<>, unsigned char, ARRAY('[', '\"', 0xF0u, 0x80u, 0x80u, 0xAFu, '\"', ']', '\0')); - // 4.2 Maximum overlong sequences + // 4.2 Maximum overlong sequences TEST_STRINGENCODING_ERROR(UTF8<>, UTF16<>, unsigned char, ARRAY('[', '\"', 0xC1u, 0xBFu, '\"', ']', '\0')); TEST_STRINGENCODING_ERROR(UTF8<>, UTF16<>, unsigned char, ARRAY('[', '\"', 0xE0u, 0x9Fu, 0xBFu, '\"', ']', '\0')); TEST_STRINGENCODING_ERROR(UTF8<>, UTF16<>, unsigned char, ARRAY('[', '\"', 0xF0u, 0x8Fu, 0xBFu, 0xBFu, '\"', ']', '\0')); - // 4.3 Overlong representation of the NUL character + // 4.3 Overlong representation of the NUL character TEST_STRINGENCODING_ERROR(UTF8<>, UTF16<>, unsigned char, ARRAY('[', '\"', 0xC0u, 0x80u, '\"', ']', '\0')); TEST_STRINGENCODING_ERROR(UTF8<>, UTF16<>, unsigned char, ARRAY('[', '\"', 0xE0u, 0x80u, 0x80u, '\"', ']', '\0')); TEST_STRINGENCODING_ERROR(UTF8<>, UTF16<>, unsigned char, ARRAY('[', '\"', 0xF0u, 0x80u, 0x80u, 0x80u, '\"', ']', '\0')); @@ -790,14 +790,14 @@ struct ParseObjectHandler : BaseReaderHandler, ParseObjectHandler> { bool Default() { ADD_FAILURE(); return false; } bool Null() { EXPECT_EQ(8u, step_); step_++; return true; } - bool Bool(bool b) { + bool Bool(bool b) { switch(step_) { case 4: EXPECT_TRUE(b); step_++; return true; case 6: EXPECT_FALSE(b); step_++; return true; default: ADD_FAILURE(); return false; } } - bool Int(int i) { + bool Int(int i) { switch(step_) { case 10: EXPECT_EQ(123, i); step_++; return true; case 15: EXPECT_EQ(1, i); step_++; return true; @@ -808,7 +808,7 @@ struct ParseObjectHandler : BaseReaderHandler, ParseObjectHandler> { } bool Uint(unsigned i) { return Int(static_cast(i)); } bool Double(double d) { EXPECT_EQ(12u, step_); EXPECT_DOUBLE_EQ(3.1416, d); step_++; return true; } - bool String(const char* str, size_t, bool) { + bool String(const char* str, size_t, bool) { switch(step_) { case 1: EXPECT_STREQ("hello", str); step_++; return true; case 2: EXPECT_STREQ("world", str); step_++; return true; @@ -1045,7 +1045,7 @@ struct StreamTraits > { }; } // namespace rapidjson -#endif +#endif TEST(Reader, CustomStringStream) { const char* json = "{ \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3] } "; @@ -1069,7 +1069,7 @@ public: return c == std::char_traits::eof() ? '\0' : static_cast(c); } - Ch Take() { + Ch Take() { int c = is_.get(); return c == std::char_traits::eof() ? '\0' : static_cast(c); } @@ -1097,7 +1097,7 @@ TEST(Reader, Parse_IStreamWrapper_StringStream) { Reader reader; ParseArrayHandler<4> h; reader.Parse(is, h); - EXPECT_FALSE(reader.HasParseError()); + EXPECT_FALSE(reader.HasParseError()); } // Test iterative parsing. @@ -1195,7 +1195,7 @@ struct IterativeParsingReaderHandler { bool StartObject() { RAPIDJSON_ASSERT(LogCount < LogCapacity); Logs[LogCount++] = LOG_STARTOBJECT; return true; } bool Key (const Ch*, SizeType, bool) { RAPIDJSON_ASSERT(LogCount < LogCapacity); Logs[LogCount++] = LOG_KEY; return true; } - + bool EndObject(SizeType c) { RAPIDJSON_ASSERT(LogCount < LogCapacity); Logs[LogCount++] = LOG_ENDOBJECT; @@ -1446,7 +1446,7 @@ TEST(Reader, ParseEmptyOnelineComment) { } TEST(Reader, ParseMultipleCommentsInARow) { - const char* json = + const char* json = "{/* first comment *//* second */\n" "/* third */ /*fourth*/// last one\n" "\"hello\" : \"world\", \"t\" : true, \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3] }"; @@ -1541,7 +1541,8 @@ struct NumbersAsStringsHandler { // 'str' is not null-terminated bool RawNumber(const char* str, SizeType length, bool) { EXPECT_TRUE(str != 0); - EXPECT_TRUE(strncmp(str, "3.1416", length) == 0); + EXPECT_TRUE(expected_len_ == length); + EXPECT_TRUE(strncmp(str, expected_, length) == 0); return true; } bool String(const char*, SizeType, bool) { return true; } @@ -1550,24 +1551,84 @@ struct NumbersAsStringsHandler { bool EndObject(SizeType) { return true; } bool StartArray() { return true; } bool EndArray(SizeType) { return true; } + + NumbersAsStringsHandler(const char* expected) + : expected_(expected) + , expected_len_(strlen(expected)) {} + + const char* expected_; + size_t expected_len_; }; TEST(Reader, NumbersAsStrings) { - { - const char* json = "{ \"pi\": 3.1416 } "; - StringStream s(json); - NumbersAsStringsHandler h; - Reader reader; - EXPECT_TRUE(reader.Parse(s, h)); - } - { - char* json = StrDup("{ \"pi\": 3.1416 } "); - InsituStringStream s(json); - NumbersAsStringsHandler h; - Reader reader; - EXPECT_TRUE(reader.Parse(s, h)); - free(json); - } + { + const char* json = "{ \"pi\": 3.1416 } "; + StringStream s(json); + NumbersAsStringsHandler h("3.1416"); + Reader reader; + EXPECT_TRUE(reader.Parse(s, h)); + } + { + char* json = StrDup("{ \"pi\": 3.1416 } "); + InsituStringStream s(json); + NumbersAsStringsHandler h("3.1416"); + Reader reader; + EXPECT_TRUE(reader.Parse(s, h)); + free(json); + } + { + const char* json = "{ \"gigabyte\": 1.0e9 } "; + StringStream s(json); + NumbersAsStringsHandler h("1.0e9"); + Reader reader; + EXPECT_TRUE(reader.Parse(s, h)); + } + { + char* json = StrDup("{ \"gigabyte\": 1.0e9 } "); + InsituStringStream s(json); + NumbersAsStringsHandler h("1.0e9"); + Reader reader; + EXPECT_TRUE(reader.Parse(s, h)); + free(json); + } + { + const char* json = "{ \"pi\": 314.159e-2 } "; + StringStream s(json); + NumbersAsStringsHandler h("314.159e-2"); + Reader reader; + EXPECT_TRUE(reader.Parse(s, h)); + } + { + char* json = StrDup("{ \"gigabyte\": 314.159e-2 } "); + InsituStringStream s(json); + NumbersAsStringsHandler h("314.159e-2"); + Reader reader; + EXPECT_TRUE(reader.Parse(s, h)); + free(json); + } + { + const char* json = "{ \"negative\": -1.54321 } "; + StringStream s(json); + NumbersAsStringsHandler h("-1.54321"); + Reader reader; + EXPECT_TRUE(reader.Parse(s, h)); + } + { + char* json = StrDup("{ \"negative\": -1.54321 } "); + InsituStringStream s(json); + NumbersAsStringsHandler h("-1.54321"); + Reader reader; + EXPECT_TRUE(reader.Parse(s, h)); + free(json); + } + { + const char* json = "{ \"pi\": 314.159e-2 } "; + std::stringstream ss(json); + IStreamWrapper s(ss); + NumbersAsStringsHandler h("314.159e-2"); + Reader reader; + EXPECT_TRUE(reader.Parse(s, h)); + } } template From 89afda0694c922afe66cde29e6ac40044bb2978c Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Thu, 31 Mar 2016 10:25:55 +0800 Subject: [PATCH 013/305] Add CMAKE verbose for appveyor --- appveyor.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/appveyor.yml b/appveyor.yml index 13d8b94..205c670 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -30,7 +30,7 @@ environment: before_build: - git submodule update --init --recursive -- cmake -H. -BBuild/VS -G "Visual Studio %VS_VERSION%" -DCMAKE_GENERATOR_PLATFORM=%VS_PLATFORM% -DBUILD_SHARED_LIBS=true -Wno-dev +- cmake -H. -BBuild/VS -G "Visual Studio %VS_VERSION%" -DCMAKE_GENERATOR_PLATFORM=%VS_PLATFORM% -DCMAKE_VERBOSE_MAKEFILE=ON -DBUILD_SHARED_LIBS=true -Wno-dev build: project: Build\VS\RapidJSON.sln From d7df1f26ba56922aed0bcdb2b39cf46bd78e36c6 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Thu, 31 Mar 2016 14:20:24 +0800 Subject: [PATCH 014/305] Add /W4 and /WX for VC --- test/unittest/CMakeLists.txt | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/test/unittest/CMakeLists.txt b/test/unittest/CMakeLists.txt index 3f76a4f..ff49bb9 100644 --- a/test/unittest/CMakeLists.txt +++ b/test/unittest/CMakeLists.txt @@ -38,12 +38,26 @@ if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror -Wall -Wextra -Weffc++ -Wswitch-default -Wfloat-equal") elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror -Wall -Wextra -Weffc++ -Wswitch-default -Wfloat-equal -Wimplicit-fallthrough -Weverything") - # If the user is running a newer version of Clang that includes the - # -Wdouble-promotion, we will ignore that warning. - if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 3.7) + # If the user is running a newer version of Clang that includes the + # -Wdouble-promotion, we will ignore that warning. + if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 3.7) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-double-promotion") - endif() + endif() elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") + # Force to always compile with /W4 + if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]") + string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") + else() + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4") + endif() + + # Force to always compile with /WX + if(CMAKE_CXX_FLAGS MATCHES "/WX-") + string(REGEX REPLACE "/WX-" "/WX" CMAKE_CXX_FLAGS + else() + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /WX") + endif() + add_definitions(-D_CRT_SECURE_NO_WARNINGS=1) endif() From 2418d7cd91a2b8c5a7e19f3656e57ead051538b1 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Thu, 31 Mar 2016 15:02:03 +0800 Subject: [PATCH 015/305] Fix cmake --- test/unittest/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unittest/CMakeLists.txt b/test/unittest/CMakeLists.txt index ff49bb9..f1918cc 100644 --- a/test/unittest/CMakeLists.txt +++ b/test/unittest/CMakeLists.txt @@ -53,7 +53,7 @@ elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") # Force to always compile with /WX if(CMAKE_CXX_FLAGS MATCHES "/WX-") - string(REGEX REPLACE "/WX-" "/WX" CMAKE_CXX_FLAGS + string(REGEX REPLACE "/WX-" "/WX" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") else() set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /WX") endif() From c843a2655bf58efd0ced40c5b55706eef45f0978 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Mon, 4 Apr 2016 15:01:34 +0800 Subject: [PATCH 016/305] Try to fix all /W4 warnings in VC2015 --- CMakeLists.txt | 1 + example/CMakeLists.txt | 2 -- include/rapidjson/document.h | 5 ++-- include/rapidjson/schema.h | 3 ++- test/unittest/CMakeLists.txt | 2 -- test/unittest/documenttest.cpp | 40 ++++++++++++++-------------- test/unittest/istreamwrappertest.cpp | 11 +++++++- test/unittest/readertest.cpp | 2 +- test/unittest/valuetest.cpp | 10 +++---- 9 files changed, 41 insertions(+), 35 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index fa2bdcf..6bdf484 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -54,6 +54,7 @@ elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang") endif() elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") add_definitions(-D_CRT_SECURE_NO_WARNINGS=1) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc") endif() #add extra search paths for libraries and includes diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index 8c546cf..6da18df 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -23,8 +23,6 @@ if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread -Werror -Wall -Wextra -Weffc++ -Wswitch-default") elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror -Wall -Wextra -Weffc++ -Wswitch-default -Wfloat-equal -Wimplicit-fallthrough -Weverything") -elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") - add_definitions(-D_CRT_SECURE_NO_WARNINGS=1) endif() foreach (example ${EXAMPLES}) diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h index e1b1fbc..0ce2d2a 100644 --- a/include/rapidjson/document.h +++ b/include/rapidjson/document.h @@ -1794,7 +1794,7 @@ private: template friend class GenericValue; template friend class GenericDocument; - enum { + static const uint16_t kBoolFlag = 0x0008, kNumberFlag = 0x0010, kIntFlag = 0x0020, @@ -1822,8 +1822,7 @@ private: kObjectFlag = kObjectType, kArrayFlag = kArrayType, - kTypeMask = 0x07 - }; + kTypeMask = 0x07; static const SizeType kDefaultArrayCapacity = 16; static const SizeType kDefaultObjectCapacity = 16; diff --git a/include/rapidjson/schema.h b/include/rapidjson/schema.h index 26da8a6..acbae36 100644 --- a/include/rapidjson/schema.h +++ b/include/rapidjson/schema.h @@ -1963,7 +1963,8 @@ public: GenericSchemaValidator validator(sd_, handler); parseResult_ = reader.template Parse(is_, validator); - if ((isValid_ = validator.IsValid())) { + isValid_ = validator.IsValid(); + if (isValid_) { invalidSchemaPointer_ = PointerType(); invalidSchemaKeyword_ = 0; invalidDocumentPointer_ = PointerType(); diff --git a/test/unittest/CMakeLists.txt b/test/unittest/CMakeLists.txt index f1918cc..3630cfe 100644 --- a/test/unittest/CMakeLists.txt +++ b/test/unittest/CMakeLists.txt @@ -57,8 +57,6 @@ elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") else() set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /WX") endif() - - add_definitions(-D_CRT_SECURE_NO_WARNINGS=1) endif() set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DRAPIDJSON_HAS_STDSTRING=1") diff --git a/test/unittest/documenttest.cpp b/test/unittest/documenttest.cpp index 0c9ffab..ecd4b79 100644 --- a/test/unittest/documenttest.cpp +++ b/test/unittest/documenttest.cpp @@ -447,10 +447,10 @@ TYPED_TEST_CASE(DocumentMove, MoveAllocatorTypes); TYPED_TEST(DocumentMove, MoveConstructor) { typedef TypeParam Allocator; - typedef GenericDocument, Allocator> Document; + typedef GenericDocument, Allocator> D; Allocator allocator; - Document a(&allocator); + D a(&allocator); a.Parse("[\"one\", \"two\", \"three\"]"); EXPECT_FALSE(a.HasParseError()); EXPECT_TRUE(a.IsArray()); @@ -458,7 +458,7 @@ TYPED_TEST(DocumentMove, MoveConstructor) { EXPECT_EQ(&a.GetAllocator(), &allocator); // Document b(a); // does not compile (!is_copy_constructible) - Document b(std::move(a)); + D b(std::move(a)); EXPECT_TRUE(a.IsNull()); EXPECT_TRUE(b.IsArray()); EXPECT_EQ(3u, b.Size()); @@ -471,7 +471,7 @@ TYPED_TEST(DocumentMove, MoveConstructor) { EXPECT_EQ(2u, b.MemberCount()); // Document c = a; // does not compile (!is_copy_constructible) - Document c = std::move(b); + D c = std::move(b); EXPECT_TRUE(b.IsNull()); EXPECT_TRUE(c.IsObject()); EXPECT_EQ(2u, c.MemberCount()); @@ -481,17 +481,17 @@ TYPED_TEST(DocumentMove, MoveConstructor) { TYPED_TEST(DocumentMove, MoveConstructorParseError) { typedef TypeParam Allocator; - typedef GenericDocument, Allocator> Document; + typedef GenericDocument, Allocator> D; ParseResult noError; - Document a; + D a; a.Parse("{ 4 = 4]"); ParseResult error(a.GetParseError(), a.GetErrorOffset()); EXPECT_TRUE(a.HasParseError()); EXPECT_NE(error.Code(), noError.Code()); EXPECT_NE(error.Offset(), noError.Offset()); - Document b(std::move(a)); + D b(std::move(a)); EXPECT_FALSE(a.HasParseError()); EXPECT_TRUE(b.HasParseError()); EXPECT_EQ(a.GetParseError(), noError.Code()); @@ -499,7 +499,7 @@ TYPED_TEST(DocumentMove, MoveConstructorParseError) { EXPECT_EQ(a.GetErrorOffset(), noError.Offset()); EXPECT_EQ(b.GetErrorOffset(), error.Offset()); - Document c(std::move(b)); + D c(std::move(b)); EXPECT_FALSE(b.HasParseError()); EXPECT_TRUE(c.HasParseError()); EXPECT_EQ(b.GetParseError(), noError.Code()); @@ -540,10 +540,10 @@ TYPED_TEST(DocumentMove, MoveConstructorStack) { TYPED_TEST(DocumentMove, MoveAssignment) { typedef TypeParam Allocator; - typedef GenericDocument, Allocator> Document; + typedef GenericDocument, Allocator> D; Allocator allocator; - Document a(&allocator); + D a(&allocator); a.Parse("[\"one\", \"two\", \"three\"]"); EXPECT_FALSE(a.HasParseError()); EXPECT_TRUE(a.IsArray()); @@ -551,7 +551,7 @@ TYPED_TEST(DocumentMove, MoveAssignment) { EXPECT_EQ(&a.GetAllocator(), &allocator); // Document b; b = a; // does not compile (!is_copy_assignable) - Document b; + D b; b = std::move(a); EXPECT_TRUE(a.IsNull()); EXPECT_TRUE(b.IsArray()); @@ -565,7 +565,7 @@ TYPED_TEST(DocumentMove, MoveAssignment) { EXPECT_EQ(2u, b.MemberCount()); // Document c; c = a; // does not compile (see static_assert) - Document c; + D c; c = std::move(b); EXPECT_TRUE(b.IsNull()); EXPECT_TRUE(c.IsObject()); @@ -576,17 +576,17 @@ TYPED_TEST(DocumentMove, MoveAssignment) { TYPED_TEST(DocumentMove, MoveAssignmentParseError) { typedef TypeParam Allocator; - typedef GenericDocument, Allocator> Document; + typedef GenericDocument, Allocator> D; ParseResult noError; - Document a; + D a; a.Parse("{ 4 = 4]"); ParseResult error(a.GetParseError(), a.GetErrorOffset()); EXPECT_TRUE(a.HasParseError()); EXPECT_NE(error.Code(), noError.Code()); EXPECT_NE(error.Offset(), noError.Offset()); - Document b; + D b; b = std::move(a); EXPECT_FALSE(a.HasParseError()); EXPECT_TRUE(b.HasParseError()); @@ -595,7 +595,7 @@ TYPED_TEST(DocumentMove, MoveAssignmentParseError) { EXPECT_EQ(a.GetErrorOffset(), noError.Offset()); EXPECT_EQ(b.GetErrorOffset(), error.Offset()); - Document c; + D c; c = std::move(b); EXPECT_FALSE(b.HasParseError()); EXPECT_TRUE(c.HasParseError()); @@ -612,9 +612,9 @@ TYPED_TEST(DocumentMove, MoveAssignmentParseError) { TYPED_TEST(DocumentMove, MoveAssignmentStack) { typedef TypeParam Allocator; typedef UTF8<> Encoding; - typedef GenericDocument Document; + typedef GenericDocument D; - Document a; + D a; size_t defaultCapacity = a.GetStackCapacity(); // Trick Document into getting GetStackCapacity() to return non-zero @@ -625,12 +625,12 @@ TYPED_TEST(DocumentMove, MoveAssignmentStack) { size_t capacity = a.GetStackCapacity(); EXPECT_GT(capacity, 0u); - Document b; + D b; b = std::move(a); EXPECT_EQ(a.GetStackCapacity(), defaultCapacity); EXPECT_EQ(b.GetStackCapacity(), capacity); - Document c; + D c; c = std::move(b); EXPECT_EQ(b.GetStackCapacity(), defaultCapacity); EXPECT_EQ(c.GetStackCapacity(), capacity); diff --git a/test/unittest/istreamwrappertest.cpp b/test/unittest/istreamwrappertest.cpp index f6b0fa9..28c756c 100644 --- a/test/unittest/istreamwrappertest.cpp +++ b/test/unittest/istreamwrappertest.cpp @@ -20,6 +20,11 @@ #include #include +#ifdef _MSC_VER +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(4702) // unreachable code +#endif + using namespace rapidjson; using namespace std; @@ -168,4 +173,8 @@ TEST(IStreamWrapper, wfstream) { EXPECT_EQ(5, d.MemberCount()); } -#endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS +#endif + +#ifdef _MSC_VER +RAPIDJSON_DIAG_POP +#endif diff --git a/test/unittest/readertest.cpp b/test/unittest/readertest.cpp index 3f11fec..329af2a 100644 --- a/test/unittest/readertest.cpp +++ b/test/unittest/readertest.cpp @@ -720,7 +720,7 @@ TEST(Reader, ParseString_Error) { TEST_STRINGENCODING_ERROR(UTF32<>, UTF8<>, unsigned, ARRAY('[', '\"', 0x110000, '\"', ']', '\0')); // Malform ASCII sequence - TEST_STRINGENCODING_ERROR(ASCII<>, UTF8<>, char, ARRAY('[', '\"', char(0x80), '\"', ']', '\0')); + TEST_STRINGENCODING_ERROR(ASCII<>, UTF8<>, char, ARRAY('[', '\"', char(0x80u), '\"', ']', '\0')); #undef ARRAY #undef TEST_STRINGARRAY_ERROR diff --git a/test/unittest/valuetest.cpp b/test/unittest/valuetest.cpp index d6c7492..aac0a44 100644 --- a/test/unittest/valuetest.cpp +++ b/test/unittest/valuetest.cpp @@ -94,23 +94,23 @@ TEST(Value, Traits) { #endif TEST(Value, MoveConstructor) { - typedef GenericValue, CrtAllocator> Value; - Value::AllocatorType allocator; + typedef GenericValue, CrtAllocator> V; + V::AllocatorType allocator; - Value x((Value(kArrayType))); + V x((V(kArrayType))); x.Reserve(4u, allocator); x.PushBack(1, allocator).PushBack(2, allocator).PushBack(3, allocator).PushBack(4, allocator); EXPECT_TRUE(x.IsArray()); EXPECT_EQ(4u, x.Size()); // Value y(x); // does not compile (!is_copy_constructible) - Value y(std::move(x)); + V y(std::move(x)); EXPECT_TRUE(x.IsNull()); EXPECT_TRUE(y.IsArray()); EXPECT_EQ(4u, y.Size()); // Value z = y; // does not compile (!is_copy_assignable) - Value z = std::move(y); + V z = std::move(y); EXPECT_TRUE(y.IsNull()); EXPECT_TRUE(z.IsArray()); EXPECT_EQ(4u, z.Size()); From 8991037ecd1a3a2bec97813b61c2a1993ad7fb6d Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Tue, 5 Apr 2016 23:26:08 +0800 Subject: [PATCH 017/305] Revert using of static const back to enum due to gcc error --- include/rapidjson/document.h | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h index 0ce2d2a..e23e1ad 100644 --- a/include/rapidjson/document.h +++ b/include/rapidjson/document.h @@ -27,6 +27,7 @@ #ifdef _MSC_VER RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_OFF(4127) // conditional expression is constant +RAPIDJSON_DIAG_OFF(4244) // conversion from kXxxFlags to 'uint16_t', possible loss of data #endif #ifdef __clang__ @@ -1794,17 +1795,17 @@ private: template friend class GenericValue; template friend class GenericDocument; - static const uint16_t - kBoolFlag = 0x0008, - kNumberFlag = 0x0010, - kIntFlag = 0x0020, - kUintFlag = 0x0040, - kInt64Flag = 0x0080, - kUint64Flag = 0x0100, - kDoubleFlag = 0x0200, - kStringFlag = 0x0400, - kCopyFlag = 0x0800, - kInlineStrFlag = 0x1000, + enum { + kBoolFlag = 0x0008, + kNumberFlag = 0x0010, + kIntFlag = 0x0020, + kUintFlag = 0x0040, + kInt64Flag = 0x0080, + kUint64Flag = 0x0100, + kDoubleFlag = 0x0200, + kStringFlag = 0x0400, + kCopyFlag = 0x0800, + kInlineStrFlag = 0x1000, // Initial flags of different types. kNullFlag = kNullType, @@ -1822,7 +1823,8 @@ private: kObjectFlag = kObjectType, kArrayFlag = kArrayType, - kTypeMask = 0x07; + kTypeMask = 0x07 + }; static const SizeType kDefaultArrayCapacity = 16; static const SizeType kDefaultObjectCapacity = 16; From 35ccca8b7430f8b354142131a632f542cf162206 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Tue, 5 Apr 2016 23:56:50 +0800 Subject: [PATCH 018/305] Try to fix VC warning C4512 --- include/rapidjson/document.h | 2 ++ include/rapidjson/encodedstream.h | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h index e23e1ad..dda799c 100644 --- a/include/rapidjson/document.h +++ b/include/rapidjson/document.h @@ -322,6 +322,8 @@ private: //! Disallow construction from non-const array template GenericStringRef(CharType (&str)[N]) /* = delete */; + + GenericStringRef& operator=(const GenericStringRef&); }; //! Mark a character pointer as constant string diff --git a/include/rapidjson/encodedstream.h b/include/rapidjson/encodedstream.h index c402e5c..c12caac 100644 --- a/include/rapidjson/encodedstream.h +++ b/include/rapidjson/encodedstream.h @@ -84,6 +84,10 @@ public: Ch* PutBegin() { return 0; } size_t PutEnd(Ch*) { return 0; } +private: + EncodedInputStream(const EncodedInputStream&); + EncodedInputStream& operator=(const EncodedInputStream&); + MemoryStream& is_; }; From 689be10891d4ab03853edda4f08db74e7d83e8e0 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Wed, 6 Apr 2016 00:11:49 +0800 Subject: [PATCH 019/305] Fix a compilation error --- include/rapidjson/encodedstream.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/rapidjson/encodedstream.h b/include/rapidjson/encodedstream.h index c12caac..1450683 100644 --- a/include/rapidjson/encodedstream.h +++ b/include/rapidjson/encodedstream.h @@ -84,11 +84,11 @@ public: Ch* PutBegin() { return 0; } size_t PutEnd(Ch*) { return 0; } + MemoryStream& is_; + private: EncodedInputStream(const EncodedInputStream&); EncodedInputStream& operator=(const EncodedInputStream&); - - MemoryStream& is_; }; //! Output byte stream wrapper with statically bound encoding. From be5a886f8fedf6f963d277bba32e57ae6d232d76 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Wed, 6 Apr 2016 00:34:45 +0800 Subject: [PATCH 020/305] Fix clang compilation error --- include/rapidjson/document.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h index dda799c..2d9bfe6 100644 --- a/include/rapidjson/document.h +++ b/include/rapidjson/document.h @@ -312,6 +312,10 @@ struct GenericStringRef { GenericStringRef(const CharType* str, SizeType len) : s(str), length(len) { RAPIDJSON_ASSERT(s != 0); } + GenericStringRef(const GenericStringRef& rhs) : s(rhs.s), length(rhs.length) {} + + GenericStringRef& operator=(const GenericStringRef& rhs) { s = rhs.s; length = rhs.length; } + //! implicit conversion to plain CharType pointer operator const Ch *() const { return s; } @@ -322,8 +326,6 @@ private: //! Disallow construction from non-const array template GenericStringRef(CharType (&str)[N]) /* = delete */; - - GenericStringRef& operator=(const GenericStringRef&); }; //! Mark a character pointer as constant string From 44d114f3ee7067380a234903997fb736a2787682 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Wed, 6 Apr 2016 00:47:16 +0800 Subject: [PATCH 021/305] Supress VC C4512 warning --- include/rapidjson/internal/regex.h | 9 +++++++++ include/rapidjson/pointer.h | 9 +++++++++ include/rapidjson/schema.h | 9 +++++++++ 3 files changed, 27 insertions(+) diff --git a/include/rapidjson/internal/regex.h b/include/rapidjson/internal/regex.h index aeb0e3e..d317daa 100644 --- a/include/rapidjson/internal/regex.h +++ b/include/rapidjson/internal/regex.h @@ -31,6 +31,11 @@ RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_OFF(effc++) #endif +#ifdef _MSC_VER +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(4512) // assignment operator could not be generated +#endif + #ifndef RAPIDJSON_REGEX_VERBOSE #define RAPIDJSON_REGEX_VERBOSE 0 #endif @@ -693,4 +698,8 @@ RAPIDJSON_NAMESPACE_END RAPIDJSON_DIAG_POP #endif +#ifdef _MSC_VER +RAPIDJSON_DIAG_POP +#endif + #endif // RAPIDJSON_INTERNAL_REGEX_H_ diff --git a/include/rapidjson/pointer.h b/include/rapidjson/pointer.h index 9444938..c985277 100644 --- a/include/rapidjson/pointer.h +++ b/include/rapidjson/pointer.h @@ -23,6 +23,11 @@ RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_OFF(switch-enum) #endif +#ifdef _MSC_VER +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(4512) // assignment operator could not be generated +#endif + RAPIDJSON_NAMESPACE_BEGIN static const SizeType kPointerInvalidIndex = ~SizeType(0); //!< Represents an invalid index in GenericPointer::Token @@ -1342,4 +1347,8 @@ RAPIDJSON_NAMESPACE_END RAPIDJSON_DIAG_POP #endif +#ifdef _MSC_VER +RAPIDJSON_DIAG_POP +#endif + #endif // RAPIDJSON_POINTER_H_ diff --git a/include/rapidjson/schema.h b/include/rapidjson/schema.h index acbae36..e12e7d2 100644 --- a/include/rapidjson/schema.h +++ b/include/rapidjson/schema.h @@ -68,6 +68,11 @@ RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_OFF(variadic-macros) #endif +#ifdef _MSC_VER +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(4512) // assignment operator could not be generated +#endif + RAPIDJSON_NAMESPACE_BEGIN /////////////////////////////////////////////////////////////////////////////// @@ -2005,4 +2010,8 @@ RAPIDJSON_DIAG_POP RAPIDJSON_DIAG_POP #endif +#ifdef _MSC_VER +RAPIDJSON_DIAG_POP +#endif + #endif // RAPIDJSON_SCHEMA_H_ From 49c29d057d3025d02b975bc9ed07e22ca166d317 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Wed, 6 Apr 2016 01:16:00 +0800 Subject: [PATCH 022/305] Fix VC warning C4189 --- test/unittest/unittest.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/test/unittest/unittest.cpp b/test/unittest/unittest.cpp index e0e8576..b754563 100644 --- a/test/unittest/unittest.cpp +++ b/test/unittest/unittest.cpp @@ -35,6 +35,7 @@ int main(int argc, char **argv) { #ifdef _MSC_VER _CrtMemState memoryState = { 0 }; + (void)memoryState; _CrtMemCheckpoint(&memoryState); //_CrtSetBreakAlloc(X); //void *testWhetherMemoryLeakDetectionWorks = malloc(1); From 12425693ba255b8b8d68ba8ce752a23a25c2118f Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Wed, 6 Apr 2016 23:33:26 +0800 Subject: [PATCH 023/305] Revert formatting of enum --- include/rapidjson/document.h | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h index 2d9bfe6..d286eb1 100644 --- a/include/rapidjson/document.h +++ b/include/rapidjson/document.h @@ -1800,16 +1800,16 @@ private: template friend class GenericDocument; enum { - kBoolFlag = 0x0008, - kNumberFlag = 0x0010, - kIntFlag = 0x0020, - kUintFlag = 0x0040, - kInt64Flag = 0x0080, - kUint64Flag = 0x0100, - kDoubleFlag = 0x0200, - kStringFlag = 0x0400, - kCopyFlag = 0x0800, - kInlineStrFlag = 0x1000, + kBoolFlag = 0x0008, + kNumberFlag = 0x0010, + kIntFlag = 0x0020, + kUintFlag = 0x0040, + kInt64Flag = 0x0080, + kUint64Flag = 0x0100, + kDoubleFlag = 0x0200, + kStringFlag = 0x0400, + kCopyFlag = 0x0800, + kInlineStrFlag = 0x1000, // Initial flags of different types. kNullFlag = kNullType, From 47e21a054cafb2bbebc68053b34b5cc47a85acad Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Thu, 7 Apr 2016 00:46:39 +0800 Subject: [PATCH 024/305] Temp revert cmake for OS X --- test/unittest/CMakeLists.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/unittest/CMakeLists.txt b/test/unittest/CMakeLists.txt index 3630cfe..4e3b071 100644 --- a/test/unittest/CMakeLists.txt +++ b/test/unittest/CMakeLists.txt @@ -40,9 +40,9 @@ elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror -Wall -Wextra -Weffc++ -Wswitch-default -Wfloat-equal -Wimplicit-fallthrough -Weverything") # If the user is running a newer version of Clang that includes the # -Wdouble-promotion, we will ignore that warning. - if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 3.7) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-double-promotion") - endif() + # if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 3.7) + # set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-double-promotion") + # endif() elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") # Force to always compile with /W4 if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]") From 75d0e4ff652769309052bbbb3745da12a572af9a Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Thu, 7 Apr 2016 00:47:26 +0800 Subject: [PATCH 025/305] Use single Peek() in SkipWhitespace Fix #594 --- include/rapidjson/reader.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/rapidjson/reader.h b/include/rapidjson/reader.h index 6f45571..8882a5d 100644 --- a/include/rapidjson/reader.h +++ b/include/rapidjson/reader.h @@ -262,7 +262,8 @@ void SkipWhitespace(InputStream& is) { internal::StreamLocalCopy copy(is); InputStream& s(copy.s); - while (s.Peek() == ' ' || s.Peek() == '\n' || s.Peek() == '\r' || s.Peek() == '\t') + typename InputStream::Ch c; + while ((c = s.Peek()) == ' ' || c == '\n' || c == '\r' || c == '\t') s.Take(); } From d35c783ec696b64339efd78c3d7ba5c72f454988 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Fri, 15 Apr 2016 09:06:50 +0800 Subject: [PATCH 026/305] Fix schema documentation --- doc/schema.md | 52 +++++++++++++------------- doc/schema.zh-cn.md | 90 ++++++++++++++++++++++----------------------- 2 files changed, 71 insertions(+), 71 deletions(-) diff --git a/doc/schema.md b/doc/schema.md index 053fc23..6d66fa5 100644 --- a/doc/schema.md +++ b/doc/schema.md @@ -20,7 +20,7 @@ Secondly, construct a `SchemaValidator` with the `SchemaDocument`. It is similar // ... Document sd; -if (!sd.Parse(schemaJson)) { +if (!sd.Parse(schemaJson).HasParseError()) { // the schema is not a valid JSON. // ... } @@ -28,7 +28,7 @@ SchemaDocument schema(sd); // Compile a Document to SchemaDocument // sd is no longer needed here. Document d; -if (!d.Parse(inputJson)) { +if (!d.Parse(inputJson).HasParseError()) { // the input is not a valid JSON. // ... } @@ -184,30 +184,30 @@ RapidJSON implemented a simple NFA regular expression engine, which is used by d |Syntax|Description| |------|-----------| -|`ab` | Concatenation -|`a|b` | Alternation -|`a?` | Zero or one -|`a*` | Zero or more -|`a+` | One or more -|`a{3}` | Exactly 3 times -|`a{3,}` | At least 3 times -|`a{3,5}`| 3 to 5 times -|`(ab)` | Grouping -|`^a` | At the beginning -|`a$` | At the end -|`.` | Any character -|`[abc]` | Character classes -|`[a-c]` | Character class range -|`[a-z0-9_]` | Character class combination -|`[^abc]` | Negated character classes -|`[^a-c]` | Negated character class range -|`[\b]` | Backspace (U+0008) -|`\|`, `\\`, ... | Escape characters -|`\f` | Form feed (U+000C) -|`\n` | Line feed (U+000A) -|`\r` | Carriage return (U+000D) -|`\t` | Tab (U+0009) -|`\v` | Vertical tab (U+000B) +|`ab` | Concatenation | +|`a|b` | Alternation | +|`a?` | Zero or one | +|`a*` | Zero or more | +|`a+` | One or more | +|`a{3}` | Exactly 3 times | +|`a{3,}` | At least 3 times | +|`a{3,5}`| 3 to 5 times | +|`(ab)` | Grouping | +|`^a` | At the beginning | +|`a$` | At the end | +|`.` | Any character | +|`[abc]` | Character classes | +|`[a-c]` | Character class range | +|`[a-z0-9_]` | Character class combination | +|`[^abc]` | Negated character classes | +|`[^a-c]` | Negated character class range | +|`[\b]` | Backspace (U+0008) | +|`\|`, `\\`, ... | Escape characters | +|`\f` | Form feed (U+000C) | +|`\n` | Line feed (U+000A) | +|`\r` | Carriage return (U+000D) | +|`\t` | Tab (U+0009) | +|`\v` | Vertical tab (U+000B) | For C++11 compiler, it is also possible to use the `std::regex` by defining `RAPIDJSON_SCHEMA_USE_INTERNALREGEX=0` and `RAPIDJSON_SCHEMA_USE_STDREGEX=1`. If your schemas do not need `pattern` and `patternProperties`, you can set both macros to zero to disable this feature, which will reduce some code size. diff --git a/doc/schema.zh-cn.md b/doc/schema.zh-cn.md index dd0ec73..a60cd82 100644 --- a/doc/schema.zh-cn.md +++ b/doc/schema.zh-cn.md @@ -20,23 +20,23 @@ RapidJSON 实现了一个 [JSON Schema Draft v4](http://json-schema.org/document // ... Document sd; -if (!sd.Parse(schemaJson)) { - // the schema is not a valid JSON. +if (!sd.Parse(schemaJson).HasParseError()) { + // 此 schema 不是合法的 JSON // ... } -SchemaDocument schema(sd); // Compile a Document to SchemaDocument -// sd is no longer needed here. +SchemaDocument schema(sd); // 把一个 Document 编译至 SchemaDocument +// 之后不再需要 sd Document d; -if (!d.Parse(inputJson)) { - // the input is not a valid JSON. +if (!d.Parse(inputJson).HasParseError()) { + // 输入不是一个合法的 JSON // ... } SchemaValidator validator(schema); if (!d.Accept(validator)) { - // Input JSON is invalid according to the schema - // Output diagnostic information + // 输入的 JSON 不合乎 schema + // 打印诊断信息 StringBuffer sb; validator.GetInvalidSchemaPointer().StringifyUriFragment(sb); printf("Invalid schema: %s\n", sb.GetString()); @@ -49,8 +49,8 @@ if (!d.Accept(validator)) { 一些注意点: -* 一个 `SchemaDocment` 能被多个 `SchemaValidator` 吊用。它不会被 `SchemaValidator` 修改。 -* 一个 `SchemaValidator` 可以重复使用来校验多个文件。在校验其他文件前,先调用 `validator.Reset()`。 +* 一个 `SchemaDocment` 能被多个 `SchemaValidator` 引用。它不会被 `SchemaValidator` 修改。 +* 可以重复使用一个 `SchemaValidator` 来校验多个文件。在校验其他文件前,须先调用 `validator.Reset()`。 ## 在解析/生成时进行校验 @@ -64,28 +64,28 @@ if (!d.Accept(validator)) { #include "rapidjson/filereadstream.h" // ... -SchemaDocument schema(sd); // Compile a Document to SchemaDocument +SchemaDocument schema(sd); // 把一个 Document 编译至 SchemaDocument -// Use reader to parse the JSON +// 使用 reader 解析 JSON FILE* fp = fopen("big.json", "r"); FileReadStream is(fp, buffer, sizeof(buffer)); -// Parse JSON from reader, validate the SAX events, and store in d. +// 用 reader 解析 JSON,校验它的 SAX 事件,并存储至 d Document d; SchemaValidatingReader > reader(is, schema); d.Populate(reader); if (!reader.GetParseResult()) { - // Not a valid JSON - // When reader.GetParseResult().Code() == kParseErrorTermination, - // it may be terminated by: - // (1) the validator found that the JSON is invalid according to schema; or - // (2) the input stream has I/O error. + // 不是一个合法的 JSON + // 当 reader.GetParseResult().Code() == kParseErrorTermination, + // 它可能是被以下原因中止: + // (1) 校验器发现 JSON 不合乎 schema;或 + // (2) 输入流有 I/O 错误。 - // Check the validation result + // 检查校验结果 if (!reader.IsValid()) { - // Input JSON is invalid according to the schema - // Output diagnostic information + // 输入的 JSON 不合乎 schema + // 打印诊断信息 StringBuffer sb; reader.GetInvalidSchemaPointer().StringifyUriFragment(sb); printf("Invalid schema: %s\n", sb.GetString()); @@ -184,30 +184,30 @@ RapidJSON 实现了一个简单的 NFA 正则表达式引擎,并预设使用 |语法|描述| |------|-----------| -|`ab` | 串联 -|`a|b` | 交替 -|`a?` | 零或一次 -|`a*` | 零或多次 -|`a+` | 一或多次 -|`a{3}` | 刚好 3 次 -|`a{3,}` | 至少 3 次 -|`a{3,5}`| 3 至 5 次 -|`(ab)` | 分组 -|`^a` | 在开始处 -|`a$` | 在结束处 -|`.` | 任何字符 -|`[abc]` | 字符组 -|`[a-c]` | 字符组范围 -|`[a-z0-9_]` | 字符组组合 -|`[^abc]` | 字符组取反 -|`[^a-c]` | 字符组范围取反 -|`[\b]` | 退格符 (U+0008) -|`\|`, `\\`, ... | 转义字符 -|`\f` | 馈页 (U+000C) -|`\n` | 馈行 (U+000A) -|`\r` | 回车 (U+000D) -|`\t` | 制表 (U+0009) -|`\v` | 垂直制表 (U+000B) +|`ab` | 串联 | +|`a|b` | 交替 | +|`a?` | 零或一次 | +|`a*` | 零或多次 | +|`a+` | 一或多次 | +|`a{3}` | 刚好 3 次 | +|`a{3,}` | 至少 3 次 | +|`a{3,5}`| 3 至 5 次 | +|`(ab)` | 分组 | +|`^a` | 在开始处 | +|`a$` | 在结束处 | +|`.` | 任何字符 | +|`[abc]` | 字符组 | +|`[a-c]` | 字符组范围 | +|`[a-z0-9_]` | 字符组组合 | +|`[^abc]` | 字符组取反 | +|`[^a-c]` | 字符组范围取反 | +|`[\b]` | 退格符 (U+0008) | +|`\|`, `\\`, ... | 转义字符 | +|`\f` | 馈页 (U+000C) | +|`\n` | 馈行 (U+000A) | +|`\r` | 回车 (U+000D) | +|`\t` | 制表 (U+0009) | +|`\v` | 垂直制表 (U+000B) | 对于使用 C++11 编译器的使用者,也可使用 `std::regex`,只需定义 `RAPIDJSON_SCHEMA_USE_INTERNALREGEX=0` 及 `RAPIDJSON_SCHEMA_USE_STDREGEX=1`。若你的 schema 无需使用 `pattern` 或 `patternProperties`,可以把两个宏都设为零,以禁用此功能,这样做可节省一些代码体积。 From 6bc606c9260fd5038f0907a929e636032acf8b9a Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Fri, 15 Apr 2016 10:09:27 +0800 Subject: [PATCH 027/305] Update changelog --- CHANGELOG.md | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ad9b3c..c70a6e3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,63 @@ This project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] +### Added +* Add GenericDocument ctor overload to specify JSON type (#369) +* Add FAQ (#372, #373, #374, #376) +* Add @PlatformIO Library Registry manifest file (#400) +* Implement assignment operator for BigInteger (#404) +* Add comments support (#443) +* Adding coapp definition (#460) +* documenttest.cpp: EXPECT_THROW when checking empty allocator (470) +* GenericDocument: add implicit conversion to ParseResult (#480) +* Use with C++ linkage on Windows ARM (#485) +* Detect little endian for Microsoft ARM targets +* Check Nan/Inf when writing a double (#510) +* Add JSON Schema Implementation (#522) +* Add iostream wrapper (#530) +* Add Jsonx example for converting JSON into JSONx (a XML format) (#531) +* Add optional unresolvedTokenIndex parameter to Pointer::Get() (#532) +* Add encoding validation option for Writer/PrettyWriter (#534) +* Add Writer::SetMaxDecimalPlaces() (#536) +* Support {0, } and {0, m} in Regex (#539) +* Add Value::Get/SetFloat(), Value::IsLossLessFloat/Double() (#540) +* Add stream position check to reader unit tests (#541) +* Add Templated accessors and range-based for (#542) +* Add (Pretty)Writer::RawValue() (#543) +* Add Document::Parse(std::string), Document::Parse(const char*, size_t length) and related APIs. (#553) +* Add move constructor for GenericSchemaDocument (#554) +* Add VS2010 and VS2015 to AppVeyor CI (#555) +* Add parse-by-parts example (#556, #562) +* Support parse number as string (#564, #589) +* Add kFormatSingleLineArray for PrettyWriter (#577) +* Added optional support for trailing commas #584 + +### Fixed +* Fix gcc/clang/vc warnings (#350, #394, #397, #444, #447, #473, #515, #582, #589, #595) +* Fix documentation (#482, #511, #550, #557) +* Fix emscripten alignment issue (#535) +* Fix missing allocator to uses of AddMember in document (#365) +* CMake will no longer complain that the minimum CMake version is not specified (#501) +* Make it usable with old VC8 (VS2005) (#383) +* Prohibit C++11 move from Document to Value (#391) +* Try to fix incorrect 64-bit alignment (#419) +* Check return of fwrite to avoid warn_unused_result build failures (#421) +* Fix UB in GenericDocument::ParseStream (#426) +* Keep Document value unchanged on parse error (#439) +* Add missing return statement (#450) +* Fix Document::Parse(const Ch*) for transcoding (#478) +* encodings.h: fix typo in preprocessor condition (#495) +* Custom Microsoft headers are necessary only for Visual Studio 2012 and lower (#559) +* + +### Changed +* Clarify problematic JSON license (#392) +* Move Travis to container based infrastructure (#504, #558) +* Make whitespace array more compact (#513) +* Optimize Writer::WriteString() with SIMD (#544) +* x86-64 48-bit pointer optimization for GenericValue (#546) + + ## [1.0.2] - 2015-05-14 ### Added @@ -12,6 +69,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). ### Fixed * Include rapidjson.h for all internal/error headers. * Parsing some numbers incorrectly in full-precision mode (`kFullPrecisionParseFlag`) (#342) +* Fix some numbers parsed incorrectly (#336) * Fix alignment of 64bit platforms (#328) * Fix MemoryPoolAllocator::Clear() to clear user-buffer (0691502573f1afd3341073dd24b12c3db20fbde4) From 006533cdea3f0872917642dcc4f435324199ed29 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Fri, 15 Apr 2016 10:18:16 +0800 Subject: [PATCH 028/305] Added documentation about kParseTrailingCommasFlag --- doc/dom.zh-cn.md | 1 + doc/features.md | 3 ++- doc/features.zh-cn.md | 3 ++- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/doc/dom.zh-cn.md b/doc/dom.zh-cn.md index 2adf343..30266a3 100644 --- a/doc/dom.zh-cn.md +++ b/doc/dom.zh-cn.md @@ -116,6 +116,7 @@ GenericDocument& GenericDocument::Parse(const Ch* str); `kParseStopWhenDoneFlag` | 当从流解析了一个完整的JSON根节点之后,停止继续处理余下的流。当使用了此标志,解析器便不会产生`kParseErrorDocumentRootNotSingular`错误。可使用本标志去解析同一个流里的多个JSON。 `kParseFullPrecisionFlag` | 使用完整的精确度去解析数字(较慢)。如不设置此标节,则会使用正常的精确度(较快)。正常精确度会有最多3个[ULP](http://en.wikipedia.org/wiki/Unit_in_the_last_place)的误差。 `kParseCommentsFlag` | 容许单行 `// ...` 及多行 `/* ... */` 注释(放宽的JSON语法)。 +`kParseTrailingCommasFlag` | 容许在对象和数组结束前含有逗号(放宽的JSON语法)。 由于使用了非类型模板参数,而不是函数参数,C++编译器能为个别组合生成代码,以改善性能及减少代码尺寸(当只用单种特化)。缺点是需要在编译期决定标志。 diff --git a/doc/features.md b/doc/features.md index 6b529a7..f092cf1 100644 --- a/doc/features.md +++ b/doc/features.md @@ -24,7 +24,8 @@ * Support null character (`"\u0000"`) * For example, `["Hello\u0000World"]` can be parsed and handled gracefully. There is API for getting/setting lengths of string. * Support optional relaxed syntax. - * Single line (`// ...`) and multiple line (`/* ... */`) comments. + * Single line (`// ...`) and multiple line (`/* ... */`) comments (`kParseCommentsFlag`). + * Trailing commas at the end of objects and arrays (`kParseTrailingCommasFlag`). ## Unicode diff --git a/doc/features.zh-cn.md b/doc/features.zh-cn.md index 85a7db1..772d0d4 100644 --- a/doc/features.zh-cn.md +++ b/doc/features.zh-cn.md @@ -24,7 +24,8 @@ * 支持空字符(`"\u0000"`)。 * 例如,可以优雅地解析及处理`["Hello\u0000World"]`。含读写字符串长度的API。 * 支持放宽的可选语法 - * 单行(`// ...`)及多行(`/* ... */`) 注释。 + * 单行(`// ...`)及多行(`/* ... */`) 注释(`kParseCommentsFlag`)。 + * 在对象和数组结束前含逗号(`kParseTrailingCommasFlag`)。 ## Unicode From f4ea0d3f640fc29e857cc2f187cef75bc2a035a5 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Fri, 15 Apr 2016 10:43:14 +0800 Subject: [PATCH 029/305] Added documentation for kParseNumbersAsStringsFlag --- doc/dom.md | 1 + doc/dom.zh-cn.md | 1 + doc/sax.md | 4 +++- doc/sax.zh-cn.md | 3 ++- 4 files changed, 7 insertions(+), 2 deletions(-) diff --git a/doc/dom.md b/doc/dom.md index 79b6817..6cccf08 100644 --- a/doc/dom.md +++ b/doc/dom.md @@ -116,6 +116,7 @@ Parse flags | Meaning `kParseStopWhenDoneFlag` | After parsing a complete JSON root from stream, stop further processing the rest of stream. When this flag is used, parser will not generate `kParseErrorDocumentRootNotSingular` error. Using this flag for parsing multiple JSONs in the same stream. `kParseFullPrecisionFlag` | Parse number in full precision (slower). If this flag is not set, the normal precision (faster) is used. Normal precision has maximum 3 [ULP](http://en.wikipedia.org/wiki/Unit_in_the_last_place) error. `kParseCommentsFlag` | Allow one-line `// ...` and multi-line `/* ... */` comments (relaxed JSON syntax). +`kParseNumbersAsStringsFlag` | Parse numerical type values as strings. `kParseTrailingCommasFlag` | Allow trailing commas at the end of objects and arrays (relaxed JSON syntax). By using a non-type template parameter, instead of a function parameter, C++ compiler can generate code which is optimized for specified combinations, improving speed, and reducing code size (if only using a single specialization). The downside is the flags needed to be determined in compile-time. diff --git a/doc/dom.zh-cn.md b/doc/dom.zh-cn.md index 30266a3..df6815e 100644 --- a/doc/dom.zh-cn.md +++ b/doc/dom.zh-cn.md @@ -116,6 +116,7 @@ GenericDocument& GenericDocument::Parse(const Ch* str); `kParseStopWhenDoneFlag` | 当从流解析了一个完整的JSON根节点之后,停止继续处理余下的流。当使用了此标志,解析器便不会产生`kParseErrorDocumentRootNotSingular`错误。可使用本标志去解析同一个流里的多个JSON。 `kParseFullPrecisionFlag` | 使用完整的精确度去解析数字(较慢)。如不设置此标节,则会使用正常的精确度(较快)。正常精确度会有最多3个[ULP](http://en.wikipedia.org/wiki/Unit_in_the_last_place)的误差。 `kParseCommentsFlag` | 容许单行 `// ...` 及多行 `/* ... */` 注释(放宽的JSON语法)。 +`kParseNumbersAsStringsFlag` | 把数字类型解析成字符串。 `kParseTrailingCommasFlag` | 容许在对象和数组结束前含有逗号(放宽的JSON语法)。 由于使用了非类型模板参数,而不是函数参数,C++编译器能为个别组合生成代码,以改善性能及减少代码尺寸(当只用单种特化)。缺点是需要在编译期决定标志。 diff --git a/doc/sax.md b/doc/sax.md index 9a6d814..9d4f202 100644 --- a/doc/sax.md +++ b/doc/sax.md @@ -106,6 +106,7 @@ class Handler { bool Int64(int64_t i); bool Uint64(uint64_t i); bool Double(double d); + bool RawNumber(const Ch* str, SizeType length, bool copy); bool String(const Ch* str, SizeType length, bool copy); bool StartObject(); bool Key(const Ch* str, SizeType length, bool copy); @@ -119,7 +120,7 @@ class Handler { `Bool(bool)` is called when the `Reader` encounters a JSON true or false value. -When the `Reader` encounters a JSON number, it chooses a suitable C++ type mapping. And then it calls *one* function out of `Int(int)`, `Uint(unsigned)`, `Int64(int64_t)`, `Uint64(uint64_t)` and `Double(double)`. +When the `Reader` encounters a JSON number, it chooses a suitable C++ type mapping. And then it calls *one* function out of `Int(int)`, `Uint(unsigned)`, `Int64(int64_t)`, `Uint64(uint64_t)` and `Double(double)`. If `kParseNumbersAsStrings` is enabled, `Reader` will always calls `RawNumber()` instead. `String(const char* str, SizeType length, bool copy)` is called when the `Reader` encounters a string. The first parameter is pointer to the string. The second parameter is the length of the string (excluding the null terminator). Note that RapidJSON supports null character `'\0'` inside a string. If such situation happens, `strlen(str) < length`. The last `copy` indicates whether the handler needs to make a copy of the string. For normal parsing, `copy = true`. Only when *insitu* parsing is used, `copy = false`. And beware that, the character type depends on the target encoding, which will be explained later. @@ -419,6 +420,7 @@ struct CapitalizeFilter { bool Int64(int64_t i) { return out_.Int64(i); } bool Uint64(uint64_t u) { return out_.Uint64(u); } bool Double(double d) { return out_.Double(d); } + bool RawNumber(const char* str, SizeType length, bool copy) { return out_.RawNumber(str, length, copy); } bool String(const char* str, SizeType length, bool) { buffer_.clear(); for (SizeType i = 0; i < length; i++) diff --git a/doc/sax.zh-cn.md b/doc/sax.zh-cn.md index f8dc7b9..47306f6 100644 --- a/doc/sax.zh-cn.md +++ b/doc/sax.zh-cn.md @@ -119,7 +119,7 @@ class Handler { 当`Reader`遇到JSON true或false值时会调用`Bool(bool)`。 -当`Reader`遇到JSON number,它会选择一个合适的C++类型映射,然后调用`Int(int)`、`Uint(unsigned)`、`Int64(int64_t)`、`Uint64(uint64_t)`及`Double(double)`的*其中之一个*。 +当`Reader`遇到JSON number,它会选择一个合适的C++类型映射,然后调用`Int(int)`、`Uint(unsigned)`、`Int64(int64_t)`、`Uint64(uint64_t)`及`Double(double)`的*其中之一个*。 若开启了 `kParseNumbersAsStrings` 选项,`Reader` 便会改为调用 `RawNumber()`。 当`Reader`遇到JSON string,它会调用`String(const char* str, SizeType length, bool copy)`。第一个参数是字符串的指针。第二个参数是字符串的长度(不包含空终止符号)。注意RapidJSON支持字串中含有空字符`'\0'`。若出现这种情况,便会有`strlen(str) < length`。最后的`copy`参数表示处理器是否需要复制该字符串。在正常解析时,`copy = true`。仅当使用原位解析时,`copy = false`。此外,还要注意字符的类型与目标编码相关,我们稍后会再谈这一点。 @@ -419,6 +419,7 @@ struct CapitalizeFilter { bool Int64(int64_t i) { return out_.Int64(i); } bool Uint64(uint64_t u) { return out_.Uint64(u); } bool Double(double d) { return out_.Double(d); } + bool RawNumber(const char* str, SizeType length, bool copy) { return out_.RawNumber(str, length, copy); } bool String(const char* str, SizeType length, bool) { buffer_.clear(); for (SizeType i = 0; i < length; i++) From 105c92ee08b6008ce7db1130dbe1020551e80e07 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Fri, 15 Apr 2016 11:18:00 +0800 Subject: [PATCH 030/305] Add example catalog in readme --- readme.md | 21 ++++++++++++++++++++- readme.zh-cn.md | 21 ++++++++++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/readme.md b/readme.md index 9a3d6a7..d7675bc 100644 --- a/readme.md +++ b/readme.md @@ -126,4 +126,23 @@ The following diagram shows the process. ![simpledom](doc/diagram/simpledom.png) -More [examples](https://github.com/miloyip/rapidjson/tree/master/example) are available. +More [examples](https://github.com/miloyip/rapidjson/tree/master/example) are available: + +* DOM API + * [tutorial](https://github.com/miloyip/rapidjson/blob/master/example/tutorial/tutorial.cpp): Basic usage of DOM API. + +* SAX API + * [simplereader](https://github.com/miloyip/rapidjson/blob/master/example/simplereader/simplereader.cpp): Dumps all SAX events while parsing a JSON by `Reader`. + * [condense](https://github.com/miloyip/rapidjson/blob/master/example/condense/condense.cpp): A command line tool to rewrite a JSON, with all whitespaces removed. + * [pretty](https://github.com/miloyip/rapidjson/blob/master/example/pretty/pretty.cpp): A command line tool to rewrite a JSON with indents and newlines by `PrettyWriter`. + * [capitalize](https://github.com/miloyip/rapidjson/blob/master/example/capitalize/capitalize.cpp): A command line tool to capitalize strings in JSON. + * [messagereader](https://github.com/miloyip/rapidjson/blob/master/example/messagereader/messagereader.cpp): Parse a JSON message with SAX API. + * [serialize](https://github.com/miloyip/rapidjson/blob/master/example/serialize/serialize.cpp): Serialize a C++ object into JSON with SAX API. + * [jsonx](https://github.com/miloyip/rapidjson/blob/master/example/jsonx/jsonx.cpp): Implements a `JsonxWriter` which stringify SAX events into [JSONx](https://www-01.ibm.com/support/knowledgecenter/SS9H2Y_7.1.0/com.ibm.dp.doc/json_jsonx.html) (a kind of XML) format. The example is a command line tool which converts input JSON into JSONx format. + +* Schema + * [schemavalidator](https://github.com/miloyip/rapidjson/blob/master/example/schemavalidator/schemavalidator.cpp) : A command line tool to validate a JSON with a JSON schema. + +* Advanced + * [prettyauto](https://github.com/miloyip/rapidjson/blob/master/example/prettyauto/prettyauto.cpp): A modified version of [pretty](https://github.com/miloyip/rapidjson/blob/master/example/pretty/pretty.cpp) to automatically handle JSON with any UTF encodings. + * [parsebyparts](https://github.com/miloyip/rapidjson/blob/master/example/parsebyparts/parsebyparts.cpp): Implements an `AsyncDocumentParser` which can parse JSON in parts, using C++11 thread. diff --git a/readme.zh-cn.md b/readme.zh-cn.md index 5124f8e..3e4c9a3 100644 --- a/readme.zh-cn.md +++ b/readme.zh-cn.md @@ -118,4 +118,23 @@ int main() { ![simpledom](doc/diagram/simpledom.png) -还有许多[例子](https://github.com/miloyip/rapidjson/tree/master/example)可供参考。 +还有许多[例子](https://github.com/miloyip/rapidjson/tree/master/example)可供参考: + +* DOM API + * [tutorial](https://github.com/miloyip/rapidjson/blob/master/example/tutorial/tutorial.cpp): DOM API 的基本使用方法。 + +* SAX API + * [simplereader](https://github.com/miloyip/rapidjson/blob/master/example/simplereader/simplereader.cpp): 使用 `Reader` 解析 JSON 时,打印所有 SAX 事件。 + * [condense](https://github.com/miloyip/rapidjson/blob/master/example/condense/condense.cpp): 移除 JSON 中所有空白符的命令行工具。 + * [pretty](https://github.com/miloyip/rapidjson/blob/master/example/pretty/pretty.cpp): 为 JSON 加入缩进与换行的命令行工具,当中使用了 `PrettyWriter`。 + * [capitalize](https://github.com/miloyip/rapidjson/blob/master/example/capitalize/capitalize.cpp): 把 JSON 中所有字符串改为大写的命令行工具。 + * [messagereader](https://github.com/miloyip/rapidjson/blob/master/example/messagereader/messagereader.cpp): 使用 SAX API 去解析一个 JSON 报文。 + * [serialize](https://github.com/miloyip/rapidjson/blob/master/example/serialize/serialize.cpp): 使用 SAX API 去序列化 C++ 对象,生成 JSON。 + * [jsonx](https://github.com/miloyip/rapidjson/blob/master/example/jsonx/jsonx.cpp): 实现了一个 `JsonxWriter`,它能把 SAX 事件写成 [JSONx](https://www-01.ibm.com/support/knowledgecenter/SS9H2Y_7.1.0/com.ibm.dp.doc/json_jsonx.html)(一种 XML)格式。这个例子是把 JSON 输入转换成 JSONx 格式的命令行工具。 + +* Schema API + * [schemavalidator](https://github.com/miloyip/rapidjson/blob/master/example/schemavalidator/schemavalidator.cpp): 使用 JSON Schema 去校验 JSON 的命令行工具。 + +* 进阶 + * [prettyauto](https://github.com/miloyip/rapidjson/blob/master/example/prettyauto/prettyauto.cpp): [pretty](https://github.com/miloyip/rapidjson/blob/master/example/pretty/pretty.cpp) 的修改版本,可自动处理任何 UTF 编码的 JSON。 + * [parsebyparts](https://github.com/miloyip/rapidjson/blob/master/example/parsebyparts/parsebyparts.cpp): 这例子中的`AsyncDocumentParser` 类使用 C++ 线程来逐段解析 JSON。 From 1bfa188d18785062179d0e760aca27e7af00b743 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Fri, 15 Apr 2016 13:50:24 +0800 Subject: [PATCH 031/305] Improve encodings coverage --- test/unittest/encodingstest.cpp | 25 ++++++++++++++++ test/unittest/writertest.cpp | 52 ++++++++++++++++++++++++--------- 2 files changed, 63 insertions(+), 14 deletions(-) diff --git a/test/unittest/encodingstest.cpp b/test/unittest/encodingstest.cpp index b3cbb76..4104880 100644 --- a/test/unittest/encodingstest.cpp +++ b/test/unittest/encodingstest.cpp @@ -423,3 +423,28 @@ TEST(EncodingsTest, UTF32) { } } } + +TEST(EncodingsTest, ASCII) { + StringBuffer os, os2; + for (unsigned codepoint = 0; codepoint < 128; codepoint++) { + os.Clear(); + ASCII<>::Encode(os, codepoint); + const ASCII<>::Ch* encodedStr = os.GetString(); + { + StringStream is(encodedStr); + unsigned decodedCodepoint; + bool result = ASCII<>::Decode(is, &decodedCodepoint); + if (!result || codepoint != decodedCodepoint) + std::cout << std::hex << codepoint << " " << decodedCodepoint << std::endl; + } + + // Validate + { + StringStream is(encodedStr); + os2.Clear(); + bool result = ASCII<>::Validate(is, os2); + EXPECT_TRUE(result); + EXPECT_EQ(0, StrCmp(encodedStr, os2.GetString())); + } + } +} diff --git a/test/unittest/writertest.cpp b/test/unittest/writertest.cpp index 238aa79..4e08d7e 100644 --- a/test/unittest/writertest.cpp +++ b/test/unittest/writertest.cpp @@ -18,6 +18,7 @@ #include "rapidjson/reader.h" #include "rapidjson/writer.h" #include "rapidjson/stringbuffer.h" +#include "rapidjson/memorybuffer.h" using namespace rapidjson; @@ -107,35 +108,58 @@ TEST(Writer, Double) { } +// UTF8 -> TargetEncoding -> UTF8 +template +void TestTranscode(const char* json) { + StringStream s(json); + GenericStringBuffer buffer; + Writer, UTF8<>, TargetEncoding> writer(buffer); + Reader reader; + reader.Parse(s, writer); + + StringBuffer buffer2; + Writer writer2(buffer2); + GenericReader > reader2; + GenericStringStream s2(buffer.GetString()); + reader2.Parse(s2, writer2); + + EXPECT_STREQ(json, buffer2.GetString()); +} + TEST(Writer, Transcode) { const char json[] = "{\"hello\":\"world\",\"t\":true,\"f\":false,\"n\":null,\"i\":123,\"pi\":3.1416,\"a\":[1,2,3],\"dollar\":\"\x24\",\"cents\":\"\xC2\xA2\",\"euro\":\"\xE2\x82\xAC\",\"gclef\":\"\xF0\x9D\x84\x9E\"}"; // UTF8 -> UTF16 -> UTF8 - { - StringStream s(json); - StringBuffer buffer; - Writer, UTF8<> > writer(buffer); - GenericReader, UTF16<> > reader; - reader.Parse(s, writer); - EXPECT_STREQ(json, buffer.GetString()); - } + TestTranscode >(json); - // UTF8 -> UTF8 -> ASCII -> UTF8 -> UTF8 + // UTF8 -> ASCII -> UTF8 + TestTranscode >(json); + + // UTF8 -> UTF16 -> UTF8 + TestTranscode >(json); + + // UTF8 -> UTF32 -> UTF8 + TestTranscode >(json); + + // UTF8 -> AutoUTF (UTF16BE) -> UTF8 { StringStream s(json); - StringBuffer buffer; - Writer, ASCII<> > writer(buffer); + MemoryBuffer buffer; + AutoUTFOutputStream os(buffer, kUTF16BE, true); + Writer, UTF8<>, AutoUTF > writer(os); Reader reader; reader.Parse(s, writer); StringBuffer buffer2; Writer writer2(buffer2); - GenericReader, UTF8<> > reader2; - StringStream s2(buffer.GetString()); - reader2.Parse(s2, writer2); + GenericReader, UTF8<> > reader2; + MemoryStream s2(buffer.GetBuffer(), buffer.GetSize()); + AutoUTFInputStream is(s2); + reader2.Parse(is, writer2); EXPECT_STREQ(json, buffer2.GetString()); } + } #include From a8970be54315d342c798234a14ebeb811151a48b Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Fri, 15 Apr 2016 14:15:08 +0800 Subject: [PATCH 032/305] Improve UTF8::Encode() coverage via writing to AutoUTF --- test/unittest/writertest.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/test/unittest/writertest.cpp b/test/unittest/writertest.cpp index 4e08d7e..7db1c62 100644 --- a/test/unittest/writertest.cpp +++ b/test/unittest/writertest.cpp @@ -141,11 +141,12 @@ TEST(Writer, Transcode) { // UTF8 -> UTF32 -> UTF8 TestTranscode >(json); - // UTF8 -> AutoUTF (UTF16BE) -> UTF8 - { + // UTF8 -> AutoUTF -> UTF8 + UTFType types[] = { kUTF8, kUTF16LE , kUTF16BE, kUTF32LE , kUTF32BE }; + for (size_t i = 0; i < 5; i++) { StringStream s(json); MemoryBuffer buffer; - AutoUTFOutputStream os(buffer, kUTF16BE, true); + AutoUTFOutputStream os(buffer, types[i], true); Writer, UTF8<>, AutoUTF > writer(os); Reader reader; reader.Parse(s, writer); From 8f9ff88c29bfc294a7905751020a1b42bcaadb54 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Fri, 15 Apr 2016 17:03:23 +0800 Subject: [PATCH 033/305] Add Writer. ScanWriteUnescapedString to try to improve coverage --- test/unittest/writertest.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/unittest/writertest.cpp b/test/unittest/writertest.cpp index 7db1c62..cd0a32e 100644 --- a/test/unittest/writertest.cpp +++ b/test/unittest/writertest.cpp @@ -95,6 +95,18 @@ TEST(Writer, String) { #endif } +TEST(Writer, ScanWriteUnescapedString) { + const char json[] = "[\" \\\"\"]"; + char buffer2[sizeof(json) + 32]; + + // Use different offset to test different alignments + for (int i = 0; i < 32; i++) { + char* p = buffer2 + i; + memcpy(p, json, sizeof(json)); + TEST_ROUNDTRIP(p); + } +} + TEST(Writer, Double) { TEST_ROUNDTRIP("[1.2345,1.2345678,0.123456789012,1234567.8]"); TEST_ROUNDTRIP("0.0"); From 8fcc65bf581b6da1c3fc04a5c39222f2dbdce08f Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Fri, 15 Apr 2016 19:51:50 +0800 Subject: [PATCH 034/305] Adjust ScanWriteUnescapedString test case --- test/unittest/writertest.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/unittest/writertest.cpp b/test/unittest/writertest.cpp index cd0a32e..af09f8b 100644 --- a/test/unittest/writertest.cpp +++ b/test/unittest/writertest.cpp @@ -96,7 +96,8 @@ TEST(Writer, String) { } TEST(Writer, ScanWriteUnescapedString) { - const char json[] = "[\" \\\"\"]"; + const char json[] = "[\" \\\"0123456789ABCDEF\"]"; + // ^ scanning stops here. char buffer2[sizeof(json) + 32]; // Use different offset to test different alignments From 3da4afd259667ce57b5b41db4a0e589ba861d1a9 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Sat, 16 Apr 2016 15:19:34 +0800 Subject: [PATCH 035/305] Another trial on writer coverage --- test/unittest/simdtest.cpp | 38 ++++++++++++++++++++---------------- test/unittest/writertest.cpp | 6 +++++- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/test/unittest/simdtest.cpp b/test/unittest/simdtest.cpp index 1b6fcef..84f8cb0 100644 --- a/test/unittest/simdtest.cpp +++ b/test/unittest/simdtest.cpp @@ -105,24 +105,28 @@ struct ScanCopyUnescapedStringHandler : BaseReaderHandler, ScanCopyUnesca template void TestScanCopyUnescapedString() { - for (size_t step = 0; step < 1024; step++) { - char json[1024 + 5]; - char *p = json; - *p ++= '\"'; - for (size_t i = 0; i < step; i++) - *p++= "ABCD"[i % 4]; - *p++ = '\\'; - *p++ = '\\'; - *p++ = '\"'; - *p++ = '\0'; + char buffer[1024 + 5 + 32]; - StreamType s(json); - Reader reader; - ScanCopyUnescapedStringHandler h; - reader.Parse(s, h); - EXPECT_TRUE(memcmp(h.buffer, json + 1, step) == 0); - EXPECT_EQ('\\', h.buffer[step]); // escaped - EXPECT_EQ('\0', h.buffer[step + 1]); + for (size_t offset = 0; offset < 32; offset++) { + for (size_t step = 0; step < 1024; step++) { + char* json = buffer + offset; + char *p = json; + *p++ = '\"'; + for (size_t i = 0; i < step; i++) + *p++ = "ABCD"[i % 4]; + *p++ = '\\'; + *p++ = '\\'; + *p++ = '\"'; + *p++ = '\0'; + + StreamType s(json); + Reader reader; + ScanCopyUnescapedStringHandler h; + reader.Parse(s, h); + EXPECT_TRUE(memcmp(h.buffer, json + 1, step) == 0); + EXPECT_EQ('\\', h.buffer[step]); // escaped + EXPECT_EQ('\0', h.buffer[step + 1]); + } } } diff --git a/test/unittest/writertest.cpp b/test/unittest/writertest.cpp index af09f8b..9c68c53 100644 --- a/test/unittest/writertest.cpp +++ b/test/unittest/writertest.cpp @@ -448,6 +448,10 @@ TEST(Writer, NaN) { StringBuffer buffer; Writer writer(buffer); EXPECT_FALSE(writer.Double(nan)); + + GenericStringBuffer > buffer2; + Writer > > writer2(buffer2); + EXPECT_FALSE(writer2.Double(nan)); } TEST(Writer, Inf) { @@ -456,7 +460,7 @@ TEST(Writer, Inf) { StringBuffer buffer; { Writer writer(buffer); - EXPECT_FALSE(writer.Double(inf)); + EXPECT_FALSE(writer.Double(inf)); } { Writer writer(buffer); From a6f9cb85abfb5ae0a508c7a1119497bc223c36cc Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Sat, 16 Apr 2016 16:11:34 +0800 Subject: [PATCH 036/305] Third trial on writer coverage --- test/unittest/simdtest.cpp | 79 ++++++++++++++++++++------------------ 1 file changed, 41 insertions(+), 38 deletions(-) diff --git a/test/unittest/simdtest.cpp b/test/unittest/simdtest.cpp index 84f8cb0..a81b4c1 100644 --- a/test/unittest/simdtest.cpp +++ b/test/unittest/simdtest.cpp @@ -136,47 +136,50 @@ TEST(SIMD, SIMD_SUFFIX(ScanCopyUnescapedString)) { } TEST(SIMD, SIMD_SUFFIX(ScanWriteUnescapedString)) { - for (size_t step = 0; step < 1024; step++) { - char s[2048 + 1]; - char *p = s; - for (size_t i = 0; i < step; i++) - *p++= "ABCD"[i % 4]; - char escape = "\0\n\\\""[step % 4]; - *p++ = escape; - for (size_t i = 0; i < step; i++) - *p++= "ABCD"[i % 4]; + char buffer[2048 + 1 + 32]; + for (size_t offset = 0; offset < 32; offset++) { + for (size_t step = 0; step < 1024; step++) { + char* s = buffer + offset; + char* p = s; + for (size_t i = 0; i < step; i++) + *p++ = "ABCD"[i % 4]; + char escape = "\0\n\\\""[step % 4]; + *p++ = escape; + for (size_t i = 0; i < step; i++) + *p++ = "ABCD"[i % 4]; - StringBuffer sb; - Writer writer(sb); - writer.String(s, SizeType(step * 2 + 1)); - const char* q = sb.GetString(); - EXPECT_EQ('\"', *q++); - for (size_t i = 0; i < step; i++) - EXPECT_EQ("ABCD"[i % 4], *q++); - if (escape == '\0') { - EXPECT_EQ('\\', *q++); - EXPECT_EQ('u', *q++); - EXPECT_EQ('0', *q++); - EXPECT_EQ('0', *q++); - EXPECT_EQ('0', *q++); - EXPECT_EQ('0', *q++); - } - else if (escape == '\n') { - EXPECT_EQ('\\', *q++); - EXPECT_EQ('n', *q++); - } - else if (escape == '\\') { - EXPECT_EQ('\\', *q++); - EXPECT_EQ('\\', *q++); - } - else if (escape == '\"') { - EXPECT_EQ('\\', *q++); + StringBuffer sb; + Writer writer(sb); + writer.String(s, SizeType(step * 2 + 1)); + const char* q = sb.GetString(); EXPECT_EQ('\"', *q++); + for (size_t i = 0; i < step; i++) + EXPECT_EQ("ABCD"[i % 4], *q++); + if (escape == '\0') { + EXPECT_EQ('\\', *q++); + EXPECT_EQ('u', *q++); + EXPECT_EQ('0', *q++); + EXPECT_EQ('0', *q++); + EXPECT_EQ('0', *q++); + EXPECT_EQ('0', *q++); + } + else if (escape == '\n') { + EXPECT_EQ('\\', *q++); + EXPECT_EQ('n', *q++); + } + else if (escape == '\\') { + EXPECT_EQ('\\', *q++); + EXPECT_EQ('\\', *q++); + } + else if (escape == '\"') { + EXPECT_EQ('\\', *q++); + EXPECT_EQ('\"', *q++); + } + for (size_t i = 0; i < step; i++) + EXPECT_EQ("ABCD"[i % 4], *q++); + EXPECT_EQ('\"', *q++); + EXPECT_EQ('\0', *q++); } - for (size_t i = 0; i < step; i++) - EXPECT_EQ("ABCD"[i % 4], *q++); - EXPECT_EQ('\"', *q++); - EXPECT_EQ('\0', *q++); } } From bdfa0447ece96ea0bee5824ba656bdec69d1c74f Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Sat, 16 Apr 2016 21:44:33 +0800 Subject: [PATCH 037/305] Add test cases for ScanCopyUnescapedString --- test/unittest/simdtest.cpp | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/test/unittest/simdtest.cpp b/test/unittest/simdtest.cpp index a81b4c1..b01b559 100644 --- a/test/unittest/simdtest.cpp +++ b/test/unittest/simdtest.cpp @@ -100,13 +100,15 @@ struct ScanCopyUnescapedStringHandler : BaseReaderHandler, ScanCopyUnesca memcpy(buffer, str, length + 1); return true; } - char buffer[1024 + 5]; + char buffer[1024 + 5 + 32]; }; template void TestScanCopyUnescapedString() { char buffer[1024 + 5 + 32]; + char backup[1024 + 5 + 32]; + // Test "ABCDABCD...\\" for (size_t offset = 0; offset < 32; offset++) { for (size_t step = 0; step < 1024; step++) { char* json = buffer + offset; @@ -118,16 +120,41 @@ void TestScanCopyUnescapedString() { *p++ = '\\'; *p++ = '\"'; *p++ = '\0'; + strcpy(backup, json); // insitu parsing will overwrite buffer, so need to backup first StreamType s(json); Reader reader; ScanCopyUnescapedStringHandler h; reader.Parse(s, h); - EXPECT_TRUE(memcmp(h.buffer, json + 1, step) == 0); + EXPECT_TRUE(memcmp(h.buffer, backup + 1, step) == 0); EXPECT_EQ('\\', h.buffer[step]); // escaped EXPECT_EQ('\0', h.buffer[step + 1]); } } + + // Test "\\ABCDABCD..." + for (size_t offset = 0; offset < 32; offset++) { + for (size_t step = 0; step < 1024; step++) { + char* json = buffer + offset; + char *p = json; + *p++ = '\"'; + *p++ = '\\'; + *p++ = '\\'; + for (size_t i = 0; i < step; i++) + *p++ = "ABCD"[i % 4]; + *p++ = '\"'; + *p++ = '\0'; + strcpy(backup, json); // insitu parsing will overwrite buffer, so need to backup first + + StreamType s(json); + Reader reader; + ScanCopyUnescapedStringHandler h; + reader.Parse(s, h); + EXPECT_TRUE(memcmp(h.buffer + 1, backup + 3, step) == 0); + EXPECT_EQ('\\', h.buffer[0]); // escaped + EXPECT_EQ('\0', h.buffer[step + 1]); + } + } } TEST(SIMD, SIMD_SUFFIX(ScanCopyUnescapedString)) { From fdd443120f753d375fa2d71260c43abada40a6da Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Sat, 16 Apr 2016 22:09:23 +0800 Subject: [PATCH 038/305] Move break into same line to make coverage happy --- include/rapidjson/reader.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/include/rapidjson/reader.h b/include/rapidjson/reader.h index 8882a5d..243e0d2 100644 --- a/include/rapidjson/reader.h +++ b/include/rapidjson/reader.h @@ -635,8 +635,7 @@ private: RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); return; default: - RAPIDJSON_PARSE_ERROR(kParseErrorObjectMissCommaOrCurlyBracket, is.Tell()); - break; + RAPIDJSON_PARSE_ERROR(kParseErrorObjectMissCommaOrCurlyBracket, is.Tell()); break; // This useless break is only for making warning and coverage happy } if (parseFlags & kParseTrailingCommasFlag) { From c71825f80ea2eb0f40efc7494361f5fde81eb642 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Sat, 16 Apr 2016 22:14:38 +0800 Subject: [PATCH 039/305] Improve Value::IsFloat() coverage --- test/unittest/valuetest.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/unittest/valuetest.cpp b/test/unittest/valuetest.cpp index aac0a44..feec049 100644 --- a/test/unittest/valuetest.cpp +++ b/test/unittest/valuetest.cpp @@ -402,6 +402,7 @@ TEST(Value, Int) { EXPECT_TRUE(x.IsUint64()); EXPECT_FALSE(x.IsDouble()); + EXPECT_FALSE(x.IsFloat()); EXPECT_FALSE(x.IsNull()); EXPECT_FALSE(x.IsBool()); EXPECT_FALSE(x.IsFalse()); @@ -456,6 +457,7 @@ TEST(Value, Uint) { EXPECT_NEAR(1234.0, x.GetDouble(), 0.0); // Number can always be cast as double but !IsDouble(). EXPECT_FALSE(x.IsDouble()); + EXPECT_FALSE(x.IsFloat()); EXPECT_FALSE(x.IsNull()); EXPECT_FALSE(x.IsBool()); EXPECT_FALSE(x.IsFalse()); @@ -500,6 +502,7 @@ TEST(Value, Int64) { EXPECT_TRUE(x.IsUint64()); EXPECT_FALSE(x.IsDouble()); + EXPECT_FALSE(x.IsFloat()); EXPECT_FALSE(x.IsNull()); EXPECT_FALSE(x.IsBool()); EXPECT_FALSE(x.IsFalse()); @@ -561,6 +564,7 @@ TEST(Value, Uint64) { EXPECT_TRUE(x.IsUint64()); EXPECT_FALSE(x.IsDouble()); + EXPECT_FALSE(x.IsFloat()); EXPECT_FALSE(x.IsNull()); EXPECT_FALSE(x.IsBool()); EXPECT_FALSE(x.IsFalse()); From ecd8fa3437d5f1f0560cba296f55ceeddb58baff Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Sat, 16 Apr 2016 23:04:40 +0800 Subject: [PATCH 040/305] Improve coverage of regex --- include/rapidjson/internal/regex.h | 12 +++++------- test/unittest/regextest.cpp | 11 +++++++++++ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/include/rapidjson/internal/regex.h b/include/rapidjson/internal/regex.h index d317daa..c206294 100644 --- a/include/rapidjson/internal/regex.h +++ b/include/rapidjson/internal/regex.h @@ -375,14 +375,14 @@ private: bool Eval(Stack& operandStack, Operator op) { switch (op) { case kConcatenation: - if (operandStack.GetSize() >= sizeof(Frag) * 2) { + RAPIDJSON_ASSERT(operandStack.GetSize() >= sizeof(Frag) * 2); + { Frag e2 = *operandStack.template Pop(1); Frag e1 = *operandStack.template Pop(1); Patch(e1.out, e2.start); *operandStack.template Push() = Frag(e1.start, e2.out, Min(e1.minIndex, e2.minIndex)); - return true; } - return false; + return true; case kAlternation: if (operandStack.GetSize() >= sizeof(Frag) * 2) { @@ -430,8 +430,7 @@ private: bool EvalQuantifier(Stack& operandStack, unsigned n, unsigned m) { RAPIDJSON_ASSERT(n <= m); - if (operandStack.GetSize() < sizeof(Frag)) - return false; + RAPIDJSON_ASSERT(operandStack.GetSize() >= sizeof(Frag)); if (n == 0) { if (m == 0) // a{0} not support @@ -647,8 +646,7 @@ private: // Return whether the added states is a match state bool AddState(Stack& l, SizeType index) const { - if (index == kRegexInvalidState) - return true; + RAPIDJSON_ASSERT(index != kRegexInvalidState); const State& s = GetState(index); if (s.out1 != kRegexInvalidState) { // Split diff --git a/test/unittest/regextest.cpp b/test/unittest/regextest.cpp index e3371d1..b497df6 100644 --- a/test/unittest/regextest.cpp +++ b/test/unittest/regextest.cpp @@ -17,6 +17,14 @@ using namespace rapidjson::internal; +TEST(Regex, Single) { + Regex re("a"); + ASSERT_TRUE(re.IsValid()); + EXPECT_TRUE(re.Match("a")); + EXPECT_FALSE(re.Match("")); + EXPECT_FALSE(re.Match("b")); +} + TEST(Regex, Concatenation) { Regex re("abc"); ASSERT_TRUE(re.IsValid()); @@ -560,6 +568,9 @@ TEST(Regex, Invalid) { TEST_INVALID("a{1,0}"); TEST_INVALID("a{-1,0}"); TEST_INVALID("a{-1,1}"); + TEST_INVALID("a{4294967296}"); // overflow of unsigned + TEST_INVALID("a{1a}"); + TEST_INVALID("["); TEST_INVALID("[]"); TEST_INVALID("[^]"); TEST_INVALID("[\\a]"); From 26e69ffde95ba4773ab06db6457b78f308716f4b Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Sun, 17 Apr 2016 00:48:02 +0800 Subject: [PATCH 041/305] Fix a bug in schema minimum/maximum keywords for 64-bit integer --- include/rapidjson/schema.h | 9 +++++ test/unittest/schematest.cpp | 74 +++++++++++++++++++++++++++++++++++- 2 files changed, 82 insertions(+), 1 deletion(-) diff --git a/include/rapidjson/schema.h b/include/rapidjson/schema.h index e12e7d2..5efbf24 100644 --- a/include/rapidjson/schema.h +++ b/include/rapidjson/schema.h @@ -1108,6 +1108,9 @@ private: if (exclusiveMinimum_ ? i <= minimum_.GetInt64() : i < minimum_.GetInt64()) RAPIDJSON_INVALID_KEYWORD_RETURN(GetMinimumString()); } + else if (minimum_.IsUint64()) { + RAPIDJSON_INVALID_KEYWORD_RETURN(GetMinimumString()); // i <= max(int64_t) < minimum.GetUint64() + } else if (!CheckDoubleMinimum(context, static_cast(i))) return false; } @@ -1117,6 +1120,8 @@ private: if (exclusiveMaximum_ ? i >= maximum_.GetInt64() : i > maximum_.GetInt64()) RAPIDJSON_INVALID_KEYWORD_RETURN(GetMaximumString()); } + else if (maximum_.IsUint64()) + /* do nothing */; // i <= max(int64_t) < maximum_.GetUint64() else if (!CheckDoubleMaximum(context, static_cast(i))) return false; } @@ -1142,6 +1147,8 @@ private: if (exclusiveMinimum_ ? i <= minimum_.GetUint64() : i < minimum_.GetUint64()) RAPIDJSON_INVALID_KEYWORD_RETURN(GetMinimumString()); } + else if (minimum_.IsInt64()) + /* do nothing */; // i >= 0 > minimum.Getint64() else if (!CheckDoubleMinimum(context, static_cast(i))) return false; } @@ -1151,6 +1158,8 @@ private: if (exclusiveMaximum_ ? i >= maximum_.GetUint64() : i > maximum_.GetUint64()) RAPIDJSON_INVALID_KEYWORD_RETURN(GetMaximumString()); } + else if (maximum_.IsInt64()) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetMaximumString()); // i >= 0 > maximum_ else if (!CheckDoubleMaximum(context, static_cast(i))) return false; } diff --git a/test/unittest/schematest.cpp b/test/unittest/schematest.cpp index 7182ad2..23aac0e 100644 --- a/test/unittest/schematest.cpp +++ b/test/unittest/schematest.cpp @@ -51,6 +51,10 @@ TEST(SchemaValidator, Hasher) { TEST_HASHER("false", "null", false); TEST_HASHER("1", "1", true); + TEST_HASHER("2147483648", "2147483648", true); // 2^31 can only be fit in unsigned + TEST_HASHER("-2147483649", "-2147483649", true); // -2^31 - 1 can only be fit in int64_t + TEST_HASHER("2147483648", "2147483648", true); // 2^31 can only be fit in unsigned + TEST_HASHER("4294967296", "4294967296", true); // 2^32 can only be fit in int64_t TEST_HASHER("1.5", "1.5", true); TEST_HASHER("1", "1.0", true); TEST_HASHER("1", "-1", false); @@ -316,6 +320,10 @@ TEST(SchemaValidator, String) { VALIDATE(s, "\"I'm a string\"", true); INVALIDATE(s, "42", "", "type", ""); + INVALIDATE(s, "2147483648", "", "type", ""); // 2^31 can only be fit in unsigned + INVALIDATE(s, "-2147483649", "", "type", ""); // -2^31 - 1 can only be fit in int64_t + INVALIDATE(s, "4294967296", "", "type", ""); // 2^32 can only be fit in int64_t + INVALIDATE(s, "3.1415926", "", "type", ""); } TEST(SchemaValidator, String_LengthRange) { @@ -340,6 +348,16 @@ TEST(SchemaValidator, String_Pattern) { INVALIDATE(s, "\"(888)555-1212 ext. 532\"", "", "pattern", ""); INVALIDATE(s, "\"(800)FLOWERS\"", "", "pattern", ""); } + +TEST(SchemaValidator, String_Pattern_Invalid) { + Document sd; + sd.Parse("{\"type\":\"string\",\"pattern\":\"a{0}\"}"); // TODO: report regex is invalid somehow + SchemaDocument s(sd); + + VALIDATE(s, "\"\"", true); + VALIDATE(s, "\"a\"", true); + VALIDATE(s, "\"aa\"", true); +} #endif TEST(SchemaValidator, Integer) { @@ -349,6 +367,10 @@ TEST(SchemaValidator, Integer) { VALIDATE(s, "42", true); VALIDATE(s, "-1", true); + VALIDATE(s, "2147483648", true); // 2^31 can only be fit in unsigned + VALIDATE(s, "-2147483649", true); // -2^31 - 1 can only be fit in int64_t + VALIDATE(s, "2147483648", true); // 2^31 can only be fit in unsigned + VALIDATE(s, "4294967296", true); // 2^32 can only be fit in int64_t INVALIDATE(s, "3.1415926", "", "type", ""); INVALIDATE(s, "\"42\"", "", "type", ""); } @@ -368,11 +390,34 @@ TEST(SchemaValidator, Integer_Range) { TEST(SchemaValidator, Integer_Range64Boundary) { Document sd; - sd.Parse("{\"type\":\"integer\",\"minimum\":-9223372036854775807,\"maximum\":18446744073709551614}"); + sd.Parse("{\"type\":\"integer\",\"minimum\":-9223372036854775807,\"maximum\":9223372036854775806}"); SchemaDocument s(sd); INVALIDATE(s, "-9223372036854775808", "", "minimum", ""); VALIDATE(s, "-9223372036854775807", true); + VALIDATE(s, "-2147483648", true); // int min + VALIDATE(s, "0", true); + VALIDATE(s, "2147483647", true); // int max + VALIDATE(s, "2147483648", true); // unsigned first + VALIDATE(s, "4294967296", true); // unsigned max + VALIDATE(s, "9223372036854775806", true); + INVALIDATE(s, "9223372036854775807", "", "maximum", ""); + INVALIDATE(s, "18446744073709551615", "", "maximum", ""); // uint64_t max +} + +TEST(SchemaValidator, Integer_RangeU64Boundary) { + Document sd; + sd.Parse("{\"type\":\"integer\",\"minimum\":9223372036854775808,\"maximum\":18446744073709551614}"); + SchemaDocument s(sd); + + INVALIDATE(s, "-9223372036854775808", "", "minimum", ""); + INVALIDATE(s, "9223372036854775807", "", "minimum", ""); + INVALIDATE(s, "-2147483648", "", "minimum", ""); // int min + INVALIDATE(s, "0", "", "minimum", ""); + INVALIDATE(s, "2147483647", "", "minimum", ""); // int max + INVALIDATE(s, "2147483648", "", "minimum", ""); // unsigned first + INVALIDATE(s, "4294967296", "", "minimum", ""); // unsigned max + VALIDATE(s, "9223372036854775808", true); VALIDATE(s, "18446744073709551614", true); INVALIDATE(s, "18446744073709551615", "", "maximum", ""); } @@ -418,10 +463,37 @@ TEST(SchemaValidator, Number_Range) { INVALIDATE(s, "-1", "", "minimum", ""); VALIDATE(s, "0", true); + VALIDATE(s, "0.1", true); VALIDATE(s, "10", true); VALIDATE(s, "99", true); + VALIDATE(s, "99.9", true); INVALIDATE(s, "100", "", "maximum", ""); + INVALIDATE(s, "100.0", "", "maximum", ""); + INVALIDATE(s, "101.5", "", "maximum", ""); +} + +TEST(SchemaValidator, Number_RangeDouble) { + Document sd; + sd.Parse("{\"type\":\"number\",\"minimum\":0.1,\"maximum\":100.1,\"exclusiveMaximum\":true}"); + SchemaDocument s(sd); + + INVALIDATE(s, "-9223372036854775808", "", "minimum", ""); + INVALIDATE(s, "-2147483648", "", "minimum", ""); // int min + INVALIDATE(s, "-1", "", "minimum", ""); + VALIDATE(s, "0.1", true); + VALIDATE(s, "10", true); + VALIDATE(s, "99", true); + VALIDATE(s, "100", true); INVALIDATE(s, "101", "", "maximum", ""); + INVALIDATE(s, "101.5", "", "maximum", ""); + INVALIDATE(s, "18446744073709551614", "", "maximum", ""); + INVALIDATE(s, "18446744073709551615", "", "maximum", ""); + INVALIDATE(s, "2147483647", "", "maximum", ""); // int max + INVALIDATE(s, "2147483648", "", "maximum", ""); // unsigned first + INVALIDATE(s, "4294967296", "", "maximum", ""); // unsigned max + INVALIDATE(s, "9223372036854775808", "", "maximum", ""); + INVALIDATE(s, "18446744073709551614", "", "maximum", ""); + INVALIDATE(s, "18446744073709551615", "", "maximum", ""); } TEST(SchemaValidator, Number_MultipleOf) { From e7149d665941068ccf8c565e77495521331cf390 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Sun, 17 Apr 2016 00:58:32 +0800 Subject: [PATCH 042/305] Fix memory leak for invalid regex --- include/rapidjson/schema.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/rapidjson/schema.h b/include/rapidjson/schema.h index 5efbf24..4fdb854 100644 --- a/include/rapidjson/schema.h +++ b/include/rapidjson/schema.h @@ -1006,6 +1006,7 @@ private: RegexType* r = new (allocator_->Malloc(sizeof(RegexType))) RegexType(value.GetString()); if (!r->IsValid()) { r->~RegexType(); + AllocatorType::Free(r); r = 0; } return r; From 954f80872d885deca3841a5669b69ce34d286540 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Sun, 17 Apr 2016 01:55:55 +0800 Subject: [PATCH 043/305] Improve schema minimum/maximum/multipleOf coverage --- test/unittest/schematest.cpp | 50 +++++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 3 deletions(-) diff --git a/test/unittest/schematest.cpp b/test/unittest/schematest.cpp index 23aac0e..4ceacd6 100644 --- a/test/unittest/schematest.cpp +++ b/test/unittest/schematest.cpp @@ -55,6 +55,7 @@ TEST(SchemaValidator, Hasher) { TEST_HASHER("-2147483649", "-2147483649", true); // -2^31 - 1 can only be fit in int64_t TEST_HASHER("2147483648", "2147483648", true); // 2^31 can only be fit in unsigned TEST_HASHER("4294967296", "4294967296", true); // 2^32 can only be fit in int64_t + TEST_HASHER("9223372036854775808", "9223372036854775808", true); // 2^63 can only be fit in uint64_t TEST_HASHER("1.5", "1.5", true); TEST_HASHER("1", "1.0", true); TEST_HASHER("1", "-1", false); @@ -399,7 +400,7 @@ TEST(SchemaValidator, Integer_Range64Boundary) { VALIDATE(s, "0", true); VALIDATE(s, "2147483647", true); // int max VALIDATE(s, "2147483648", true); // unsigned first - VALIDATE(s, "4294967296", true); // unsigned max + VALIDATE(s, "4294967295", true); // unsigned max VALIDATE(s, "9223372036854775806", true); INVALIDATE(s, "9223372036854775807", "", "maximum", ""); INVALIDATE(s, "18446744073709551615", "", "maximum", ""); // uint64_t max @@ -416,7 +417,7 @@ TEST(SchemaValidator, Integer_RangeU64Boundary) { INVALIDATE(s, "0", "", "minimum", ""); INVALIDATE(s, "2147483647", "", "minimum", ""); // int max INVALIDATE(s, "2147483648", "", "minimum", ""); // unsigned first - INVALIDATE(s, "4294967296", "", "minimum", ""); // unsigned max + INVALIDATE(s, "4294967295", "", "minimum", ""); // unsigned max VALIDATE(s, "9223372036854775808", true); VALIDATE(s, "18446744073709551614", true); INVALIDATE(s, "18446744073709551615", "", "maximum", ""); @@ -472,6 +473,26 @@ TEST(SchemaValidator, Number_Range) { INVALIDATE(s, "101.5", "", "maximum", ""); } +TEST(SchemaValidator, Number_RangeInt) { + Document sd; + sd.Parse("{\"type\":\"number\",\"minimum\":-100,\"maximum\":-1,\"exclusiveMaximum\":true}"); + SchemaDocument s(sd); + + INVALIDATE(s, "-101", "", "minimum", ""); + INVALIDATE(s, "-100.1", "", "minimum", ""); + VALIDATE(s, "-100", true); + VALIDATE(s, "-2", true); + INVALIDATE(s, "-1", "", "maximum", ""); + INVALIDATE(s, "-0.9", "", "maximum", ""); + INVALIDATE(s, "0", "", "maximum", ""); + INVALIDATE(s, "2147483647", "", "maximum", ""); // int max + INVALIDATE(s, "2147483648", "", "maximum", ""); // unsigned first + INVALIDATE(s, "4294967295", "", "maximum", ""); // unsigned max + INVALIDATE(s, "9223372036854775808", "", "maximum", ""); + INVALIDATE(s, "18446744073709551614", "", "maximum", ""); + INVALIDATE(s, "18446744073709551615", "", "maximum", ""); +} + TEST(SchemaValidator, Number_RangeDouble) { Document sd; sd.Parse("{\"type\":\"number\",\"minimum\":0.1,\"maximum\":100.1,\"exclusiveMaximum\":true}"); @@ -490,12 +511,28 @@ TEST(SchemaValidator, Number_RangeDouble) { INVALIDATE(s, "18446744073709551615", "", "maximum", ""); INVALIDATE(s, "2147483647", "", "maximum", ""); // int max INVALIDATE(s, "2147483648", "", "maximum", ""); // unsigned first - INVALIDATE(s, "4294967296", "", "maximum", ""); // unsigned max + INVALIDATE(s, "4294967295", "", "maximum", ""); // unsigned max INVALIDATE(s, "9223372036854775808", "", "maximum", ""); INVALIDATE(s, "18446744073709551614", "", "maximum", ""); INVALIDATE(s, "18446744073709551615", "", "maximum", ""); } +TEST(SchemaValidator, Number_RangeDoubleU64Boundary) { + Document sd; + sd.Parse("{\"type\":\"number\",\"minimum\":9223372036854775808.0,\"maximum\":18446744073709550000.0}"); + SchemaDocument s(sd); + + INVALIDATE(s, "-9223372036854775808", "", "minimum", ""); + INVALIDATE(s, "-2147483648", "", "minimum", ""); // int min + INVALIDATE(s, "0", "", "minimum", ""); + INVALIDATE(s, "2147483647", "", "minimum", ""); // int max + INVALIDATE(s, "2147483648", "", "minimum", ""); // unsigned first + INVALIDATE(s, "4294967295", "", "minimum", ""); // unsigned max + VALIDATE(s, "9223372036854775808", true); + VALIDATE(s, "18446744073709540000", true); + INVALIDATE(s, "18446744073709551615", "", "maximum", ""); +} + TEST(SchemaValidator, Number_MultipleOf) { Document sd; sd.Parse("{\"type\":\"number\",\"multipleOf\":10.0}"); @@ -506,6 +543,13 @@ TEST(SchemaValidator, Number_MultipleOf) { VALIDATE(s, "-10", true); VALIDATE(s, "20", true); INVALIDATE(s, "23", "", "multipleOf", ""); + INVALIDATE(s, "-2147483648", "", "multipleOf", ""); // int min + VALIDATE(s, "-2147483640", true); + INVALIDATE(s, "2147483647", "", "multipleOf", ""); // int max + INVALIDATE(s, "2147483648", "", "multipleOf", ""); // unsigned first + VALIDATE(s, "2147483650", true); + INVALIDATE(s, "4294967295", "", "multipleOf", ""); // unsigned max + VALIDATE(s, "4294967300", true); } TEST(SchemaValidator, Number_MultipleOfOne) { From ed6fdb6d78d32c25c7e6482dbe3e2b0fe49f8bff Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Sun, 17 Apr 2016 09:09:25 +0800 Subject: [PATCH 044/305] Improve coverage for SchemaValidator:::AppendToken() --- test/unittest/schematest.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/unittest/schematest.cpp b/test/unittest/schematest.cpp index 4ceacd6..ff8b5d6 100644 --- a/test/unittest/schematest.cpp +++ b/test/unittest/schematest.cpp @@ -960,6 +960,19 @@ TEST(SchemaValidator, AllOf_Nested) { INVALIDATE(s, "123", "", "allOf", ""); } +TEST(SchemaValidator, EscapedPointer) { + Document sd; + sd.Parse( + "{" + " \"type\": \"object\"," + " \"properties\": {" + " \"~/\": { \"type\": \"number\" }" + " }" + "}"); + SchemaDocument s(sd); + INVALIDATE(s, "{\"~/\":true}", "/properties/~0~1", "type", "/~0~1"); +} + template static char* ReadFile(const char* filename, Allocator& allocator) { const char *paths[] = { From cb2f340d55a9114ab34bb08b9cf0187fe8b83a81 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Sun, 17 Apr 2016 09:11:00 +0800 Subject: [PATCH 045/305] Remove ISchemaStateFactory::ReallocState() --- include/rapidjson/schema.h | 5 ----- 1 file changed, 5 deletions(-) diff --git a/include/rapidjson/schema.h b/include/rapidjson/schema.h index 4fdb854..f7a5237 100644 --- a/include/rapidjson/schema.h +++ b/include/rapidjson/schema.h @@ -159,7 +159,6 @@ public: virtual uint64_t GetHashCode(void* hasher) = 0; virtual void DestroryHasher(void* hasher) = 0; virtual void* MallocState(size_t size) = 0; - virtual void* ReallocState(void* originalPtr, size_t originalSize, size_t newSize) = 0; virtual void FreeState(void* p) = 0; }; @@ -1776,10 +1775,6 @@ RAPIDJSON_MULTILINEMACRO_END return GetStateAllocator().Malloc(size); } - virtual void* ReallocState(void* originalPtr, size_t originalSize, size_t newSize) { - return GetStateAllocator().Realloc(originalPtr, originalSize, newSize); - } - virtual void FreeState(void* p) { return StateAllocator::Free(p); } From ba0a137b9c99db1af641575ae589d2c757146c31 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Sun, 17 Apr 2016 09:17:05 +0800 Subject: [PATCH 046/305] Remove unnecessary code in GenericSchemaDocument::CreateSchemaRecursive() --- include/rapidjson/schema.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/include/rapidjson/schema.h b/include/rapidjson/schema.h index f7a5237..33b6a10 100644 --- a/include/rapidjson/schema.h +++ b/include/rapidjson/schema.h @@ -1437,8 +1437,6 @@ private: const SchemaType* s = GetSchema(pointer); if (!s) CreateSchema(schema, pointer, v, document); - else if (schema) - *schema = s; for (typename ValueType::ConstMemberIterator itr = v.MemberBegin(); itr != v.MemberEnd(); ++itr) CreateSchemaRecursive(0, pointer.Append(itr->name, allocator_), itr->value, document); From a28e4befed0ee03ab64843695c43358a04f0d05e Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Sun, 17 Apr 2016 09:34:04 +0800 Subject: [PATCH 047/305] Improve coverage of Regex by removing default case. --- include/rapidjson/internal/regex.h | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/include/rapidjson/internal/regex.h b/include/rapidjson/internal/regex.h index c206294..c0a3ec5 100644 --- a/include/rapidjson/internal/regex.h +++ b/include/rapidjson/internal/regex.h @@ -413,7 +413,8 @@ private: } return false; - case kOneOrMore: + default: + RAPIDJSON_ASSERT(op == kOneOrMore); if (operandStack.GetSize() >= sizeof(Frag)) { Frag e = *operandStack.template Pop(1); SizeType s = NewState(kRegexInvalidState, e.start, 0); @@ -422,9 +423,6 @@ private: return true; } return false; - - default: - return false; } } From 01aeebf9bfd49e39556fa853bb860b77417025f7 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Sun, 17 Apr 2016 09:47:29 +0800 Subject: [PATCH 048/305] Improve reader coverage by removing a default case --- include/rapidjson/reader.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/include/rapidjson/reader.h b/include/rapidjson/reader.h index 243e0d2..16e2d07 100644 --- a/include/rapidjson/reader.h +++ b/include/rapidjson/reader.h @@ -1792,8 +1792,7 @@ private: case IterativeParsingKeyValueDelimiterState: case IterativeParsingArrayInitialState: case IterativeParsingElementDelimiterState: RAPIDJSON_PARSE_ERROR(kParseErrorValueInvalid, is.Tell()); return; - case IterativeParsingElementState: RAPIDJSON_PARSE_ERROR(kParseErrorArrayMissCommaOrSquareBracket, is.Tell()); return; - default: RAPIDJSON_PARSE_ERROR(kParseErrorUnspecificSyntaxError, is.Tell()); return; + default: RAPIDJSON_ASSERT(src == IterativeParsingElementState); RAPIDJSON_PARSE_ERROR(kParseErrorArrayMissCommaOrSquareBracket, is.Tell()); return; } } From d7ee08621a364693cdc610a98fb5bf556efef084 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Sun, 17 Apr 2016 10:11:40 +0800 Subject: [PATCH 049/305] Update changelog --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c70a6e3..c5d126a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -51,7 +51,8 @@ This project adheres to [Semantic Versioning](http://semver.org/). * Fix Document::Parse(const Ch*) for transcoding (#478) * encodings.h: fix typo in preprocessor condition (#495) * Custom Microsoft headers are necessary only for Visual Studio 2012 and lower (#559) -* +* Fix memory leak for invalid regex (26e69ffde95ba4773ab06db6457b78f308716f4b) +* Fix a bug in schema minimum/maximum keywords for 64-bit integer (e7149d665941068ccf8c565e77495521331cf390) ### Changed * Clarify problematic JSON license (#392) From be352d954818c027bd09d075135420da1ea5921c Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Sun, 17 Apr 2016 11:59:09 +0800 Subject: [PATCH 050/305] Fix a bug in regex Due to dereferencing a pointer which may be invalidated --- include/rapidjson/internal/regex.h | 8 ++++---- test/unittest/regextest.cpp | 5 +++++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/include/rapidjson/internal/regex.h b/include/rapidjson/internal/regex.h index c0a3ec5..422a524 100644 --- a/include/rapidjson/internal/regex.h +++ b/include/rapidjson/internal/regex.h @@ -468,17 +468,17 @@ private: static SizeType Min(SizeType a, SizeType b) { return a < b ? a : b; } void CloneTopOperand(Stack& operandStack) { - const Frag *src = operandStack.template Top(); - SizeType count = stateCount_ - src->minIndex; // Assumes top operand contains states in [src->minIndex, stateCount_) + const Frag src = *operandStack.template Top(); // Copy constructor to prevent invalidation + SizeType count = stateCount_ - src.minIndex; // Assumes top operand contains states in [src->minIndex, stateCount_) State* s = states_.template Push(count); - memcpy(s, &GetState(src->minIndex), count * sizeof(State)); + memcpy(s, &GetState(src.minIndex), count * sizeof(State)); for (SizeType j = 0; j < count; j++) { if (s[j].out != kRegexInvalidState) s[j].out += count; if (s[j].out1 != kRegexInvalidState) s[j].out1 += count; } - *operandStack.template Push() = Frag(src->start + count, src->out + count, src->minIndex + count); + *operandStack.template Push() = Frag(src.start + count, src.out + count, src.minIndex + count); stateCount_ += count; } diff --git a/test/unittest/regextest.cpp b/test/unittest/regextest.cpp index b497df6..4fb5b22 100644 --- a/test/unittest/regextest.cpp +++ b/test/unittest/regextest.cpp @@ -584,4 +584,9 @@ TEST(Regex, Issue538) { EXPECT_TRUE(re.IsValid()); } +TEST(Regex, Issue583) { + Regex re("[0-9]{99999}"); + ASSERT_TRUE(re.IsValid()); +} + #undef EURO From fa8c676b37056d83992119e4ebdc6954befff3e8 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Sun, 17 Apr 2016 12:10:44 +0800 Subject: [PATCH 051/305] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c5d126a..0ed193b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -53,6 +53,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). * Custom Microsoft headers are necessary only for Visual Studio 2012 and lower (#559) * Fix memory leak for invalid regex (26e69ffde95ba4773ab06db6457b78f308716f4b) * Fix a bug in schema minimum/maximum keywords for 64-bit integer (e7149d665941068ccf8c565e77495521331cf390) +* Fix a crash bug in regex (#605) ### Changed * Clarify problematic JSON license (#392) From c8a1d51753c603c7bbea7dba791a8124a909c813 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Tue, 19 Apr 2016 15:05:15 +0800 Subject: [PATCH 052/305] Add reproduction test case --- test/unittest/schematest.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/unittest/schematest.cpp b/test/unittest/schematest.cpp index ff8b5d6..d1027ad 100644 --- a/test/unittest/schematest.cpp +++ b/test/unittest/schematest.cpp @@ -1299,6 +1299,15 @@ TEST(Schema, Issue552) { #endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS +TEST(SchemaValidator, Issue608) { + Document sd; + sd.Parse("{\"required\": [\"a\", \"b\"] }"); + SchemaDocument s(sd); + + VALIDATE(s, "{\"a\" : null, \"b\": null}", true); + INVALIDATE(s, "{\"a\" : null, \"a\" : null}", "", "required", ""); +} + #ifdef __clang__ RAPIDJSON_DIAG_POP #endif From f586edd33d5b201b1b640da924904ba360a06e58 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Tue, 19 Apr 2016 15:06:41 +0800 Subject: [PATCH 053/305] Fix required for duplicated keys Fix #608 --- include/rapidjson/schema.h | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/include/rapidjson/schema.h b/include/rapidjson/schema.h index 33b6a10..45bcebf 100644 --- a/include/rapidjson/schema.h +++ b/include/rapidjson/schema.h @@ -290,6 +290,7 @@ struct SchemaValidationContext { patternPropertiesSchemaCount(), valuePatternValidatorType(kPatternValidatorOnly), objectDependencies(), + objectRequired(), inArray(false), valueUniqueness(false), arrayUniqueness(false) @@ -313,6 +314,8 @@ struct SchemaValidationContext { factory.FreeState(patternPropertiesSchemas); if (objectDependencies) factory.FreeState(objectDependencies); + if (objectRequired) + factory.FreeState(objectRequired); } SchemaValidatorFactoryType& factory; @@ -329,9 +332,9 @@ struct SchemaValidationContext { SizeType patternPropertiesSchemaCount; PatternValidatorType valuePatternValidatorType; PatternValidatorType objectPatternValidatorType; - SizeType objectRequiredCount; SizeType arrayElementIndex; bool* objectDependencies; + bool* objectRequired; bool inArray; bool valueUniqueness; bool arrayUniqueness; @@ -365,11 +368,11 @@ public: patternProperties_(), patternPropertyCount_(), propertyCount_(), - requiredCount_(), minProperties_(), maxProperties_(SizeType(~0)), additionalProperties_(true), hasDependencies_(), + hasRequired_(), hasSchemaDependencies_(), additionalItemsSchema_(), itemsList_(), @@ -490,7 +493,7 @@ public: SizeType index; if (FindPropertyIndex(*itr, &index)) { properties_[index].required = true; - requiredCount_++; + hasRequired_ = true; } } @@ -767,7 +770,11 @@ public: if (!(type_ & (1 << kObjectSchemaType))) RAPIDJSON_INVALID_KEYWORD_RETURN(GetTypeString()); - context.objectRequiredCount = 0; + if (hasRequired_) { + context.objectRequired = static_cast(context.factory.MallocState(sizeof(bool) * propertyCount_)); + std::memset(context.objectRequired, 0, sizeof(bool) * propertyCount_); + } + if (hasDependencies_) { context.objectDependencies = static_cast(context.factory.MallocState(sizeof(bool) * propertyCount_)); std::memset(context.objectDependencies, 0, sizeof(bool) * propertyCount_); @@ -801,8 +808,8 @@ public: else context.valueSchema = properties_[index].schema; - if (properties_[index].required) - context.objectRequiredCount++; + if (hasRequired_) + context.objectRequired[index] = true; if (hasDependencies_) context.objectDependencies[index] = true; @@ -832,8 +839,12 @@ public: } bool EndObject(Context& context, SizeType memberCount) const { - if (context.objectRequiredCount != requiredCount_) - RAPIDJSON_INVALID_KEYWORD_RETURN(GetRequiredString()); + if (hasRequired_) + for (SizeType index = 0; index < propertyCount_; index++) { + if (properties_[index].required) + if (!context.objectRequired[index]) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetRequiredString()); + } if (memberCount < minProperties_) RAPIDJSON_INVALID_KEYWORD_RETURN(GetMinPropertiesString()); @@ -1236,11 +1247,11 @@ private: PatternProperty* patternProperties_; SizeType patternPropertyCount_; SizeType propertyCount_; - SizeType requiredCount_; SizeType minProperties_; SizeType maxProperties_; bool additionalProperties_; bool hasDependencies_; + bool hasRequired_; bool hasSchemaDependencies_; const SchemaType* additionalItemsSchema_; From a6571d504aeccf819af919e653361d1ad5fd7065 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Tue, 19 Apr 2016 15:10:28 +0800 Subject: [PATCH 054/305] Combine objectDependices and objectRequired into propertyExist array --- include/rapidjson/schema.h | 39 +++++++++++++------------------------- 1 file changed, 13 insertions(+), 26 deletions(-) diff --git a/include/rapidjson/schema.h b/include/rapidjson/schema.h index 45bcebf..0a8bb7c 100644 --- a/include/rapidjson/schema.h +++ b/include/rapidjson/schema.h @@ -289,8 +289,7 @@ struct SchemaValidationContext { patternPropertiesSchemas(), patternPropertiesSchemaCount(), valuePatternValidatorType(kPatternValidatorOnly), - objectDependencies(), - objectRequired(), + propertyExist(), inArray(false), valueUniqueness(false), arrayUniqueness(false) @@ -312,10 +311,8 @@ struct SchemaValidationContext { } if (patternPropertiesSchemas) factory.FreeState(patternPropertiesSchemas); - if (objectDependencies) - factory.FreeState(objectDependencies); - if (objectRequired) - factory.FreeState(objectRequired); + if (propertyExist) + factory.FreeState(propertyExist); } SchemaValidatorFactoryType& factory; @@ -333,8 +330,7 @@ struct SchemaValidationContext { PatternValidatorType valuePatternValidatorType; PatternValidatorType objectPatternValidatorType; SizeType arrayElementIndex; - bool* objectDependencies; - bool* objectRequired; + bool* propertyExist; bool inArray; bool valueUniqueness; bool arrayUniqueness; @@ -770,14 +766,9 @@ public: if (!(type_ & (1 << kObjectSchemaType))) RAPIDJSON_INVALID_KEYWORD_RETURN(GetTypeString()); - if (hasRequired_) { - context.objectRequired = static_cast(context.factory.MallocState(sizeof(bool) * propertyCount_)); - std::memset(context.objectRequired, 0, sizeof(bool) * propertyCount_); - } - - if (hasDependencies_) { - context.objectDependencies = static_cast(context.factory.MallocState(sizeof(bool) * propertyCount_)); - std::memset(context.objectDependencies, 0, sizeof(bool) * propertyCount_); + if (hasDependencies_ || hasRequired_) { + context.propertyExist = static_cast(context.factory.MallocState(sizeof(bool) * propertyCount_)); + std::memset(context.propertyExist, 0, sizeof(bool) * propertyCount_); } if (patternProperties_) { // pre-allocate schema array @@ -808,11 +799,8 @@ public: else context.valueSchema = properties_[index].schema; - if (hasRequired_) - context.objectRequired[index] = true; - - if (hasDependencies_) - context.objectDependencies[index] = true; + if (context.propertyExist) + context.propertyExist[index] = true; return true; } @@ -840,11 +828,10 @@ public: bool EndObject(Context& context, SizeType memberCount) const { if (hasRequired_) - for (SizeType index = 0; index < propertyCount_; index++) { + for (SizeType index = 0; index < propertyCount_; index++) if (properties_[index].required) - if (!context.objectRequired[index]) + if (!context.propertyExist[index]) RAPIDJSON_INVALID_KEYWORD_RETURN(GetRequiredString()); - } if (memberCount < minProperties_) RAPIDJSON_INVALID_KEYWORD_RETURN(GetMinPropertiesString()); @@ -854,10 +841,10 @@ public: if (hasDependencies_) { for (SizeType sourceIndex = 0; sourceIndex < propertyCount_; sourceIndex++) - if (context.objectDependencies[sourceIndex]) { + if (context.propertyExist[sourceIndex]) { if (properties_[sourceIndex].dependencies) { for (SizeType targetIndex = 0; targetIndex < propertyCount_; targetIndex++) - if (properties_[sourceIndex].dependencies[targetIndex] && !context.objectDependencies[targetIndex]) + if (properties_[sourceIndex].dependencies[targetIndex] && !context.propertyExist[targetIndex]) RAPIDJSON_INVALID_KEYWORD_RETURN(GetDependenciesString()); } else if (properties_[sourceIndex].dependenciesSchema) From bbcdb8b574b4098f95d95efda046705a39f888c9 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Tue, 19 Apr 2016 15:44:50 +0800 Subject: [PATCH 055/305] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ed193b..6de511e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -54,6 +54,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). * Fix memory leak for invalid regex (26e69ffde95ba4773ab06db6457b78f308716f4b) * Fix a bug in schema minimum/maximum keywords for 64-bit integer (e7149d665941068ccf8c565e77495521331cf390) * Fix a crash bug in regex (#605) +* Fix schema "required" keyword cannot handle duplicated keys (#609) ### Changed * Clarify problematic JSON license (#392) From cb927a24ca5f7b0794f049369a427012d5df8695 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Tue, 19 Apr 2016 15:48:02 +0800 Subject: [PATCH 056/305] Adding spaces in Chinese doc --- doc/dom.zh-cn.md | 132 +++++++++++------------ doc/encoding.zh-cn.md | 66 ++++++------ doc/faq.zh-cn.md | 200 +++++++++++++++++----------------- doc/features.zh-cn.md | 76 ++++++------- doc/performance.zh-cn.md | 4 +- doc/pointer.zh-cn.md | 6 +- doc/sax.zh-cn.md | 116 ++++++++++---------- doc/schema.zh-cn.md | 2 +- doc/stream.zh-cn.md | 116 ++++++++++---------- doc/tutorial.zh-cn.md | 228 +++++++++++++++++++-------------------- readme.zh-cn.md | 52 ++++----- 11 files changed, 499 insertions(+), 499 deletions(-) diff --git a/doc/dom.zh-cn.md b/doc/dom.zh-cn.md index df6815e..13e8c20 100644 --- a/doc/dom.zh-cn.md +++ b/doc/dom.zh-cn.md @@ -1,12 +1,12 @@ # DOM -文档对象模型(Document Object Model, DOM)是一种罝于内存中的JSON表示方式,以供查询及操作。我们己于[教程](doc/tutorial.md)中介绍了DOM的基本用法,本节将讲述一些细节及高级用法。 +文档对象模型(Document Object Model, DOM)是一种罝于内存中的 JSON 表示方式,以供查询及操作。我们己于 [教程](doc/tutorial.md) 中介绍了 DOM 的基本用法,本节将讲述一些细节及高级用法。 [TOC] # 模板 {#Template} -教程中使用了`Value`和`Document`类型。与`std::string`相似,这些类型其实是两个模板类的`typedef`: +教程中使用了 `Value` 和 `Document` 类型。与 `std::string` 相似,这些类型其实是两个模板类的 `typedef`: ~~~~~~~~~~cpp namespace rapidjson { @@ -31,9 +31,9 @@ typedef GenericDocument > Document; ## 编码 {#Encoding} -`Encoding`参数指明在内存中的JSON String使用哪种编码。可行的选项有`UTF8`、`UTF16`、`UTF32`。要注意这3个类型其实也是模板类。`UTF8<>`等同`UTF8`,这代表它使用`char`来存储字符串。更多细节可以参考[编码](encoding.md)。 +`Encoding` 参数指明在内存中的 JSON String 使用哪种编码。可行的选项有 `UTF8`、`UTF16`、`UTF32`。要注意这 3 个类型其实也是模板类。`UTF8<>` 等同 `UTF8`,这代表它使用 `char` 来存储字符串。更多细节可以参考 [编码](encoding.md)。 -这里是一个例子。假设一个Windows应用软件希望查询存储于JSON中的本地化字符串。Windows中含Unicode的函数使用UTF-16(宽字符)编码。无论JSON文件使用哪种编码,我们都可以把字符串以UTF-16形式存储在内存。 +这里是一个例子。假设一个 Windows 应用软件希望查询存储于 JSON 中的本地化字符串。Windows 中含 Unicode 的函数使用 UTF-16(宽字符)编码。无论 JSON 文件使用哪种编码,我们都可以把字符串以 UTF-16 形式存储在内存。 ~~~~~~~~~~cpp using namespace rapidjson; @@ -41,12 +41,12 @@ using namespace rapidjson; typedef GenericDocument > WDocument; typedef GenericValue > WValue; -FILE* fp = fopen("localization.json", "rb"); // 非Windows平台使用"r" +FILE* fp = fopen("localization.json", "rb"); // 非 Windows 平台使用 "r" char readBuffer[256]; FileReadStream bis(fp, readBuffer, sizeof(readBuffer)); -AutoUTFInputStream eis(bis); // 包装bis成eis +AutoUTFInputStream eis(bis); // 包装 bis 成 eis WDocument d; d.ParseStream<0, AutoUTF >(eis); @@ -58,15 +58,15 @@ MessageBoxW(hWnd, d[locale].GetString(), L"Test", MB_OK); ## 分配器 {#Allocator} -`Allocator`定义当`Document`/`Value`分配或释放内存时使用那个分配类。`Document`拥有或引用到一个`Allocator`实例。而为了节省内存,`Value`没有这么做。 +`Allocator` 定义当 `Document`/`Value` 分配或释放内存时使用那个分配类。`Document` 拥有或引用到一个 `Allocator` 实例。而为了节省内存,`Value` 没有这么做。 -`GenericDocument`的缺省分配器是`MemoryPoolAllocator`。此分配器实际上会顺序地分配内存,并且不能逐一释放。当要解析一个JSON并生成DOM,这种分配器是非常合适的。 +`GenericDocument` 的缺省分配器是 `MemoryPoolAllocator`。此分配器实际上会顺序地分配内存,并且不能逐一释放。当要解析一个 JSON 并生成 DOM,这种分配器是非常合适的。 -RapidJSON还提供另一个分配器`CrtAllocator`,当中CRT是C运行库(C RunTime library)的缩写。此分配器简单地读用标准的`malloc()`/`realloc()`/`free()`。当我们需要许多增减操作,这种分配器会更为适合。然而这种分配器远远比`MemoryPoolAllocator`低效。 +RapidJSON 还提供另一个分配器 `CrtAllocator`,当中 CRT 是 C 运行库(C RunTime library)的缩写。此分配器简单地读用标准的 `malloc()`/`realloc()`/`free()`。当我们需要许多增减操作,这种分配器会更为适合。然而这种分配器远远比 `MemoryPoolAllocator` 低效。 # 解析 {#Parsing} -`Document`提供几个解析函数。以下的(1)是根本的函数,其他都是调用(1)的协助函数。 +`Document` 提供几个解析函数。以下的 (1) 是根本的函数,其他都是调用 (1) 的协助函数。 ~~~~~~~~~~cpp using namespace rapidjson; @@ -94,7 +94,7 @@ GenericDocument& GenericDocument::ParseInsitu(Ch* str); template GenericDocument& GenericDocument::Parse(const Ch* str); -// (7) 正常解析一个字符串,使用Document的编码 +// (7) 正常解析一个字符串,使用 Document 的编码 template GenericDocument& GenericDocument::Parse(const Ch* str); @@ -102,32 +102,32 @@ GenericDocument& GenericDocument::Parse(const Ch* str); GenericDocument& GenericDocument::Parse(const Ch* str); ~~~~~~~~~~ -[教程](tutorial.md)中的例使用(8)去正常解析字符串。而[流](stream.md)的例子使用前3个函数。我们将稍后介绍原位(*In situ*) 解析。 +[教程](tutorial.md) 中的例使用 (8) 去正常解析字符串。而 [流](stream.md) 的例子使用前 3 个函数。我们将稍后介绍原位(*In situ*) 解析。 -`parseFlags`是以下位标置的组合: +`parseFlags` 是以下位标置的组合: 解析位标志 | 意义 ------------------------------|----------------------------------- `kParseNoFlags` | 没有任何标志。 -`kParseDefaultFlags` | 缺省的解析选项。它等于`RAPIDJSON_PARSE_DEFAULT_FLAGS`宏,此宏定义为`kParseNoFlags`。 +`kParseDefaultFlags` | 缺省的解析选项。它等于 `RAPIDJSON_PARSE_DEFAULT_FLAGS` 宏,此宏定义为 `kParseNoFlags`。 `kParseInsituFlag` | 原位(破坏性)解析。 -`kParseValidateEncodingFlag` | 校验JSON字符串的编码。 +`kParseValidateEncodingFlag` | 校验 JSON 字符串的编码。 `kParseIterativeFlag` | 迭代式(调用堆栈大小为常数复杂度)解析。 -`kParseStopWhenDoneFlag` | 当从流解析了一个完整的JSON根节点之后,停止继续处理余下的流。当使用了此标志,解析器便不会产生`kParseErrorDocumentRootNotSingular`错误。可使用本标志去解析同一个流里的多个JSON。 -`kParseFullPrecisionFlag` | 使用完整的精确度去解析数字(较慢)。如不设置此标节,则会使用正常的精确度(较快)。正常精确度会有最多3个[ULP](http://en.wikipedia.org/wiki/Unit_in_the_last_place)的误差。 -`kParseCommentsFlag` | 容许单行 `// ...` 及多行 `/* ... */` 注释(放宽的JSON语法)。 +`kParseStopWhenDoneFlag` | 当从流解析了一个完整的 JSON 根节点之后,停止继续处理余下的流。当使用了此标志,解析器便不会产生 `kParseErrorDocumentRootNotSingular` 错误。可使用本标志去解析同一个流里的多个 JSON。 +`kParseFullPrecisionFlag` | 使用完整的精确度去解析数字(较慢)。如不设置此标节,则会使用正常的精确度(较快)。正常精确度会有最多 3 个 [ULP](http://en.wikipedia.org/wiki/Unit_in_the_last_place) 的误差。 +`kParseCommentsFlag` | 容许单行 `// ...` 及多行 `/* ... */` 注释(放宽的 JSON 语法)。 `kParseNumbersAsStringsFlag` | 把数字类型解析成字符串。 -`kParseTrailingCommasFlag` | 容许在对象和数组结束前含有逗号(放宽的JSON语法)。 +`kParseTrailingCommasFlag` | 容许在对象和数组结束前含有逗号(放宽的 JSON 语法)。 -由于使用了非类型模板参数,而不是函数参数,C++编译器能为个别组合生成代码,以改善性能及减少代码尺寸(当只用单种特化)。缺点是需要在编译期决定标志。 +由于使用了非类型模板参数,而不是函数参数,C++ 编译器能为个别组合生成代码,以改善性能及减少代码尺寸(当只用单种特化)。缺点是需要在编译期决定标志。 -`SourceEncoding`参数定义流使用了什么编码。这与`Document`的`Encoding`不相同。细节可参考[转码和校验](#TranscodingAndValidation)一节。 +`SourceEncoding` 参数定义流使用了什么编码。这与 `Document` 的 `Encoding` 不相同。细节可参考 [转码和校验](#TranscodingAndValidation) 一节。 -此外`InputStream`是输入流的类型。 +此外 `InputStream` 是输入流的类型。 ## 解析错误 {#ParseError} -当解析过程顺利完成,`Document`便会含有解析结果。当过程出现错误,原来的DOM会*维持不便*。可使用`bool HasParseError()`、`ParseErrorCode GetParseError()`及`size_t GetParseOffset()`获取解析的错误状态。 +当解析过程顺利完成,`Document` 便会含有解析结果。当过程出现错误,原来的 DOM 会 * 维持不便 *。可使用 `bool HasParseError()`、`ParseErrorCode GetParseError()` 及 `size_t GetParseOffset()` 获取解析的错误状态。 解析错误代号 | 描述 --------------------------------------------|--------------------------------------------------- @@ -135,22 +135,22 @@ GenericDocument& GenericDocument::Parse(const Ch* str); `kParseErrorDocumentEmpty` | 文档是空的。 `kParseErrorDocumentRootNotSingular` | 文档的根后面不能有其它值。 `kParseErrorValueInvalid` | 不合法的值。 -`kParseErrorObjectMissName` | Object成员缺少名字。 -`kParseErrorObjectMissColon` | Object成员名字后缺少冒号。 -`kParseErrorObjectMissCommaOrCurlyBracket` | Object成员后缺少逗号或`}`。 -`kParseErrorArrayMissCommaOrSquareBracket` | Array元素后缺少逗号或`]` 。 -`kParseErrorStringUnicodeEscapeInvalidHex` | String中的`\\u`转义符后含非十六进位数字。 -`kParseErrorStringUnicodeSurrogateInvalid` | String中的代理对(surrogate pair)不合法。 -`kParseErrorStringEscapeInvalid` | String含非法转义字符。 -`kParseErrorStringMissQuotationMark` | String缺少关闭引号。 -`kParseErrorStringInvalidEncoding` | String含非法编码。 -`kParseErrorNumberTooBig` | Number的值太大,不能存储于`double`。 -`kParseErrorNumberMissFraction` | Number缺少了小数部分。 -`kParseErrorNumberMissExponent` | Number缺少了指数。 +`kParseErrorObjectMissName` | Object 成员缺少名字。 +`kParseErrorObjectMissColon` | Object 成员名字后缺少冒号。 +`kParseErrorObjectMissCommaOrCurlyBracket` | Object 成员后缺少逗号或 `}`。 +`kParseErrorArrayMissCommaOrSquareBracket` | Array 元素后缺少逗号或 `]` 。 +`kParseErrorStringUnicodeEscapeInvalidHex` | String 中的 `\\u` 转义符后含非十六进位数字。 +`kParseErrorStringUnicodeSurrogateInvalid` | String 中的代理对(surrogate pair)不合法。 +`kParseErrorStringEscapeInvalid` | String 含非法转义字符。 +`kParseErrorStringMissQuotationMark` | String 缺少关闭引号。 +`kParseErrorStringInvalidEncoding` | String 含非法编码。 +`kParseErrorNumberTooBig` | Number 的值太大,不能存储于 `double`。 +`kParseErrorNumberMissFraction` | Number 缺少了小数部分。 +`kParseErrorNumberMissExponent` | Number 缺少了指数。 -错误的偏移量定义为从流开始至错误处的字符数量。目前RapidJSON不记录错误行号。 +错误的偏移量定义为从流开始至错误处的字符数量。目前 RapidJSON 不记录错误行号。 -要取得错误讯息,RapidJSON在`rapidjson/error/en.h`中提供了英文错误讯息。使用者可以修改它用于其他语言环境,或使用一个自定义的本地化系统。 +要取得错误讯息,RapidJSON 在 `rapidjson/error/en.h` 中提供了英文错误讯息。使用者可以修改它用于其他语言环境,或使用一个自定义的本地化系统。 以下是一个处理错误的例子。 @@ -170,7 +170,7 @@ if (d.Parse(json).HasParseError()) { ## 原位解析 {#InSituParsing} -根据[维基百科](http://en.wikipedia.org/wiki/In_situ): +根据 [维基百科](http://en.wikipedia.org/wiki/In_situ): > *In situ* ... is a Latin phrase that translates literally to "on site" or "in position". It means "locally", "on site", "on the premises" or "in place" to describe an event where it takes place, and is used in many different contexts. > ... @@ -178,24 +178,24 @@ if (d.Parse(json).HasParseError()) { > 翻译:*In situ*……是一个拉丁文片语,字面上的意思是指「现场」、「在位置」。在许多不同语境中,它描述一个事件发生的位置,意指「本地」、「现场」、「在处所」、「就位」。 > …… -> (在计算机科学中)一个算法若称为原位算法,或在位算法,是指执行该算法所需的额外内存空间是O(1)的,换句话说,无论输入大小都只需要常数空间。例如,堆排序是一个原位排序算法。 +> (在计算机科学中)一个算法若称为原位算法,或在位算法,是指执行该算法所需的额外内存空间是 O(1) 的,换句话说,无论输入大小都只需要常数空间。例如,堆排序是一个原位排序算法。 -在正常的解析过程中,对JSON string解码并复制至其他缓冲区是一个很大的开销。原位解析(*in situ* parsing)把这些JSON string直接解码于它原来存储的地方。由于解码后的string长度总是短于或等于原来储存于JSON的string,所以这是可行的。在这个语境下,对JSON string进行解码是指处理转义符,如`"\n"`、`"\u1234"`等,以及在string末端加入空终止符号(`'\0'`)。 +在正常的解析过程中,对 JSON string 解码并复制至其他缓冲区是一个很大的开销。原位解析(*in situ* parsing)把这些 JSON string 直接解码于它原来存储的地方。由于解码后的 string 长度总是短于或等于原来储存于 JSON 的 string,所以这是可行的。在这个语境下,对 JSON string 进行解码是指处理转义符,如 `"\n"`、`"\u1234"` 等,以及在 string 末端加入空终止符号 (`'\0'`)。 -以下的图比较正常及原位解析。JSON string值包含指向解码后的字符串。 +以下的图比较正常及原位解析。JSON string 值包含指向解码后的字符串。 ![正常解析](diagram/normalparsing.png) -在正常解析中,解码后的字符串被复制至全新分配的缓冲区中。`"\\n"`(2个字符)被解码成`"\n"`(1个字符)。`"\\u0073"`(6个字符)被解码成`"s"`(1个字符)。 +在正常解析中,解码后的字符串被复制至全新分配的缓冲区中。`"\\n"`(2 个字符)被解码成 `"\n"`(1 个字符)。`"\\u0073"`(6 个字符)被解码成 `"s"`(1 个字符)。 ![原位解析](diagram/insituparsing.png) -原位解析直接修改了原来的JSON。图中高亮了被更新的字符。若JSON string不含转义符,例如`"msg"`,那么解析过程仅仅是以空字符代替结束双引号。 +原位解析直接修改了原来的 JSON。图中高亮了被更新的字符。若 JSON string 不含转义符,例如 `"msg"`,那么解析过程仅仅是以空字符代替结束双引号。 -由于原位解析修改了输入,其解析API需要`char*`而非`const char*`。 +由于原位解析修改了输入,其解析 API 需要 `char*` 而非 `const char*`。 ~~~~~~~~~~cpp -// 把整个文件读入buffer +// 把整个文件读入 buffer FILE* fp = fopen("test.json", "r"); fseek(fp, 0, SEEK_END); size_t filesize = (size_t)ftell(fp); @@ -205,46 +205,46 @@ size_t readLength = fread(buffer, 1, filesize, fp); buffer[readLength] = '\0'; fclose(fp); -// 原位解析buffer至d,buffer内容会被修改。 +// 原位解析 buffer 至 d,buffer 内容会被修改。 Document d; d.ParseInsitu(buffer); -// 在此查询、修改DOM…… +// 在此查询、修改 DOM…… free(buffer); -// 注意:在这个位置,d可能含有指向已被释放的buffer的悬空指针 +// 注意:在这个位置,d 可能含有指向已被释放的 buffer 的悬空指针 ~~~~~~~~~~ -JSON string会被打上const-string的标志。但它们可能并非真正的「常数」。它的生命周期取决于存储JSON的缓冲区。 +JSON string 会被打上 const-string 的标志。但它们可能并非真正的「常数」。它的生命周期取决于存储 JSON 的缓冲区。 原位解析把分配开销及内存复制减至最小。通常这样做能改善缓存一致性,而这对现代计算机来说是一个重要的性能因素。 原位解析有以下限制: -1. 整个JSON须存储在内存之中。 +1. 整个 JSON 须存储在内存之中。 2. 流的来源缓码与文档的目标编码必须相同。 3. 需要保留缓冲区,直至文档不再被使用。 -4. 若DOM需要在解析后被长期使用,而DOM内只有很少JSON string,保留缓冲区可能造成内存浪费。 +4. 若 DOM 需要在解析后被长期使用,而 DOM 内只有很少 JSON string,保留缓冲区可能造成内存浪费。 -原位解析最适合用于短期的、用完即弃的JSON。实际应用中,这些场合是非常普遍的,例如反序列化JSON至C++对象、处理以JSON表示的web请求等。 +原位解析最适合用于短期的、用完即弃的 JSON。实际应用中,这些场合是非常普遍的,例如反序列化 JSON 至 C++ 对象、处理以 JSON 表示的 web 请求等。 ## 转码与校验 {#TranscodingAndValidation} -RapidJSON内部支持不同Unicode格式(正式的术语是UCS变换格式)间的转换。在DOM解析时,流的来源编码与DOM的编码可以不同。例如,来源流可能含有UTF-8的JSON,而DOM则使用UTF-16编码。在[EncodedInputStream](doc/stream.md)一节里有一个例子。 +RapidJSON 内部支持不同 Unicode 格式(正式的术语是 UCS 变换格式)间的转换。在 DOM 解析时,流的来源编码与 DOM 的编码可以不同。例如,来源流可能含有 UTF-8 的 JSON,而 DOM 则使用 UTF-16 编码。在 [EncodedInputStream](doc/stream.md) 一节里有一个例子。 -当从DOM输出一个JSON至输出流之时,也可以使用转码功能。在[EncodedOutputStream](doc/stream.md)一节里有一个例子。 +当从 DOM 输出一个 JSON 至输出流之时,也可以使用转码功能。在 [EncodedOutputStream](doc/stream.md) 一节里有一个例子。 -在转码过程中,会把来源string解码成Unicode码点,然后把码点编码成目标格式。在解码时,它会校验来源string的字节序列是否合法。若遇上非合法序列,解析器会停止并返回`kParseErrorStringInvalidEncoding`错误。 +在转码过程中,会把来源 string 解码成 Unicode 码点,然后把码点编码成目标格式。在解码时,它会校验来源 string 的字节序列是否合法。若遇上非合法序列,解析器会停止并返回 `kParseErrorStringInvalidEncoding` 错误。 -当来源编码与DOM的编码相同,解析器缺省地*不会*校验序列。使用者可开启`kParseValidateEncodingFlag`去强制校验。 +当来源编码与 DOM 的编码相同,解析器缺省地 * 不会 * 校验序列。使用者可开启 `kParseValidateEncodingFlag` 去强制校验。 # 技巧 {#Techniques} -这里讨论一些DOM API的使用技巧。 +这里讨论一些 DOM API 的使用技巧。 -## 把DOM作为SAX事件发表者 +## 把 DOM 作为 SAX 事件发表者 -在RapidJSON中,利用`Writer`把DOM生成JSON的做法,看来有点奇怪。 +在 RapidJSON 中,利用 `Writer` 把 DOM 生成 JSON 的做法,看来有点奇怪。 ~~~~~~~~~~cpp // ... @@ -252,19 +252,19 @@ Writer writer(buffer); d.Accept(writer); ~~~~~~~~~~ -实际上,`Value::Accept()`是负责发布该值相关的SAX事件至处理器的。通过这个设计,`Value`及`Writer`解除了偶合。`Value`可生成SAX事件,而`Writer`则可以处理这些事件。 +实际上,`Value::Accept()` 是负责发布该值相关的 SAX 事件至处理器的。通过这个设计,`Value` 及 `Writer` 解除了偶合。`Value` 可生成 SAX 事件,而 `Writer` 则可以处理这些事件。 -使用者可以创建自定义的处理器,去把DOM转换成其它格式。例如,一个把DOM转换成XML的处理器。 +使用者可以创建自定义的处理器,去把 DOM 转换成其它格式。例如,一个把 DOM 转换成 XML 的处理器。 -要知道更多关于SAX事件与处理器,可参阅[SAX](doc/sax.md)。 +要知道更多关于 SAX 事件与处理器,可参阅 [SAX](doc/sax.md)。 ## 使用者缓冲区{ #UserBuffer} 许多应用软件可能需要尽量减少内存分配。 -`MemoryPoolAllocator`可以帮助这方面,它容许使用者提供一个缓冲区。该缓冲区可能置于程序堆栈,或是一个静态分配的「草稿缓冲区(scratch buffer)」(一个静态/全局的数组),用于储存临时数据。 +`MemoryPoolAllocator` 可以帮助这方面,它容许使用者提供一个缓冲区。该缓冲区可能置于程序堆栈,或是一个静态分配的「草稿缓冲区(scratch buffer)」(一个静态/全局的数组),用于储存临时数据。 -`MemoryPoolAllocator`会先用使用者缓冲区去解决分配请求。当使用者缓冲区用完,就会从基础分配器(缺省为`CrtAllocator`)分配一块内存。 +`MemoryPoolAllocator` 会先用使用者缓冲区去解决分配请求。当使用者缓冲区用完,就会从基础分配器(缺省为 `CrtAllocator`)分配一块内存。 以下是使用堆栈内存的例子,第一个分配器用于存储值,第二个用于解析时的临时缓冲。 @@ -278,6 +278,6 @@ DocumentType d(&valueAllocator, sizeof(parseBuffer), &parseAllocator); d.Parse(json); ~~~~~~~~~~ -若解析时分配总量少于4096+1024字节时,这段代码不会造成任何堆内存分配(经`new`或`malloc()`)。 +若解析时分配总量少于 4096+1024 字节时,这段代码不会造成任何堆内存分配(经 `new` 或 `malloc()`)。 -使用者可以通过`MemoryPoolAllocator::Size()`查询当前已分的内存大小。那么使用者可以拟定使用者缓冲区的合适大小。 +使用者可以通过 `MemoryPoolAllocator::Size()` 查询当前已分的内存大小。那么使用者可以拟定使用者缓冲区的合适大小。 diff --git a/doc/encoding.zh-cn.md b/doc/encoding.zh-cn.md index 3435c3a..4858bae 100644 --- a/doc/encoding.zh-cn.md +++ b/doc/encoding.zh-cn.md @@ -1,45 +1,45 @@ # 编码 -根据[ECMA-404](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf): +根据 [ECMA-404](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf): > (in Introduction) JSON text is a sequence of Unicode code points. > -> 翻译:JSON文本是Unicode码点的序列。 +> 翻译:JSON 文本是 Unicode 码点的序列。 -较早的[RFC4627](http://www.ietf.org/rfc/rfc4627.txt)申明: +较早的 [RFC4627](http://www.ietf.org/rfc/rfc4627.txt) 申明: > (in §3) JSON text SHALL be encoded in Unicode. The default encoding is UTF-8. > -> 翻译:JSON文本应该以Unicode编码。缺省的编码为UTF-8。 +> 翻译:JSON 文本应该以 Unicode 编码。缺省的编码为 UTF-8。 > (in §6) JSON may be represented using UTF-8, UTF-16, or UTF-32. When JSON is written in UTF-8, JSON is 8bit compatible. When JSON is written in UTF-16 or UTF-32, the binary content-transfer-encoding must be used. > -> 翻译:JSON可使用UTF-8、UTF-16或UTF-18表示。当JSON以UTF-8写入,该JSON是8位兼容的。当JSON以UTF-16或UTF-32写入,就必须使用二进制的内容传送编码。 +> 翻译:JSON 可使用 UTF-8、UTF-16 或 UTF-18 表示。当 JSON 以 UTF-8 写入,该 JSON 是 8 位兼容的。当 JSON 以 UTF-16 或 UTF-32 写入,就必须使用二进制的内容传送编码。 -RapidJSON支持多种编码。它也能检查JSON的编码,以及在不同编码中进行转码。所有这些功能都是在内部实现,无需使用外部的程序库(如[ICU](http://site.icu-project.org/))。 +RapidJSON 支持多种编码。它也能检查 JSON 的编码,以及在不同编码中进行转码。所有这些功能都是在内部实现,无需使用外部的程序库(如 [ICU](http://site.icu-project.org/))。 [TOC] # Unicode {#Unicode} -根据 [Unicode的官方网站](http://www.unicode.org/standard/translations/t-chinese.html): ->Unicode给每个字符提供了一个唯一的数字, +根据 [Unicode 的官方网站](http://www.unicode.org/standard/translations/t-chinese.html): +>Unicode 给每个字符提供了一个唯一的数字, 不论是什么平台、 不论是什么程序、 不论是什么语言。 -这些唯一数字称为码点(code point),其范围介乎`0x0`至`0x10FFFF`之间。 +这些唯一数字称为码点(code point),其范围介乎 `0x0` 至 `0x10FFFF` 之间。 -## Unicode转换格式 {#UTF} +## Unicode 转换格式 {#UTF} -存储Unicode码点有多种编码方式。这些称为Unicode转换格式(Unicode Transformation Format, UTF)。RapidJSON支持最常用的UTF,包括: +存储 Unicode 码点有多种编码方式。这些称为 Unicode 转换格式(Unicode Transformation Format, UTF)。RapidJSON 支持最常用的 UTF,包括: -* UTF-8:8位可变长度编码。它把一个码点映射至1至4个字节。 -* UTF-16:16位可变长度编码。它把一个码点映射至1至2个16位编码单元(即2至4个字节)。 -* UTF-32:32位固定长度编码。它直接把码点映射至单个32位编码单元(即4字节)。 +* UTF-8:8 位可变长度编码。它把一个码点映射至 1 至 4 个字节。 +* UTF-16:16 位可变长度编码。它把一个码点映射至 1 至 2 个 16 位编码单元(即 2 至 4 个字节)。 +* UTF-32:32 位固定长度编码。它直接把码点映射至单个 32 位编码单元(即 4 字节)。 -对于UTF-16及UTF-32来说,字节序(endianness)是有影响的。在内存中,它们通常都是以该计算机的字节序来存储。然而,当要储存在文件中或在网上传输,我们需要指明字节序列的字节序,是小端(little endian, LE)还是大端(big-endian, BE)。 +对于 UTF-16 及 UTF-32 来说,字节序(endianness)是有影响的。在内存中,它们通常都是以该计算机的字节序来存储。然而,当要储存在文件中或在网上传输,我们需要指明字节序列的字节序,是小端(little endian, LE)还是大端(big-endian, BE)。 -RapidJSON通过`rapidjson/encodings.h`中的struct去提供各种编码: +RapidJSON 通过 `rapidjson/encodings.h` 中的 struct 去提供各种编码: ~~~~~~~~~~cpp namespace rapidjson { @@ -68,38 +68,38 @@ struct UTF32BE; } // namespace rapidjson ~~~~~~~~~~ -对于在内存中的文本,我们正常会使用`UTF8`、`UTF16`或`UTF32`。对于处理经过I/O的文本,我们可使用`UTF8`、`UTF16LE`、`UTF16BE`、`UTF32LE`或`UTF32BE`。 +对于在内存中的文本,我们正常会使用 `UTF8`、`UTF16` 或 `UTF32`。对于处理经过 I/O 的文本,我们可使用 `UTF8`、`UTF16LE`、`UTF16BE`、`UTF32LE` 或 `UTF32BE`。 -当使用DOM风格的API,`GenericValue`及`GenericDocument`里的`Encoding`模板参数是用于指明内存中存储的JSON字符串使用哪种编码。因此通常我们会在此参数中使用`UTF8`、`UTF16`或`UTF32`。如何选择,视乎应用软件所使用的操作系统及其他程序库。例如,Windows API使用UTF-16表示Unicode字符,而多数的Linux发行版本及应用软件则更喜欢UTF-8。 +当使用 DOM 风格的 API,`GenericValue` 及 `GenericDocument` 里的 `Encoding` 模板参数是用于指明内存中存储的 JSON 字符串使用哪种编码。因此通常我们会在此参数中使用 `UTF8`、`UTF16` 或 `UTF32`。如何选择,视乎应用软件所使用的操作系统及其他程序库。例如,Windows API 使用 UTF-16 表示 Unicode 字符,而多数的 Linux 发行版本及应用软件则更喜欢 UTF-8。 -使用UTF-16的DOM声明例子: +使用 UTF-16 的 DOM 声明例子: ~~~~~~~~~~cpp typedef GenericDocument > WDocument; typedef GenericValue > WValue; ~~~~~~~~~~ -可以在[DOM's Encoding](doc/stream.md)一节看到更详细的使用例子。 +可以在 [DOM's Encoding](doc/stream.md) 一节看到更详细的使用例子。 ## 字符类型 {#CharacterType} -从之前的声明中可以看到,每个编码都有一个`CharType`模板参数。这可能比较容易混淆,实际上,每个`CharType`存储一个编码单元,而不是一个字符(码点)。如之前所谈及,在UTF-8中一个码点可能会编码成1至4个编码单元。 +从之前的声明中可以看到,每个编码都有一个 `CharType` 模板参数。这可能比较容易混淆,实际上,每个 `CharType` 存储一个编码单元,而不是一个字符(码点)。如之前所谈及,在 UTF-8 中一个码点可能会编码成 1 至 4 个编码单元。 -对于`UTF16(LE|BE)`及`UTF32(LE|BE)`来说,`CharType`必须分别是一个至少2及4字节的整数类型。 +对于 `UTF16(LE|BE)` 及 `UTF32(LE|BE)` 来说,`CharType` 必须分别是一个至少 2 及 4 字节的整数类型。 -注意C++11新添了`char16_t`及`char32_t`类型,也可分别用于`UTF16`及`UTF32`。 +注意 C++11 新添了 `char16_t` 及 `char32_t` 类型,也可分别用于 `UTF16` 及 `UTF32`。 ## AutoUTF {#AutoUTF} 上述所介绍的编码都是在编译期静态挷定的。换句话说,使用者必须知道内存或流之中使用了哪种编码。然而,有时候我们可能需要读写不同编码的文件,而且这些编码需要在运行时才能决定。 -`AutoUTF`是为此而设计的编码。它根据输入或输出流来选择使用哪种编码。目前它应该与`EncodedInputStream`及`EncodedOutputStream`结合使用。 +`AutoUTF` 是为此而设计的编码。它根据输入或输出流来选择使用哪种编码。目前它应该与 `EncodedInputStream` 及 `EncodedOutputStream` 结合使用。 ## ASCII {#ASCII} -虽然JSON标准并未提及[ASCII](http://en.wikipedia.org/wiki/ASCII),有时候我们希望写入7位的ASCII JSON,以供未能处理UTF-8的应用程序使用。由于任JSON都可以把Unicode字符表示为`\uXXXX`转义序列,JSON总是可用ASCII来编码。 +虽然 JSON 标准并未提及 [ASCII](http://en.wikipedia.org/wiki/ASCII),有时候我们希望写入 7 位的 ASCII JSON,以供未能处理 UTF-8 的应用程序使用。由于任 JSON 都可以把 Unicode 字符表示为 `\uXXXX` 转义序列,JSON 总是可用 ASCII 来编码。 -以下的例子把UTF-8的DOM写成ASCII的JSON: +以下的例子把 UTF-8 的 DOM 写成 ASCII 的 JSON: ~~~~~~~~~~cpp using namespace rapidjson; @@ -111,21 +111,21 @@ d.Accept(writer); std::cout << buffer.GetString(); ~~~~~~~~~~ -ASCII可用于输入流。当输入流包含大于127的字节,就会导致`kParseErrorStringInvalidEncoding`错误。 +ASCII 可用于输入流。当输入流包含大于 127 的字节,就会导致 `kParseErrorStringInvalidEncoding` 错误。 -ASCII *不能* 用于内存(`Document`的编码,或`Reader`的目标编码),因为它不能表示Unicode码点。 +ASCII * 不能 * 用于内存(`Document` 的编码,或 `Reader` 的目标编码),因为它不能表示 Unicode 码点。 # 校验及转码 {#ValidationTranscoding} -当RapidJSON解析一个JSON时,它能校验输入JSON,判断它是否所标明编码的合法序列。要开启此选项,请把`kParseValidateEncodingFlag`加入`parseFlags`模板参数。 +当 RapidJSON 解析一个 JSON 时,它能校验输入 JSON,判断它是否所标明编码的合法序列。要开启此选项,请把 `kParseValidateEncodingFlag` 加入 `parseFlags` 模板参数。 -若输入编码和输出编码并不相同,`Reader`及`Writer`会算把文本转码。在这种情况下,并不需要`kParseValidateEncodingFlag`,因为它必须解码输入序列。若序列不能被解码,它必然是不合法的。 +若输入编码和输出编码并不相同,`Reader` 及 `Writer` 会算把文本转码。在这种情况下,并不需要 `kParseValidateEncodingFlag`,因为它必须解码输入序列。若序列不能被解码,它必然是不合法的。 ## 转码器 {#Transcoder} -虽然RapidJSON的编码功能是为JSON解析/生成而设计,使用者也可以“滥用”它们来为非JSON字符串转码。 +虽然 RapidJSON 的编码功能是为 JSON 解析/生成而设计,使用者也可以“滥用”它们来为非 JSON 字符串转码。 -以下的例子把UTF-8字符串转码成UTF-16: +以下的例子把 UTF-8 字符串转码成 UTF-16: ~~~~~~~~~~cpp #include "rapidjson/encodings.h" @@ -149,4 +149,4 @@ if (!hasError) { } ~~~~~~~~~~ -你也可以用`AutoUTF`及对应的流来在运行时设置内源/目的之编码。 +你也可以用 `AutoUTF` 及对应的流来在运行时设置内源/目的之编码。 diff --git a/doc/faq.zh-cn.md b/doc/faq.zh-cn.md index 7127283..cc985e7 100644 --- a/doc/faq.zh-cn.md +++ b/doc/faq.zh-cn.md @@ -4,107 +4,107 @@ ## 一般问题 -1. RapidJSON是什么? +1. RapidJSON 是什么? - RapidJSON是一个C++库,用于解析及生成JSON。读者可参考它的所有[特点](doc/features.zh-cn.md)。 + RapidJSON 是一个 C++ 库,用于解析及生成 JSON。读者可参考它的所有 [特点](doc/features.zh-cn.md)。 -2. 为什么称作RapidJSON? +2. 为什么称作 RapidJSON? - 它的灵感来自于[RapidXML](http://rapidxml.sourceforge.net/),RapidXML是一个高速的XML DOM解析器。 + 它的灵感来自于 [RapidXML](http://rapidxml.sourceforge.net/),RapidXML 是一个高速的 XML DOM 解析器。 -3. RapidJSON与RapidXML相似么? +3. RapidJSON 与 RapidXML 相似么? - RapidJSON借镜了RapidXML的一些设计, 包括原位(*in situ*)解析、只有头文件的库。但两者的API是完全不同的。此外RapidJSON也提供许多RapidXML没有的特点。 + RapidJSON 借镜了 RapidXML 的一些设计, 包括原位(*in situ*)解析、只有头文件的库。但两者的 API 是完全不同的。此外 RapidJSON 也提供许多 RapidXML 没有的特点。 -4. RapidJSON是免费的么? +4. RapidJSON 是免费的么? - 是的,它在MIT特許條款下免费。它可用于商业软件。详情请参看[license.txt](https://github.com/miloyip/rapidjson/blob/master/license.txt)。 + 是的,它在 MIT 特許條款下免费。它可用于商业软件。详情请参看 [license.txt](https://github.com/miloyip/rapidjson/blob/master/license.txt)。 -5. RapidJSON很小么?它有何依赖? +5. RapidJSON 很小么?它有何依赖? - 是的。在Windows上,一个解析JSON并打印出统计的可执行文件少于30KB。 + 是的。在 Windows 上,一个解析 JSON 并打印出统计的可执行文件少于 30KB。 - RapidJSON仅依赖于C++标准库。 + RapidJSON 仅依赖于 C++ 标准库。 -6. 怎样安装RapidJSON? +6. 怎样安装 RapidJSON? - 见[安装一节](../readme.zh-cn.md#安装)。 + 见 [安装一节](../readme.zh-cn.md#安装)。 -7. RapidJSON能否运行于我的平台? +7. RapidJSON 能否运行于我的平台? - 社区已在多个操作系统/编译器/CPU架构的组合上测试RapidJSON。但我们无法确保它能运行于你特定的平台上。只需要生成及执行单元测试便能获取答案。 + 社区已在多个操作系统/编译器/CPU 架构的组合上测试 RapidJSON。但我们无法确保它能运行于你特定的平台上。只需要生成及执行单元测试便能获取答案。 -8. RapidJSON支持C++03么?C++11呢? +8. RapidJSON 支持 C++03 么?C++11 呢? - RapidJSON开始时在C++03上实现。后来加入了可选的C++11特性支持(如转移构造函数、`noexcept`)。RapidJSON应该兼容所有遵从C++03或C++11的编译器。 + RapidJSON 开始时在 C++03 上实现。后来加入了可选的 C++11 特性支持(如转移构造函数、`noexcept`)。RapidJSON 应该兼容所有遵从 C++03 或 C++11 的编译器。 -9. RapidJSON是否真的用于实际应用? +9. RapidJSON 是否真的用于实际应用? - 是的。它被配置于前台及后台的真实应用中。一个社区成员说RapidJSON在他们的系统中每日解析5千万个JSON。 + 是的。它被配置于前台及后台的真实应用中。一个社区成员说 RapidJSON 在他们的系统中每日解析 5 千万个 JSON。 -10. RapidJSON是如何被测试的? +10. RapidJSON 是如何被测试的? - RapidJSON包含一组单元测试去执行自动测试。[Travis](https://travis-ci.org/miloyip/rapidjson/)(供Linux平台)及[AppVeyor](https://ci.appveyor.com/project/miloyip/rapidjson/)(供Windows平台)会对所有修改进行编译及执行单元测试。在Linux下还会使用Valgrind去检测内存泄漏。 + RapidJSON 包含一组单元测试去执行自动测试。[Travis](https://travis-ci.org/miloyip/rapidjson/)(供 Linux 平台)及 [AppVeyor](https://ci.appveyor.com/project/miloyip/rapidjson/)(供 Windows 平台)会对所有修改进行编译及执行单元测试。在 Linux 下还会使用 Valgrind 去检测内存泄漏。 -11. RapidJSON是否有完整的文档? +11. RapidJSON 是否有完整的文档? - RapidJSON提供了使用手册及API说明文档。 + RapidJSON 提供了使用手册及 API 说明文档。 12. 有没有其他替代品? - 有许多替代品。例如[nativejson-benchmark](https://github.com/miloyip/nativejson-benchmark)列出了一些开源的C/C++ JSON库。[json.org](http://www.json.org/)也有一个列表。 + 有许多替代品。例如 [nativejson-benchmark](https://github.com/miloyip/nativejson-benchmark) 列出了一些开源的 C/C++ JSON 库。[json.org](http://www.json.org/) 也有一个列表。 ## JSON -1. 什么是JSON? +1. 什么是 JSON? - JSON (JavaScript Object Notation)是一个轻量的数据交换格式。它使用人类可读的文本格式。更多关于JSON的细节可考[RFC7159](http://www.ietf.org/rfc/rfc7159.txt)及[ECMA-404](http://www.ecma-international.org/publications/standards/Ecma-404.htm)。 + JSON (JavaScript Object Notation) 是一个轻量的数据交换格式。它使用人类可读的文本格式。更多关于 JSON 的细节可考 [RFC7159](http://www.ietf.org/rfc/rfc7159.txt) 及 [ECMA-404](http://www.ecma-international.org/publications/standards/Ecma-404.htm)。 -2. JSON有什么应用场合? +2. JSON 有什么应用场合? - JSON常用于网页应用程序,以传送结构化数据。它也可作为文件格式用于数据持久化。 + JSON 常用于网页应用程序,以传送结构化数据。它也可作为文件格式用于数据持久化。 -2. RapidJSON是否符合JSON标准? +2. RapidJSON 是否符合 JSON 标准? - 是。RapidJSON完全符合[RFC7159](http://www.ietf.org/rfc/rfc7159.txt)及[ECMA-404](http://www.ecma-international.org/publications/standards/Ecma-404.htm)。它能处理一些特殊情况,例如支持JSON字符串中含有空字符及代理对(surrogate pair)。 + 是。RapidJSON 完全符合 [RFC7159](http://www.ietf.org/rfc/rfc7159.txt) 及 [ECMA-404](http://www.ecma-international.org/publications/standards/Ecma-404.htm)。它能处理一些特殊情况,例如支持 JSON 字符串中含有空字符及代理对(surrogate pair)。 -3. RapidJSON是否支持宽松的语法? +3. RapidJSON 是否支持宽松的语法? - 现时不支持。RapidJSON只支持严格的标准格式。宽松语法现时在这[issue](https://github.com/miloyip/rapidjson/issues/36)中进行讨论。 + 现时不支持。RapidJSON 只支持严格的标准格式。宽松语法现时在这 [issue](https://github.com/miloyip/rapidjson/issues/36) 中进行讨论。 -## DOM与SAX +## DOM 与 SAX -1. 什么是DOM风格API? +1. 什么是 DOM 风格 API? - Document Object Model(DOM)是一个储存于内存的JSON表示方式,用于查询及修改JSON。 + Document Object Model(DOM)是一个储存于内存的 JSON 表示方式,用于查询及修改 JSON。 -2. 什么是SAX风格API? +2. 什么是 SAX 风格 API? - SAX是一个事件驱动的API,用于解析及生成JSON。 + SAX 是一个事件驱动的 API,用于解析及生成 JSON。 -3. 我应用DOM还是SAX? +3. 我应用 DOM 还是 SAX? - DOM易于查询及修改。SAX则是非常快及省内存的,但通常较难使用。 + DOM 易于查询及修改。SAX 则是非常快及省内存的,但通常较难使用。 4. 什么是原位(*in situ*)解析? - 原位解析会把JSON字符串直接解码至输入的JSON中。这是一个优化,可减少内存消耗及提升性能,但输入的JSON会被更改。进一步细节请参考[原位解析](doc/dom.md) 。 + 原位解析会把 JSON 字符串直接解码至输入的 JSON 中。这是一个优化,可减少内存消耗及提升性能,但输入的 JSON 会被更改。进一步细节请参考 [原位解析](doc/dom.md) 。 5. 什么时候会产生解析错误? - 当输入的JSON包含非法语法,或不能表示一个值(如Number太大),或解析器的处理器中断解析过程,解析器都会产生一个错误。详情请参考[解析错误](doc/dom.md)。 + 当输入的 JSON 包含非法语法,或不能表示一个值(如 Number 太大),或解析器的处理器中断解析过程,解析器都会产生一个错误。详情请参考 [解析错误](doc/dom.md)。 6. 有什么错误信息? - 错误信息存储在`ParseResult`,它包含错误代号及偏移值(从JSON开始至错误处的字符数目)。可以把错误代号翻译为人类可读的错误讯息。 + 错误信息存储在 `ParseResult`,它包含错误代号及偏移值(从 JSON 开始至错误处的字符数目)。可以把错误代号翻译为人类可读的错误讯息。 -7. 为何不只使用`double`去表示JSON number? +7. 为何不只使用 `double` 去表示 JSON number? - 一些应用需要使用64位无号/有号整数。这些整数不能无损地转换成`double`。因此解析器会检测一个JSON number是否能转换至各种整数类型及`double`。 + 一些应用需要使用 64 位无号/有号整数。这些整数不能无损地转换成 `double`。因此解析器会检测一个 JSON number 是否能转换至各种整数类型及 `double`。 -8. 如何清空并最小化`document`或`value`的容量? +8. 如何清空并最小化 `document` 或 `value` 的容量? - 调用 `SetXXX()` 方法 - 这些方法会调用析构函数,并重建空的Object或Array: + 调用 `SetXXX()` 方法 - 这些方法会调用析构函数,并重建空的 Object 或 Array: ~~~~~~~~~~cpp Document d; @@ -112,7 +112,7 @@ d.SetObject(); // clear and minimize ~~~~~~~~~~ - 另外,也可以参考在 [C++ swap with temporary idiom](https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Clear-and-minimize)中的一种等价的方法: + 另外,也可以参考在 [C++ swap with temporary idiom](https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Clear-and-minimize) 中的一种等价的方法: ~~~~~~~~~~cpp Value(kObjectType).Swap(d); ~~~~~~~~~~ @@ -121,9 +121,9 @@ d.Swap(Value(kObjectType).Move()); ~~~~~~~~~~ -9. 如何将一个`document`节点插入到另一个`document`中? +9. 如何将一个 `document` 节点插入到另一个 `document` 中? - 比如有以下两个document(DOM): + 比如有以下两个 document(DOM): ~~~~~~~~~~cpp Document person; person.Parse("{\"person\":{\"name\":{\"first\":\"Adam\",\"last\":\"Thomas\"}}}"); @@ -131,7 +131,7 @@ Document address; address.Parse("{\"address\":{\"city\":\"Moscow\",\"street\":\"Quiet\"}}"); ~~~~~~~~~~ - 假设我们希望将整个 `address` 插入到`person`中,作为其的一个子节点: + 假设我们希望将整个 `address` 插入到 `person` 中,作为其的一个子节点: ~~~~~~~~~~js { "person": { "name": { "first": "Adam", "last": "Thomas" }, @@ -140,22 +140,22 @@ } ~~~~~~~~~~ - 在插入节点的过程中需要注意`document`和`value`的生命周期并且正确地使用allocator进行内存分配和管理。 + 在插入节点的过程中需要注意 `document` 和 `value` 的生命周期并且正确地使用 allocator 进行内存分配和管理。 - 一个简单有效的方法就是修改上述`address`变量的定义,让其使用`person`的allocator初始化,然后将其添加到根节点。 + 一个简单有效的方法就是修改上述 `address` 变量的定义,让其使用 `person` 的 allocator 初始化,然后将其添加到根节点。 ~~~~~~~~~~cpp Documnet address(person.GetAllocator()); ... person["person"].AddMember("address", address["address"], person.GetAllocator()); ~~~~~~~~~~ - 当然,如果你不想通过显式地写出`address`的key来得到其值,可以使用迭代器来实现: + 当然,如果你不想通过显式地写出 `address` 的 key 来得到其值,可以使用迭代器来实现: ~~~~~~~~~~cpp auto addressRoot = address.MemberBegin(); person["person"].AddMember(addressRoot->name, addressRoot->value, person.GetAllocator()); ~~~~~~~~~~ - 此外,还可以通过深拷贝address document来实现: + 此外,还可以通过深拷贝 address document 来实现: ~~~~~~~~~~cpp Value addressValue = Value(address["address"], person.GetAllocator()); person["person"].AddMember("address", addressValue, person.GetAllocator()); @@ -165,126 +165,126 @@ 1. 什么是转移语意?为什么? - `Value`不用复制语意,而使用了转移语意。这是指,当把来源值赋值于目标值时,来源值的所有权会转移至目标值。 + `Value` 不用复制语意,而使用了转移语意。这是指,当把来源值赋值于目标值时,来源值的所有权会转移至目标值。 由于转移快于复制,此设计决定强迫使用者注意到复制的消耗。 2. 怎样去复制一个值? - 有两个API可用:含allocator的构造函数,以及`CopyFrom()`。可参考[深复制Value](doc/tutorial.md)里的用例。 + 有两个 API 可用:含 allocator 的构造函数,以及 `CopyFrom()`。可参考 [深复制 Value](doc/tutorial.md) 里的用例。 3. 为什么我需要提供字符串的长度? - 由于C字符串是空字符结尾的,需要使用`strlen()`去计算其长度,这是线性复杂度的操作。若使用者已知字符串的长度,对很多操作来说会造成不必要的消耗。 + 由于 C 字符串是空字符结尾的,需要使用 `strlen()` 去计算其长度,这是线性复杂度的操作。若使用者已知字符串的长度,对很多操作来说会造成不必要的消耗。 - 此外,RapidJSON可处理含有`\u0000`(空字符)的字符串。若一个字符串含有空字符,`strlen()`便不能返回真正的字符串长度。在这种情况下使用者必须明确地提供字符串长度。 + 此外,RapidJSON 可处理含有 `\u0000`(空字符)的字符串。若一个字符串含有空字符,`strlen()` 便不能返回真正的字符串长度。在这种情况下使用者必须明确地提供字符串长度。 -4. 为什么在许多DOM操作API中要提供分配器作为参数? +4. 为什么在许多 DOM 操作 API 中要提供分配器作为参数? - 由于这些API是`Value`的成员函数,我们不希望为每个`Value`储存一个分配器指针。 + 由于这些 API 是 `Value` 的成员函数,我们不希望为每个 `Value` 储存一个分配器指针。 5. 它会转换各种数值类型么? - 当使用`GetInt()`、`GetUint()`等API时,可能会发生转换。对于整数至整数转换,仅当保证转换安全才会转换(否则会断言失败)。然而,当把一个64位有号/无号整数转换至double时,它会转换,但有可能会损失精度。含有小数的数字、或大于64位的整数,都只能使用`GetDouble()`获取其值。 + 当使用 `GetInt()`、`GetUint()` 等 API 时,可能会发生转换。对于整数至整数转换,仅当保证转换安全才会转换(否则会断言失败)。然而,当把一个 64 位有号/无号整数转换至 double 时,它会转换,但有可能会损失精度。含有小数的数字、或大于 64 位的整数,都只能使用 `GetDouble()` 获取其值。 ## Reader/Writer (SAX) -1. 为什么不仅仅用`printf`输出一个JSON?为什么需要`Writer`? +1. 为什么不仅仅用 `printf` 输出一个 JSON?为什么需要 `Writer`? - 最重要的是,`Writer`能确保输出的JSON是格式正确的。错误地调用SAX事件(如`StartObject()`错配`EndArray()`)会造成断言失败。此外,`Writer`会把字符串进行转义(如`\n`)。最后,`printf()`的数值输出可能并不是一个合法的JSON number,特别是某些locale会有数字分隔符。而且`Writer`的数值字符串转换是使用非常快的算法来实现的,胜过`printf()`及`iostream`。 + 最重要的是,`Writer` 能确保输出的 JSON 是格式正确的。错误地调用 SAX 事件(如 `StartObject()` 错配 `EndArray()`)会造成断言失败。此外,`Writer` 会把字符串进行转义(如 `\n`)。最后,`printf()` 的数值输出可能并不是一个合法的 JSON number,特别是某些 locale 会有数字分隔符。而且 `Writer` 的数值字符串转换是使用非常快的算法来实现的,胜过 `printf()` 及 `iostream`。 2. 我能否暂停解析过程,并在稍后继续? - 基于性能考虑,目前版本并不直接支持此功能。然而,若执行环境支持多线程,使用者可以在另一线程解析JSON,并通过阻塞输入流去暂停。 + 基于性能考虑,目前版本并不直接支持此功能。然而,若执行环境支持多线程,使用者可以在另一线程解析 JSON,并通过阻塞输入流去暂停。 ## Unicode -1. 它是否支持UTF-8、UTF-16及其他格式? +1. 它是否支持 UTF-8、UTF-16 及其他格式? - 是。它完全支持UTF-8、UTF-16(大端/小端)、UTF-32(大端/小端)及ASCII。 + 是。它完全支持 UTF-8、UTF-16(大端/小端)、UTF-32(大端/小端)及 ASCII。 2. 它能否检测编码的合法性? - 能。只需把`kParseValidateEncodingFlag`参考传给`Parse()`。若发现在输入流中有非法的编码,它就会产生`kParseErrorStringInvalidEncoding`错误。 + 能。只需把 `kParseValidateEncodingFlag` 参考传给 `Parse()`。若发现在输入流中有非法的编码,它就会产生 `kParseErrorStringInvalidEncoding` 错误。 -3. 什么是代理对(surrogate pair)?RapidJSON是否支持? +3. 什么是代理对(surrogate pair)?RapidJSON 是否支持? - JSON使用UTF-16编码去转义Unicode字符,例如`\u5927`表示中文字“大”。要处理基本多文种平面(basic multilingual plane,BMP)以外的字符时,UTF-16会把那些字符编码成两个16位值,这称为UTF-16代理对。例如,绘文字字符U+1F602在JSON中可被编码成`\uD83D\uDE02`。 + JSON 使用 UTF-16 编码去转义 Unicode 字符,例如 `\u5927` 表示中文字“大”。要处理基本多文种平面(basic multilingual plane,BMP)以外的字符时,UTF-16 会把那些字符编码成两个 16 位值,这称为 UTF-16 代理对。例如,绘文字字符 U+1F602 在 JSON 中可被编码成 `\uD83D\uDE02`。 - RapidJSON完全支持解析及生成UTF-16代理对。 + RapidJSON 完全支持解析及生成 UTF-16 代理对。 -4. 它能否处理JSON字符串中的`\u0000`(空字符)? +4. 它能否处理 JSON 字符串中的 `\u0000`(空字符)? - 能。RapidJSON完全支持JSON字符串中的空字符。然而,使用者需要注意到这件事,并使用`GetStringLength()`及相关API去取得字符串真正长度。 + 能。RapidJSON 完全支持 JSON 字符串中的空字符。然而,使用者需要注意到这件事,并使用 `GetStringLength()` 及相关 API 去取得字符串真正长度。 -5. 能否对所有非ASCII字符输出成`\uxxxx`形式? +5. 能否对所有非 ASCII 字符输出成 `\uxxxx` 形式? - 可以。只要在`Writer`中使用`ASCII<>`作为输出编码参数,就可以强逼转义那些字符。 + 可以。只要在 `Writer` 中使用 `ASCII<>` 作为输出编码参数,就可以强逼转义那些字符。 ## 流 -1. 我有一个很大的JSON文件。我应否把它整个载入内存中? +1. 我有一个很大的 JSON 文件。我应否把它整个载入内存中? - 使用者可使用`FileReadStream`去逐块读入文件。但若使用于原位解析,必须载入整个文件。 + 使用者可使用 `FileReadStream` 去逐块读入文件。但若使用于原位解析,必须载入整个文件。 -2. 我能否解析一个从网络上串流进来的JSON? +2. 我能否解析一个从网络上串流进来的 JSON? - 可以。使用者可根据`FileReadStream`的实现,去实现一个自定义的流。 + 可以。使用者可根据 `FileReadStream` 的实现,去实现一个自定义的流。 -3. 我不知道一些JSON将会使用哪种编码。怎样处理它们? +3. 我不知道一些 JSON 将会使用哪种编码。怎样处理它们? - 你可以使用`AutoUTFInputStream`,它能自动检测输入流的编码。然而,它会带来一些性能开销。 + 你可以使用 `AutoUTFInputStream`,它能自动检测输入流的编码。然而,它会带来一些性能开销。 -4. 什么是BOM?RapidJSON怎样处理它? +4. 什么是 BOM?RapidJSON 怎样处理它? - [字节顺序标记(byte order mark, BOM)](http://en.wikipedia.org/wiki/Byte_order_mark)有时会出现于文件/流的开始,以表示其UTF编码类型。 + [字节顺序标记(byte order mark, BOM)](http://en.wikipedia.org/wiki/Byte_order_mark) 有时会出现于文件/流的开始,以表示其 UTF 编码类型。 - RapidJSON的`EncodedInputStream`可检测/跳过BOM。`EncodedOutputStream`可选择是否写入BOM。可参考[编码流](doc/stream.md)中的例子。 + RapidJSON 的 `EncodedInputStream` 可检测/跳过 BOM。`EncodedOutputStream` 可选择是否写入 BOM。可参考 [编码流](doc/stream.md) 中的例子。 5. 为什么会涉及大端/小端? - 流的大端/小端是UTF-16及UTF-32流要处理的问题,而UTF-8不需要处理。 + 流的大端/小端是 UTF-16 及 UTF-32 流要处理的问题,而 UTF-8 不需要处理。 ## 性能 -1. RapidJSON是否真的快? +1. RapidJSON 是否真的快? - 是。它可能是最快的开源JSON库。有一个[评测](https://github.com/miloyip/nativejson-benchmark)评估C/C++ JSON库的性能。 + 是。它可能是最快的开源 JSON 库。有一个 [评测](https://github.com/miloyip/nativejson-benchmark) 评估 C/C++ JSON 库的性能。 2. 为什么它会快? - RapidJSON的许多设计是针对时间/空间性能来设计的,这些决定可能会影响API的易用性。此外,它也使用了许多底层优化(内部函数/intrinsic、SIMD)及特别的算法(自定义的double至字符串转换、字符串至double的转换)。 + RapidJSON 的许多设计是针对时间/空间性能来设计的,这些决定可能会影响 API 的易用性。此外,它也使用了许多底层优化(内部函数/intrinsic、SIMD)及特别的算法(自定义的 double 至字符串转换、字符串至 double 的转换)。 -3. 什是是SIMD?它如何用于RapidJSON? +3. 什是是 SIMD?它如何用于 RapidJSON? - [SIMD](http://en.wikipedia.org/wiki/SIMD)指令可以在现代CPU中执行并行运算。RapidJSON支持了Intel的SSE2/SSE4.2去加速跳过空白字符。在解析含缩进的JSON时,这能提升性能。只要定义名为`RAPIDJSON_SSE2`或`RAPIDJSON_SSE42`的宏,就能启动这个功能。然而,若在不支持这些指令集的机器上执行这些可执行文件,会导致崩溃。 + [SIMD](http://en.wikipedia.org/wiki/SIMD) 指令可以在现代 CPU 中执行并行运算。RapidJSON 支持了 Intel 的 SSE2/SSE4.2 去加速跳过空白字符。在解析含缩进的 JSON 时,这能提升性能。只要定义名为 `RAPIDJSON_SSE2` 或 `RAPIDJSON_SSE42` 的宏,就能启动这个功能。然而,若在不支持这些指令集的机器上执行这些可执行文件,会导致崩溃。 4. 它会消耗许多内存么? - RapidJSON的设计目标是减低内存占用。 + RapidJSON 的设计目标是减低内存占用。 - 在SAX API中,`Reader`消耗的内存与JSON树深度加上最长JSON字符成正比。 + 在 SAX API 中,`Reader` 消耗的内存与 JSON 树深度加上最长 JSON 字符成正比。 - 在DOM API中,每个`Value`在32/64位架构下分别消耗16/24字节。RapidJSON也使用一个特殊的内存分配器去减少分配的额外开销。 + 在 DOM API 中,每个 `Value` 在 32/64 位架构下分别消耗 16/24 字节。RapidJSON 也使用一个特殊的内存分配器去减少分配的额外开销。 5. 高性能的意义何在? - 有些应用程序需要处理非常大的JSON文件。而有些后台应用程序需要处理大量的JSON。达到高性能同时改善延时及吞吐量。更广义来说,这也可以节省能源。 + 有些应用程序需要处理非常大的 JSON 文件。而有些后台应用程序需要处理大量的 JSON。达到高性能同时改善延时及吞吐量。更广义来说,这也可以节省能源。 ## 八挂 -1. 谁是RapidJSON的开发者? +1. 谁是 RapidJSON 的开发者? - 叶劲峰(Milo Yip,[miloyip](https://github.com/miloyip))是RapidJSON的原作者。全世界许多贡献者一直在改善RapidJSON。Philipp A. Hartmann([pah](https://github.com/pah))实现了许多改进,也设置了自动化测试,而且还参与许多社区讨论。丁欧南(Don Ding,[thebusytypist](https://github.com/thebusytypist))实现了迭代式解析器。Andrii Senkovych([jollyroger](https://github.com/jollyroger))完成了向CMake的迁移。Kosta([Kosta-Github](https://github.com/Kosta-Github))提供了一个非常灵巧的短字符串优化。也需要感谢其他献者及社区成员。 + 叶劲峰(Milo Yip,[miloyip](https://github.com/miloyip))是 RapidJSON 的原作者。全世界许多贡献者一直在改善 RapidJSON。Philipp A. Hartmann([pah](https://github.com/pah))实现了许多改进,也设置了自动化测试,而且还参与许多社区讨论。丁欧南(Don Ding,[thebusytypist](https://github.com/thebusytypist))实现了迭代式解析器。Andrii Senkovych([jollyroger](https://github.com/jollyroger))完成了向 CMake 的迁移。Kosta([Kosta-Github](https://github.com/Kosta-Github))提供了一个非常灵巧的短字符串优化。也需要感谢其他献者及社区成员。 -2. 为何你要开发RapidJSON? +2. 为何你要开发 RapidJSON? - 在2011年开始这项目是,它仅一个兴趣项目。Milo Yip是一个游戏程序员,他在那时候认识到JSON并希望在未来的项目中使用。由于JSON好像很简单,他希望写一个仅有头文件并且快速的程序库。 + 在 2011 年开始这项目是,它仅一个兴趣项目。Milo Yip 是一个游戏程序员,他在那时候认识到 JSON 并希望在未来的项目中使用。由于 JSON 好像很简单,他希望写一个仅有头文件并且快速的程序库。 3. 为什么开发中段有一段长期空档? - 主要是个人因素,例如加入新家庭成员。另外,Milo Yip也花了许多业馀时间去翻译Jason Gregory的《Game Engine Architecture》至中文版《游戏引擎架构》。 + 主要是个人因素,例如加入新家庭成员。另外,Milo Yip 也花了许多业馀时间去翻译 Jason Gregory 的《Game Engine Architecture》至中文版《游戏引擎架构》。 -4. 为什么这个项目从Google Code搬到GitHub? +4. 为什么这个项目从 Google Code 搬到 GitHub? - 这是大势所趋,而且GitHub更为强大及方便。 + 这是大势所趋,而且 GitHub 更为强大及方便。 diff --git a/doc/features.zh-cn.md b/doc/features.zh-cn.md index 772d0d4..623cf62 100644 --- a/doc/features.zh-cn.md +++ b/doc/features.zh-cn.md @@ -3,50 +3,50 @@ ## 总体 * 跨平台 - * 编译器:Visual Studio、gcc、clang等 - * 架构:x86、x64、ARM等 - * 操作系统:Windows、Mac OS X、Linux、iOS、Android等 + * 编译器:Visual Studio、gcc、clang 等 + * 架构:x86、x64、ARM 等 + * 操作系统:Windows、Mac OS X、Linux、iOS、Android 等 * 容易安装 * 只有头文件的库。只需把头文件复制至你的项目中。 * 独立、最小依赖 - * 不需依赖STL、BOOST等。 - * 只包含``, ``, ``, ``, ``, ``。 -* 没使用C++异常、RTTI + * 不需依赖 STL、BOOST 等。 + * 只包含 ``, ``, ``, ``, ``, ``。 +* 没使用 C++ 异常、RTTI * 高性能 * 使用模版及内联函数去降低函数调用开销。 - * 内部经优化的Grisu2及浮点数解析实现。 - * 可选的SSE2/SSE4.2支持。 + * 内部经优化的 Grisu2 及浮点数解析实现。 + * 可选的 SSE2/SSE4.2 支持。 ## 符合标准 -* RapidJSON应完全符合RFC4627/ECMA-404标准。 -* 支持Unicod代理对(surrogate pair)。 +* RapidJSON 应完全符合 RFC4627/ECMA-404 标准。 +* 支持 Unicod 代理对(surrogate pair)。 * 支持空字符(`"\u0000"`)。 - * 例如,可以优雅地解析及处理`["Hello\u0000World"]`。含读写字符串长度的API。 + * 例如,可以优雅地解析及处理 `["Hello\u0000World"]`。含读写字符串长度的 API。 * 支持放宽的可选语法 - * 单行(`// ...`)及多行(`/* ... */`) 注释(`kParseCommentsFlag`)。 - * 在对象和数组结束前含逗号(`kParseTrailingCommasFlag`)。 + * 单行(`// ...`)及多行(`/* ... */`) 注释 (`kParseCommentsFlag`)。 + * 在对象和数组结束前含逗号 (`kParseTrailingCommasFlag`)。 ## Unicode -* 支持UTF-8、UTF-16、UTF-32编码,包括小端序和大端序。 +* 支持 UTF-8、UTF-16、UTF-32 编码,包括小端序和大端序。 * 这些编码用于输入输出流,以及内存中的表示。 * 支持从输入流自动检测编码。 * 内部支持编码的转换。 - * 例如,你可以读取一个UTF-8文件,让RapidJSON把JSON字符串转换至UTF-16的DOM。 + * 例如,你可以读取一个 UTF-8 文件,让 RapidJSON 把 JSON 字符串转换至 UTF-16 的 DOM。 * 内部支持编码校验。 - * 例如,你可以读取一个UTF-8文件,让RapidJSON检查是否所有JSON字符串是合法的UTF-8字节序列。 + * 例如,你可以读取一个 UTF-8 文件,让 RapidJSON 检查是否所有 JSON 字符串是合法的 UTF-8 字节序列。 * 支持自定义的字符类型。 - * 预设的字符类型是:UTF-8为`char`,UTF-16为`wchar_t`,UTF32为`uint32_t`。 + * 预设的字符类型是:UTF-8 为 `char`,UTF-16 为 `wchar_t`,UTF32 为 `uint32_t`。 * 支持自定义的编码。 -## API风格 +## API 风格 -* SAX(Simple API for XML)风格API - * 类似于[SAX](http://en.wikipedia.org/wiki/Simple_API_for_XML), RapidJSON提供一个事件循序访问的解析器API(`rapidjson::GenericReader`)。RapidJSON也提供一个生成器API(`rapidjson::Writer`),可以处理相同的事件集合。 -* DOM(Document Object Model)风格API - * 类似于HTML/XML的[DOM](http://en.wikipedia.org/wiki/Document_Object_Model),RapidJSON可把JSON解析至一个DOM表示方式(`rapidjson::GenericDocument`),以方便操作。如有需要,可把DOM转换(stringify)回JSON。 - * DOM风格API(`rapidjson::GenericDocument`)实际上是由SAX风格API(`rapidjson::GenericReader`)实现的。SAX更快,但有时DOM更易用。用户可根据情况作出选择。 +* SAX(Simple API for XML)风格 API + * 类似于 [SAX](http://en.wikipedia.org/wiki/Simple_API_for_XML), RapidJSON 提供一个事件循序访问的解析器 API(`rapidjson::GenericReader`)。RapidJSON 也提供一个生成器 API(`rapidjson::Writer`),可以处理相同的事件集合。 +* DOM(Document Object Model)风格 API + * 类似于 HTML/XML 的 [DOM](http://en.wikipedia.org/wiki/Document_Object_Model),RapidJSON 可把 JSON 解析至一个 DOM 表示方式(`rapidjson::GenericDocument`),以方便操作。如有需要,可把 DOM 转换(stringify)回 JSON。 + * DOM 风格 API(`rapidjson::GenericDocument`)实际上是由 SAX 风格 API(`rapidjson::GenericReader`)实现的。SAX 更快,但有时 DOM 更易用。用户可根据情况作出选择。 ## 解析 @@ -54,45 +54,45 @@ * 递归式解析器较快,但在极端情况下可出现堆栈溢出。 * 迭代式解析器使用自定义的堆栈去维持解析状态。 * 支持原位(*in situ*)解析。 - * 把JSON字符串的值解析至原JSON之中,然后让DOM指向那些字符串。 + * 把 JSON 字符串的值解析至原 JSON 之中,然后让 DOM 指向那些字符串。 * 比常规分析更快:不需字符串的内存分配、不需复制(如字符串不含转义符)、缓存友好。 -* 对于JSON数字类型,支持32-bit/64-bit的有号/无号整数,以及`double`。 +* 对于 JSON 数字类型,支持 32-bit/64-bit 的有号/无号整数,以及 `double`。 * 错误处理 * 支持详尽的解析错误代号。 * 支持本地化错误信息。 ## DOM (Document) -* RapidJSON在类型转换时会检查数值的范围。 +* RapidJSON 在类型转换时会检查数值的范围。 * 字符串字面量的优化 * 只储存指针,不作复制 * 优化“短”字符串 - * 在`Value`内储存短字符串,无需额外分配。 - * 对UTF-8字符串来说,32位架构下可存储最多11字符,64位下15字符。 -* 可选地支持`std::string`(定义`RAPIDJSON_HAS_STDSTRING=1`) + * 在 `Value` 内储存短字符串,无需额外分配。 + * 对 UTF-8 字符串来说,32 位架构下可存储最多 11 字符,64 位下 15 字符。 +* 可选地支持 `std::string`(定义 `RAPIDJSON_HAS_STDSTRING=1`) ## 生成 -* 支持`rapidjson::PrettyWriter`去加入换行及缩进。 +* 支持 `rapidjson::PrettyWriter` 去加入换行及缩进。 ## 输入输出流 -* 支持`rapidjson::GenericStringBuffer`,把输出的JSON储存于字符串内。 -* 支持`rapidjson::FileReadStream`及`rapidjson::FileWriteStream`,使用`FILE`对象作输入输出。 +* 支持 `rapidjson::GenericStringBuffer`,把输出的 JSON 储存于字符串内。 +* 支持 `rapidjson::FileReadStream` 及 `rapidjson::FileWriteStream`,使用 `FILE` 对象作输入输出。 * 支持自定义输入输出流。 ## 内存 -* 最小化DOM的内存开销。 - * 对大部分32/64位机器而言,每个JSON值只占16或20字节(不包含字符串)。 +* 最小化 DOM 的内存开销。 + * 对大部分 32/64 位机器而言,每个 JSON 值只占 16 或 20 字节(不包含字符串)。 * 支持快速的预设分配器。 * 它是一个堆栈形式的分配器(顺序分配,不容许单独释放,适合解析过程之用)。 - * 使用者也可提供一个预分配的缓冲区。(有可能达至无需CRT分配就能解析多个JSON) -* 支持标准CRT(C-runtime)分配器。 + * 使用者也可提供一个预分配的缓冲区。(有可能达至无需 CRT 分配就能解析多个 JSON) +* 支持标准 CRT(C-runtime)分配器。 * 支持自定义分配器。 ## 其他 -* 一些C++11的支持(可选) +* 一些 C++11 的支持(可选) * 右值引用(rvalue reference) - * `noexcept`修饰符 + * `noexcept` 修饰符 diff --git a/doc/performance.zh-cn.md b/doc/performance.zh-cn.md index b590fe0..da5d0c6 100644 --- a/doc/performance.zh-cn.md +++ b/doc/performance.zh-cn.md @@ -1,10 +1,10 @@ # 性能 -有一个[native JSON benchmark collection][1]项目,能评估20个JSON库在不同操作下的速度、內存用量及代码大小。 +有一个 [native JSON benchmark collection][1] 项目,能评估 20 个 JSON 库在不同操作下的速度、內存用量及代码大小。 [1]: https://github.com/miloyip/nativejson-benchmark -RapidJSON 0.1版本的性能测试文章位于[这里](https://code.google.com/p/rapidjson/wiki/Performance). +RapidJSON 0.1 版本的性能测试文章位于 [这里](https://code.google.com/p/rapidjson/wiki/Performance). 此外,你也可以参考以下这些第三方的评测。 diff --git a/doc/pointer.zh-cn.md b/doc/pointer.zh-cn.md index b340deb..d9bd9c3 100644 --- a/doc/pointer.zh-cn.md +++ b/doc/pointer.zh-cn.md @@ -25,7 +25,7 @@ JSON Pointer 是一个标准化([RFC6901])的方式去选取一个 JSON Docu 3. `"/foo/1"` → `"baz"` 4. `"/pi"` → `3.1416` -要注意,一个空 JSON Pointer `""` (零个token)解析为整个 JSON。 +要注意,一个空 JSON Pointer `""` (零个 token)解析为整个 JSON。 # 基本使用方法 {#BasicUsage} @@ -123,7 +123,7 @@ assert(success); Token `"0"` 在第一个 pointer 中被当作成员名字。它在第二个 pointer 中被当作成数组索引。 -其他函数会改变 DOM,包括`Create()`、`GetWithDefault()`、`Set()`、`Swap()`。这些函数总是成功的。若一些父值不存在,就会创建它们。若父值类型不匹配 token,也会强行改变其类型。改变类型也意味着完全移除其 DOM 子树的内容。 +其他函数会改变 DOM,包括 `Create()`、`GetWithDefault()`、`Set()`、`Swap()`。这些函数总是成功的。若一些父值不存在,就会创建它们。若父值类型不匹配 token,也会强行改变其类型。改变类型也意味着完全移除其 DOM 子树的内容。 例如,把上面的 JSON 解译至 `d` 之后, @@ -185,7 +185,7 @@ private: # URI 片段表示方式 {#URIFragment} -除了我们一直在使用的字符串方式表示 JSON pointer,[RFC6901]也定义了一个 JSON Pointer 的 URI 片段(fragment)表示方式。URI 片段是定义于 [RFC3986] "Uniform Resource Identifier (URI): Generic Syntax"。 +除了我们一直在使用的字符串方式表示 JSON pointer,[RFC6901] 也定义了一个 JSON Pointer 的 URI 片段(fragment)表示方式。URI 片段是定义于 [RFC3986] "Uniform Resource Identifier (URI): Generic Syntax"。 URI 片段的主要分别是必然以 `#` (pound sign)开头,而一些字符也会以百分比编码成 UTF-8 序列。例如,以下的表展示了不同表示法下的 C/C++ 字符串常数。 diff --git a/doc/sax.zh-cn.md b/doc/sax.zh-cn.md index 47306f6..b66957c 100644 --- a/doc/sax.zh-cn.md +++ b/doc/sax.zh-cn.md @@ -1,16 +1,16 @@ # SAX -"SAX"此术语源于[Simple API for XML](http://en.wikipedia.org/wiki/Simple_API_for_XML)。我们借了此术语去套用在JSON的解析及生成。 +"SAX" 此术语源于 [Simple API for XML](http://en.wikipedia.org/wiki/Simple_API_for_XML)。我们借了此术语去套用在 JSON 的解析及生成。 -在RapidJSON中,`Reader`(`GenericReader<...>`的typedef)是JSON的SAX风格解析器,而`Writer`(`GenericWriter<...>`的typedef)则是JSON的SAX风格生成器。 +在 RapidJSON 中,`Reader`(`GenericReader<...>` 的 typedef)是 JSON 的 SAX 风格解析器,而 `Writer`(`GenericWriter<...>` 的 typedef)则是 JSON 的 SAX 风格生成器。 [TOC] # Reader {#Reader} -`Reader`从输入流解析一个JSON。当它从流中读取字符时,它会基于JSON的语法去分析字符,并向处理器发送事件。 +`Reader` 从输入流解析一个 JSON。当它从流中读取字符时,它会基于 JSON 的语法去分析字符,并向处理器发送事件。 -例如,以下是一个JSON。 +例如,以下是一个 JSON。 ~~~~~~~~~~js { @@ -24,7 +24,7 @@ } ~~~~~~~~~~ -当一个`Reader`解析此JSON时,它会顺序地向处理器发送以下的事件: +当一个 `Reader` 解析此 JSON 时,它会顺序地向处理器发送以下的事件: ~~~~~~~~~~ StartObject() @@ -50,7 +50,7 @@ EndArray(4) EndObject(7) ~~~~~~~~~~ -除了一些事件参数需要再作解释,这些事件可以轻松地与JSON对上。我们可以看看`simplereader`例子怎样产生和以上完全相同的结果: +除了一些事件参数需要再作解释,这些事件可以轻松地与 JSON 对上。我们可以看看 `simplereader` 例子怎样产生和以上完全相同的结果: ~~~~~~~~~~cpp #include "rapidjson/reader.h" @@ -91,11 +91,11 @@ void main() { } ~~~~~~~~~~ -注意RapidJSON使用模板去静态挷定`Reader`类型及处理器的类形,而不是使用含虚函数的类。这个范式可以通过把函数内联而改善性能。 +注意 RapidJSON 使用模板去静态挷定 `Reader` 类型及处理器的类形,而不是使用含虚函数的类。这个范式可以通过把函数内联而改善性能。 ## 处理器 {#Handler} -如前例所示,使用者需要实现一个处理器(handler),用于处理来自`Reader`的事件(函数调用)。处理器必须包含以下的成员函数。 +如前例所示,使用者需要实现一个处理器(handler),用于处理来自 `Reader` 的事件(函数调用)。处理器必须包含以下的成员函数。 ~~~~~~~~~~cpp class Handler { @@ -115,25 +115,25 @@ class Handler { }; ~~~~~~~~~~ -当`Reader`遇到JSON null值时会调用`Null()`。 +当 `Reader` 遇到 JSON null 值时会调用 `Null()`。 -当`Reader`遇到JSON true或false值时会调用`Bool(bool)`。 +当 `Reader` 遇到 JSON true 或 false 值时会调用 `Bool(bool)`。 -当`Reader`遇到JSON number,它会选择一个合适的C++类型映射,然后调用`Int(int)`、`Uint(unsigned)`、`Int64(int64_t)`、`Uint64(uint64_t)`及`Double(double)`的*其中之一个*。 若开启了 `kParseNumbersAsStrings` 选项,`Reader` 便会改为调用 `RawNumber()`。 +当 `Reader` 遇到 JSON number,它会选择一个合适的 C++ 类型映射,然后调用 `Int(int)`、`Uint(unsigned)`、`Int64(int64_t)`、`Uint64(uint64_t)` 及 `Double(double)` 的 * 其中之一个 *。 若开启了 `kParseNumbersAsStrings` 选项,`Reader` 便会改为调用 `RawNumber()`。 -当`Reader`遇到JSON string,它会调用`String(const char* str, SizeType length, bool copy)`。第一个参数是字符串的指针。第二个参数是字符串的长度(不包含空终止符号)。注意RapidJSON支持字串中含有空字符`'\0'`。若出现这种情况,便会有`strlen(str) < length`。最后的`copy`参数表示处理器是否需要复制该字符串。在正常解析时,`copy = true`。仅当使用原位解析时,`copy = false`。此外,还要注意字符的类型与目标编码相关,我们稍后会再谈这一点。 +当 `Reader` 遇到 JSON string,它会调用 `String(const char* str, SizeType length, bool copy)`。第一个参数是字符串的指针。第二个参数是字符串的长度(不包含空终止符号)。注意 RapidJSON 支持字串中含有空字符 `'\0'`。若出现这种情况,便会有 `strlen(str) < length`。最后的 `copy` 参数表示处理器是否需要复制该字符串。在正常解析时,`copy = true`。仅当使用原位解析时,`copy = false`。此外,还要注意字符的类型与目标编码相关,我们稍后会再谈这一点。 -当`Reader`遇到JSON object的开始之时,它会调用`StartObject()`。JSON的object是一个键值对(成员)的集合。若object包含成员,它会先为成员的名字调用`Key()`,然后再按值的类型调用函数。它不断调用这些键值对,直至最终调用`EndObject(SizeType memberCount)`。注意`memberCount`参数对处理器来说只是协助性质,使用者可能不需要此参数。 +当 `Reader` 遇到 JSON object 的开始之时,它会调用 `StartObject()`。JSON 的 object 是一个键值对(成员)的集合。若 object 包含成员,它会先为成员的名字调用 `Key()`,然后再按值的类型调用函数。它不断调用这些键值对,直至最终调用 `EndObject(SizeType memberCount)`。注意 `memberCount` 参数对处理器来说只是协助性质,使用者可能不需要此参数。 -JSON array与object相似,但更简单。在array开始时,`Reader`会调用`BeginArary()`。若array含有元素,它会按元素的类型来读用函数。相似地,最后它会调用`EndArray(SizeType elementCount)`,其中`elementCount`参数对处理器来说只是协助性质。 +JSON array 与 object 相似,但更简单。在 array 开始时,`Reader` 会调用 `BeginArary()`。若 array 含有元素,它会按元素的类型来读用函数。相似地,最后它会调用 `EndArray(SizeType elementCount)`,其中 `elementCount` 参数对处理器来说只是协助性质。 -每个处理器函数都返回一个`bool`。正常它们应返回`true`。若处理器遇到错误,它可以返回`false`去通知事件发送方停止继续处理。 +每个处理器函数都返回一个 `bool`。正常它们应返回 `true`。若处理器遇到错误,它可以返回 `false` 去通知事件发送方停止继续处理。 -例如,当我们用`Reader`解析一个JSON时,处理器检测到该JSON并不符合所需的schema,那么处理器可以返回`false`,令`Reader`停止之后的解析工作。而`Reader`会进入一个错误状态,并以`kParseErrorTermination`错误码标识。 +例如,当我们用 `Reader` 解析一个 JSON 时,处理器检测到该 JSON 并不符合所需的 schema,那么处理器可以返回 `false`,令 `Reader` 停止之后的解析工作。而 `Reader` 会进入一个错误状态,并以 `kParseErrorTermination` 错误码标识。 ## GenericReader {#GenericReader} -前面提及,`Reader`是`GenericReader`模板类的typedef: +前面提及,`Reader` 是 `GenericReader` 模板类的 typedef: ~~~~~~~~~~cpp namespace rapidjson { @@ -148,19 +148,19 @@ typedef GenericReader, UTF8<> > Reader; } // namespace rapidjson ~~~~~~~~~~ -`Reader`使用UTF-8作为来源及目标编码。来源编码是指JSON流的编码。目标编码是指`String()`的`str`参数所用的编码。例如,要解析一个UTF-8流并输出至UTF-16 string事件,你需要这么定义一个reader: +`Reader` 使用 UTF-8 作为来源及目标编码。来源编码是指 JSON 流的编码。目标编码是指 `String()` 的 `str` 参数所用的编码。例如,要解析一个 UTF-8 流并输出至 UTF-16 string 事件,你需要这么定义一个 reader: ~~~~~~~~~~cpp GenericReader, UTF16<> > reader; ~~~~~~~~~~ -注意到`UTF16`的缺省类型是`wchar_t`。因此这个`reader`需要调用处理器的`String(const wchar_t*, SizeType, bool)`。 +注意到 `UTF16` 的缺省类型是 `wchar_t`。因此这个 `reader` 需要调用处理器的 `String(const wchar_t*, SizeType, bool)`。 -第三个模板参数`Allocator`是内部数据结构(实际上是一个堆栈)的分配器类型。 +第三个模板参数 `Allocator` 是内部数据结构(实际上是一个堆栈)的分配器类型。 ## 解析 {#Parsing} -`Reader`的唯一功能就是解析JSON。 +`Reader` 的唯一功能就是解析 JSON。 ~~~~~~~~~~cpp template @@ -171,15 +171,15 @@ template bool Parse(InputStream& is, Handler& handler); ~~~~~~~~~~ -若在解析中出现错误,它会返回`false`。使用者可调用`bool HasParseEror()`, `ParseErrorCode GetParseErrorCode()`及`size_t GetErrorOffset()`获取错误状态。实际上`Document`使用这些`Reader`函数去获取解析错误。请参考[DOM](doc/dom.md)去了解有关解析错误的细节。 +若在解析中出现错误,它会返回 `false`。使用者可调用 `bool HasParseEror()`, `ParseErrorCode GetParseErrorCode()` 及 `size_t GetErrorOffset()` 获取错误状态。实际上 `Document` 使用这些 `Reader` 函数去获取解析错误。请参考 [DOM](doc/dom.md) 去了解有关解析错误的细节。 # Writer {#Writer} -`Reader`把JSON转换(解析)成为事件。`Writer`做完全相反的事情。它把事件转换成JSON。 +`Reader` 把 JSON 转换(解析)成为事件。`Writer` 做完全相反的事情。它把事件转换成 JSON。 -`Writer`是非常容易使用的。若你的应用程序只需把一些数据转换成JSON,可能直接使用`Writer`,会比建立一个`Document`然后用`Writer`把它转换成JSON更加方便。 +`Writer` 是非常容易使用的。若你的应用程序只需把一些数据转换成 JSON,可能直接使用 `Writer`,会比建立一个 `Document` 然后用 `Writer` 把它转换成 JSON 更加方便。 -在`simplewriter`例子里,我们做`simplereader`完全相反的事情。 +在 `simplewriter` 例子里,我们做 `simplereader` 完全相反的事情。 ~~~~~~~~~~cpp #include "rapidjson/writer.h" @@ -221,24 +221,24 @@ void main() { {"hello":"world","t":true,"f":false,"n":null,"i":123,"pi":3.1416,"a":[0,1,2,3]} ~~~~~~~~~~ -`String()`及`Key()`各有两个重载。一个是如处理器concept般,有3个参数。它能处理含空字符的字符串。另一个是如上中使用的较简单版本。 +`String()` 及 `Key()` 各有两个重载。一个是如处理器 concept 般,有 3 个参数。它能处理含空字符的字符串。另一个是如上中使用的较简单版本。 -注意到,例子代码中的`EndArray()`及`EndObject()`并没有参数。可以传递一个`SizeType`的参数,但它会被`Writer`忽略。 +注意到,例子代码中的 `EndArray()` 及 `EndObject()` 并没有参数。可以传递一个 `SizeType` 的参数,但它会被 `Writer` 忽略。 -你可能会怀疑,为什么不使用`sprintf()`或`std::stringstream`去建立一个JSON? +你可能会怀疑,为什么不使用 `sprintf()` 或 `std::stringstream` 去建立一个 JSON? 这有几个原因: -1. `Writer`必然会输出一个结构良好(well-formed)的JSON。若然有错误的事件次序(如`Int()`紧随`StartObject()`出现),它会在调试模式中产生断言失败。 -2. `Writer::String()`可处理字符串转义(如把码点`U+000A`转换成`\n`)及进行Unicode转码。 -3. `Writer`一致地处理number的输出。 -4. `Writer`实现了事件处理器concept。可用于处理来自`Reader`、`Document`或其他事件发生器。 -5. `Writer`可对不同平台进行优化。 +1. `Writer` 必然会输出一个结构良好(well-formed)的 JSON。若然有错误的事件次序(如 `Int()` 紧随 `StartObject()` 出现),它会在调试模式中产生断言失败。 +2. `Writer::String()` 可处理字符串转义(如把码点 `U+000A` 转换成 `\n`)及进行 Unicode 转码。 +3. `Writer` 一致地处理 number 的输出。 +4. `Writer` 实现了事件处理器 concept。可用于处理来自 `Reader`、`Document` 或其他事件发生器。 +5. `Writer` 可对不同平台进行优化。 -无论如何,使用`Writer` API去生成JSON甚至乎比这些临时方法更简单。 +无论如何,使用 `Writer` API 去生成 JSON 甚至乎比这些临时方法更简单。 ## 模板 {#WriterTemplate} -`Writer`与`Reader`有少许设计区别。`Writer`是一个模板类,而不是一个typedef。 并没有`GenericWriter`。以下是`Writer`的声明。 +`Writer` 与 `Reader` 有少许设计区别。`Writer` 是一个模板类,而不是一个 typedef。 并没有 `GenericWriter`。以下是 `Writer` 的声明。 ~~~~~~~~~~cpp namespace rapidjson { @@ -253,39 +253,39 @@ public: } // namespace rapidjson ~~~~~~~~~~ -`OutputStream`模板参数是输出流的类型。它的类型不可以被自动推断,必须由使用者提供。 +`OutputStream` 模板参数是输出流的类型。它的类型不可以被自动推断,必须由使用者提供。 -`SourceEncoding`模板参数指定了`String(const Ch*, ...)`的编码。 +`SourceEncoding` 模板参数指定了 `String(const Ch*, ...)` 的编码。 -`TargetEncoding`模板参数指定输出流的编码。 +`TargetEncoding` 模板参数指定输出流的编码。 -最后一个`Allocator`是分配器的类型,用于分配内部数据结构(一个堆栈)。 +最后一个 `Allocator` 是分配器的类型,用于分配内部数据结构(一个堆栈)。 -此外,`Writer`的构造函数有一`levelDepth`参数。存储每层阶信息的初始内存分配量受此参数影响。 +此外,`Writer` 的构造函数有一 `levelDepth` 参数。存储每层阶信息的初始内存分配量受此参数影响。 ## PrettyWriter {#PrettyWriter} -`Writer`所输出的是没有空格字符的最紧凑JSON,适合网络传输或储存,但不适合人类阅读。 +`Writer` 所输出的是没有空格字符的最紧凑 JSON,适合网络传输或储存,但不适合人类阅读。 -因此,RapidJSON提供了一个`PrettyWriter`,它在输出中加入缩进及换行。 +因此,RapidJSON 提供了一个 `PrettyWriter`,它在输出中加入缩进及换行。 -`PrettyWriter`的用法与`Writer`几乎一样,不同之处是`PrettyWriter`提供了一个`SetIndent(Ch indentChar, unsigned indentCharCount)`函数。缺省的缩进是4个空格。 +`PrettyWriter` 的用法与 `Writer` 几乎一样,不同之处是 `PrettyWriter` 提供了一个 `SetIndent(Ch indentChar, unsigned indentCharCount)` 函数。缺省的缩进是 4 个空格。 ## 完整性及重置 {#CompletenessReset} -一个`Writer`只可输出单个JSON,其根节点可以是任何JSON类型。当处理完单个根节点事件(如`String()`),或匹配的最后`EndObject()`或`EndArray()`事件,输出的JSON是结构完整(well-formed)及完整的。使用者可调用`Writer::IsComplete()`去检测完整性。 +一个 `Writer` 只可输出单个 JSON,其根节点可以是任何 JSON 类型。当处理完单个根节点事件(如 `String()`),或匹配的最后 `EndObject()` 或 `EndArray()` 事件,输出的 JSON 是结构完整(well-formed)及完整的。使用者可调用 `Writer::IsComplete()` 去检测完整性。 -当JSON完整时,`Writer`不能再接受新的事件。不然其输出便会是不合法的(例如有超过一个根节点)。为了重新利用`Writer`对象,使用者可调用`Writer::Reset(OutputStream& os)`去重置其所有内部状态及设置新的输出流。 +当 JSON 完整时,`Writer` 不能再接受新的事件。不然其输出便会是不合法的(例如有超过一个根节点)。为了重新利用 `Writer` 对象,使用者可调用 `Writer::Reset(OutputStream& os)` 去重置其所有内部状态及设置新的输出流。 # 技巧 {#Techniques} -## 解析JSON至自定义结构 {#CustomDataStructure} +## 解析 JSON 至自定义结构 {#CustomDataStructure} -`Document`的解析功能完全依靠`Reader`。实际上`Document`是一个处理器,在解析JSON时接收事件去建立一个DOM。 +`Document` 的解析功能完全依靠 `Reader`。实际上 `Document` 是一个处理器,在解析 JSON 时接收事件去建立一个 DOM。 -使用者可以直接使用`Reader`去建立其他数据结构。这消除了建立DOM的步骤,从而减少了内存开销并改善性能。 +使用者可以直接使用 `Reader` 去建立其他数据结构。这消除了建立 DOM 的步骤,从而减少了内存开销并改善性能。 -在以下的`messagereader`例子中,`ParseMessages()`解析一个JSON,该JSON应该是一个含键值对的object。 +在以下的 `messagereader` 例子中,`ParseMessages()` 解析一个 JSON,该 JSON 应该是一个含键值对的 object。 ~~~~~~~~~~cpp #include "rapidjson/reader.h" @@ -386,15 +386,15 @@ Error: Terminate parsing due to Handler error. at offset 59 near '} }...' ~~~~~~~~~~ -第一个JSON(`json1`)被成功地解析至`MessageMap`。由于`MessageMap`是一个`std::map`,打印次序按键值排序。此次序与JSON中的次序不同。 +第一个 JSON(`json1`)被成功地解析至 `MessageMap`。由于 `MessageMap` 是一个 `std::map`,打印次序按键值排序。此次序与 JSON 中的次序不同。 -在第二个JSON(`json2`)中,`foo`的值是一个空object。由于它是一个object,`MessageHandler::StartObject()`会被调用。然而,在`state_ = kExpectValue`的情况下,该函数会返回`false`,并导致解析过程终止。错误代码是`kParseErrorTermination`。 +在第二个 JSON(`json2`)中,`foo` 的值是一个空 object。由于它是一个 object,`MessageHandler::StartObject()` 会被调用。然而,在 `state_ = kExpectValue` 的情况下,该函数会返回 `false`,并导致解析过程终止。错误代码是 `kParseErrorTermination`。 -## 过滤JSON {#Filtering} +## 过滤 JSON {#Filtering} -如前面提及过,`Writer`可处理`Reader`发出的事件。`example/condense/condense.cpp`例子简单地设置`Writer`作为一个`Reader`的处理器,因此它能移除JSON中的所有空白字符。`example/pretty/pretty.cpp`例子使用同样的关系,只是以`PrettyWriter`取代`Writer`。因此`pretty`能够重新格式化JSON,加入缩进及换行。 +如前面提及过,`Writer` 可处理 `Reader` 发出的事件。`example/condense/condense.cpp` 例子简单地设置 `Writer` 作为一个 `Reader` 的处理器,因此它能移除 JSON 中的所有空白字符。`example/pretty/pretty.cpp` 例子使用同样的关系,只是以 `PrettyWriter` 取代 `Writer`。因此 `pretty` 能够重新格式化 JSON,加入缩进及换行。 -实际上,我们可以使用SAX风格API去加入(多个)中间层去过滤JSON的内容。例如`capitalize`例子可以把所有JSON string改为大写。 +实际上,我们可以使用 SAX 风格 API 去加入(多个)中间层去过滤 JSON 的内容。例如 `capitalize` 例子可以把所有 JSON string 改为大写。 ~~~~~~~~~~cpp #include "rapidjson/reader.h" @@ -458,20 +458,20 @@ int main(int, char*[]) { } ~~~~~~~~~~ -注意到,不可简单地把JSON当作字符串去改为大写。例如: +注意到,不可简单地把 JSON 当作字符串去改为大写。例如: ~~~~~~~~~~ ["Hello\nWorld"] ~~~~~~~~~~ -简单地把整个JSON转为大写的话会产生错误的转义符: +简单地把整个 JSON 转为大写的话会产生错误的转义符: ~~~~~~~~~~ ["HELLO\NWORLD"] ~~~~~~~~~~ -而`capitalize`就会产生正确的结果: +而 `capitalize` 就会产生正确的结果: ~~~~~~~~~~ ["HELLO\nWORLD"] ~~~~~~~~~~ -我们还可以开发更复杂的过滤器。然而,由于SAX风格API在某一时间点只能提供单一事件的信息,使用者需要自行记录一些上下文信息(例如从根节点起的路径、储存其他相关值)。对于处理某些情况,用DOM会比SAX更容易实现。 +我们还可以开发更复杂的过滤器。然而,由于 SAX 风格 API 在某一时间点只能提供单一事件的信息,使用者需要自行记录一些上下文信息(例如从根节点起的路径、储存其他相关值)。对于处理某些情况,用 DOM 会比 SAX 更容易实现。 diff --git a/doc/schema.zh-cn.md b/doc/schema.zh-cn.md index a60cd82..95f5a69 100644 --- a/doc/schema.zh-cn.md +++ b/doc/schema.zh-cn.md @@ -146,7 +146,7 @@ if (!d.Accept(validator)) { ## 远程 Schema -JSON Schema 支持 [`$ref` 关键字](http://spacetelescope.github.io/understanding-json-schema/structuring.html),它是一个[JSON pointer](pointer.md) 引用至一个本地(local)或远程(remote) schema。本地指针的首字符是 `#`,而远程指针是一个相对或绝对 URI。例如: +JSON Schema 支持 [`$ref` 关键字](http://spacetelescope.github.io/understanding-json-schema/structuring.html),它是一个 [JSON pointer](pointer.md) 引用至一个本地(local)或远程(remote) schema。本地指针的首字符是 `#`,而远程指针是一个相对或绝对 URI。例如: ~~~js { "$ref": "definitions.json#/address" } diff --git a/doc/stream.zh-cn.md b/doc/stream.zh-cn.md index 5cc9c0d..f2c54f7 100644 --- a/doc/stream.zh-cn.md +++ b/doc/stream.zh-cn.md @@ -1,16 +1,16 @@ # 流 -在RapidJSON中,`rapidjson::Stream`是用於读写JSON的概念(概念是指C++的concept)。在这里我们先介绍如何使用RapidJSON提供的各种流。然后再看看如何自行定义流。 +在 RapidJSON 中,`rapidjson::Stream` 是用於读写 JSON 的概念(概念是指 C++ 的 concept)。在这里我们先介绍如何使用 RapidJSON 提供的各种流。然后再看看如何自行定义流。 [TOC] # 内存流 {#MemoryStreams} -内存流把JSON存储在内存之中。 +内存流把 JSON 存储在内存之中。 ## StringStream(输入){#StringStream} -`StringStream`是最基本的输入流,它表示一个完整的、只读的、存储于内存的JSON。它在`rapidjson/rapidjson.h`中定义。 +`StringStream` 是最基本的输入流,它表示一个完整的、只读的、存储于内存的 JSON。它在 `rapidjson/rapidjson.h` 中定义。 ~~~~~~~~~~cpp #include "rapidjson/document.h" // 会包含 "rapidjson/rapidjson.h" @@ -25,7 +25,7 @@ Document d; d.ParseStream(s); ~~~~~~~~~~ -由于这是非常常用的用法,RapidJSON提供`Document::Parse(const char*)`去做完全相同的事情: +由于这是非常常用的用法,RapidJSON 提供 `Document::Parse(const char*)` 去做完全相同的事情: ~~~~~~~~~~cpp // ... @@ -34,11 +34,11 @@ Document d; d.Parse(json); ~~~~~~~~~~ -需要注意,`StringStream`是`GenericStringStream >`的typedef,使用者可用其他编码类去代表流所使用的字符集。 +需要注意,`StringStream` 是 `GenericStringStream >` 的 typedef,使用者可用其他编码类去代表流所使用的字符集。 ## StringBuffer(输出){#StringBuffer} -`StringBuffer`是一个简单的输出流。它分配一个内存缓冲区,供写入整个JSON。可使用`GetString()`来获取该缓冲区。 +`StringBuffer` 是一个简单的输出流。它分配一个内存缓冲区,供写入整个 JSON。可使用 `GetString()` 来获取该缓冲区。 ~~~~~~~~~~cpp #include "rapidjson/stringbuffer.h" @@ -50,26 +50,26 @@ d.Accept(writer); const char* output = buffer.GetString(); ~~~~~~~~~~ -当缓冲区满溢,它将自动增加容量。缺省容量是256个字符(UTF8是256字节,UTF16是512字节等)。使用者能自行提供分配器及初始容量。 +当缓冲区满溢,它将自动增加容量。缺省容量是 256 个字符(UTF8 是 256 字节,UTF16 是 512 字节等)。使用者能自行提供分配器及初始容量。 ~~~~~~~~~~cpp StringBuffer buffer1(0, 1024); // 使用它的分配器,初始大小 = 1024 StringBuffer buffer2(allocator, 1024); ~~~~~~~~~~ -如无设置分配器,`StringBuffer`会自行实例化一个内部分配器。 +如无设置分配器,`StringBuffer` 会自行实例化一个内部分配器。 -相似地,`StringBuffer`是`GenericStringBuffer >`的typedef。 +相似地,`StringBuffer` 是 `GenericStringBuffer >` 的 typedef。 # 文件流 {#FileStreams} -当要从文件解析一个JSON,你可以把整个JSON读入内存并使用上述的`StringStream`。 +当要从文件解析一个 JSON,你可以把整个 JSON 读入内存并使用上述的 `StringStream`。 -然而,若JSON很大,或是内存有限,你可以改用`FileReadStream`。它只会从文件读取一部分至缓冲区,然后让那部分被解析。若缓冲区的字符都被读完,它会再从文件读取下一部分。 +然而,若 JSON 很大,或是内存有限,你可以改用 `FileReadStream`。它只会从文件读取一部分至缓冲区,然后让那部分被解析。若缓冲区的字符都被读完,它会再从文件读取下一部分。 ## FileReadStream(输入) {#FileReadStream} -`FileReadStream`通过`FILE`指针读取文件。使用者需要提供一个缓冲区。 +`FileReadStream` 通过 `FILE` 指针读取文件。使用者需要提供一个缓冲区。 ~~~~~~~~~~cpp #include "rapidjson/filereadstream.h" @@ -77,7 +77,7 @@ StringBuffer buffer2(allocator, 1024); using namespace rapidjson; -FILE* fp = fopen("big.json", "rb"); // 非Windows平台使用"r" +FILE* fp = fopen("big.json", "rb"); // 非 Windows 平台使用 "r" char readBuffer[65536]; FileReadStream is(fp, readBuffer, sizeof(readBuffer)); @@ -88,13 +88,13 @@ d.ParseStream(is); fclose(fp); ~~~~~~~~~~ -与`StringStreams`不一样,`FileReadStream`是一个字节流。它不处理编码。若文件并非UTF-8编码,可以把字节流用`EncodedInputStream`包装。我们很快会讨论这个问题。 +与 `StringStreams` 不一样,`FileReadStream` 是一个字节流。它不处理编码。若文件并非 UTF-8 编码,可以把字节流用 `EncodedInputStream` 包装。我们很快会讨论这个问题。 -除了读取文件,使用者也可以使用`FileReadStream`来读取`stdin`。 +除了读取文件,使用者也可以使用 `FileReadStream` 来读取 `stdin`。 ## FileWriteStream(输出){#FileWriteStream} -`FileWriteStream`是一个含缓冲功能的输出流。它的用法与`FileReadStream`非常相似。 +`FileWriteStream` 是一个含缓冲功能的输出流。它的用法与 `FileReadStream` 非常相似。 ~~~~~~~~~~cpp #include "rapidjson/filewritestream.h" @@ -106,7 +106,7 @@ Document d; d.Parse(json); // ... -FILE* fp = fopen("output.json", "wb"); // 非Windows平台使用"w" +FILE* fp = fopen("output.json", "wb"); // 非 Windows 平台使用 "w" char writeBuffer[65536]; FileWriteStream os(fp, writeBuffer, sizeof(writeBuffer)); @@ -117,11 +117,11 @@ d.Accept(writer); fclose(fp); ~~~~~~~~~~ -它也可以把输出导向`stdout`。 +它也可以把输出导向 `stdout`。 # iostream 包装类 {#iostreamWrapper} -基于用户的要求,RapidJSON提供了正式的 `std::basic_istream` 和 `std::basic_ostream` 包装类。然而,请注意其性能会大大低于以上的其他流。 +基于用户的要求,RapidJSON 提供了正式的 `std::basic_istream` 和 `std::basic_ostream` 包装类。然而,请注意其性能会大大低于以上的其他流。 ## IStreamWrapper {#IStreamWrapper} @@ -173,19 +173,19 @@ d.Accept(writer); # 编码流 {#EncodedStreams} -编码流(encoded streams)本身不存储JSON,它们是通过包装字节流来提供基本的编码/解码功能。 +编码流(encoded streams)本身不存储 JSON,它们是通过包装字节流来提供基本的编码/解码功能。 -如上所述,我们可以直接读入UTF-8字节流。然而,UTF-16及UTF-32有字节序(endian)问题。要正确地处理字节序,需要在读取时把字节转换成字符(如对UTF-16使用`wchar_t`),以及在写入时把字符转换为字节。 +如上所述,我们可以直接读入 UTF-8 字节流。然而,UTF-16 及 UTF-32 有字节序(endian)问题。要正确地处理字节序,需要在读取时把字节转换成字符(如对 UTF-16 使用 `wchar_t`),以及在写入时把字符转换为字节。 -除此以外,我们也需要处理[字节顺序标记(byte order mark, BOM)](http://en.wikipedia.org/wiki/Byte_order_mark)。当从一个字节流读取时,需要检测BOM,或者仅仅是把存在的BOM消去。当把JSON写入字节流时,也可选择写入BOM。 +除此以外,我们也需要处理 [字节顺序标记(byte order mark, BOM)](http://en.wikipedia.org/wiki/Byte_order_mark)。当从一个字节流读取时,需要检测 BOM,或者仅仅是把存在的 BOM 消去。当把 JSON 写入字节流时,也可选择写入 BOM。 -若一个流的编码在编译期已知,你可使用`EncodedInputStream`及`EncodedOutputStream`。若一个流可能存储UTF-8、UTF-16LE、UTF-16BE、UTF-32LE、UTF-32BE的JSON,并且编码只能在运行时得知,你便可以使用`AutoUTFInputStream`及`AutoUTFOutputStream`。这些流定义在`rapidjson/encodedstream.h`。 +若一个流的编码在编译期已知,你可使用 `EncodedInputStream` 及 `EncodedOutputStream`。若一个流可能存储 UTF-8、UTF-16LE、UTF-16BE、UTF-32LE、UTF-32BE 的 JSON,并且编码只能在运行时得知,你便可以使用 `AutoUTFInputStream` 及 `AutoUTFOutputStream`。这些流定义在 `rapidjson/encodedstream.h`。 注意到,这些编码流可以施于文件以外的流。例如,你可以用编码流包装内存中的文件或自定义的字节流。 ## EncodedInputStream {#EncodedInputStream} -`EncodedInputStream`含两个模板参数。第一个是`Encoding`类型,例如定义于`rapidjson/encodings.h`的`UTF8`、`UTF16LE`。第二个参数是被包装的流的类型。 +`EncodedInputStream` 含两个模板参数。第一个是 `Encoding` 类型,例如定义于 `rapidjson/encodings.h` 的 `UTF8`、`UTF16LE`。第二个参数是被包装的流的类型。 ~~~~~~~~~~cpp #include "rapidjson/document.h" @@ -195,50 +195,50 @@ d.Accept(writer); using namespace rapidjson; -FILE* fp = fopen("utf16le.json", "rb"); // 非Windows平台使用"r" +FILE* fp = fopen("utf16le.json", "rb"); // 非 Windows 平台使用 "r" char readBuffer[256]; FileReadStream bis(fp, readBuffer, sizeof(readBuffer)); -EncodedInputStream, FileReadStream> eis(bis); // 用eis包装bis +EncodedInputStream, FileReadStream> eis(bis); // 用 eis 包装 bis -Document d; // Document为GenericDocument > -d.ParseStream<0, UTF16LE<> >(eis); // 把UTF-16LE文件解析至内存中的UTF-8 +Document d; // Document 为 GenericDocument > +d.ParseStream<0, UTF16LE<> >(eis); // 把 UTF-16LE 文件解析至内存中的 UTF-8 fclose(fp); ~~~~~~~~~~ ## EncodedOutputStream {#EncodedOutputStream} -`EncodedOutputStream`也是相似的,但它的构造函数有一个`bool putBOM`参数,用于控制是否在输出字节流写入BOM。 +`EncodedOutputStream` 也是相似的,但它的构造函数有一个 `bool putBOM` 参数,用于控制是否在输出字节流写入 BOM。 ~~~~~~~~~~cpp #include "rapidjson/filewritestream.h" // FileWriteStream #include "rapidjson/encodedstream.h" // EncodedOutputStream #include -Document d; // Document为GenericDocument > +Document d; // Document 为 GenericDocument > // ... -FILE* fp = fopen("output_utf32le.json", "wb"); // 非Windows平台使用"w" +FILE* fp = fopen("output_utf32le.json", "wb"); // 非 Windows 平台使用 "w" char writeBuffer[256]; FileWriteStream bos(fp, writeBuffer, sizeof(writeBuffer)); typedef EncodedOutputStream, FileWriteStream> OutputStream; -OutputStream eos(bos, true); // 写入BOM +OutputStream eos(bos, true); // 写入 BOM Writer, UTF8<>> writer(eos); -d.Accept(writer); // 这里从内存的UTF-8生成UTF32-LE文件 +d.Accept(writer); // 这里从内存的 UTF-8 生成 UTF32-LE 文件 fclose(fp); ~~~~~~~~~~ ## AutoUTFInputStream {#AutoUTFInputStream} -有时候,应用软件可能需要㲃理所有可支持的JSON编码。`AutoUTFInputStream`会先使用BOM来检测编码。若BOM不存在,它便会使用合法JSON的特性来检测。若两种方法都失败,它就会倒退至构造函数提供的UTF类型。 +有时候,应用软件可能需要㲃理所有可支持的 JSON 编码。`AutoUTFInputStream` 会先使用 BOM 来检测编码。若 BOM 不存在,它便会使用合法 JSON 的特性来检测。若两种方法都失败,它就会倒退至构造函数提供的 UTF 类型。 -由于字符(编码单元/code unit)可能是8位、16位或32位,`AutoUTFInputStream` 需要一个能至少储存32位的字符类型。我们可以使用`unsigned`作为模板参数: +由于字符(编码单元/code unit)可能是 8 位、16 位或 32 位,`AutoUTFInputStream` 需要一个能至少储存 32 位的字符类型。我们可以使用 `unsigned` 作为模板参数: ~~~~~~~~~~cpp #include "rapidjson/document.h" @@ -248,26 +248,26 @@ fclose(fp); using namespace rapidjson; -FILE* fp = fopen("any.json", "rb"); // 非Windows平台使用"r" +FILE* fp = fopen("any.json", "rb"); // 非 Windows 平台使用 "r" char readBuffer[256]; FileReadStream bis(fp, readBuffer, sizeof(readBuffer)); -AutoUTFInputStream eis(bis); // 用eis包装bis +AutoUTFInputStream eis(bis); // 用 eis 包装 bis -Document d; // Document为GenericDocument > -d.ParseStream<0, AutoUTF >(eis); // 把任何UTF编码的文件解析至内存中的UTF-8 +Document d; // Document 为 GenericDocument > +d.ParseStream<0, AutoUTF >(eis); // 把任何 UTF 编码的文件解析至内存中的 UTF-8 fclose(fp); ~~~~~~~~~~ -当要指定流的编码,可使用上面例子中`ParseStream()`的参数`AutoUTF`。 +当要指定流的编码,可使用上面例子中 `ParseStream()` 的参数 `AutoUTF`。 -你可以使用`UTFType GetType()`去获取UTF类型,并且用`HasBOM()`检测输入流是否含有BOM。 +你可以使用 `UTFType GetType()` 去获取 UTF 类型,并且用 `HasBOM()` 检测输入流是否含有 BOM。 ## AutoUTFOutputStream {#AutoUTFOutputStream} -相似地,要在运行时选择输出的编码,我们可使用`AutoUTFOutputStream`。这个类本身并非「自动」。你需要在运行时指定UTF类型,以及是否写入BOM。 +相似地,要在运行时选择输出的编码,我们可使用 `AutoUTFOutputStream`。这个类本身并非「自动」。你需要在运行时指定 UTF 类型,以及是否写入 BOM。 ~~~~~~~~~~cpp using namespace rapidjson; @@ -284,13 +284,13 @@ void WriteJSONFile(FILE* fp, UTFType type, bool putBOM, const Document& d) { } ~~~~~~~~~~ -`AutoUTFInputStream`/`AutoUTFOutputStream`是比`EncodedInputStream`/`EncodedOutputStream`方便。但前者会产生一点运行期额外开销。 +`AutoUTFInputStream`/`AutoUTFOutputStream` 是比 `EncodedInputStream`/`EncodedOutputStream` 方便。但前者会产生一点运行期额外开销。 # 自定义流 {#CustomStream} -除了内存/文件流,使用者可创建自行定义适配RapidJSON API的流类。例如,你可以创建网络流、从压缩文件读取的流等等。 +除了内存/文件流,使用者可创建自行定义适配 RapidJSON API 的流类。例如,你可以创建网络流、从压缩文件读取的流等等。 -RapidJSON利用模板结合不同的类型。只要一个类包含所有所需的接口,就可以作为一个流。流的接合定义在`rapidjson/rapidjson.h`的注释里: +RapidJSON 利用模板结合不同的类型。只要一个类包含所有所需的接口,就可以作为一个流。流的接合定义在 `rapidjson/rapidjson.h` 的注释里: ~~~~~~~~~~cpp concept Stream { @@ -317,19 +317,19 @@ concept Stream { void Flush(); //! 完成写作操作。 - //! \param begin PutBegin()返回的开始写入指针。 + //! \param begin PutBegin() 返回的开始写入指针。 //! \return 已写入的字符数量。 size_t PutEnd(Ch* begin); } ~~~~~~~~~~ -输入流必须实现`Peek()`、`Take()`及`Tell()`。 -输出流必须实现`Put()`及`Flush()`。 -`PutBegin()`及`PutEnd()`是特殊的接口,仅用于原位(*in situ*)解析。一般的流不需实现它们。然而,即使接口不需用于某些流,仍然需要提供空实现,否则会产生编译错误。 +输入流必须实现 `Peek()`、`Take()` 及 `Tell()`。 +输出流必须实现 `Put()` 及 `Flush()`。 +`PutBegin()` 及 `PutEnd()` 是特殊的接口,仅用于原位(*in situ*)解析。一般的流不需实现它们。然而,即使接口不需用于某些流,仍然需要提供空实现,否则会产生编译错误。 -## 例子:istream的包装类 {#ExampleIStreamWrapper} +## 例子:istream 的包装类 {#ExampleIStreamWrapper} -以下的简单例子是`std::istream`的包装类,它只需现3个函数。 +以下的简单例子是 `std::istream` 的包装类,它只需现 3 个函数。 ~~~~~~~~~~cpp class MyIStreamWrapper { @@ -364,7 +364,7 @@ private: }; ~~~~~~~~~~ -使用者能用它来包装`std::stringstream`、`std::ifstream`的实例。 +使用者能用它来包装 `std::stringstream`、`std::ifstream` 的实例。 ~~~~~~~~~~cpp const char* json = "[1,2,3,4]"; @@ -375,11 +375,11 @@ Document d; d.ParseStream(is); ~~~~~~~~~~ -但要注意,由于标准库的内部开销问,此实现的性能可能不如RapidJSON的内存/文件流。 +但要注意,由于标准库的内部开销问,此实现的性能可能不如 RapidJSON 的内存/文件流。 -## 例子:ostream的包装类 {#ExampleOStreamWrapper} +## 例子:ostream 的包装类 {#ExampleOStreamWrapper} -以下的例子是`std::istream`的包装类,它只需实现2个函数。 +以下的例子是 `std::istream` 的包装类,它只需实现 2 个函数。 ~~~~~~~~~~cpp class MyOStreamWrapper { @@ -406,7 +406,7 @@ private: }; ~~~~~~~~~~ -使用者能用它来包装`std::stringstream`、`std::ofstream`的实例。 +使用者能用它来包装 `std::stringstream`、`std::ofstream` 的实例。 ~~~~~~~~~~cpp Document d; @@ -419,8 +419,8 @@ Writer writer(os); d.Accept(writer); ~~~~~~~~~~ -但要注意,由于标准库的内部开销问,此实现的性能可能不如RapidJSON的内存/文件流。 +但要注意,由于标准库的内部开销问,此实现的性能可能不如 RapidJSON 的内存/文件流。 # 总结 {#Summary} -本节描述了RapidJSON提供的各种流的类。内存流很简单。若JSON存储在文件中,文件流可减少JSON解析及生成所需的内存量。编码流在字节流和字符流之间作转换。最后,使用者可使用一个简单接口创建自定义的流。 +本节描述了 RapidJSON 提供的各种流的类。内存流很简单。若 JSON 存储在文件中,文件流可减少 JSON 解析及生成所需的内存量。编码流在字节流和字符流之间作转换。最后,使用者可使用一个简单接口创建自定义的流。 diff --git a/doc/tutorial.zh-cn.md b/doc/tutorial.zh-cn.md index 37808b0..7a0e6e5 100644 --- a/doc/tutorial.zh-cn.md +++ b/doc/tutorial.zh-cn.md @@ -2,19 +2,19 @@ 本教程简介文件对象模型(Document Object Model, DOM)API。 -如[用法一览](../readme.zh-cn.md#用法一览)中所示,可以解析一个JSON至DOM,然后就可以轻松查询及修改DOM,并最终转换回JSON。 +如 [用法一览](../readme.zh-cn.md#用法一览) 中所示,可以解析一个 JSON 至 DOM,然后就可以轻松查询及修改 DOM,并最终转换回 JSON。 [TOC] # Value 及 Document {#ValueDocument} -每个JSON值都储存为`Value`类,而`Document`类则表示整个DOM,它存储了一个DOM树的根`Value`。RapidJSON的所有公开类型及函数都在`rapidjson`命名空间中。 +每个 JSON 值都储存为 `Value` 类,而 `Document` 类则表示整个 DOM,它存储了一个 DOM 树的根 `Value`。RapidJSON 的所有公开类型及函数都在 `rapidjson` 命名空间中。 -# 查询Value {#QueryValue} +# 查询 Value {#QueryValue} -在本节中,我们会使用到`example/tutorial/tutorial.cpp`中的代码片段。 +在本节中,我们会使用到 `example/tutorial/tutorial.cpp` 中的代码片段。 -假设我们用C语言的字符串储存一个JSON(`const char* json`): +假设我们用 C 语言的字符串储存一个 JSON(`const char* json`): ~~~~~~~~~~js { "hello": "world", @@ -27,7 +27,7 @@ } ~~~~~~~~~~ -把它解析至一个`Document`: +把它解析至一个 `Document`: ~~~~~~~~~~cpp #include "rapidjson/document.h" @@ -38,16 +38,16 @@ Document document; document.Parse(json); ~~~~~~~~~~ -那么现在该JSON就会被解析至`document`中,成为一棵*DOM树*: +那么现在该 JSON 就会被解析至 `document` 中,成为一棵 *DOM 树 *: -![教程中的DOM](diagram/tutorial.png) +![教程中的 DOM](diagram/tutorial.png) -自从RFC 7159作出更新,合法JSON文件的根可以是任何类型的JSON值。而在较早的RFC 4627中,根值只允许是Object或Array。而在上述例子中,根是一个Object。 +自从 RFC 7159 作出更新,合法 JSON 文件的根可以是任何类型的 JSON 值。而在较早的 RFC 4627 中,根值只允许是 Object 或 Array。而在上述例子中,根是一个 Object。 ~~~~~~~~~~cpp assert(document.IsObject()); ~~~~~~~~~~ -让我们查询一下根Object中有没有`"hello"`成员。由于一个`Value`可包含不同类型的值,我们可能需要验证它的类型,并使用合适的API去获取其值。在此例中,`"hello"`成员关联到一个JSON String。 +让我们查询一下根 Object 中有没有 `"hello"` 成员。由于一个 `Value` 可包含不同类型的值,我们可能需要验证它的类型,并使用合适的 API 去获取其值。在此例中,`"hello"` 成员关联到一个 JSON String。 ~~~~~~~~~~cpp assert(document.HasMember("hello")); assert(document["hello"].IsString()); @@ -58,7 +58,7 @@ printf("hello = %s\n", document["hello"].GetString()); world ~~~~~~~~~~ -JSON True/False值是以`bool`表示的。 +JSON True/False 值是以 `bool` 表示的。 ~~~~~~~~~~cpp assert(document["t"].IsBool()); printf("t = %s\n", document["t"].GetBool() ? "true" : "false"); @@ -68,7 +68,7 @@ printf("t = %s\n", document["t"].GetBool() ? "true" : "false"); true ~~~~~~~~~~ -JSON Null值可用`IsNull()`查询。 +JSON Null 值可用 `IsNull()` 查询。 ~~~~~~~~~~cpp printf("n = %s\n", document["n"].IsNull() ? "null" : "?"); ~~~~~~~~~~ @@ -77,12 +77,12 @@ printf("n = %s\n", document["n"].IsNull() ? "null" : "?"); null ~~~~~~~~~~ -JSON Number类型表示所有数值。然而,C++需要使用更专门的类型。 +JSON Number 类型表示所有数值。然而,C++ 需要使用更专门的类型。 ~~~~~~~~~~cpp assert(document["i"].IsNumber()); -// 在此情况下,IsUint()/IsInt64()/IsUInt64()也会返回 true +// 在此情况下,IsUint()/IsInt64()/IsUInt64() 也会返回 true assert(document["i"].IsInt()); printf("i = %d\n", document["i"].GetInt()); // 另一种用法: (int)document["i"] @@ -97,7 +97,7 @@ i = 123 pi = 3.1416 ~~~~~~~~~~ -JSON Array包含一些元素。 +JSON Array 包含一些元素。 ~~~~~~~~~~cpp // 使用引用来连续访问,方便之余还更高效。 const Value& a = document["a"]; @@ -113,17 +113,17 @@ a[2] = 3 a[3] = 4 ~~~~~~~~~~ -注意,RapidJSON并不自动转换各种JSON类型。例如,对一个String的Value调用`GetInt()`是非法的。在调试模式下,它会被断言失败。在发布模式下,其行为是未定义的。 +注意,RapidJSON 并不自动转换各种 JSON 类型。例如,对一个 String 的 Value 调用 `GetInt()` 是非法的。在调试模式下,它会被断言失败。在发布模式下,其行为是未定义的。 以下将会讨论有关查询各类型的细节。 -## 查询Array {#QueryArray} +## 查询 Array {#QueryArray} -缺省情况下,`SizeType`是`unsigned`的typedef。在多数系统中,Array最多能存储2^32-1个元素。 +缺省情况下,`SizeType` 是 `unsigned` 的 typedef。在多数系统中,Array 最多能存储 2^32-1 个元素。 -你可以用整数字面量访问元素,如`a[0]`、`a[1]`、`a[2]`。 +你可以用整数字面量访问元素,如 `a[0]`、`a[1]`、`a[2]`。 -Array与`std::vector`相似,除了使用索引,也可使用迭代器来访问所有元素。 +Array 与 `std::vector` 相似,除了使用索引,也可使用迭代器来访问所有元素。 ~~~~~~~~~~cpp for (Value::ConstValueIterator itr = a.Begin(); itr != a.End(); ++itr) printf("%d ", itr->GetInt()); @@ -133,9 +133,9 @@ for (Value::ConstValueIterator itr = a.Begin(); itr != a.End(); ++itr) * `SizeType Capacity() const` * `bool Empty() const` -## 查询Object {#QueryObject} +## 查询 Object {#QueryObject} -和Array相似,我们可以用迭代器去访问所有Object成员: +和 Array 相似,我们可以用迭代器去访问所有 Object 成员: ~~~~~~~~~~cpp static const char* kTypeNames[] = @@ -159,9 +159,9 @@ Type of member pi is Number Type of member a is Array ~~~~~~~~~~ -注意,当`operator[](const char*)`找不到成员,它会断言失败。 +注意,当 `operator[](const char*)` 找不到成员,它会断言失败。 -若我们不确定一个成员是否存在,便需要在调用`operator[](const char*)`前先调用`HasMember()`。然而,这会导致两次查找。更好的做法是调用`FindMember()`,它能同时检查成员是否存在并返回它的Value: +若我们不确定一个成员是否存在,便需要在调用 `operator[](const char*)` 前先调用 `HasMember()`。然而,这会导致两次查找。更好的做法是调用 `FindMember()`,它能同时检查成员是否存在并返回它的 Value: ~~~~~~~~~~cpp Value::ConstMemberIterator itr = document.FindMember("hello"); @@ -169,23 +169,23 @@ if (itr != document.MemberEnd()) printf("%s %s\n", itr->value.GetString()); ~~~~~~~~~~ -## 查询Number {#QueryNumber} +## 查询 Number {#QueryNumber} -JSON只提供一种数值类型──Number。数字可以是整数或实数。RFC 4627规定数字的范围由解析器指定。 +JSON 只提供一种数值类型──Number。数字可以是整数或实数。RFC 4627 规定数字的范围由解析器指定。 -由于C++提供多种整数及浮点数类型,DOM尝试尽量提供最广的范围及良好性能。 +由于 C++ 提供多种整数及浮点数类型,DOM 尝试尽量提供最广的范围及良好性能。 -当解析一个Number时, 它会被存储在DOM之中,成为下列其中一个类型: +当解析一个 Number 时, 它会被存储在 DOM 之中,成为下列其中一个类型: 类型 | 描述 -----------|--------------------------------------- -`unsigned` | 32位无号整数 -`int` | 32位有号整数 -`uint64_t` | 64位无号整数 -`int64_t` | 64位有号整数 -`double` | 64位双精度浮点数 +`unsigned` | 32 位无号整数 +`int` | 32 位有号整数 +`uint64_t` | 64 位无号整数 +`int64_t` | 64 位有号整数 +`double` | 64 位双精度浮点数 -当查询一个Number时, 你可以检查该数字是否能以目标类型来提取: +当查询一个 Number 时, 你可以检查该数字是否能以目标类型来提取: 查检 | 提取 ------------------|--------------------- @@ -196,28 +196,28 @@ JSON只提供一种数值类型──Number。数字可以是整数或实数。R `bool IsInt64()` | `int64_t GetInt64()` `bool IsDouble()` | `double GetDouble()` -注意,一个整数可能用几种类型来提取,而无需转换。例如,一个名为`x`的Value包含123,那么`x.IsInt() == x.IsUint() == x.IsInt64() == x.IsUint64() == true`。但如果一个名为`y`的Value包含-3000000000,那么仅会令`x.IsInt64() == true`。 +注意,一个整数可能用几种类型来提取,而无需转换。例如,一个名为 `x` 的 Value 包含 123,那么 `x.IsInt() == x.IsUint() == x.IsInt64() == x.IsUint64() == true`。但如果一个名为 `y` 的 Value 包含 -3000000000,那么仅会令 `x.IsInt64() == true`。 -当要提取Number类型,`GetDouble()`是会把内部整数的表示转换成`double`。注意`int` 和`unsigned`可以安全地转换至`double`,但`int64_t`及`uint64_t`可能会丧失精度(因为`double`的尾数只有52位)。 +当要提取 Number 类型,`GetDouble()` 是会把内部整数的表示转换成 `double`。注意 `int` 和 `unsigned` 可以安全地转换至 `double`,但 `int64_t` 及 `uint64_t` 可能会丧失精度(因为 `double` 的尾数只有 52 位)。 -## 查询String {#QueryString} +## 查询 String {#QueryString} -除了`GetString()`,`Value`类也有一个`GetStringLength()`。这里会解释个中原因。 +除了 `GetString()`,`Value` 类也有一个 `GetStringLength()`。这里会解释个中原因。 -根据RFC 4627,JSON String可包含Unicode字符`U+0000`,在JSON中会表示为`"\u0000"`。问题是,C/C++通常使用空字符结尾字符串(null-terminated string),这种字符串把``\0'`作为结束符号。 +根据 RFC 4627,JSON String 可包含 Unicode 字符 `U+0000`,在 JSON 中会表示为 `"\u0000"`。问题是,C/C++ 通常使用空字符结尾字符串(null-terminated string),这种字符串把 ``\0'` 作为结束符号。 -为了符合RFC 4627,RapidJSON支持包含`U+0000`的String。若你需要处理这些String,便可使用`GetStringLength()`去获得正确的字符串长度。 +为了符合 RFC 4627,RapidJSON 支持包含 `U+0000` 的 String。若你需要处理这些 String,便可使用 `GetStringLength()` 去获得正确的字符串长度。 -例如,当解析以下的JSON至`Document d`之后: +例如,当解析以下的 JSON 至 `Document d` 之后: ~~~~~~~~~~js { "s" : "a\u0000b" } ~~~~~~~~~~ -`"a\u0000b"`值的正确长度应该是3。但`strlen()`会返回1。 +`"a\u0000b"` 值的正确长度应该是 3。但 `strlen()` 会返回 1。 -`GetStringLength()`也可以提高性能,因为用户可能需要调用`strlen()`去分配缓冲。 +`GetStringLength()` 也可以提高性能,因为用户可能需要调用 `strlen()` 去分配缓冲。 -此外,`std::string`也支持这个构造函数: +此外,`std::string` 也支持这个构造函数: ~~~~~~~~~~cpp string(const char* s, size_t count); @@ -225,27 +225,27 @@ string(const char* s, size_t count); 此构造函数接受字符串长度作为参数。它支持在字符串中存储空字符,也应该会有更好的性能。 -## 比较两个Value +## 比较两个 Value -你可使用`==`及`!=`去比较两个Value。当且仅当两个Value的类型及内容相同,它们才当作相等。你也可以比较Value和它的原生类型值。以下是一个例子。 +你可使用 `==` 及 `!=` 去比较两个 Value。当且仅当两个 Value 的类型及内容相同,它们才当作相等。你也可以比较 Value 和它的原生类型值。以下是一个例子。 ~~~~~~~~~~cpp if (document["hello"] == document["n"]) /*...*/; // 比较两个值 if (document["hello"] == "world") /*...*/; // 与字符串家面量作比较 if (document["i"] != 123) /*...*/; // 与整数作比较 -if (document["pi"] != 3.14) /*...*/; // 与double作比较 +if (document["pi"] != 3.14) /*...*/; // 与 double 作比较 ~~~~~~~~~~ -Array/Object顺序以它们的元素/成员作比较。当且仅当它们的整个子树相等,它们才当作相等。 +Array/Object 顺序以它们的元素/成员作比较。当且仅当它们的整个子树相等,它们才当作相等。 -注意,现时若一个Object含有重复命名的成员,它与任何Object作比较都总会返回`false`。 +注意,现时若一个 Object 含有重复命名的成员,它与任何 Object 作比较都总会返回 `false`。 # 创建/修改值 {#CreateModifyValues} -有多种方法去创建值。 当一个DOM树被创建或修改后,可使用`Writer`再次存储为JSON。 +有多种方法去创建值。 当一个 DOM 树被创建或修改后,可使用 `Writer` 再次存储为 JSON。 -## 改变Value类型 {#ChangeValueType} -当使用默认构造函数创建一个Value或Document,它的类型便会是Null。要改变其类型,需调用`SetXXX()`或赋值操作,例如: +## 改变 Value 类型 {#ChangeValueType} +当使用默认构造函数创建一个 Value 或 Document,它的类型便会是 Null。要改变其类型,需调用 `SetXXX()` 或赋值操作,例如: ~~~~~~~~~~cpp Document d; // Null @@ -260,13 +260,13 @@ v = 10; // 简写,和上面的相同 几个类型也有重载构造函数: ~~~~~~~~~~cpp -Value b(true); // 调用Value(bool) +Value b(true); // 调用 Value(bool) Value i(-123); // 调用 Value(int) -Value u(123u); // 调用Value(unsigned) -Value d(1.5); // 调用Value(double) +Value u(123u); // 调用 Value(unsigned) +Value d(1.5); // 调用 Value(double) ~~~~~~~~~~ -要重建空Object或Array,可在默认构造函数后使用 `SetObject()`/`SetArray()`,或一次性使用`Value(Type)`: +要重建空 Object 或 Array,可在默认构造函数后使用 `SetObject()`/`SetArray()`,或一次性使用 `Value(Type)`: ~~~~~~~~~~cpp Value o(kObjectType); @@ -275,40 +275,40 @@ Value a(kArrayType); ## 转移语意(Move Semantics) {#MoveSemantics} -在设计RapidJSON时有一个非常特别的决定,就是Value赋值并不是把来源Value复制至目的Value,而是把把来源Value转移(move)至目的Value。例如: +在设计 RapidJSON 时有一个非常特别的决定,就是 Value 赋值并不是把来源 Value 复制至目的 Value,而是把把来源 Value 转移(move)至目的 Value。例如: ~~~~~~~~~~cpp Value a(123); Value b(456); -b = a; // a变成Null,b变成数字123。 +b = a; // a 变成 Null,b 变成数字 123。 ~~~~~~~~~~ ![使用移动语意赋值。](diagram/move1.png) 为什么?此语意有何优点? -最简单的答案就是性能。对于固定大小的JSON类型(Number、True、False、Null),复制它们是简单快捷。然而,对于可变大小的JSON类型(String、Array、Object),复制它们会产生大量开销,而且这些开销常常不被察觉。尤其是当我们需要创建临时Object,把它复制至另一变量,然后再析构它。 +最简单的答案就是性能。对于固定大小的 JSON 类型(Number、True、False、Null),复制它们是简单快捷。然而,对于可变大小的 JSON 类型(String、Array、Object),复制它们会产生大量开销,而且这些开销常常不被察觉。尤其是当我们需要创建临时 Object,把它复制至另一变量,然后再析构它。 -例如,若使用正常*复制*语意: +例如,若使用正常 * 复制 * 语意: ~~~~~~~~~~cpp Value o(kObjectType); { Value contacts(kArrayType); - // 把元素加进contacts数组。 + // 把元素加进 contacts 数组。 // ... - o.AddMember("contacts", contacts, d.GetAllocator()); // 深度复制contacts (可能有大量内存分配) - // 析构contacts。 + o.AddMember("contacts", contacts, d.GetAllocator()); // 深度复制 contacts (可能有大量内存分配) + // 析构 contacts。 } ~~~~~~~~~~ ![复制语意产生大量的复制操作。](diagram/move2.png) -那个`o` Object需要分配一个和contacts相同大小的缓冲区,对conacts做深度复制,并最终要析构contacts。这样会产生大量无必要的内存分配/释放,以及内存复制。 +那个 `o` Object 需要分配一个和 contacts 相同大小的缓冲区,对 conacts 做深度复制,并最终要析构 contacts。这样会产生大量无必要的内存分配/释放,以及内存复制。 有一些方案可避免实质地复制这些数据,例如引用计数(reference counting)、垃圾回收(garbage collection, GC)。 -为了使RapidJSON简单及快速,我们选择了对赋值采用*转移*语意。这方法与`std::auto_ptr`相似,都是在赋值时转移拥有权。转移快得多简单得多,只需要析构原来的Value,把来源`memcpy()`至目标,最后把来源设置为Null类型。 +为了使 RapidJSON 简单及快速,我们选择了对赋值采用 * 转移 * 语意。这方法与 `std::auto_ptr` 相似,都是在赋值时转移拥有权。转移快得多简单得多,只需要析构原来的 Value,把来源 `memcpy()` 至目标,最后把来源设置为 Null 类型。 因此,使用转移语意后,上面的例子变成: @@ -317,18 +317,18 @@ Value o(kObjectType); { Value contacts(kArrayType); // adding elements to contacts array. - o.AddMember("contacts", contacts, d.GetAllocator()); // 只需 memcpy() contacts本身至新成员的Value(16字节) - // contacts在这里变成Null。它的析构是平凡的。 + o.AddMember("contacts", contacts, d.GetAllocator()); // 只需 memcpy() contacts 本身至新成员的 Value(16 字节) + // contacts 在这里变成 Null。它的析构是平凡的。 } ~~~~~~~~~~ ![转移语意不需复制。](diagram/move3.png) -在C++11中这称为转移赋值操作(move assignment operator)。由于RapidJSON 支持C++03,它在赋值操作采用转移语意,其它修改形函数如`AddMember()`, `PushBack()`也采用转移语意。 +在 C++11 中这称为转移赋值操作(move assignment operator)。由于 RapidJSON 支持 C++03,它在赋值操作采用转移语意,其它修改形函数如 `AddMember()`, `PushBack()` 也采用转移语意。 ### 转移语意及临时值 {#TemporaryValues} -有时候,我们想直接构造一个Value并传递给一个“转移”函数(如`PushBack()`、`AddMember()`)。由于临时对象是不能转换为正常的Value引用,我们加入了一个方便的`Move()`函数: +有时候,我们想直接构造一个 Value 并传递给一个“转移”函数(如 `PushBack()`、`AddMember()`)。由于临时对象是不能转换为正常的 Value 引用,我们加入了一个方便的 `Move()` 函数: ~~~~~~~~~~cpp Value a(kArrayType); @@ -338,17 +338,17 @@ a.PushBack(Value().SetInt(42), allocator); // fluent API a.PushBack(Value(42).Move(), allocator); // 和上一行相同 ~~~~~~~~~~ -## 创建String {#CreateString} -RapidJSON提供两个String的存储策略。 +## 创建 String {#CreateString} +RapidJSON 提供两个 String 的存储策略。 1. copy-string: 分配缓冲区,然后把来源数据复制至它。 2. const-string: 简单地储存字符串的指针。 -Copy-string总是安全的,因为它拥有数据的克隆。Const-string可用于存储字符串字面量,以及用于在DOM一节中将会提到的in-situ解析中。 +Copy-string 总是安全的,因为它拥有数据的克隆。Const-string 可用于存储字符串字面量,以及用于在 DOM 一节中将会提到的 in-situ 解析中。 -为了让用户自定义内存分配方式,当一个操作可能需要内存分配时,RapidJSON要求用户传递一个allocator实例作为API参数。此设计避免了在每个Value存储allocator(或document)的指针。 +为了让用户自定义内存分配方式,当一个操作可能需要内存分配时,RapidJSON 要求用户传递一个 allocator 实例作为 API 参数。此设计避免了在每个 Value 存储 allocator(或 document)的指针。 -因此,当我们把一个copy-string赋值时, 调用含有allocator的`SetString()`重载函数: +因此,当我们把一个 copy-string 赋值时, 调用含有 allocator 的 `SetString()` 重载函数: ~~~~~~~~~~cpp Document document; @@ -357,14 +357,14 @@ char buffer[10]; int len = sprintf(buffer, "%s %s", "Milo", "Yip"); // 动态创建的字符串。 author.SetString(buffer, len, document.GetAllocator()); memset(buffer, 0, sizeof(buffer)); -// 清空buffer后author.GetString() 仍然包含 "Milo Yip" +// 清空 buffer 后 author.GetString() 仍然包含 "Milo Yip" ~~~~~~~~~~ -在此例子中,我们使用`Document`实例的allocator。这是使用RapidJSON时常用的惯用法。但你也可以用其他allocator实例。 +在此例子中,我们使用 `Document` 实例的 allocator。这是使用 RapidJSON 时常用的惯用法。但你也可以用其他 allocator 实例。 -另外,上面的`SetString()`需要长度参数。这个API能处理含有空字符的字符串。另一个`SetString()`重载函数没有长度参数,它假设输入是空字符结尾的,并会调用类似`strlen()`的函数去获取长度。 +另外,上面的 `SetString()` 需要长度参数。这个 API 能处理含有空字符的字符串。另一个 `SetString()` 重载函数没有长度参数,它假设输入是空字符结尾的,并会调用类似 `strlen()` 的函数去获取长度。 -最后,对于字符串字面量或有安全生命周期的字符串,可以使用const-string版本的`SetString()`,它没有allocator参数。对于字符串家面量(或字符数组常量),只需简单地传递字面量,又安全又高效: +最后,对于字符串字面量或有安全生命周期的字符串,可以使用 const-string 版本的 `SetString()`,它没有 allocator 参数。对于字符串家面量(或字符数组常量),只需简单地传递字面量,又安全又高效: ~~~~~~~~~~cpp Value s; @@ -372,7 +372,7 @@ s.SetString("rapidjson"); // 可包含空字符,长度在编译萁推导 s = "rapidjson"; // 上行的缩写 ~~~~~~~~~~ -对于字符指针,RapidJSON需要作一个标记,代表它不复制也是安全的。可以使用`StringRef`函数: +对于字符指针,RapidJSON 需要作一个标记,代表它不复制也是安全的。可以使用 `StringRef` 函数: ~~~~~~~~~cpp const char * cstr = getenv("USER"); @@ -386,8 +386,8 @@ s = StringRef(cstr, cstr_len); // 上行的缩写 ~~~~~~~~~ -## 修改Array {#ModifyArray} -Array类型的Value提供与`std::vector`相似的API。 +## 修改 Array {#ModifyArray} +Array 类型的 Value 提供与 `std::vector` 相似的 API。 * `Clear()` * `Reserve(SizeType, Allocator&)` @@ -397,37 +397,37 @@ Array类型的Value提供与`std::vector`相似的API。 * `ValueIterator Erase(ConstValueIterator pos)` * `ValueIterator Erase(ConstValueIterator first, ConstValueIterator last)` -注意,`Reserve(...)`及`PushBack(...)`可能会为数组元素分配内存,所以需要一个allocator。 +注意,`Reserve(...)` 及 `PushBack(...)` 可能会为数组元素分配内存,所以需要一个 allocator。 -以下是`PushBack()`的例子: +以下是 `PushBack()` 的例子: ~~~~~~~~~~cpp Value a(kArrayType); Document::AllocatorType& allocator = document.GetAllocator(); for (int i = 5; i <= 10; i++) - a.PushBack(i, allocator); // 可能需要调用realloc()所以需要allocator + a.PushBack(i, allocator); // 可能需要调用 realloc() 所以需要 allocator // 流畅接口(Fluent interface) a.PushBack("Lua", allocator).PushBack("Mio", allocator); ~~~~~~~~~~ -与STL不一样的是,`PushBack()`/`PopBack()`返回Array本身的引用。这称为流畅接口(_fluent interface_)。 +与 STL 不一样的是,`PushBack()`/`PopBack()` 返回 Array 本身的引用。这称为流畅接口(_fluent interface_)。 -如果你想在Array中加入一个非常量字符串,或是一个没有足够生命周期的字符串(见[Create String](#CreateString)),你需要使用copy-string API去创建一个String。为了避免加入中间变量,可以就地使用一个[临时值](#TemporaryValues): +如果你想在 Array 中加入一个非常量字符串,或是一个没有足够生命周期的字符串(见 [Create String](#CreateString)),你需要使用 copy-string API 去创建一个 String。为了避免加入中间变量,可以就地使用一个 [临时值](#TemporaryValues): ~~~~~~~~~~cpp -// 就地Value参数 +// 就地 Value 参数 contact.PushBack(Value("copy", document.GetAllocator()).Move(), // copy string document.GetAllocator()); -// 显式Value参数 +// 显式 Value 参数 Value val("key", document.GetAllocator()); // copy string contact.PushBack(val, document.GetAllocator()); ~~~~~~~~~~ -## 修改Object {#ModifyObject} -Object是键值对的集合。每个键必须为String。要修改Object,方法是增加或移除成员。以下的API用来增加城员: +## 修改 Object {#ModifyObject} +Object 是键值对的集合。每个键必须为 String。要修改 Object,方法是增加或移除成员。以下的 API 用来增加城员: * `Value& AddMember(Value&, Value&, Allocator& allocator)` * `Value& AddMember(StringRefType, Value&, Allocator&)` @@ -441,34 +441,34 @@ contact.AddMember("name", "Milo", document.GetAllocator()); contact.AddMember("married", true, document.GetAllocator()); ~~~~~~~~~~ -使用`StringRefType`作为name参数的重载版本与字符串的`SetString`的接口相似。 这些重载是为了避免复制`name`字符串,因为JSON object中经常会使用常数键名。 +使用 `StringRefType` 作为 name 参数的重载版本与字符串的 `SetString` 的接口相似。 这些重载是为了避免复制 `name` 字符串,因为 JSON object 中经常会使用常数键名。 -如果你需要从非常数字符串或生命周期不足的字符串创建键名(见[创建String](#CreateString)),你需要使用copy-string API。为了避免中间变量,可以就地使用[临时值](#TemporaryValues): +如果你需要从非常数字符串或生命周期不足的字符串创建键名(见 [创建 String](#CreateString)),你需要使用 copy-string API。为了避免中间变量,可以就地使用 [临时值](#TemporaryValues): ~~~~~~~~~~cpp -// 就地Value参数 +// 就地 Value 参数 contact.AddMember(Value("copy", document.GetAllocator()).Move(), // copy string Value().Move(), // null value document.GetAllocator()); // 显式参数 Value key("key", document.GetAllocator()); // copy string name -Value val(42); // 某Value +Value val(42); // 某 Value contact.AddMember(key, val, document.GetAllocator()); ~~~~~~~~~~ 移除成员有几个选择: * `bool RemoveMember(const Ch* name)`:使用键名来移除成员(线性时间复杂度)。 -* `bool RemoveMember(const Value& name)`:除了`name`是一个Value,和上一行相同。 -* `MemberIterator RemoveMember(MemberIterator)`:使用迭代器移除成员(_常数_时间复杂度)。 +* `bool RemoveMember(const Value& name)`:除了 `name` 是一个 Value,和上一行相同。 +* `MemberIterator RemoveMember(MemberIterator)`:使用迭代器移除成员(_ 常数 _ 时间复杂度)。 * `MemberIterator EraseMember(MemberIterator)`:和上行相似但维持成员次序(线性时间复杂度)。 * `MemberIterator EraseMember(MemberIterator first, MemberIterator last)`:移除一个范围内的成员,维持次序(线性时间复杂度)。 -`MemberIterator RemoveMember(MemberIterator)`使用了“转移最后”手法来达成常数时间复杂度。基本上就是析构迭代器位置的成员,然后把最后的成员转移至迭代器位置。因此,成员的次序会被改变。 +`MemberIterator RemoveMember(MemberIterator)` 使用了“转移最后”手法来达成常数时间复杂度。基本上就是析构迭代器位置的成员,然后把最后的成员转移至迭代器位置。因此,成员的次序会被改变。 -## 深复制Value {#DeepCopyValue} -若我们真的要复制一个DOM树,我们可使用两个APIs作深复制:含allocator的构造函数及`CopyFrom()`。 +## 深复制 Value {#DeepCopyValue} +若我们真的要复制一个 DOM 树,我们可使用两个 APIs 作深复制:含 allocator 的构造函数及 `CopyFrom()`。 ~~~~~~~~~~cpp Document d; @@ -477,19 +477,19 @@ Value v1("foo"); // Value v2(v1); // 不容许 Value v2(v1, a); // 制造一个克隆 -assert(v1.IsString()); // v1不变 +assert(v1.IsString()); // v1 不变 d.SetArray().PushBack(v1, a).PushBack(v2, a); -assert(v1.IsNull() && v2.IsNull()); // 两个都转移动d +assert(v1.IsNull() && v2.IsNull()); // 两个都转移动 d -v2.CopyFrom(d, a); // 把整个document复制至v2 -assert(d.IsArray() && d.Size() == 2); // d不变 +v2.CopyFrom(d, a); // 把整个 document 复制至 v2 +assert(d.IsArray() && d.Size() == 2); // d 不变 v1.SetObject().AddMember("array", v2, a); d.PushBack(v1, a); ~~~~~~~~~~ -## 交换Value {#SwapValues} +## 交换 Value {#SwapValues} -RapidJSON也提供`Swap()`。 +RapidJSON 也提供 `Swap()`。 ~~~~~~~~~~cpp Value a(123); @@ -499,17 +499,17 @@ assert(a.IsString()); assert(b.IsInt()); ~~~~~~~~~~ -无论两棵DOM树有多复杂,交换是很快的(常数时间)。 +无论两棵 DOM 树有多复杂,交换是很快的(常数时间)。 # 下一部分 {#WhatsNext} -本教程展示了如何询查及修改DOM树。RapidJSON还有一个重要概念: +本教程展示了如何询查及修改 DOM 树。RapidJSON 还有一个重要概念: -1. [流](doc/stream.zh-cn.md) 是读写JSON的通道。流可以是内存字符串、文件流等。用户也可以自定义流。 -2. [编码](doc/encoding.zh-cn.md)定义在流或内存中使用的字符编码。RapidJSON也在内部提供Unicode转换及校验功能。 -3. [DOM](doc/dom.zh-cn.md)的基本功能已在本教程里介绍。还有更高级的功能,如原位(*in situ*)解析、其他解析选项及高级用法。 -4. [SAX](doc/sax.zh-cn.md) 是RapidJSON解析/生成功能的基础。学习使用`Reader`/`Writer`去实现更高性能的应用程序。也可以使用`PrettyWriter`去格式化JSON。 -5. [性能](doc/performance.zh-cn.md)展示一些我们做的及第三方的性能测试。 -6. [技术内幕](doc/internals.zh-cn.md)讲述一些RapidJSON内部的设计及技术。 +1. [流](doc/stream.zh-cn.md) 是读写 JSON 的通道。流可以是内存字符串、文件流等。用户也可以自定义流。 +2. [编码](doc/encoding.zh-cn.md) 定义在流或内存中使用的字符编码。RapidJSON 也在内部提供 Unicode 转换及校验功能。 +3. [DOM](doc/dom.zh-cn.md) 的基本功能已在本教程里介绍。还有更高级的功能,如原位(*in situ*)解析、其他解析选项及高级用法。 +4. [SAX](doc/sax.zh-cn.md) 是 RapidJSON 解析/生成功能的基础。学习使用 `Reader`/`Writer` 去实现更高性能的应用程序。也可以使用 `PrettyWriter` 去格式化 JSON。 +5. [性能](doc/performance.zh-cn.md) 展示一些我们做的及第三方的性能测试。 +6. [技术内幕](doc/internals.zh-cn.md) 讲述一些 RapidJSON 内部的设计及技术。 -你也可以参考[常见问题](faq.zh-cn.md)、API文档、例子及单元测试。 +你也可以参考 [常见问题](faq.zh-cn.md)、API 文档、例子及单元测试。 diff --git a/readme.zh-cn.md b/readme.zh-cn.md index 3e4c9a3..d7772c5 100644 --- a/readme.zh-cn.md +++ b/readme.zh-cn.md @@ -2,7 +2,7 @@ ![](https://img.shields.io/badge/release-v1.0.2-blue.png) -## 高效的C++ JSON解析/生成器,提供SAX及DOM风格API +## 高效的 C++ JSON 解析/生成器,提供 SAX 及 DOM 风格 API Tencent is pleased to support the open source community by making RapidJSON available. @@ -12,7 +12,7 @@ Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights * RapidJSON 文档 * [English](http://rapidjson.org/) * [简体中文](http://rapidjson.org/zh-cn/) - * [GitBook](https://www.gitbook.com/book/miloyip/rapidjson/)可下载PDF/EPUB/MOBI,但不含API参考手册。 + * [GitBook](https://www.gitbook.com/book/miloyip/rapidjson/) 可下载 PDF/EPUB/MOBI,但不含 API 参考手册。 ## Build 状态 @@ -29,28 +29,28 @@ Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights ## 简介 -RapidJSON是一个C++的JSON解析器及生成器。它的灵感来自[RapidXml](http://rapidxml.sourceforge.net/)。 +RapidJSON 是一个 C++ 的 JSON 解析器及生成器。它的灵感来自 [RapidXml](http://rapidxml.sourceforge.net/)。 -* RapidJSON小而全。它同时支持SAX和DOM风格的API。SAX解析器只有约500行代码。 +* RapidJSON 小而全。它同时支持 SAX 和 DOM 风格的 API。SAX 解析器只有约 500 行代码。 -* RapidJSON快。它的性能可与`strlen()`相比。可支持SSE2/SSE4.2加速。 +* RapidJSON 快。它的性能可与 `strlen()` 相比。可支持 SSE2/SSE4.2 加速。 -* RapidJSON独立。它不依赖于BOOST等外部库。它甚至不依赖于STL。 +* RapidJSON 独立。它不依赖于 BOOST 等外部库。它甚至不依赖于 STL。 -* RapidJSON对内存友好。在大部分32/64位机器上,每个JSON值只占16或20字节(除字符串外)。它预设使用一个快速的内存分配器,令分析器可以紧凑地分配内存。 +* RapidJSON 对内存友好。在大部分 32/64 位机器上,每个 JSON 值只占 16 或 20 字节(除字符串外)。它预设使用一个快速的内存分配器,令分析器可以紧凑地分配内存。 -* RapidJSON对Unicode友好。它支持UTF-8、UTF-16、UTF-32 (大端序/小端序),并内部支持这些编码的检测、校验及转码。例如,RapidJSON可以在分析一个UTF-8文件至DOM时,把当中的JSON字符串转码至UTF-16。它也支持代理对(surrogate pair)及`"\u0000"`(空字符)。 +* RapidJSON 对 Unicode 友好。它支持 UTF-8、UTF-16、UTF-32 (大端序/小端序),并内部支持这些编码的检测、校验及转码。例如,RapidJSON 可以在分析一个 UTF-8 文件至 DOM 时,把当中的 JSON 字符串转码至 UTF-16。它也支持代理对(surrogate pair)及 `"\u0000"`(空字符)。 -在[这里](doc/features.md)可读取更多特点。 +在 [这里](doc/features.md) 可读取更多特点。 -JSON(JavaScript Object Notation)是一个轻量的数据交换格式。RapidJSON应该完全遵从RFC7159/ECMA-404。 关于JSON的更多信息可参考: +JSON(JavaScript Object Notation)是一个轻量的数据交换格式。RapidJSON 应该完全遵从 RFC7159/ECMA-404。 关于 JSON 的更多信息可参考: * [Introducing JSON](http://json.org/) * [RFC7159: The JavaScript Object Notation (JSON) Data Interchange Format](http://www.ietf.org/rfc/rfc7159.txt) * [Standard ECMA-404: The JSON Data Interchange Format](http://www.ecma-international.org/publications/standards/Ecma-404.htm) ## 兼容性 -RapidJSON是跨平台的。以下是一些曾测试的平台/编译器组合: +RapidJSON 是跨平台的。以下是一些曾测试的平台/编译器组合: * Visual C++ 2008/2010/2013 在 Windows (32/64-bit) * GNU C++ 3.8.x 在 Cygwin * Clang 3.4 在 Mac OS X (32/64-bit) 及 iOS @@ -60,27 +60,27 @@ RapidJSON是跨平台的。以下是一些曾测试的平台/编译器组合 ## 安装 -RapidJSON是只有头文件的C++库。只需把`include/rapidjson`目录复制至系统或项目的include目录中。 +RapidJSON 是只有头文件的 C++ 库。只需把 `include/rapidjson` 目录复制至系统或项目的 include 目录中。 -RapidJSON依赖于以下软件: +RapidJSON 依赖于以下软件: * [CMake](http://www.cmake.org) 作为通用生成工具 -* (optional)[Doxygen](http://www.doxygen.org)用于生成文档 -* (optional)[googletest](https://code.google.com/p/googletest/)用于单元及性能测试 +* (optional)[Doxygen](http://www.doxygen.org) 用于生成文档 +* (optional)[googletest](https://code.google.com/p/googletest/) 用于单元及性能测试 生成测试及例子的步骤: 1. 执行 `git submodule update --init` 去获取 thirdparty submodules (google test)。 -2. 在rapidjson目渌下,建立一个`build`目录。 -3. 在`build`目录下执行`cmake ..`命令以设置生成。Windows用户可使用cmake-gui应用程序。 -4. 在Windows下,编译生成在build目录中的solution。在Linux下,于build目录运行`make`。 +2. 在 rapidjson 目渌下,建立一个 `build` 目录。 +3. 在 `build` 目录下执行 `cmake ..` 命令以设置生成。Windows 用户可使用 cmake-gui 应用程序。 +4. 在 Windows 下,编译生成在 build 目录中的 solution。在 Linux 下,于 build 目录运行 `make`。 -成功生成后,你会在`bin`的目录下找到编译后的测试及例子可执行文件。而生成的文档将位于build下的`doc/html`目录。要执行测试,请在build下执行`make test`或`ctest`。使用`ctest -V`命令可获取详细的输出。 +成功生成后,你会在 `bin` 的目录下找到编译后的测试及例子可执行文件。而生成的文档将位于 build 下的 `doc/html` 目录。要执行测试,请在 build 下执行 `make test` 或 `ctest`。使用 `ctest -V` 命令可获取详细的输出。 -我们也可以把程序库安装至全系统中,只要在具管理權限下从build目录执行`make install`命令。这样会按系统的偏好设置安装所有文件。当安装RapidJSON后,其他的CMake项目需要使用它时,可以通过在`CMakeLists.txt`加入一句`find_package(RapidJSON)`。 +我们也可以把程序库安装至全系统中,只要在具管理權限下从 build 目录执行 `make install` 命令。这样会按系统的偏好设置安装所有文件。当安装 RapidJSON 后,其他的 CMake 项目需要使用它时,可以通过在 `CMakeLists.txt` 加入一句 `find_package(RapidJSON)`。 ## 用法一览 -此简单例子解析一个JSON字符串至一个document (DOM),对DOM作出简单修改,最终把DOM转换(stringify)至JSON字符串。 +此简单例子解析一个 JSON 字符串至一个 document (DOM),对 DOM 作出简单修改,最终把 DOM 转换(stringify)至 JSON 字符串。 ~~~~~~~~~~cpp // rapidjson/example/simpledom/simpledom.cpp` @@ -92,16 +92,16 @@ RapidJSON依赖于以下软件: using namespace rapidjson; int main() { - // 1. 把JSON解析至DOM。 + // 1. 把 JSON 解析至 DOM。 const char* json = "{\"project\":\"rapidjson\",\"stars\":10}"; Document d; d.Parse(json); - // 2. 利用DOM作出修改。 + // 2. 利用 DOM 作出修改。 Value& s = d["stars"]; s.SetInt(s.GetInt() + 1); - // 3. 把DOM转换(stringify)成JSON。 + // 3. 把 DOM 转换(stringify)成 JSON。 StringBuffer buffer; Writer writer(buffer); d.Accept(writer); @@ -118,7 +118,7 @@ int main() { ![simpledom](doc/diagram/simpledom.png) -还有许多[例子](https://github.com/miloyip/rapidjson/tree/master/example)可供参考: +还有许多 [例子](https://github.com/miloyip/rapidjson/tree/master/example) 可供参考: * DOM API * [tutorial](https://github.com/miloyip/rapidjson/blob/master/example/tutorial/tutorial.cpp): DOM API 的基本使用方法。 @@ -137,4 +137,4 @@ int main() { * 进阶 * [prettyauto](https://github.com/miloyip/rapidjson/blob/master/example/prettyauto/prettyauto.cpp): [pretty](https://github.com/miloyip/rapidjson/blob/master/example/pretty/pretty.cpp) 的修改版本,可自动处理任何 UTF 编码的 JSON。 - * [parsebyparts](https://github.com/miloyip/rapidjson/blob/master/example/parsebyparts/parsebyparts.cpp): 这例子中的`AsyncDocumentParser` 类使用 C++ 线程来逐段解析 JSON。 + * [parsebyparts](https://github.com/miloyip/rapidjson/blob/master/example/parsebyparts/parsebyparts.cpp): 这例子中的 `AsyncDocumentParser` 类使用 C++ 线程来逐段解析 JSON。 From 0fe08c222fc7170f67be5a079921e2d70efbcc1c Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Tue, 19 Apr 2016 16:22:20 +0800 Subject: [PATCH 057/305] Fix english error message gramma Fix #606 --- include/rapidjson/error/en.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/rapidjson/error/en.h b/include/rapidjson/error/en.h index c2315fd..2db838b 100644 --- a/include/rapidjson/error/en.h +++ b/include/rapidjson/error/en.h @@ -38,7 +38,7 @@ inline const RAPIDJSON_ERROR_CHARTYPE* GetParseError_En(ParseErrorCode parseErro case kParseErrorNone: return RAPIDJSON_ERROR_STRING("No error."); case kParseErrorDocumentEmpty: return RAPIDJSON_ERROR_STRING("The document is empty."); - case kParseErrorDocumentRootNotSingular: return RAPIDJSON_ERROR_STRING("The document root must not follow by other values."); + case kParseErrorDocumentRootNotSingular: return RAPIDJSON_ERROR_STRING("The document root must not be followed by other values."); case kParseErrorValueInvalid: return RAPIDJSON_ERROR_STRING("Invalid value."); From aae2fbfc9968759f5955f10cdbb6bba7408eac75 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Thu, 21 Apr 2016 23:12:31 +0800 Subject: [PATCH 058/305] Try to fix cmake CMP0054 warning --- CMakeLists.txt | 4 ++++ example/CMakeLists.txt | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6bdf484..d315b74 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,8 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.8) +if(POLICY CMP0054) + cmake_policy(SET CMP0054 NEW) +endif() + SET(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/CMakeModules) PROJECT(RapidJSON CXX) diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index 6da18df..fd0e6eb 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -1,5 +1,9 @@ cmake_minimum_required(VERSION 2.8) +if(POLICY CMP0054) + cmake_policy(SET CMP0054 NEW) +endif() + set(EXAMPLES capitalize condense From 5b6e40df26db79a33cd1974bcdeb6a433c49c003 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Thu, 21 Apr 2016 23:59:01 +0800 Subject: [PATCH 059/305] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6de511e..d1c948a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -55,6 +55,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). * Fix a bug in schema minimum/maximum keywords for 64-bit integer (e7149d665941068ccf8c565e77495521331cf390) * Fix a crash bug in regex (#605) * Fix schema "required" keyword cannot handle duplicated keys (#609) +* Fix cmake CMP0054 warning (#612) ### Changed * Clarify problematic JSON license (#392) From 05b2ed7532bcaa17f0e2794a7fab67155d3e5cd3 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Sat, 23 Apr 2016 16:02:40 +0800 Subject: [PATCH 060/305] Add filterkey and filterkeydom examples --- example/CMakeLists.txt | 2 + example/filterkey/filterkey.cpp | 130 +++++++++++++++++++++ example/filterkeydom/filterkeydom.cpp | 161 ++++++++++++++++++++++++++ readme.md | 2 + readme.zh-cn.md | 2 + 5 files changed, 297 insertions(+) create mode 100644 example/filterkey/filterkey.cpp create mode 100644 example/filterkeydom/filterkeydom.cpp diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index fd0e6eb..4d448cc 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -7,6 +7,8 @@ endif() set(EXAMPLES capitalize condense + filterkey + filterkeydom jsonx messagereader parsebyparts diff --git a/example/filterkey/filterkey.cpp b/example/filterkey/filterkey.cpp new file mode 100644 index 0000000..1416362 --- /dev/null +++ b/example/filterkey/filterkey.cpp @@ -0,0 +1,130 @@ +// JSON filterkey example with SAX-style API. + +// This example parses JSON text from stdin with validation. +// During parsing, specified key will be filtered using a SAX handler. +// It re-output the JSON content to stdout without whitespace. + +#include "rapidjson/reader.h" +#include "rapidjson/writer.h" +#include "rapidjson/filereadstream.h" +#include "rapidjson/filewritestream.h" +#include "rapidjson/error/en.h" +#include + +using namespace rapidjson; + +// This handler forwards event into an output handler, with filtering the descendent events of specified key. +template +struct FilterKeyHandler { + typedef char Ch; + + FilterKeyHandler(OutputHandler& outputHandler, const Ch* keyString, SizeType keyLength) : + outputHandler_(outputHandler), keyString_(keyString), keyLength_(keyLength), filterValueDepth_(), filteredKeyCount_() + {} + + bool Null() { return filterValueDepth_ > 0 ? EndValue() : outputHandler_.Null() && EndValue(); } + bool Bool(bool b) { return filterValueDepth_ > 0 ? EndValue() : outputHandler_.Bool(b) && EndValue(); } + bool Int(int i) { return filterValueDepth_ > 0 ? EndValue() : outputHandler_.Int(i) && EndValue(); } + bool Uint(unsigned u) { return filterValueDepth_ > 0 ? EndValue() : outputHandler_.Uint(u) && EndValue(); } + bool Int64(int64_t i) { return filterValueDepth_ > 0 ? EndValue() : outputHandler_.Int64(i) && EndValue(); } + bool Uint64(uint64_t u) { return filterValueDepth_ > 0 ? EndValue() : outputHandler_.Uint64(u) && EndValue(); } + bool Double(double d) { return filterValueDepth_ > 0 ? EndValue() : outputHandler_.Double(d) && EndValue(); } + bool RawNumber(const Ch* str, SizeType len, bool copy) { return filterValueDepth_ > 0 ? EndValue() : outputHandler_.RawNumber(str, len, copy) && EndValue(); } + bool String (const Ch* str, SizeType len, bool copy) { return filterValueDepth_ > 0 ? EndValue() : outputHandler_.String (str, len, copy) && EndValue(); } + + bool StartObject() { + if (filterValueDepth_ > 0) { + filterValueDepth_++; + return true; + } + else { + filteredKeyCount_.push(0); + return outputHandler_.StartObject(); + } + } + + bool Key(const Ch* str, SizeType len, bool copy) { + if (filterValueDepth_ > 0) + return true; + else if (len == keyLength_ && std::memcmp(str, keyString_, len) == 0) { + filterValueDepth_ = 1; + return true; + } + else { + ++filteredKeyCount_.top(); + return outputHandler_.Key(str, len, copy); + } + } + + bool EndObject(SizeType) { + if (filterValueDepth_ > 0) { + filterValueDepth_--; + return EndValue(); + } + else { + // Use our own filtered memberCount + SizeType memberCount = filteredKeyCount_.top(); + filteredKeyCount_.pop(); + return outputHandler_.EndObject(memberCount) && EndValue(); + } + } + + bool StartArray() { + if (filterValueDepth_ > 0) { + filterValueDepth_++; + return true; + } + else + return outputHandler_.StartArray(); + } + + bool EndArray(SizeType elementCount) { + if (filterValueDepth_ > 0) { + filterValueDepth_--; + return EndValue(); + } + else + return outputHandler_.EndArray(elementCount) && EndValue(); + } + + bool EndValue() { + if (filterValueDepth_ == 1) // Just at the end of value after filtered key + filterValueDepth_ = 0; + return true; + } + + OutputHandler& outputHandler_; + const char* keyString_; + const SizeType keyLength_; + unsigned filterValueDepth_; + std::stack filteredKeyCount_; +}; + +int main(int argc, char* argv[]) { + if (argc != 2) { + fprintf(stderr, "filterkey key < input.json > output.json\n"); + return 1; + } + + // Prepare JSON reader and input stream. + Reader reader; + char readBuffer[65536]; + FileReadStream is(stdin, readBuffer, sizeof(readBuffer)); + + // Prepare JSON writer and output stream. + char writeBuffer[65536]; + FileWriteStream os(stdout, writeBuffer, sizeof(writeBuffer)); + Writer writer(os); + + // Prepare Filter + FilterKeyHandler > filter(writer, argv[1], static_cast(strlen(argv[1]))); + + // JSON reader parse from the input stream, filter handler filters the events, and forward to writer. + // i.e. the events flow is: reader -> filter -> writer + if (!reader.Parse(is, filter)) { + fprintf(stderr, "\nError(%u): %s\n", static_cast(reader.GetErrorOffset()), GetParseError_En(reader.GetParseErrorCode())); + return 1; + } + + return 0; +} diff --git a/example/filterkeydom/filterkeydom.cpp b/example/filterkeydom/filterkeydom.cpp new file mode 100644 index 0000000..aba50bd --- /dev/null +++ b/example/filterkeydom/filterkeydom.cpp @@ -0,0 +1,161 @@ +// JSON filterkey example which populates filtered SAX events into a Document. + +// This example parses JSON text from stdin with validation. +// During parsing, specified key will be filtered using a SAX handler. +// And finally the filtered events are used to populate a Document. +// As an example, the document is written to standard output. + +#include "rapidjson/document.h" +#include "rapidjson/writer.h" +#include "rapidjson/filereadstream.h" +#include "rapidjson/filewritestream.h" +#include "rapidjson/error/en.h" +#include + +using namespace rapidjson; + +// This handler forwards event into an output handler, with filtering the descendent events of specified key. +template +struct FilterKeyHandler { + typedef char Ch; + + FilterKeyHandler(OutputHandler& outputHandler, const Ch* keyString, SizeType keyLength) : + outputHandler_(outputHandler), keyString_(keyString), keyLength_(keyLength), filterValueDepth_(), filteredKeyCount_() + {} + + bool Null() { return filterValueDepth_ > 0 ? EndValue() : outputHandler_.Null() && EndValue(); } + bool Bool(bool b) { return filterValueDepth_ > 0 ? EndValue() : outputHandler_.Bool(b) && EndValue(); } + bool Int(int i) { return filterValueDepth_ > 0 ? EndValue() : outputHandler_.Int(i) && EndValue(); } + bool Uint(unsigned u) { return filterValueDepth_ > 0 ? EndValue() : outputHandler_.Uint(u) && EndValue(); } + bool Int64(int64_t i) { return filterValueDepth_ > 0 ? EndValue() : outputHandler_.Int64(i) && EndValue(); } + bool Uint64(uint64_t u) { return filterValueDepth_ > 0 ? EndValue() : outputHandler_.Uint64(u) && EndValue(); } + bool Double(double d) { return filterValueDepth_ > 0 ? EndValue() : outputHandler_.Double(d) && EndValue(); } + bool RawNumber(const Ch* str, SizeType len, bool copy) { return filterValueDepth_ > 0 ? EndValue() : outputHandler_.RawNumber(str, len, copy) && EndValue(); } + bool String (const Ch* str, SizeType len, bool copy) { return filterValueDepth_ > 0 ? EndValue() : outputHandler_.String (str, len, copy) && EndValue(); } + + bool StartObject() { + if (filterValueDepth_ > 0) { + filterValueDepth_++; + return true; + } + else { + filteredKeyCount_.push(0); + return outputHandler_.StartObject(); + } + } + + bool Key(const Ch* str, SizeType len, bool copy) { + if (filterValueDepth_ > 0) + return true; + else if (len == keyLength_ && std::memcmp(str, keyString_, len) == 0) { + filterValueDepth_ = 1; + return true; + } + else { + ++filteredKeyCount_.top(); + return outputHandler_.Key(str, len, copy); + } + } + + bool EndObject(SizeType) { + if (filterValueDepth_ > 0) { + filterValueDepth_--; + return EndValue(); + } + else { + // Use our own filtered memberCount + SizeType memberCount = filteredKeyCount_.top(); + filteredKeyCount_.pop(); + return outputHandler_.EndObject(memberCount) && EndValue(); + } + } + + bool StartArray() { + if (filterValueDepth_ > 0) { + filterValueDepth_++; + return true; + } + else + return outputHandler_.StartArray(); + } + + bool EndArray(SizeType elementCount) { + if (filterValueDepth_ > 0) { + filterValueDepth_--; + return EndValue(); + } + else + return outputHandler_.EndArray(elementCount) && EndValue(); + } + + bool EndValue() { + if (filterValueDepth_ == 1) // Just at the end of value after filtered key + filterValueDepth_ = 0; + return true; + } + + OutputHandler& outputHandler_; + const char* keyString_; + const SizeType keyLength_; + unsigned filterValueDepth_; + std::stack filteredKeyCount_; +}; + +// Implements a generator for Document::Populate() +template +class FilterKeyReader { +public: + typedef char Ch; + + FilterKeyReader(InputStream& is, const Ch* keyString, SizeType keyLength) : + is_(is), keyString_(keyString), keyLength_(keyLength) + {} + + // SAX event flow: reader -> filter -> handler + template + bool operator()(Handler& handler) { + FilterKeyHandler filter(handler, keyString_, keyLength_); + Reader reader; + return parseResult_ = reader.Parse(is_, filter); + } + + const ParseResult& GetParseResult() const { return parseResult_; } + +private: + InputStream& is_; + const char* keyString_; + const SizeType keyLength_; + ParseResult parseResult_; +}; + +int main(int argc, char* argv[]) { + if (argc != 2) { + fprintf(stderr, "filterkeydom key < input.json > output.json\n"); + return 1; + } + + // Prepare input stream. + char readBuffer[65536]; + FileReadStream is(stdin, readBuffer, sizeof(readBuffer)); + + // Prepare Filter + FilterKeyReader reader(is, argv[1], static_cast(strlen(argv[1]))); + + // Populates the filtered events from reader + Document document; + document.Populate(reader); + ParseResult pr = reader.GetParseResult(); + if (!pr) { + fprintf(stderr, "\nError(%u): %s\n", static_cast(pr.Offset()), GetParseError_En(pr.Code())); + return 1; + } + + // Prepare JSON writer and output stream. + char writeBuffer[65536]; + FileWriteStream os(stdout, writeBuffer, sizeof(writeBuffer)); + Writer writer(os); + + // Write the document to standard output + document.Accept(writer); + return 0; +} diff --git a/readme.md b/readme.md index d7675bc..77c3b5b 100644 --- a/readme.md +++ b/readme.md @@ -146,3 +146,5 @@ More [examples](https://github.com/miloyip/rapidjson/tree/master/example) are av * Advanced * [prettyauto](https://github.com/miloyip/rapidjson/blob/master/example/prettyauto/prettyauto.cpp): A modified version of [pretty](https://github.com/miloyip/rapidjson/blob/master/example/pretty/pretty.cpp) to automatically handle JSON with any UTF encodings. * [parsebyparts](https://github.com/miloyip/rapidjson/blob/master/example/parsebyparts/parsebyparts.cpp): Implements an `AsyncDocumentParser` which can parse JSON in parts, using C++11 thread. + * [filterkey](https://github.com/miloyip/rapidjson/blob/master/example/filterkey/filterkey.cpp): A command line tool to remove all values with user-specified key. + * [filterkeydom](https://github.com/miloyip/rapidjson/blob/master/example/filterkey/filterkey.cpp): Same tool as above, but it demonstrates how to use a generator to populate a `Document`. diff --git a/readme.zh-cn.md b/readme.zh-cn.md index d7772c5..97101d1 100644 --- a/readme.zh-cn.md +++ b/readme.zh-cn.md @@ -138,3 +138,5 @@ int main() { * 进阶 * [prettyauto](https://github.com/miloyip/rapidjson/blob/master/example/prettyauto/prettyauto.cpp): [pretty](https://github.com/miloyip/rapidjson/blob/master/example/pretty/pretty.cpp) 的修改版本,可自动处理任何 UTF 编码的 JSON。 * [parsebyparts](https://github.com/miloyip/rapidjson/blob/master/example/parsebyparts/parsebyparts.cpp): 这例子中的 `AsyncDocumentParser` 类使用 C++ 线程来逐段解析 JSON。 + * [filterkey](https://github.com/miloyip/rapidjson/blob/master/example/filterkey/filterkey.cpp): 移取使用者指定的键值的命令行工具。 + * [filterkeydom](https://github.com/miloyip/rapidjson/blob/master/example/filterkey/filterkey.cpp): 如上的工具,但展示如何使用生成器(generator)去填充一个 `Document`。 \ No newline at end of file From b010f388d10a61cfe2b8640ca9e88e8f0c725b7f Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Sat, 23 Apr 2016 20:11:05 +0800 Subject: [PATCH 061/305] Change FilterKeyHandler from struct to class Also disable copy constructor/assignment operator --- example/filterkey/filterkey.cpp | 9 +++++++-- example/filterkeydom/filterkeydom.cpp | 12 ++++++++++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/example/filterkey/filterkey.cpp b/example/filterkey/filterkey.cpp index 1416362..c34a050 100644 --- a/example/filterkey/filterkey.cpp +++ b/example/filterkey/filterkey.cpp @@ -15,7 +15,8 @@ using namespace rapidjson; // This handler forwards event into an output handler, with filtering the descendent events of specified key. template -struct FilterKeyHandler { +class FilterKeyHandler { +public: typedef char Ch; FilterKeyHandler(OutputHandler& outputHandler, const Ch* keyString, SizeType keyLength) : @@ -87,12 +88,16 @@ struct FilterKeyHandler { return outputHandler_.EndArray(elementCount) && EndValue(); } +private: + FilterKeyHandler(const FilterKeyHandler&); + FilterKeyHandler& operator=(const FilterKeyHandler&); + bool EndValue() { if (filterValueDepth_ == 1) // Just at the end of value after filtered key filterValueDepth_ = 0; return true; } - + OutputHandler& outputHandler_; const char* keyString_; const SizeType keyLength_; diff --git a/example/filterkeydom/filterkeydom.cpp b/example/filterkeydom/filterkeydom.cpp index aba50bd..cd6119f 100644 --- a/example/filterkeydom/filterkeydom.cpp +++ b/example/filterkeydom/filterkeydom.cpp @@ -16,7 +16,8 @@ using namespace rapidjson; // This handler forwards event into an output handler, with filtering the descendent events of specified key. template -struct FilterKeyHandler { +class FilterKeyHandler { +public: typedef char Ch; FilterKeyHandler(OutputHandler& outputHandler, const Ch* keyString, SizeType keyLength) : @@ -88,12 +89,16 @@ struct FilterKeyHandler { return outputHandler_.EndArray(elementCount) && EndValue(); } +private: + FilterKeyHandler(const FilterKeyHandler&); + FilterKeyHandler& operator=(const FilterKeyHandler&); + bool EndValue() { if (filterValueDepth_ == 1) // Just at the end of value after filtered key filterValueDepth_ = 0; return true; } - + OutputHandler& outputHandler_; const char* keyString_; const SizeType keyLength_; @@ -122,6 +127,9 @@ public: const ParseResult& GetParseResult() const { return parseResult_; } private: + FilterKeyReader(const FilterKeyReader&); + FilterKeyReader& operator=(const FilterKeyReader&); + InputStream& is_; const char* keyString_; const SizeType keyLength_; From 00ed0a5f91618257f4700b575af2e10247f19e67 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Sat, 23 Apr 2016 20:54:06 +0800 Subject: [PATCH 062/305] Fix gcc warning --- example/filterkeydom/filterkeydom.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/example/filterkeydom/filterkeydom.cpp b/example/filterkeydom/filterkeydom.cpp index cd6119f..732cc81 100644 --- a/example/filterkeydom/filterkeydom.cpp +++ b/example/filterkeydom/filterkeydom.cpp @@ -98,7 +98,7 @@ private: filterValueDepth_ = 0; return true; } - + OutputHandler& outputHandler_; const char* keyString_; const SizeType keyLength_; @@ -113,7 +113,7 @@ public: typedef char Ch; FilterKeyReader(InputStream& is, const Ch* keyString, SizeType keyLength) : - is_(is), keyString_(keyString), keyLength_(keyLength) + is_(is), keyString_(keyString), keyLength_(keyLength), parseResult_() {} // SAX event flow: reader -> filter -> handler @@ -121,7 +121,8 @@ public: bool operator()(Handler& handler) { FilterKeyHandler filter(handler, keyString_, keyLength_); Reader reader; - return parseResult_ = reader.Parse(is_, filter); + parseResult_ = reader.Parse(is_, filter); + return parseResult_; } const ParseResult& GetParseResult() const { return parseResult_; } From ee4207b3f0d8f6d646ada8657f0b6976a2fb8ed8 Mon Sep 17 00:00:00 2001 From: Bruce Stephens Date: Mon, 25 Apr 2016 12:32:14 +0100 Subject: [PATCH 063/305] Define RAPIDJSON_HAS_CXX11_RVALUE_REFS directly in clang This makes no difference except that it avoids "warning: macro expansion producing 'defined' has undefined behavior" messages. --- include/rapidjson/rapidjson.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/include/rapidjson/rapidjson.h b/include/rapidjson/rapidjson.h index c441064..062e25e 100644 --- a/include/rapidjson/rapidjson.h +++ b/include/rapidjson/rapidjson.h @@ -529,8 +529,12 @@ RAPIDJSON_NAMESPACE_END #ifndef RAPIDJSON_HAS_CXX11_RVALUE_REFS #if defined(__clang__) -#define RAPIDJSON_HAS_CXX11_RVALUE_REFS __has_feature(cxx_rvalue_references) && \ +#if __has_feature(cxx_rvalue_references) && \ (defined(_LIBCPP_VERSION) || defined(__GLIBCXX__) && __GLIBCXX__ >= 20080306) +#define RAPIDJSON_HAS_CXX11_RVALUE_REFS 1 +#else +#define RAPIDJSON_HAS_CXX11_RVALUE_REFS 0 +#endif #elif (defined(RAPIDJSON_GNUC) && (RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4,3,0)) && defined(__GXX_EXPERIMENTAL_CXX0X__)) || \ (defined(_MSC_VER) && _MSC_VER >= 1600) From 6b6b121ff0a2cd6d1e82769eefc3da9d95cd079e Mon Sep 17 00:00:00 2001 From: Dan Vanderkam Date: Tue, 26 Apr 2016 19:24:52 -0400 Subject: [PATCH 064/305] Fix filterkeydom link --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 77c3b5b..fd5d4c6 100644 --- a/readme.md +++ b/readme.md @@ -147,4 +147,4 @@ More [examples](https://github.com/miloyip/rapidjson/tree/master/example) are av * [prettyauto](https://github.com/miloyip/rapidjson/blob/master/example/prettyauto/prettyauto.cpp): A modified version of [pretty](https://github.com/miloyip/rapidjson/blob/master/example/pretty/pretty.cpp) to automatically handle JSON with any UTF encodings. * [parsebyparts](https://github.com/miloyip/rapidjson/blob/master/example/parsebyparts/parsebyparts.cpp): Implements an `AsyncDocumentParser` which can parse JSON in parts, using C++11 thread. * [filterkey](https://github.com/miloyip/rapidjson/blob/master/example/filterkey/filterkey.cpp): A command line tool to remove all values with user-specified key. - * [filterkeydom](https://github.com/miloyip/rapidjson/blob/master/example/filterkey/filterkey.cpp): Same tool as above, but it demonstrates how to use a generator to populate a `Document`. + * [filterkeydom](https://github.com/miloyip/rapidjson/blob/master/example/filterkeydom/filterkeydom.cpp): Same tool as above, but it demonstrates how to use a generator to populate a `Document`. From c02d52ad56595dc70b38daf46b5f315d3a7115fa Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Fri, 29 Apr 2016 17:45:09 +0800 Subject: [PATCH 065/305] Fix documentation mistake in #620 --- doc/dom.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/dom.md b/doc/dom.md index 6cccf08..60480c3 100644 --- a/doc/dom.md +++ b/doc/dom.md @@ -163,7 +163,7 @@ Document d; if (d.Parse(json).HasParseError()) { fprintf(stderr, "\nError(offset %u): %s\n", (unsigned)d.GetErrorOffset(), - GetParseError_En(d.GetParseErrorCode())); + GetParseError_En(d.GetParseError())); // ... } ~~~~~~~~~~ From 5bff05963c44987c13d7d1172e27703c1a117e69 Mon Sep 17 00:00:00 2001 From: Andrea Colaci Date: Mon, 9 May 2016 16:19:10 +0100 Subject: [PATCH 066/305] package json --- package.json | Bin 0 -> 566 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 package.json diff --git a/package.json b/package.json new file mode 100644 index 0000000000000000000000000000000000000000..e45eb1118e4d7fb04d4d3a388cd6757b5b32d430 GIT binary patch literal 566 zcma))F;BxV5QX>t3S*=S1WG7lSH!@~NEuxFNUlwM)}4b|RsHYSP8vFPviy8?#XFwHv^EmeR z1`7hcgG05GiGmC^5+g|BpORfNOq^_%mmDA|a4>!7O1R5*szi{Oo6DM-&n5C0?FxBm z1KX0?=yzt~weqBLK}i6MKL~E4)pDuefKjdVOk)Tnm{K19Gsa^maT<)5kOOx)Nm8EL z7=x_qI_=wGTUqMs9=pBqPH5n(`Q+=fj~QZ1oEhVAI<(-W*nE7xjoNl-uAVhn-fCB) a$dD#IJGRtAFio+z6>`sDT>2=B^WqOMJE}4O literal 0 HcmV?d00001 From cf8f08f9d01ad50324e3faa6ca707fb9c1fe6a10 Mon Sep 17 00:00:00 2001 From: Andrea Colaci Date: Mon, 9 May 2016 16:29:15 +0100 Subject: [PATCH 067/305] include dirs --- include_dirs.js | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 include_dirs.js diff --git a/include_dirs.js b/include_dirs.js new file mode 100644 index 0000000..801d832 --- /dev/null +++ b/include_dirs.js @@ -0,0 +1,2 @@ +console.log(require('path').relative('.', __dirname)); + From af327524edf916c4c33cabfe2a584bc807df9282 Mon Sep 17 00:00:00 2001 From: Andrea Colaci Date: Mon, 9 May 2016 16:31:37 +0100 Subject: [PATCH 068/305] package update --- package.json | Bin 566 -> 595 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/package.json b/package.json index e45eb1118e4d7fb04d4d3a388cd6757b5b32d430..cf1685b7f8ccefadcc1a275cf3c54d20d5a50607 100644 GIT binary patch delta 45 zcmdnSa+zg|0|V!f Date: Mon, 9 May 2016 16:33:35 +0100 Subject: [PATCH 069/305] refs --- package.json | Bin 595 -> 583 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/package.json b/package.json index cf1685b7f8ccefadcc1a275cf3c54d20d5a50607..9e5e4f23ce6e46f3e24fec65ebd28287b53b5d53 100644 GIT binary patch delta 34 hcmcc2a-3yDDkDpNWoE(TT*h<=;|`-OgrUe}1OVL>3 Date: Mon, 9 May 2016 16:45:18 +0100 Subject: [PATCH 070/305] include folder added --- include_dirs.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include_dirs.js b/include_dirs.js index 801d832..b373e85 100644 --- a/include_dirs.js +++ b/include_dirs.js @@ -1,2 +1,2 @@ -console.log(require('path').relative('.', __dirname)); - +var path = require('path'); +console.log(path.join(path.relative('.', __dirname), 'include')); From 7cc76a9d46257c0e71c88ca7dbc995897cfb070f Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Tue, 10 May 2016 13:20:03 +0800 Subject: [PATCH 071/305] Fix #630 --- include/rapidjson/internal/biginteger.h | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 include/rapidjson/internal/biginteger.h diff --git a/include/rapidjson/internal/biginteger.h b/include/rapidjson/internal/biginteger.h old mode 100755 new mode 100644 From 819ba73b17f9e9ce06eab74fd52d8d9d97b0b4ac Mon Sep 17 00:00:00 2001 From: Vlad Lipskiy Date: Tue, 10 May 2016 18:03:03 +0300 Subject: [PATCH 072/305] Added missing include guards in istreamwrapper.h and ostreamwrapper.h --- include/rapidjson/istreamwrapper.h | 5 +++++ include/rapidjson/ostreamwrapper.h | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/include/rapidjson/istreamwrapper.h b/include/rapidjson/istreamwrapper.h index c73586e..f5fe289 100644 --- a/include/rapidjson/istreamwrapper.h +++ b/include/rapidjson/istreamwrapper.h @@ -12,6 +12,9 @@ // CONDITIONS OF ANY KIND, either express or implied. See the License for the // specific language governing permissions and limitations under the License. +#ifndef RAPIDJSON_ISTREAMWRAPPER_H_ +#define RAPIDJSON_ISTREAMWRAPPER_H_ + #include "stream.h" #include @@ -108,3 +111,5 @@ RAPIDJSON_DIAG_POP #endif RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_ISTREAMWRAPPER_H_ diff --git a/include/rapidjson/ostreamwrapper.h b/include/rapidjson/ostreamwrapper.h index 8bf36dc..6f4667c 100644 --- a/include/rapidjson/ostreamwrapper.h +++ b/include/rapidjson/ostreamwrapper.h @@ -12,6 +12,9 @@ // CONDITIONS OF ANY KIND, either express or implied. See the License for the // specific language governing permissions and limitations under the License. +#ifndef RAPIDJSON_OSTREAMWRAPPER_H_ +#define RAPIDJSON_OSTREAMWRAPPER_H_ + #include "stream.h" #include @@ -74,3 +77,5 @@ RAPIDJSON_DIAG_POP #endif RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_OSTREAMWRAPPER_H_ From 7b32bbaae7fb4474bc848a783e8189ee0184349d Mon Sep 17 00:00:00 2001 From: liujiayang Date: Wed, 11 May 2016 10:59:56 +0800 Subject: [PATCH 073/305] fix document problem --- doc/encoding.md | 4 ++-- doc/encoding.zh-cn.md | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/doc/encoding.md b/doc/encoding.md index e9ebe8f..62ceb4b 100644 --- a/doc/encoding.md +++ b/doc/encoding.md @@ -131,8 +131,8 @@ StringStream source(s); GenericStringBuffer > target; bool hasError = false; -while (source.Peak() != '\0') - if (!Transcoder::Transcode, UTF16<> >(source, target)) { +while (source.Peek() != '\0') + if (!Transcoder, UTF16<>>::Transcode(source, target)) { hasError = true; break; } diff --git a/doc/encoding.zh-cn.md b/doc/encoding.zh-cn.md index 4858bae..8ab0bd3 100644 --- a/doc/encoding.zh-cn.md +++ b/doc/encoding.zh-cn.md @@ -137,8 +137,8 @@ StringStream source(s); GenericStringBuffer > target; bool hasError = false; -while (source.Peak() != '\0') - if (!Transcoder::Transcode, UTF16<> >(source, target)) { +while (source.Peek() != '\0') + if (!Transcoder, UTF16<>>::Transcode(source, target)) { hasError = true; break; } From e154f8e9594b770dea4562cb2b6cc25ac72346fd Mon Sep 17 00:00:00 2001 From: liujiayang Date: Wed, 11 May 2016 11:01:47 +0800 Subject: [PATCH 074/305] add space for template --- doc/encoding.md | 2 +- doc/encoding.zh-cn.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/encoding.md b/doc/encoding.md index 62ceb4b..8f8ff7f 100644 --- a/doc/encoding.md +++ b/doc/encoding.md @@ -132,7 +132,7 @@ GenericStringBuffer > target; bool hasError = false; while (source.Peek() != '\0') - if (!Transcoder, UTF16<>>::Transcode(source, target)) { + if (!Transcoder, UTF16<> >::Transcode(source, target)) { hasError = true; break; } diff --git a/doc/encoding.zh-cn.md b/doc/encoding.zh-cn.md index 8ab0bd3..163eade 100644 --- a/doc/encoding.zh-cn.md +++ b/doc/encoding.zh-cn.md @@ -138,7 +138,7 @@ GenericStringBuffer > target; bool hasError = false; while (source.Peek() != '\0') - if (!Transcoder, UTF16<>>::Transcode(source, target)) { + if (!Transcoder, UTF16<> >::Transcode(source, target)) { hasError = true; break; } From 77089614841eda75823cfbf0642c99f7cc09c869 Mon Sep 17 00:00:00 2001 From: Andrea Colaci Date: Tue, 17 May 2016 13:33:26 +0100 Subject: [PATCH 075/305] npm docs --- doc/features.md | 1 + doc/npm.md | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 doc/npm.md diff --git a/doc/features.md b/doc/features.md index f092cf1..984c6ab 100644 --- a/doc/features.md +++ b/doc/features.md @@ -26,6 +26,7 @@ * Support optional relaxed syntax. * Single line (`// ...`) and multiple line (`/* ... */`) comments (`kParseCommentsFlag`). * Trailing commas at the end of objects and arrays (`kParseTrailingCommasFlag`). +* [NPM compliant](doc/npm.md). ## Unicode diff --git a/doc/npm.md b/doc/npm.md new file mode 100644 index 0000000..5efa768 --- /dev/null +++ b/doc/npm.md @@ -0,0 +1,31 @@ +## NPM + +# package.json {#package} + +~~~~~~~~~~js +{ + ... + "dependencies": { + ... + "rapidjson": "git@github.com:miloyip/rapidjson.git" + }, + ... + "gypfile": true +} +~~~~~~~~~~ + +# binding.gyp {#binding} + +~~~~~~~~~~js +{ + ... + 'targets': [ + { + ... + 'include_dirs': [ + ' Date: Tue, 17 May 2016 13:33:43 +0100 Subject: [PATCH 076/305] 1.0.4 --- package.json | Bin 583 -> 561 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/package.json b/package.json index 9e5e4f23ce6e46f3e24fec65ebd28287b53b5d53..cc6087a5ca36cfd95aacccceb7c07d909f085f4d 100644 GIT binary patch delta 23 fcmX@kvXNziHlxWz9f!$gOne)Qw=gnu)p7v Date: Wed, 18 May 2016 21:09:21 +0200 Subject: [PATCH 077/305] Allow options for writing and parsing NaN/Infinity This adds kWriteNanAndInfFlag to Writer to allow writing of nan, inf and -inf doubles as "NaN", "Infinity" and "-Infinity", respectively, and kParseNanAndInfFlag to Reader to allow parsing of "NaN", "Inf", "Infinity", "-Inf" and "-Infinity". This is part of issue #36, adding optional support for relaxed JSON syntax. --- doc/dom.md | 1 + include/rapidjson/reader.h | 23 ++++++++++++- include/rapidjson/writer.h | 44 +++++++++++++++++++++--- test/unittest/readertest.cpp | 65 ++++++++++++++++++++++++++++++++++++ test/unittest/writertest.cpp | 23 ++++++++++--- 5 files changed, 146 insertions(+), 10 deletions(-) diff --git a/doc/dom.md b/doc/dom.md index 60480c3..6c541fe 100644 --- a/doc/dom.md +++ b/doc/dom.md @@ -118,6 +118,7 @@ Parse flags | Meaning `kParseCommentsFlag` | Allow one-line `// ...` and multi-line `/* ... */` comments (relaxed JSON syntax). `kParseNumbersAsStringsFlag` | Parse numerical type values as strings. `kParseTrailingCommasFlag` | Allow trailing commas at the end of objects and arrays (relaxed JSON syntax). +`kParseNanAndInfFlag` | Allow parsing `NaN`, `Inf`, `Infinity`, `-Inf` and `-Infinity` as `double` values (relaxed JSON syntax). By using a non-type template parameter, instead of a function parameter, C++ compiler can generate code which is optimized for specified combinations, improving speed, and reducing code size (if only using a single specialization). The downside is the flags needed to be determined in compile-time. diff --git a/include/rapidjson/reader.h b/include/rapidjson/reader.h index 16e2d07..13fd126 100644 --- a/include/rapidjson/reader.h +++ b/include/rapidjson/reader.h @@ -23,6 +23,7 @@ #include "internal/meta.h" #include "internal/stack.h" #include "internal/strtod.h" +#include #if defined(RAPIDJSON_SIMD) && defined(_MSC_VER) #include @@ -150,6 +151,7 @@ enum ParseFlag { kParseCommentsFlag = 32, //!< Allow one-line (//) and multi-line (/**/) comments. kParseNumbersAsStringsFlag = 64, //!< Parse all numbers (ints/doubles) as strings. kParseTrailingCommasFlag = 128, //!< Allow trailing commas at the end of objects and arrays. + kParseNanAndInfFlag = 256, //!< Allow parsing NaN, Inf, Infinity, -Inf and -Infinity as doubles. kParseDefaultFlags = RAPIDJSON_PARSE_DEFAULT_FLAGS //!< Default parse flags. Can be customized by defining RAPIDJSON_PARSE_DEFAULT_FLAGS }; @@ -1137,6 +1139,8 @@ private: (parseFlags & kParseInsituFlag) == 0> s(*this, copy.s); size_t startOffset = s.Tell(); + double d = 0.0; + bool useNanOrInf = false; // Parse minus bool minus = Consume(s, '-'); @@ -1178,12 +1182,26 @@ private: significandDigit++; } } + // Parse NaN or Infinity here + else if ((parseFlags & kParseNanAndInfFlag) && RAPIDJSON_LIKELY((s.Peek() == 'I' || s.Peek() == 'N'))) { + useNanOrInf = true; + if (RAPIDJSON_LIKELY(Consume(s, 'N') && Consume(s, 'a') && Consume(s, 'N'))) { + d = std::numeric_limits::quiet_NaN(); + } + else if (RAPIDJSON_LIKELY(Consume(s, 'I') && Consume(s, 'n') && Consume(s, 'f'))) { + d = (minus ? -std::numeric_limits::infinity() : std::numeric_limits::infinity()); + if (RAPIDJSON_UNLIKELY(s.Peek() == 'i' && !(Consume(s, 'i') && Consume(s, 'n') + && Consume(s, 'i') && Consume(s, 't') && Consume(s, 'y')))) + RAPIDJSON_PARSE_ERROR(kParseErrorValueInvalid, s.Tell()); + } + else + RAPIDJSON_PARSE_ERROR(kParseErrorValueInvalid, s.Tell()); + } else RAPIDJSON_PARSE_ERROR(kParseErrorValueInvalid, s.Tell()); // Parse 64bit int bool useDouble = false; - double d = 0.0; if (use64bit) { if (minus) while (RAPIDJSON_LIKELY(s.Peek() >= '0' && s.Peek() <= '9')) { @@ -1346,6 +1364,9 @@ private: cont = handler.Double(minus ? -d : d); } + else if (useNanOrInf) { + cont = handler.Double(d); + } else { if (use64bit) { if (minus) diff --git a/include/rapidjson/writer.h b/include/rapidjson/writer.h index 2809f70..82797bb 100644 --- a/include/rapidjson/writer.h +++ b/include/rapidjson/writer.h @@ -62,6 +62,7 @@ RAPIDJSON_NAMESPACE_BEGIN enum WriteFlag { kWriteNoFlags = 0, //!< No flags are set. kWriteValidateEncodingFlag = 1, //!< Validate encoding of JSON strings. + kWriteNanAndInfFlag = 2, //!< Allow writing of Inf, -Inf and NaN. kWriteDefaultFlags = RAPIDJSON_WRITE_DEFAULT_FLAGS //!< Default write flags. Can be customized by defining RAPIDJSON_WRITE_DEFAULT_FLAGS }; @@ -319,9 +320,25 @@ protected: } bool WriteDouble(double d) { - if (internal::Double(d).IsNanOrInf()) - return false; - + if (internal::Double(d).IsNanOrInf()) { + if (!(writeFlags & kWriteNanAndInfFlag)) + return false; + if (internal::Double(d).IsNan()) { + PutReserve(*os_, 3); + PutUnsafe(*os_, 'N'); PutUnsafe(*os_, 'a'); PutUnsafe(*os_, 'N'); + return true; + } + if (internal::Double(d).Sign()) { + PutReserve(*os_, 9); + PutUnsafe(*os_, '-'); + } + else + PutReserve(*os_, 8); + PutUnsafe(*os_, 'I'); PutUnsafe(*os_, 'n'); PutUnsafe(*os_, 'f'); + PutUnsafe(*os_, 'i'); PutUnsafe(*os_, 'n'); PutUnsafe(*os_, 'i'); PutUnsafe(*os_, 't'); PutUnsafe(*os_, 'y'); + return true; + } + char buffer[25]; char* end = internal::dtoa(d, buffer, maxDecimalPlaces_); PutReserve(*os_, static_cast(end - buffer)); @@ -489,8 +506,25 @@ inline bool Writer::WriteUint64(uint64_t u) { template<> inline bool Writer::WriteDouble(double d) { - if (internal::Double(d).IsNanOrInf()) - return false; + if (internal::Double(d).IsNanOrInf()) { + // Note: This code path can only be reached if (RAPIDJSON_WRITE_DEFAULT_FLAGS & kWriteNanAndInfFlag). + if (!(kWriteDefaultFlags & kWriteNanAndInfFlag)) + return false; + if (internal::Double(d).IsNan()) { + PutReserve(*os_, 3); + PutUnsafe(*os_, 'N'); PutUnsafe(*os_, 'a'); PutUnsafe(*os_, 'N'); + return true; + } + if (internal::Double(d).Sign()) { + PutReserve(*os_, 9); + PutUnsafe(*os_, '-'); + } + else + PutReserve(*os_, 8); + PutUnsafe(*os_, 'I'); PutUnsafe(*os_, 'n'); PutUnsafe(*os_, 'f'); + PutUnsafe(*os_, 'i'); PutUnsafe(*os_, 'n'); PutUnsafe(*os_, 'i'); PutUnsafe(*os_, 't'); PutUnsafe(*os_, 'y'); + return true; + } char *buffer = os_->Push(25); char* end = internal::dtoa(d, buffer, maxDecimalPlaces_); diff --git a/test/unittest/readertest.cpp b/test/unittest/readertest.cpp index 329af2a..69c3cc4 100644 --- a/test/unittest/readertest.cpp +++ b/test/unittest/readertest.cpp @@ -19,6 +19,8 @@ #include "rapidjson/internal/itoa.h" #include "rapidjson/memorystream.h" +#include + using namespace rapidjson; #ifdef __GNUC__ @@ -1774,6 +1776,69 @@ TEST(Reader, TrailingCommaHandlerTerminationIterative) { TestTrailingCommaHandlerTermination(); } +TEST(Reader, ParseNanAndInfinity) { +#define TEST_NAN_INF(str, x) \ + { \ + { \ + StringStream s(str); \ + ParseDoubleHandler h; \ + Reader reader; \ + ASSERT_EQ(kParseErrorNone, reader.Parse(s, h).Code()); \ + EXPECT_EQ(1u, h.step_); \ + internal::Double e(x), a(h.actual_); \ + EXPECT_EQ(e.IsNan(), a.IsNan()); \ + EXPECT_EQ(e.IsInf(), a.IsInf()); \ + if (!e.IsNan()) \ + EXPECT_EQ(e.Sign(), a.Sign()); \ + } \ + { \ + const char* json = "{ \"naninfdouble\": " str " } "; \ + StringStream s(json); \ + NumbersAsStringsHandler h(str); \ + Reader reader; \ + EXPECT_TRUE(reader.Parse(s, h)); \ + } \ + { \ + char* json = StrDup("{ \"naninfdouble\": " str " } "); \ + InsituStringStream s(json); \ + NumbersAsStringsHandler h(str); \ + Reader reader; \ + EXPECT_TRUE(reader.Parse(s, h)); \ + free(json); \ + } \ + } +#define TEST_NAN_INF_ERROR(errorCode, str, errorOffset) \ + { \ + int streamPos = errorOffset; \ + char buffer[1001]; \ + strncpy(buffer, str, 1000); \ + InsituStringStream s(buffer); \ + BaseReaderHandler<> h; \ + Reader reader; \ + EXPECT_FALSE(reader.Parse(s, h)); \ + EXPECT_EQ(errorCode, reader.GetParseErrorCode());\ + EXPECT_EQ(errorOffset, reader.GetErrorOffset());\ + EXPECT_EQ(streamPos, s.Tell());\ + } + + double nan = std::numeric_limits::quiet_NaN(); + double inf = std::numeric_limits::infinity(); + + TEST_NAN_INF("NaN", nan); + TEST_NAN_INF("-NaN", nan); + TEST_NAN_INF("Inf", inf); + TEST_NAN_INF("Infinity", inf); + TEST_NAN_INF("-Inf", -inf); + TEST_NAN_INF("-Infinity", -inf); + TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "nan", 1); + TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "-nan", 1); + TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "NAN", 1); + TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "-Infinty", 6); + +#undef TEST_NAN_INF_ERROR +#undef TEST_NAN_INF +} + #ifdef __GNUC__ RAPIDJSON_DIAG_POP #endif diff --git a/test/unittest/writertest.cpp b/test/unittest/writertest.cpp index 9c68c53..22f428e 100644 --- a/test/unittest/writertest.cpp +++ b/test/unittest/writertest.cpp @@ -446,9 +446,15 @@ TEST(Writer, NaN) { double nan = zero / zero; EXPECT_TRUE(internal::Double(nan).IsNan()); StringBuffer buffer; - Writer writer(buffer); - EXPECT_FALSE(writer.Double(nan)); - + { + Writer writer(buffer); + EXPECT_FALSE(writer.Double(nan)); + } + { + Writer, UTF8<>, CrtAllocator, kWriteNanAndInfFlag> writer(buffer); + EXPECT_TRUE(writer.Double(nan)); + EXPECT_STREQ("NaN", buffer.GetString()); + } GenericStringBuffer > buffer2; Writer > > writer2(buffer2); EXPECT_FALSE(writer2.Double(nan)); @@ -460,12 +466,21 @@ TEST(Writer, Inf) { StringBuffer buffer; { Writer writer(buffer); - EXPECT_FALSE(writer.Double(inf)); + EXPECT_FALSE(writer.Double(inf)); } { Writer writer(buffer); EXPECT_FALSE(writer.Double(-inf)); } + { + Writer, UTF8<>, CrtAllocator, kWriteNanAndInfFlag> writer(buffer); + EXPECT_TRUE(writer.Double(inf)); + } + { + Writer, UTF8<>, CrtAllocator, kWriteNanAndInfFlag> writer(buffer); + EXPECT_TRUE(writer.Double(-inf)); + } + EXPECT_STREQ("Infinity-Infinity", buffer.GetString()); } TEST(Writer, RawValue) { From 47d73200966bc5e9a5450a794051526c0349e978 Mon Sep 17 00:00:00 2001 From: Zhang Ye Date: Tue, 7 Jun 2016 20:17:33 +0800 Subject: [PATCH 078/305] Update reference to gtest thirdparty module. --- .gitmodules | 2 +- thirdparty/gtest | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitmodules b/.gitmodules index 8e9d1f3..5e41f7c 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,3 @@ [submodule "thirdparty/gtest"] path = thirdparty/gtest - url = https://chromium.googlesource.com/external/googletest.git + url = https://github.com/google/googletest.git diff --git a/thirdparty/gtest b/thirdparty/gtest index 0476e15..0a43962 160000 --- a/thirdparty/gtest +++ b/thirdparty/gtest @@ -1 +1 @@ -Subproject commit 0476e154db5fab1721c2a0f32abf4aa773679b52 +Subproject commit 0a439623f75c029912728d80cb7f1b8b48739ca4 From ce3ca58fee67c1b2e90bcb742bc8d7f1dc87ac34 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Fri, 10 Jun 2016 10:15:45 +0800 Subject: [PATCH 079/305] Change googletest search path for cmake --- CMakeModules/FindGTestSrc.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeModules/FindGTestSrc.cmake b/CMakeModules/FindGTestSrc.cmake index f942a8d..f3cb8c9 100644 --- a/CMakeModules/FindGTestSrc.cmake +++ b/CMakeModules/FindGTestSrc.cmake @@ -1,7 +1,7 @@ SET(GTEST_SEARCH_PATH "${GTEST_SOURCE_DIR}" - "${CMAKE_CURRENT_LIST_DIR}/../thirdparty/gtest") + "${CMAKE_CURRENT_LIST_DIR}/../thirdparty/gtest/googletest") IF(UNIX) IF(RAPIDJSON_BUILD_THIRDPARTY_GTEST) From d1697f74379a03eee986adee29d8b3d7fc6785ea Mon Sep 17 00:00:00 2001 From: Eli Fidler Date: Wed, 8 Jun 2016 12:20:39 -0400 Subject: [PATCH 080/305] use included clang on travis This should fix the Travis clang builds, since the upstream LLVM apt repo is down. --- .travis.yml | 97 ++++++----------------------------------------------- 1 file changed, 10 insertions(+), 87 deletions(-) diff --git a/.travis.yml b/.travis.yml index 9266277..f9319f2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,16 +1,12 @@ +sudo: required +dist: precise + language: cpp -sudo: false cache: - ccache -addons: - apt: - packages: &default_packages - - cmake - - valgrind - env: -global: + global: - USE_CCACHE=1 - CCACHE_SLOPPINESS=pch_defines,time_macros - CCACHE_COMPRESS=1 @@ -20,108 +16,41 @@ global: - GITHUB_REPO='miloyip/rapidjson' - secure: "HrsaCb+N66EG1HR+LWH1u51SjaJyRwJEDzqJGYMB7LJ/bfqb9mWKF1fLvZGk46W5t7TVaXRDD5KHFx9DPWvKn4gRUVkwTHEy262ah5ORh8M6n/6VVVajeV/AYt2C0sswdkDBDO4Xq+xy5gdw3G8s1A4Inbm73pUh+6vx+7ltBbk=" +before_install: + - sudo apt-add-repository -y ppa:ubuntu-toolchain-r/test + - sudo apt-get update -qq + - sudo apt-get install -y cmake valgrind g++-multilib libc6-dbg:i386 + matrix: include: # gcc - env: CONF=release ARCH=x86 CXX11=ON compiler: gcc - addons: - apt: - packages: - - *default_packages - - g++-multilib - - libc6-dbg:i386 - env: CONF=release ARCH=x86_64 CXX11=ON compiler: gcc - env: CONF=debug ARCH=x86 CXX11=OFF compiler: gcc - addons: - apt: - packages: - - *default_packages - - g++-multilib - - libc6-dbg:i386 - env: CONF=debug ARCH=x86_64 CXX11=OFF compiler: gcc # clang - env: CONF=debug ARCH=x86 CXX11=ON CCACHE_CPP2=yes compiler: clang - addons: - apt: - sources: - - llvm-toolchain-precise-3.7 - - ubuntu-toolchain-r-test - packages: - - *default_packages - - g++-multilib - - libc6-dbg:i386 - - clang-3.7 - env: CONF=debug ARCH=x86_64 CXX11=ON CCACHE_CPP2=yes compiler: clang - addons: - apt: - sources: - - llvm-toolchain-precise-3.7 - - ubuntu-toolchain-r-test - packages: - - *default_packages - - clang-3.7 - env: CONF=debug ARCH=x86 CXX11=OFF CCACHE_CPP2=yes compiler: clang - addons: - apt: - sources: - - llvm-toolchain-precise-3.7 - - ubuntu-toolchain-r-test - packages: - - *default_packages - - g++-multilib - - libc6-dbg:i386 - - clang-3.7 - env: CONF=debug ARCH=x86_64 CXX11=OFF CCACHE_CPP2=yes compiler: clang - addons: - apt: - sources: - - llvm-toolchain-precise-3.7 - - ubuntu-toolchain-r-test - packages: - - *default_packages - - clang-3.7 - env: CONF=release ARCH=x86 CXX11=ON CCACHE_CPP2=yes compiler: clang - addons: - apt: - sources: - - llvm-toolchain-precise-3.7 - - ubuntu-toolchain-r-test - packages: - - *default_packages - - g++-multilib - - libc6-dbg:i386 - - clang-3.7 - env: CONF=release ARCH=x86_64 CXX11=ON CCACHE_CPP2=yes compiler: clang - addons: - apt: - sources: - - llvm-toolchain-precise-3.7 - - ubuntu-toolchain-r-test - packages: - - *default_packages - - clang-3.7 # coverage report - env: CONF=debug ARCH=x86 CXX11=ON GCOV_FLAGS='--coverage' compiler: gcc cache: - ccache - pip - addons: - apt: - packages: - - *default_packages - - g++-multilib - - libc6-dbg:i386 after_success: - pip install --user cpp-coveralls - coveralls -r .. --gcov-options '\-lp' -e thirdparty -e example -e test -e build/CMakeFiles -e include/rapidjson/msinttypes -e include/rapidjson/internal/meta.h -e include/rapidjson/error/en.h @@ -130,12 +59,6 @@ matrix: cache: - ccache - pip - addons: - apt: - packages: - - *default_packages - - g++-multilib - - libc6-dbg:i386 after_success: - pip install --user cpp-coveralls - coveralls -r .. --gcov-options '\-lp' -e thirdparty -e example -e test -e build/CMakeFiles -e include/rapidjson/msinttypes -e include/rapidjson/internal/meta.h -e include/rapidjson/error/en.h @@ -158,7 +81,7 @@ before_script: - mkdir build script: - - if [ "$CXX" = "clang++" ]; then export CXX="clang++-3.7" CC="clang-3.7"; fi + - if [ "$CXX" = "clang++" ]; then export CXXFLAGS="-stdlib=libc++ ${CXXFLAGS}"; fi - > eval "ARCH_FLAGS=\${ARCH_FLAGS_${ARCH}}" ; (cd build && cmake From fee5190defa1971603e68a2eb136e975275c468a Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Mon, 13 Jun 2016 09:34:47 +0800 Subject: [PATCH 081/305] Fix a clang warning --- include/rapidjson/writer.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/rapidjson/writer.h b/include/rapidjson/writer.h index 82797bb..7d0610e 100644 --- a/include/rapidjson/writer.h +++ b/include/rapidjson/writer.h @@ -41,6 +41,7 @@ RAPIDJSON_DIAG_OFF(4127) // conditional expression is constant #ifdef __clang__ RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_OFF(padded) +RAPIDJSON_DIAG_OFF(unreachable-code) #endif RAPIDJSON_NAMESPACE_BEGIN From 2e6633913718b923a949f31ce76ce14ccf4bea8d Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Mon, 13 Jun 2016 09:54:02 +0800 Subject: [PATCH 082/305] Disable parsebyparts example for clang --- example/parsebyparts/parsebyparts.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/example/parsebyparts/parsebyparts.cpp b/example/parsebyparts/parsebyparts.cpp index 919d908..57eed00 100644 --- a/example/parsebyparts/parsebyparts.cpp +++ b/example/parsebyparts/parsebyparts.cpp @@ -1,7 +1,8 @@ // Example of parsing JSON to document by parts. // Using C++11 threads -#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1700) +// Temporarily disable for clang (older version) due to incompatibility with libstdc++ +#if (__cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1700)) && !defined(__clang__) #include "rapidjson/document.h" #include "rapidjson/error/en.h" From 56bb9992b0fb327a5d147c6e0fc17a290d698a72 Mon Sep 17 00:00:00 2001 From: Eli Fidler Date: Tue, 31 May 2016 10:46:52 -0400 Subject: [PATCH 083/305] support building with ASAN and UBSAN on Clang and GCC --- CMakeLists.txt | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index d315b74..96bfdc2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,8 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.8) +if(POLICY CMP0025) + # detect Apple's Clang + cmake_policy(SET CMP0025 NEW) +endif() if(POLICY CMP0054) cmake_policy(SET CMP0054 NEW) endif() @@ -28,6 +32,9 @@ option(RAPIDJSON_BUILD_THIRDPARTY_GTEST option(RAPIDJSON_BUILD_CXX11 "Build rapidjson with C++11 (gcc/clang)" ON) +option(RAPIDJSON_BUILD_ASAN "Build rapidjson with address sanitizer (gcc/clang)" OFF) +option(RAPIDJSON_BUILD_UBSAN "Build rapidjson with undefined behavior sanitizer (gcc/clang)" OFF) + option(RAPIDJSON_HAS_STDSTRING "" OFF) if(RAPIDJSON_HAS_STDSTRING) add_definitions(-DRAPIDJSON_HAS_STDSTRING) @@ -51,11 +58,35 @@ if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") endif() endif() + if (RAPIDJSON_BUILD_ASAN) + if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "4.8.0") + message(FATAL_ERROR "GCC < 4.8 doesn't support the address sanitizer") + else() + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address") + endif() + endif() + if (RAPIDJSON_BUILD_UBSAN) + if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "4.9.0") + message(FATAL_ERROR "GCC < 4.9 doesn't support the undefined behavior sanitizer") + else() + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined") + endif() + endif() elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native -Wall -Wextra -Werror -Wno-missing-field-initializers") if (RAPIDJSON_BUILD_CXX11) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") endif() + if (RAPIDJSON_BUILD_ASAN) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address") + endif() + if (RAPIDJSON_BUILD_UBSAN) + if (CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined-trap -fsanitize-undefined-trap-on-error") + else() + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined") + endif() + endif() elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") add_definitions(-D_CRT_SECURE_NO_WARNINGS=1) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc") From 89f6b8a380c326bd37defb0e330f31a0d894617d Mon Sep 17 00:00:00 2001 From: Eli Fidler Date: Tue, 31 May 2016 11:16:28 -0400 Subject: [PATCH 084/305] Clang doesn't like the C-style casts in nmmintrin.h --- include/rapidjson/reader.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/rapidjson/reader.h b/include/rapidjson/reader.h index 13fd126..19f8849 100644 --- a/include/rapidjson/reader.h +++ b/include/rapidjson/reader.h @@ -43,6 +43,7 @@ RAPIDJSON_DIAG_OFF(4702) // unreachable code #ifdef __clang__ RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(old-style-cast) RAPIDJSON_DIAG_OFF(padded) RAPIDJSON_DIAG_OFF(switch-enum) #endif From 13e3aa9b00eac83bac9f67b8adf86a9ae18cca27 Mon Sep 17 00:00:00 2001 From: Eli Fidler Date: Tue, 31 May 2016 11:24:38 -0400 Subject: [PATCH 085/305] we do need to avoid the double-promotion warning on clang, since we're compiling with -Werror --- test/unittest/CMakeLists.txt | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/unittest/CMakeLists.txt b/test/unittest/CMakeLists.txt index 4e3b071..fae09cd 100644 --- a/test/unittest/CMakeLists.txt +++ b/test/unittest/CMakeLists.txt @@ -38,11 +38,11 @@ if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror -Wall -Wextra -Weffc++ -Wswitch-default -Wfloat-equal") elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror -Wall -Wextra -Weffc++ -Wswitch-default -Wfloat-equal -Wimplicit-fallthrough -Weverything") - # If the user is running a newer version of Clang that includes the - # -Wdouble-promotion, we will ignore that warning. - # if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 3.7) - # set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-double-promotion") - # endif() + # If the user is running a newer version of Clang that includes the + # -Wdouble-promotion, we will ignore that warning. + if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 3.7) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-double-promotion") + endif() elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") # Force to always compile with /W4 if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]") From 035271091faa49734f40782b6cfad254b0302fa2 Mon Sep 17 00:00:00 2001 From: Eli Fidler Date: Tue, 31 May 2016 11:32:17 -0400 Subject: [PATCH 086/305] with recent clang, when expected is false, this code triggers -Wunreachable-code clang advises: "note: silence by adding parentheses to mark code as explicitly dead" --- test/unittest/schematest.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unittest/schematest.cpp b/test/unittest/schematest.cpp index d1027ad..d75b1e5 100644 --- a/test/unittest/schematest.cpp +++ b/test/unittest/schematest.cpp @@ -111,7 +111,7 @@ TEST(SchemaValidator, Hasher) { EXPECT_FALSE(d.HasParseError());\ EXPECT_TRUE(expected == d.Accept(validator));\ EXPECT_TRUE(expected == validator.IsValid());\ - if (expected && !validator.IsValid()) {\ + if ((expected) && !validator.IsValid()) {\ StringBuffer sb;\ validator.GetInvalidSchemaPointer().StringifyUriFragment(sb);\ printf("Invalid schema: %s\n", sb.GetString());\ From 5c77c9248cd429dfd07db290fc1caeadd1b76dc5 Mon Sep 17 00:00:00 2001 From: Eli Fidler Date: Tue, 31 May 2016 11:37:39 -0400 Subject: [PATCH 087/305] with recent clang, this triggers -Wunevaluated-expression specifically, "expression with side effects has no effect in an unevaluated context" --- test/unittest/valuetest.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/test/unittest/valuetest.cpp b/test/unittest/valuetest.cpp index feec049..619a6a5 100644 --- a/test/unittest/valuetest.cpp +++ b/test/unittest/valuetest.cpp @@ -1119,14 +1119,18 @@ TEST(Value, ArrayHelperRangeFor) { { int i = 0; - for (auto& v : x.GetArray()) - EXPECT_EQ(i++, v.GetInt()); + for (auto& v : x.GetArray()) { + EXPECT_EQ(i, v.GetInt()); + i++; + } EXPECT_EQ(i, 10); } { int i = 0; - for (const auto& v : const_cast(x).GetArray()) - EXPECT_EQ(i++, v.GetInt()); + for (const auto& v : const_cast(x).GetArray()) { + EXPECT_EQ(i, v.GetInt()); + i++; + } EXPECT_EQ(i, 10); } From fe550f38669fe0f488926c1ef0feb6c101f586d6 Mon Sep 17 00:00:00 2001 From: Eli Fidler Date: Tue, 31 May 2016 11:51:37 -0400 Subject: [PATCH 088/305] avoid array index out-of-bounds UBSAN gave "runtime error: index 13 out of bounds for type 'const uint32_t [10]'" --- include/rapidjson/internal/dtoa.h | 3 ++- test/unittest/dtoatest.cpp | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/include/rapidjson/internal/dtoa.h b/include/rapidjson/internal/dtoa.h index bc45496..8d6350e 100644 --- a/include/rapidjson/internal/dtoa.h +++ b/include/rapidjson/internal/dtoa.h @@ -102,7 +102,8 @@ inline void DigitGen(const DiyFp& W, const DiyFp& Mp, uint64_t delta, char* buff kappa--; if (p2 < delta) { *K += kappa; - GrisuRound(buffer, *len, delta, p2, one.f, wp_w.f * kPow10[-static_cast(kappa)]); + int index = -static_cast(kappa); + GrisuRound(buffer, *len, delta, p2, one.f, wp_w.f * (index < 9 ? kPow10[-static_cast(kappa)] : 0)); return; } } diff --git a/test/unittest/dtoatest.cpp b/test/unittest/dtoatest.cpp index fe28271..afd76eb 100644 --- a/test/unittest/dtoatest.cpp +++ b/test/unittest/dtoatest.cpp @@ -37,6 +37,7 @@ TEST(dtoa, normal) { TEST_DTOA(1.2345678, "1.2345678"); TEST_DTOA(0.123456789012, "0.123456789012"); TEST_DTOA(1234567.8, "1234567.8"); + TEST_DTOA(-79.39773355813419, "-79.39773355813419"); TEST_DTOA(0.000001, "0.000001"); TEST_DTOA(0.0000001, "1e-7"); TEST_DTOA(1e30, "1e30"); From 8074b722f0e13a3aad37460f40891660258efece Mon Sep 17 00:00:00 2001 From: Eli Fidler Date: Tue, 31 May 2016 11:54:58 -0400 Subject: [PATCH 089/305] avoid reference to null pointer and member access within null pointer UBSAN gave issues with the typeless Schema: runtime error: reference binding to null pointer of type 'rapidjson::GenericSchemaDocument, rapidjson::MemoryPoolAllocator >, rapidjson::CrtAllocator>' and runtime error: member access within null pointer of type 'AllocatorType' (aka 'rapidjson::CrtAllocator') --- include/rapidjson/schema.h | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/include/rapidjson/schema.h b/include/rapidjson/schema.h index 0a8bb7c..80812f0 100644 --- a/include/rapidjson/schema.h +++ b/include/rapidjson/schema.h @@ -413,9 +413,11 @@ public: } } - AssignIfExist(allOf_, *schemaDocument, p, value, GetAllOfString(), document); - AssignIfExist(anyOf_, *schemaDocument, p, value, GetAnyOfString(), document); - AssignIfExist(oneOf_, *schemaDocument, p, value, GetOneOfString(), document); + if (schemaDocument) { + AssignIfExist(allOf_, *schemaDocument, p, value, GetAllOfString(), document); + AssignIfExist(anyOf_, *schemaDocument, p, value, GetAnyOfString(), document); + AssignIfExist(oneOf_, *schemaDocument, p, value, GetOneOfString(), document); + } if (const ValueType* v = GetMember(value, GetNotString())) { schemaDocument->CreateSchema(¬_, p.Append(GetNotString(), allocator_), *v, document); @@ -578,7 +580,9 @@ public: } ~Schema() { - allocator_->Free(enum_); + if (allocator_) { + allocator_->Free(enum_); + } if (properties_) { for (SizeType i = 0; i < propertyCount_; i++) properties_[i].~Property(); From 61637d338244f316c86e875b3175a362d01820e0 Mon Sep 17 00:00:00 2001 From: Eli Fidler Date: Tue, 31 May 2016 12:03:50 -0400 Subject: [PATCH 090/305] avoid passing a null pointer to memcpy UBSAN on Clang/Linux gave: runtime error: null pointer passed as argument 2, which is declared to never be null /usr/include/string.h:43:45: note: nonnull attribute specified here --- include/rapidjson/pointer.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/include/rapidjson/pointer.h b/include/rapidjson/pointer.h index c985277..0206ac1 100644 --- a/include/rapidjson/pointer.h +++ b/include/rapidjson/pointer.h @@ -767,8 +767,12 @@ private: tokenCount_ = rhs.tokenCount_ + extraToken; tokens_ = static_cast(allocator_->Malloc(tokenCount_ * sizeof(Token) + (nameBufferSize + extraNameBufferSize) * sizeof(Ch))); nameBuffer_ = reinterpret_cast(tokens_ + tokenCount_); - std::memcpy(tokens_, rhs.tokens_, rhs.tokenCount_ * sizeof(Token)); - std::memcpy(nameBuffer_, rhs.nameBuffer_, nameBufferSize * sizeof(Ch)); + if (rhs.tokenCount_ > 0) { + std::memcpy(tokens_, rhs.tokens_, rhs.tokenCount_ * sizeof(Token)); + } + if (nameBufferSize > 0) { + std::memcpy(nameBuffer_, rhs.nameBuffer_, nameBufferSize * sizeof(Ch)); + } // Adjust pointers to name buffer std::ptrdiff_t diff = nameBuffer_ - rhs.nameBuffer_; From be1eedf808c2c2520079f134114d6707ae5117b4 Mon Sep 17 00:00:00 2001 From: Eli Fidler Date: Tue, 31 May 2016 12:22:21 -0400 Subject: [PATCH 091/305] avoid signed-integer overflow, which is undefined behavior UBSAN gave for test/unittest/itoatest.cpp:87: runtime error: signed integer overflow: 4611686018427387904 * 2 cannot be represented in type 'long' --- test/unittest/itoatest.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/unittest/itoatest.cpp b/test/unittest/itoatest.cpp index 79db1c7..b752a6a 100644 --- a/test/unittest/itoatest.cpp +++ b/test/unittest/itoatest.cpp @@ -84,6 +84,8 @@ static void Verify(void(*f)(T, char*), char* (*g)(T, char*)) { VerifyValue(Traits::Negate(i + 1), f, g); } last = i; + if (i > static_cast(std::numeric_limits::max() / static_cast(power))) + break; i *= power; } while (last < i); } From 760ea4316c9596a08c7150b470a09853228abc33 Mon Sep 17 00:00:00 2001 From: Eli Fidler Date: Tue, 31 May 2016 12:26:31 -0400 Subject: [PATCH 092/305] avoid signed-integer underflow, which is undefined behavior maybe these tests should just be deleted? UBSAN gave: runtime error: signed integer overflow: -9223372036854775808 - 1 cannot be represented in type 'long' runtime error: signed integer overflow: -9223372036854775808 - 2 cannot be represented in type 'long' --- test/unittest/valuetest.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/unittest/valuetest.cpp b/test/unittest/valuetest.cpp index 619a6a5..430a828 100644 --- a/test/unittest/valuetest.cpp +++ b/test/unittest/valuetest.cpp @@ -545,8 +545,10 @@ TEST(Value, Int64) { // Templated functions EXPECT_TRUE(z.Is()); EXPECT_EQ(i, z.Get()); +#if 0 // signed integer underflow is undefined behaviour EXPECT_EQ(i - 1, z.Set(i - 1).Get()); EXPECT_EQ(i - 2, z.Set(i - 2).Get()); +#endif } TEST(Value, Uint64) { From df9b45a6568ee8002ad64cabcc5124192f38d749 Mon Sep 17 00:00:00 2001 From: Eli Fidler Date: Tue, 31 May 2016 12:29:09 -0400 Subject: [PATCH 093/305] avoid division by zero, which is undefined behavior UBSAN gave: runtime error: division by zero --- test/unittest/writertest.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/test/unittest/writertest.cpp b/test/unittest/writertest.cpp index 22f428e..29f7626 100644 --- a/test/unittest/writertest.cpp +++ b/test/unittest/writertest.cpp @@ -439,11 +439,9 @@ TEST(Writer, InvalidEventSequence) { } } -extern double zero; // clang -Wmissing-variable-declarations -double zero = 0.0; // Use global variable to prevent compiler warning - TEST(Writer, NaN) { - double nan = zero / zero; + double nan = std::numeric_limits::quiet_NaN(); + EXPECT_TRUE(internal::Double(nan).IsNan()); StringBuffer buffer; { @@ -461,7 +459,8 @@ TEST(Writer, NaN) { } TEST(Writer, Inf) { - double inf = 1.0 / zero; + double inf = std::numeric_limits::infinity(); + EXPECT_TRUE(internal::Double(inf).IsInf()); StringBuffer buffer; { From 9dcf51c3a1c8c08411c706b3936e6fba5d25e69d Mon Sep 17 00:00:00 2001 From: Eli Fidler Date: Tue, 31 May 2016 12:39:09 -0400 Subject: [PATCH 094/305] avoid shift out-of-range error UBSAN gave during Reader.ParseNumber_FullPrecisionDouble test: include/rapidjson/internal/strtod.h:149:11: runtime error: shift exponent 46 is too large for 32-bit type 'int' --- include/rapidjson/internal/strtod.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/rapidjson/internal/strtod.h b/include/rapidjson/internal/strtod.h index fd4b01e..289c413 100644 --- a/include/rapidjson/internal/strtod.h +++ b/include/rapidjson/internal/strtod.h @@ -142,7 +142,7 @@ inline bool StrtodDiyFp(const char* decimals, size_t length, size_t decimalPosit size_t remaining = length - i; const unsigned kUlpShift = 3; const unsigned kUlp = 1 << kUlpShift; - int error = (remaining == 0) ? 0 : kUlp / 2; + int64_t error = (remaining == 0) ? 0 : kUlp / 2; DiyFp v(significand, 0); v = v.Normalize(); From 05f0592b34cb943e7c96b8767d716431a3d9eaef Mon Sep 17 00:00:00 2001 From: Eli Fidler Date: Tue, 31 May 2016 12:45:52 -0400 Subject: [PATCH 095/305] avoid shift out-of-range error UBSAN gave in Regex.Unicode test: include/rapidjson/encodings.h:157:28: runtime error: shift exponent 32 is too large for 32-bit type 'int' --- include/rapidjson/encodings.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/include/rapidjson/encodings.h b/include/rapidjson/encodings.h index edfc990..baa7c2b 100644 --- a/include/rapidjson/encodings.h +++ b/include/rapidjson/encodings.h @@ -154,7 +154,11 @@ struct UTF8 { } unsigned char type = GetRange(static_cast(c)); - *codepoint = (0xFF >> type) & static_cast(c); + if (type >= 32) { + *codepoint = 0; + } else { + *codepoint = (0xFF >> type) & static_cast(c); + } bool result = true; switch (type) { case 2: TAIL(); return result; From c52cec7e518feb30ec01bc0a978b620f8d9462ab Mon Sep 17 00:00:00 2001 From: Eli Fidler Date: Tue, 31 May 2016 13:46:04 -0400 Subject: [PATCH 096/305] fix undefined double to uint64_t cast note that std::numeric_limits::max() and std::numeric_limits::max() aren't exactly representable in a double, so we need to be strictly less to be definitely lossless UBSAN gave during Value.IsLosslessDouble test: include/rapidjson/document.h:955:42: runtime error: value 1.84467e+19 is outside the range of representable values of type 'unsigned long' --- include/rapidjson/document.h | 9 +++++++-- test/unittest/valuetest.cpp | 16 ++++++++++++++-- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h index d286eb1..f1857f5 100644 --- a/include/rapidjson/document.h +++ b/include/rapidjson/document.h @@ -23,6 +23,7 @@ #include "memorystream.h" #include "encodedstream.h" #include // placement new +#include #ifdef _MSC_VER RAPIDJSON_DIAG_PUSH @@ -952,12 +953,16 @@ public: if (IsUint64()) { uint64_t u = GetUint64(); volatile double d = static_cast(u); - return static_cast(d) == u; + return (d >= 0.0) + && (d < static_cast(std::numeric_limits::max())) + && (u == static_cast(d)); } if (IsInt64()) { int64_t i = GetInt64(); volatile double d = static_cast(i); - return static_cast< int64_t>(d) == i; + return (d >= static_cast(std::numeric_limits::min())) + && (d < static_cast(std::numeric_limits::max())) + && (i == static_cast(d)); } return true; // double, int, uint are always lossless } diff --git a/test/unittest/valuetest.cpp b/test/unittest/valuetest.cpp index 430a828..fefc001 100644 --- a/test/unittest/valuetest.cpp +++ b/test/unittest/valuetest.cpp @@ -673,6 +673,7 @@ TEST(Value, Float) { } TEST(Value, IsLosslessDouble) { + EXPECT_TRUE(Value(0.0).IsLosslessDouble()); EXPECT_TRUE(Value(12.34).IsLosslessDouble()); EXPECT_TRUE(Value(-123).IsLosslessDouble()); EXPECT_TRUE(Value(2147483648u).IsLosslessDouble()); @@ -681,8 +682,19 @@ TEST(Value, IsLosslessDouble) { EXPECT_TRUE(Value(RAPIDJSON_UINT64_C2(0xA0000000, 0x00000000)).IsLosslessDouble()); #endif - EXPECT_FALSE(Value(-static_cast(RAPIDJSON_UINT64_C2(0x7FFFFFFF, 0xFFFFFFFF))).IsLosslessDouble()); - EXPECT_FALSE(Value(RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0xFFFFFFFF)).IsLosslessDouble()); + EXPECT_FALSE(Value(static_cast(RAPIDJSON_UINT64_C2(0x7FFFFFFF, 0xFFFFFFFF))).IsLosslessDouble()); // INT64_MAX + EXPECT_FALSE(Value(-static_cast(RAPIDJSON_UINT64_C2(0x7FFFFFFF, 0xFFFFFFFF))).IsLosslessDouble()); // -INT64_MAX + EXPECT_TRUE(Value(-static_cast(RAPIDJSON_UINT64_C2(0x7FFFFFFF, 0xFFFFFFFF)) - 1).IsLosslessDouble()); // INT64_MIN + EXPECT_FALSE(Value(RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0xFFFFFFFF)).IsLosslessDouble()); // UINT64_MAX + + EXPECT_TRUE(Value(3.4028234e38f).IsLosslessDouble()); // FLT_MAX + EXPECT_TRUE(Value(-3.4028234e38f).IsLosslessDouble()); // -FLT_MAX + EXPECT_TRUE(Value(1.17549435e-38f).IsLosslessDouble()); // FLT_MIN + EXPECT_TRUE(Value(-1.17549435e-38f).IsLosslessDouble()); // -FLT_MIN + EXPECT_TRUE(Value(1.7976931348623157e+308).IsLosslessDouble()); // DBL_MAX + EXPECT_TRUE(Value(-1.7976931348623157e+308).IsLosslessDouble()); // -DBL_MAX + EXPECT_TRUE(Value(2.2250738585072014e-308).IsLosslessDouble()); // DBL_MIN + EXPECT_TRUE(Value(-2.2250738585072014e-308).IsLosslessDouble()); // -DBL_MIN } TEST(Value, IsLosslessFloat) { From 21acc56d578821bdf2ae8e9ec06fabe18b2f12cc Mon Sep 17 00:00:00 2001 From: Eli Fidler Date: Tue, 31 May 2016 14:03:07 -0400 Subject: [PATCH 097/305] range check in IsLosslessFloat to avoid undefined double->float cast UBSAN gave in Value.IsLosslessFloat: include/rapidjson/document.h:981:38: runtime error: value 3.40282e+38 is outside the range of representable values of type 'float' --- include/rapidjson/document.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h index f1857f5..b0162f5 100644 --- a/include/rapidjson/document.h +++ b/include/rapidjson/document.h @@ -978,6 +978,9 @@ public: bool IsLosslessFloat() const { if (!IsNumber()) return false; double a = GetDouble(); + if (a < static_cast(-std::numeric_limits::max()) + || a > static_cast(std::numeric_limits::max())) + return false; double b = static_cast(static_cast(a)); return a >= b && a <= b; // Prevent -Wfloat-equal } From 8c4059766e88626ad1213d40e05c308f62644d01 Mon Sep 17 00:00:00 2001 From: Eli Fidler Date: Mon, 13 Jun 2016 07:29:34 -0700 Subject: [PATCH 098/305] test for no-double-promotion instead of just checking compiler version --- test/unittest/CMakeLists.txt | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/test/unittest/CMakeLists.txt b/test/unittest/CMakeLists.txt index fae09cd..b3204d6 100644 --- a/test/unittest/CMakeLists.txt +++ b/test/unittest/CMakeLists.txt @@ -1,3 +1,5 @@ +include(CheckCXXCompilerFlag) + set(UNITTEST_SOURCES allocatorstest.cpp bigintegertest.cpp @@ -41,7 +43,10 @@ elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang") # If the user is running a newer version of Clang that includes the # -Wdouble-promotion, we will ignore that warning. if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 3.7) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-double-promotion") + CHECK_CXX_COMPILER_FLAG("-Wno-double-promotion" HAS_NO_DOUBLE_PROMOTION) + if (HAS_NO_DOUBLE_PROMOTION) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-double-promotion") + endif() endif() elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") # Force to always compile with /W4 From b34f18525ed752568519137b0054e51ea3e5c185 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Wed, 15 Jun 2016 09:41:56 +0800 Subject: [PATCH 099/305] Fix tutorial bug --- doc/tutorial.md | 2 +- doc/tutorial.zh-cn.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/tutorial.md b/doc/tutorial.md index 1211023..0da07dc 100644 --- a/doc/tutorial.md +++ b/doc/tutorial.md @@ -166,7 +166,7 @@ If we are unsure whether a member exists, we need to call `HasMember()` before c ~~~~~~~~~~cpp Value::ConstMemberIterator itr = document.FindMember("hello"); if (itr != document.MemberEnd()) - printf("%s %s\n", itr->value.GetString()); + printf("%s\n", itr->value.GetString()); ~~~~~~~~~~ ## Querying Number {#QueryNumber} diff --git a/doc/tutorial.zh-cn.md b/doc/tutorial.zh-cn.md index 7a0e6e5..2b9229d 100644 --- a/doc/tutorial.zh-cn.md +++ b/doc/tutorial.zh-cn.md @@ -166,7 +166,7 @@ Type of member a is Array ~~~~~~~~~~cpp Value::ConstMemberIterator itr = document.FindMember("hello"); if (itr != document.MemberEnd()) - printf("%s %s\n", itr->value.GetString()); + printf("%s\n", itr->value.GetString()); ~~~~~~~~~~ ## 查询 Number {#QueryNumber} From 1c087b77cb7eb40a40aaf1d0f28feedc2d6e42ee Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Wed, 15 Jun 2016 09:46:30 +0800 Subject: [PATCH 100/305] Fix #650 SAX documentation bug --- doc/sax.md | 2 +- doc/sax.zh-cn.md | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/doc/sax.md b/doc/sax.md index 9d4f202..5b36d05 100644 --- a/doc/sax.md +++ b/doc/sax.md @@ -59,7 +59,7 @@ These events can be easily matched with the JSON, except some event parameters n using namespace rapidjson; using namespace std; -struct MyHandler { +struct MyHandler : public BaseReaderHandler, MyHandler> { bool Null() { cout << "Null()" << endl; return true; } bool Bool(bool b) { cout << "Bool(" << boolalpha << b << ")" << endl; return true; } bool Int(int i) { cout << "Int(" << i << ")" << endl; return true; } diff --git a/doc/sax.zh-cn.md b/doc/sax.zh-cn.md index b66957c..7b8aabe 100644 --- a/doc/sax.zh-cn.md +++ b/doc/sax.zh-cn.md @@ -59,7 +59,7 @@ EndObject(7) using namespace rapidjson; using namespace std; -struct MyHandler { +struct MyHandler : public BaseReaderHandler, MyHandler> { bool Null() { cout << "Null()" << endl; return true; } bool Bool(bool b) { cout << "Bool(" << boolalpha << b << ")" << endl; return true; } bool Int(int i) { cout << "Int(" << i << ")" << endl; return true; } @@ -106,6 +106,7 @@ class Handler { bool Int64(int64_t i); bool Uint64(uint64_t i); bool Double(double d); + bool RawNumber(const Ch* str, SizeType length, bool copy); bool String(const Ch* str, SizeType length, bool copy); bool StartObject(); bool Key(const Ch* str, SizeType length, bool copy); From 2246ef3e6b3259485fa299129e461af9eaee2509 Mon Sep 17 00:00:00 2001 From: Andy Deng Date: Wed, 15 Jun 2016 11:53:23 +0800 Subject: [PATCH 101/305] doc/tutorial-zh-cn: Fix a typo in tutorial --- doc/tutorial.zh-cn.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/tutorial.zh-cn.md b/doc/tutorial.zh-cn.md index 7a0e6e5..3d58966 100644 --- a/doc/tutorial.zh-cn.md +++ b/doc/tutorial.zh-cn.md @@ -379,7 +379,7 @@ const char * cstr = getenv("USER"); size_t cstr_len = ...; // 如果有长度 Value s; // s.SetString(cstr); // 这不能通过编译 -s.SetString(StringRef(cstr)); // 可以,假设它的生命周期案全,并且是以空字符结尾的 +s.SetString(StringRef(cstr)); // 可以,假设它的生命周期安全,并且是以空字符结尾的 s = StringRef(cstr); // 上行的缩写 s.SetString(StringRef(cstr, cstr_len));// 更快,可处理空字符 s = StringRef(cstr, cstr_len); // 上行的缩写 From dabbd2b0286908ba81fbf572eabc4081c1e25ffc Mon Sep 17 00:00:00 2001 From: Andy Deng Date: Wed, 15 Jun 2016 11:56:20 +0800 Subject: [PATCH 102/305] gitignore: ignore Doxyfile.zh-cn Doxyfile.zh-cn is a generated file that we need to ignore. --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 2c412c2..e7e8fba 100644 --- a/.gitignore +++ b/.gitignore @@ -20,5 +20,6 @@ Testing /googletest install_manifest.txt Doxyfile +Doxyfile.zh-cn DartConfiguration.tcl *.nupkg From f6a07692f907e89f682401f45d008f6a19e6fce4 Mon Sep 17 00:00:00 2001 From: "Philipp A. Hartmann" Date: Thu, 23 Jun 2016 21:42:16 +0200 Subject: [PATCH 103/305] Fix warnings on GCC 6 and later (closes #666) * document.h * suppress -Wterminate on GCC 6.x and later * simplify warning handling * schema.h * drop RAPIDJSON_NOEXCEPT from GenericSchemaDocument constructor (calls RAPIDJSON_NEW anyway) * simplify warning handling (avoids RAPIDJSON_POP mismatch on Clang) * encodingtest.cpp, istreamwrappertest.cpp * work around -Wdangling-else * readertest.cpp * suppress -Wdangling-else --- include/rapidjson/document.h | 18 ++++-------------- include/rapidjson/schema.h | 28 ++++++---------------------- test/unittest/encodingstest.cpp | 3 ++- test/unittest/istreamwrappertest.cpp | 3 ++- test/unittest/readertest.cpp | 12 ++++-------- 5 files changed, 18 insertions(+), 46 deletions(-) diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h index b0162f5..17af922 100644 --- a/include/rapidjson/document.h +++ b/include/rapidjson/document.h @@ -25,23 +25,24 @@ #include // placement new #include -#ifdef _MSC_VER RAPIDJSON_DIAG_PUSH +#ifdef _MSC_VER RAPIDJSON_DIAG_OFF(4127) // conditional expression is constant RAPIDJSON_DIAG_OFF(4244) // conversion from kXxxFlags to 'uint16_t', possible loss of data #endif #ifdef __clang__ -RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_OFF(padded) RAPIDJSON_DIAG_OFF(switch-enum) RAPIDJSON_DIAG_OFF(c++98-compat) #endif #ifdef __GNUC__ -RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_OFF(effc++) +#if __GNUC__ >= 6 +RAPIDJSON_DIAG_OFF(terminate) // ignore throwing RAPIDJSON_ASSERT in RAPIDJSON_NOEXCEPT functions #endif +#endif // __GNUC__ #ifndef RAPIDJSON_NOMEMBERITERATORCLASS #include // std::iterator, std::random_access_iterator_tag @@ -2569,17 +2570,6 @@ private: }; RAPIDJSON_NAMESPACE_END - -#ifdef _MSC_VER RAPIDJSON_DIAG_POP -#endif - -#ifdef __clang__ -RAPIDJSON_DIAG_POP -#endif - -#ifdef __GNUC__ -RAPIDJSON_DIAG_POP -#endif #endif // RAPIDJSON_DOCUMENT_H_ diff --git a/include/rapidjson/schema.h b/include/rapidjson/schema.h index 80812f0..4c1eacb 100644 --- a/include/rapidjson/schema.h +++ b/include/rapidjson/schema.h @@ -19,13 +19,6 @@ #include "pointer.h" #include // abs, floor -#ifdef __clang__ -RAPIDJSON_DIAG_PUSH -RAPIDJSON_DIAG_OFF(weak-vtables) -RAPIDJSON_DIAG_OFF(exit-time-destructors) -RAPIDJSON_DIAG_OFF(c++98-compat-pedantic) -#endif - #if !defined(RAPIDJSON_SCHEMA_USE_INTERNALREGEX) #define RAPIDJSON_SCHEMA_USE_INTERNALREGEX 1 #else @@ -58,18 +51,20 @@ RAPIDJSON_DIAG_OFF(c++98-compat-pedantic) #include "stringbuffer.h" #endif -#if defined(__GNUC__) RAPIDJSON_DIAG_PUSH + +#if defined(__GNUC__) RAPIDJSON_DIAG_OFF(effc++) #endif #ifdef __clang__ -RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(weak-vtables) +RAPIDJSON_DIAG_OFF(exit-time-destructors) +RAPIDJSON_DIAG_OFF(c++98-compat-pedantic) RAPIDJSON_DIAG_OFF(variadic-macros) #endif #ifdef _MSC_VER -RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_OFF(4512) // assignment operator could not be generated #endif @@ -1343,7 +1338,7 @@ public: \param remoteProvider An optional remote schema document provider for resolving remote reference. Can be null. \param allocator An optional allocator instance for allocating memory. Can be null. */ - GenericSchemaDocument(const ValueType& document, IRemoteSchemaDocumentProviderType* remoteProvider = 0, Allocator* allocator = 0) RAPIDJSON_NOEXCEPT : + GenericSchemaDocument(const ValueType& document, IRemoteSchemaDocumentProviderType* remoteProvider = 0, Allocator* allocator = 0) : remoteProvider_(remoteProvider), allocator_(allocator), ownAllocator_(), @@ -2006,17 +2001,6 @@ private: }; RAPIDJSON_NAMESPACE_END - -#if defined(__GNUC__) RAPIDJSON_DIAG_POP -#endif - -#ifdef __clang__ -RAPIDJSON_DIAG_POP -#endif - -#ifdef _MSC_VER -RAPIDJSON_DIAG_POP -#endif #endif // RAPIDJSON_SCHEMA_H_ diff --git a/test/unittest/encodingstest.cpp b/test/unittest/encodingstest.cpp index 4104880..67b0391 100644 --- a/test/unittest/encodingstest.cpp +++ b/test/unittest/encodingstest.cpp @@ -302,8 +302,9 @@ TEST(EncodingsTest, UTF8) { decodedCount++; } - if (*encodedStr) // This decoder cannot handle U+0000 + if (*encodedStr) { // This decoder cannot handle U+0000 EXPECT_EQ(1u, decodedCount); // Should only contain one code point + } EXPECT_EQ(UTF8_ACCEPT, state); if (UTF8_ACCEPT != state) diff --git a/test/unittest/istreamwrappertest.cpp b/test/unittest/istreamwrappertest.cpp index 28c756c..9d6fbcf 100644 --- a/test/unittest/istreamwrappertest.cpp +++ b/test/unittest/istreamwrappertest.cpp @@ -50,8 +50,9 @@ static void TestStringStream() { StringStreamType iss(s); BasicIStreamWrapper is(iss); EXPECT_EQ(0, is.Tell()); - if (sizeof(Ch) == 1) + if (sizeof(Ch) == 1) { EXPECT_EQ(0, is.Peek4()); // less than 4 bytes + } for (int i = 0; i < 3; i++) { EXPECT_EQ(static_cast(i), is.Tell()); EXPECT_EQ('A' + i, is.Peek()); diff --git a/test/unittest/readertest.cpp b/test/unittest/readertest.cpp index 69c3cc4..c7c7710 100644 --- a/test/unittest/readertest.cpp +++ b/test/unittest/readertest.cpp @@ -23,15 +23,17 @@ using namespace rapidjson; -#ifdef __GNUC__ RAPIDJSON_DIAG_PUSH +#ifdef __GNUC__ RAPIDJSON_DIAG_OFF(effc++) RAPIDJSON_DIAG_OFF(float-equal) RAPIDJSON_DIAG_OFF(missing-noreturn) +#if __GNUC__ >= 6 +RAPIDJSON_DIAG_OFF(dangling-else) #endif +#endif // __GNUC__ #ifdef __clang__ -RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_OFF(variadic-macros) RAPIDJSON_DIAG_OFF(c++98-compat-pedantic) #endif @@ -1839,10 +1841,4 @@ TEST(Reader, ParseNanAndInfinity) { #undef TEST_NAN_INF } -#ifdef __GNUC__ RAPIDJSON_DIAG_POP -#endif - -#ifdef __clang__ -RAPIDJSON_DIAG_POP -#endif From ad32940da8debdf2a8b4d2f57bb6dd9f7b2a3e1f Mon Sep 17 00:00:00 2001 From: "Philipp A. Hartmann" Date: Mon, 27 Jun 2016 19:05:29 +0200 Subject: [PATCH 104/305] readertest: Suppress "dangling-else" warning on GCC 7 and later GCC 6.x doesn't yet support this warning flag, as reported by @ragnar-ouchterlony. --- test/unittest/readertest.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unittest/readertest.cpp b/test/unittest/readertest.cpp index c7c7710..64a1f9c 100644 --- a/test/unittest/readertest.cpp +++ b/test/unittest/readertest.cpp @@ -28,7 +28,7 @@ RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_OFF(effc++) RAPIDJSON_DIAG_OFF(float-equal) RAPIDJSON_DIAG_OFF(missing-noreturn) -#if __GNUC__ >= 6 +#if __GNUC__ >= 7 RAPIDJSON_DIAG_OFF(dangling-else) #endif #endif // __GNUC__ From c79958a29baae3c5d56148a766cef407c6aecc50 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Wed, 29 Jun 2016 09:48:34 +0800 Subject: [PATCH 105/305] Fix #670 remote schema provider document --- doc/schema.md | 4 ++-- doc/schema.zh-cn.md | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/doc/schema.md b/doc/schema.md index 6d66fa5..1fad5fb 100644 --- a/doc/schema.md +++ b/doc/schema.md @@ -152,7 +152,7 @@ JSON Schema supports [`$ref` keyword](http://spacetelescope.github.io/understand { "$ref": "definitions.json#/address" } ~~~ -As `SchemaValidator` does not know how to resolve such URI, it needs a user-provided `IRemoteSchemaDocumentProvider` instance to do so. +As `SchemaDocument` does not know how to resolve such URI, it needs a user-provided `IRemoteSchemaDocumentProvider` instance to do so. ~~~ class MyRemoteSchemaDocumentProvider : public IRemoteSchemaDocumentProvider { @@ -165,7 +165,7 @@ public: // ... MyRemoteSchemaDocumentProvider provider; -SchemaValidator validator(schema, &provider); +SchemaDocument schema(sd, &provider); ~~~ ## Conformance diff --git a/doc/schema.zh-cn.md b/doc/schema.zh-cn.md index 95f5a69..345b7c5 100644 --- a/doc/schema.zh-cn.md +++ b/doc/schema.zh-cn.md @@ -152,7 +152,7 @@ JSON Schema 支持 [`$ref` 关键字](http://spacetelescope.github.io/understand { "$ref": "definitions.json#/address" } ~~~ -由于 `SchemaValidator` 并不知道如何处理那些 URI,它需要使用者提供一个 `IRemoteSchemaDocumentProvider` 的实例去处理。 +由于 `SchemaDocument` 并不知道如何处理那些 URI,它需要使用者提供一个 `IRemoteSchemaDocumentProvider` 的实例去处理。 ~~~ class MyRemoteSchemaDocumentProvider : public IRemoteSchemaDocumentProvider { @@ -165,7 +165,7 @@ public: // ... MyRemoteSchemaDocumentProvider provider; -SchemaValidator validator(schema, &provider); +SchemaDocument schema(sd, &provider); ~~~ ## 标准的符合程度 From 252e8122bf6275503762d9702077842ca9794f4b Mon Sep 17 00:00:00 2001 From: Jason Smith Date: Thu, 30 Jun 2016 13:56:59 -0700 Subject: [PATCH 106/305] Fix buffer overrun using PutN (closes #672) Fix inconsistent calling of template functions in PutN in stream.h. When used with a GenericStringBuffer<, MemoryPoolAllocator>, PutN would call PutReserve from stream.h, and PutUnsafe from stringbuffer.h. This resulted in bytes being added to the buffer without allocating space. This was not an issue when used with the default memory allocator, because in this case the specialized PutN is used from stringbuffer.h. --- include/rapidjson/stream.h | 2 +- test/unittest/stringbuffertest.cpp | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/include/rapidjson/stream.h b/include/rapidjson/stream.h index dd2783b..fef82c2 100644 --- a/include/rapidjson/stream.h +++ b/include/rapidjson/stream.h @@ -95,7 +95,7 @@ inline void PutUnsafe(Stream& stream, typename Stream::Ch c) { //! Put N copies of a character to a stream. template inline void PutN(Stream& stream, Ch c, size_t n) { - PutReserve(stream, n); + PutReserve(stream, n); for (size_t i = 0; i < n; i++) PutUnsafe(stream, c); } diff --git a/test/unittest/stringbuffertest.cpp b/test/unittest/stringbuffertest.cpp index 9be98fc..ded513c 100644 --- a/test/unittest/stringbuffertest.cpp +++ b/test/unittest/stringbuffertest.cpp @@ -37,6 +37,13 @@ TEST(StringBuffer, Put) { EXPECT_STREQ("A", buffer.GetString()); } +TEST(StringBuffer, PutN_Issue672) { + GenericStringBuffer, MemoryPoolAllocator<> > buffer; + EXPECT_EQ(0, buffer.GetSize()); + rapidjson::PutN(buffer, ' ', 1); + EXPECT_EQ(1, buffer.GetSize()); +} + TEST(StringBuffer, Clear) { StringBuffer buffer; buffer.Put('A'); From 899156172d80b809131e22aaae535f2033fd8b6f Mon Sep 17 00:00:00 2001 From: Jason Smith Date: Fri, 1 Jul 2016 06:40:24 -0700 Subject: [PATCH 107/305] Make GenericSchemaDocument constructor explicit Prior to this change, a user could incorrectly pass a Document object to SchemaValidator. This would implicitly construct a SchemaDocument, which would then be destructed before the validator was used. This caused unpredictable results including memory corruption and program crashes. --- include/rapidjson/schema.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/rapidjson/schema.h b/include/rapidjson/schema.h index 4c1eacb..b182aa2 100644 --- a/include/rapidjson/schema.h +++ b/include/rapidjson/schema.h @@ -1338,7 +1338,7 @@ public: \param remoteProvider An optional remote schema document provider for resolving remote reference. Can be null. \param allocator An optional allocator instance for allocating memory. Can be null. */ - GenericSchemaDocument(const ValueType& document, IRemoteSchemaDocumentProviderType* remoteProvider = 0, Allocator* allocator = 0) : + explicit GenericSchemaDocument(const ValueType& document, IRemoteSchemaDocumentProviderType* remoteProvider = 0, Allocator* allocator = 0) : remoteProvider_(remoteProvider), allocator_(allocator), ownAllocator_(), From 8c43554de6c7af90463852c2c59deca67cc1415f Mon Sep 17 00:00:00 2001 From: "yiteng.nyt" Date: Mon, 11 Jul 2016 12:38:10 +0800 Subject: [PATCH 108/305] fix rapidjson::value::Get() may returns wrong data Change-Id: Ia7325edb437e3039e29223d0ecc4d9c83d824bc0 --- include/rapidjson/document.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h index 17af922..783479c 100644 --- a/include/rapidjson/document.h +++ b/include/rapidjson/document.h @@ -480,7 +480,7 @@ template struct TypeHelper > { typedef std::basic_string StringType; static bool Is(const ValueType& v) { return v.IsString(); } - static StringType Get(const ValueType& v) { return v.GetString(); } + static StringType Get(const ValueType& v) { return StringType(v.GetString(), v.GetStringLength()); } static ValueType& Set(ValueType& v, const StringType& data, typename ValueType::AllocatorType& a) { return v.SetString(data, a); } }; #endif From b67ff2fb11307dfe03926f5433175fb67072cc64 Mon Sep 17 00:00:00 2001 From: fuzhufang Date: Thu, 14 Jul 2016 17:50:48 +0800 Subject: [PATCH 109/305] if define RAPIDJSON_HAS_STDSTRING, FindMember use std::string, but it also use internal::StrLen to get the string lengtht, when it call FindMember(StringRef(name)). Now use GenericValue construct it, then can use the std::string.size. now it will be faster. --- include/rapidjson/document.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h index 783479c..e3e20df 100644 --- a/include/rapidjson/document.h +++ b/include/rapidjson/document.h @@ -1169,8 +1169,8 @@ public: \return Iterator to member, if it exists. Otherwise returns \ref MemberEnd(). */ - MemberIterator FindMember(const std::basic_string& name) { return FindMember(StringRef(name)); } - ConstMemberIterator FindMember(const std::basic_string& name) const { return FindMember(StringRef(name)); } + MemberIterator FindMember(const std::basic_string& name) { return FindMember(GenericValue(StringRef(name))); } + ConstMemberIterator FindMember(const std::basic_string& name) const { return FindMember(GenericValue(StringRef(name))); } #endif //! Add a member (name-value pair) to the object. From db6a6f3f64897eb009f8635535f1bc6212265825 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Thu, 21 Jul 2016 09:33:00 +0800 Subject: [PATCH 110/305] Add Flush() for all value types Fixes #684 --- include/rapidjson/writer.h | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/include/rapidjson/writer.h b/include/rapidjson/writer.h index 7d0610e..112d767 100644 --- a/include/rapidjson/writer.h +++ b/include/rapidjson/writer.h @@ -169,30 +169,30 @@ public: */ //@{ - bool Null() { Prefix(kNullType); return WriteNull(); } - bool Bool(bool b) { Prefix(b ? kTrueType : kFalseType); return WriteBool(b); } - bool Int(int i) { Prefix(kNumberType); return WriteInt(i); } - bool Uint(unsigned u) { Prefix(kNumberType); return WriteUint(u); } - bool Int64(int64_t i64) { Prefix(kNumberType); return WriteInt64(i64); } - bool Uint64(uint64_t u64) { Prefix(kNumberType); return WriteUint64(u64); } + bool Null() { Prefix(kNullType); return EndValue(WriteNull()); } + bool Bool(bool b) { Prefix(b ? kTrueType : kFalseType); return EndValue(WriteBool(b)); } + bool Int(int i) { Prefix(kNumberType); return EndValue(WriteInt(i)); } + bool Uint(unsigned u) { Prefix(kNumberType); return EndValue(WriteUint(u)); } + bool Int64(int64_t i64) { Prefix(kNumberType); return EndValue(WriteInt64(i64)); } + bool Uint64(uint64_t u64) { Prefix(kNumberType); return EndValue(WriteUint64(u64)); } //! Writes the given \c double value to the stream /*! \param d The value to be written. \return Whether it is succeed. */ - bool Double(double d) { Prefix(kNumberType); return WriteDouble(d); } + bool Double(double d) { Prefix(kNumberType); return EndValue(WriteDouble(d)); } bool RawNumber(const Ch* str, SizeType length, bool copy = false) { (void)copy; Prefix(kNumberType); - return WriteString(str, length); + return EndValue(WriteString(str, length)); } bool String(const Ch* str, SizeType length, bool copy = false) { (void)copy; Prefix(kStringType); - return WriteString(str, length); + return EndValue(WriteString(str, length)); } #if RAPIDJSON_HAS_STDSTRING @@ -214,10 +214,7 @@ public: RAPIDJSON_ASSERT(level_stack_.GetSize() >= sizeof(Level)); RAPIDJSON_ASSERT(!level_stack_.template Top()->inArray); level_stack_.template Pop(1); - bool ret = WriteEndObject(); - if (RAPIDJSON_UNLIKELY(level_stack_.Empty())) // end of json text - os_->Flush(); - return ret; + return EndValue(WriteEndObject()); } bool StartArray() { @@ -231,10 +228,7 @@ public: RAPIDJSON_ASSERT(level_stack_.GetSize() >= sizeof(Level)); RAPIDJSON_ASSERT(level_stack_.template Top()->inArray); level_stack_.template Pop(1); - bool ret = WriteEndArray(); - if (RAPIDJSON_UNLIKELY(level_stack_.Empty())) // end of json text - os_->Flush(); - return ret; + return EndValue(WriteEndArray()); } //@} @@ -255,7 +249,7 @@ public: \param length Length of the json. \param type Type of the root of json. */ - bool RawValue(const Ch* json, size_t length, Type type) { Prefix(type); return WriteRawValue(json, length); } + bool RawValue(const Ch* json, size_t length, Type type) { Prefix(type); return EndValue(WriteRawValue(json, length)); } protected: //! Information for each nested level @@ -460,6 +454,13 @@ protected: } } + // Flush the value if it is the top level one. + bool EndValue(bool ret) { + if (RAPIDJSON_UNLIKELY(level_stack_.Empty())) // end of json text + os_->Flush(); + return ret; + } + OutputStream* os_; internal::Stack level_stack_; int maxDecimalPlaces_; From 332b61fe412f43dc4b81f8cc084b1d21f1b73c93 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Thu, 21 Jul 2016 17:25:17 +0800 Subject: [PATCH 111/305] Handle malloc() fail in PoolAllocator Fix #682 --- include/rapidjson/allocators.h | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/include/rapidjson/allocators.h b/include/rapidjson/allocators.h index c705969..98affe0 100644 --- a/include/rapidjson/allocators.h +++ b/include/rapidjson/allocators.h @@ -179,7 +179,8 @@ public: size = RAPIDJSON_ALIGN(size); if (chunkHead_ == 0 || chunkHead_->size + size > chunkHead_->capacity) - AddChunk(chunk_capacity_ > size ? chunk_capacity_ : size); + if (!AddChunk(chunk_capacity_ > size ? chunk_capacity_ : size)) + return NULL; void *buffer = reinterpret_cast(chunkHead_) + RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + chunkHead_->size; chunkHead_->size += size; @@ -211,11 +212,13 @@ public: } // Realloc process: allocate and copy memory, do not free original buffer. - void* newBuffer = Malloc(newSize); - RAPIDJSON_ASSERT(newBuffer != 0); // Do not handle out-of-memory explicitly. - if (originalSize) - std::memcpy(newBuffer, originalPtr, originalSize); - return newBuffer; + if (void* newBuffer = Malloc(newSize)) { + if (originalSize) + std::memcpy(newBuffer, originalPtr, originalSize); + return newBuffer; + } + else + return NULL; } //! Frees a memory block (concept Allocator) @@ -229,15 +232,20 @@ private: //! Creates a new chunk. /*! \param capacity Capacity of the chunk in bytes. + \return true if success. */ - void AddChunk(size_t capacity) { + bool AddChunk(size_t capacity) { if (!baseAllocator_) ownBaseAllocator_ = baseAllocator_ = RAPIDJSON_NEW(BaseAllocator()); - ChunkHeader* chunk = reinterpret_cast(baseAllocator_->Malloc(RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + capacity)); - chunk->capacity = capacity; - chunk->size = 0; - chunk->next = chunkHead_; - chunkHead_ = chunk; + if (ChunkHeader* chunk = reinterpret_cast(baseAllocator_->Malloc(RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + capacity))) { + chunk->capacity = capacity; + chunk->size = 0; + chunk->next = chunkHead_; + chunkHead_ = chunk; + return true; + } + else + return false; } static const int kDefaultChunkCapacity = 64 * 1024; //!< Default chunk capacity. From 78a7ecb94672190c117f9e880bbbd692407e74eb Mon Sep 17 00:00:00 2001 From: Jamie Seward Date: Tue, 26 Jul 2016 22:35:46 -0700 Subject: [PATCH 112/305] Add std::string overload to PrettyWriter::Key() when RAPIDJSON_HAS_STDSTRING is #defined Only String() has the std::string overload currently. --- include/rapidjson/prettywriter.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/include/rapidjson/prettywriter.h b/include/rapidjson/prettywriter.h index 75dc474..0dcb0fe 100644 --- a/include/rapidjson/prettywriter.h +++ b/include/rapidjson/prettywriter.h @@ -115,6 +115,12 @@ public: } bool Key(const Ch* str, SizeType length, bool copy = false) { return String(str, length, copy); } + +#if RAPIDJSON_HAS_STDSTRING + bool Key(const std::basic_string& str) { + return Key(str.data(), SizeType(str.size())); + } +#endif bool EndObject(SizeType memberCount = 0) { (void)memberCount; From 319248eb522fc05c5572a1bf5ecffef6a0cf8421 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Fri, 29 Jul 2016 14:49:08 +0800 Subject: [PATCH 113/305] Remove disqus in documentation --- doc/misc/footer.html | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/doc/misc/footer.html b/doc/misc/footer.html index 843aa11..77f1131 100644 --- a/doc/misc/footer.html +++ b/doc/misc/footer.html @@ -7,21 +7,5 @@ - - From 6d9ab582b2a48940f97a86bf926001095b58514f Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Fri, 29 Jul 2016 15:27:32 +0800 Subject: [PATCH 114/305] Fix doxygen build --- travis-doxygen.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/travis-doxygen.sh b/travis-doxygen.sh index e9eb6b9..31a50cf 100755 --- a/travis-doxygen.sh +++ b/travis-doxygen.sh @@ -42,8 +42,8 @@ abort() { skip "Running Doxygen only for updates on 'master' branch (current: ${TRAVIS_BRANCH})." # check for job number -[ "${TRAVIS_JOB_NUMBER}" = "${TRAVIS_BUILD_NUMBER}.1" ] || \ - skip "Running Doxygen only on first job of build ${TRAVIS_BUILD_NUMBER} (current: ${TRAVIS_JOB_NUMBER})." +# [ "${TRAVIS_JOB_NUMBER}" = "${TRAVIS_BUILD_NUMBER}.1" ] || \ +# skip "Running Doxygen only on first job of build ${TRAVIS_BUILD_NUMBER} (current: ${TRAVIS_JOB_NUMBER})." # install doxygen binary distribution doxygen_install() From fedae8552a618c1a68adf7ba850b55989bec79c1 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Mon, 1 Aug 2016 09:21:31 +0800 Subject: [PATCH 115/305] Remove google analytics --- doc/misc/header.html | 9 --------- 1 file changed, 9 deletions(-) diff --git a/doc/misc/header.html b/doc/misc/header.html index d43f2aa..2dbe721 100644 --- a/doc/misc/header.html +++ b/doc/misc/header.html @@ -16,15 +16,6 @@ $mathjax $extrastylesheet -
From 323a0dce43cd80d336e2571956f2339c9422ea3d Mon Sep 17 00:00:00 2001 From: Jordi Mallach Date: Mon, 1 Aug 2016 14:25:50 +0200 Subject: [PATCH 116/305] Fix builds on x32 platform. From the Debian wiki: https://wiki.debian.org/X32Port X32 is an ABI for amd64/x86_64 CPUs using 32-bit integers, longs and pointers. The idea is to combine the smaller memory and cache footprint from 32-bit data types with the larger register set of x86_64. The 64-bit registers can make computation more efficient, and with 8 additional registers available, there is less pressure compared to i386/i686. rapidjson makes an incorrect assumption in a check for 64 bit platforms, and uses __LP64__ exclusively. This fix adds an additional check for __x86_64__ && __ILP32__ defines, as a very conservative fix. However, the usage of __LP64__ would be a problem for other "mixed" applications like ARM ILP32, so a better detection scheme might be needed in the future. --- include/rapidjson/rapidjson.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/rapidjson/rapidjson.h b/include/rapidjson/rapidjson.h index 062e25e..4bdaed6 100644 --- a/include/rapidjson/rapidjson.h +++ b/include/rapidjson/rapidjson.h @@ -250,7 +250,7 @@ //! Whether using 64-bit architecture #ifndef RAPIDJSON_64BIT -#if defined(__LP64__) || defined(_WIN64) || defined(__EMSCRIPTEN__) +#if defined(__LP64__) || (defined(__x86_64__) && defined(__ILP32__)) || defined(_WIN64) || defined(__EMSCRIPTEN__) #define RAPIDJSON_64BIT 1 #else #define RAPIDJSON_64BIT 0 From 17254e090e0dc3d5d1aca8efd6303cdbd07dbae1 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Thu, 25 Aug 2016 14:35:17 +0800 Subject: [PATCH 117/305] Version 1.1.0 Change version numbers Fixed some document linkage Fix #648 --- CHANGELOG.md | 27 ++++++++++++++++++++++----- CMakeLists.txt | 4 ++-- appveyor.yml | 2 +- doc/Doxyfile.in | 1 + doc/Doxyfile.zh-cn.in | 1 + doc/dom.zh-cn.md | 15 ++++++++------- doc/encoding.zh-cn.md | 2 +- doc/faq.zh-cn.md | 8 ++++---- doc/features.md | 8 ++++++-- doc/features.zh-cn.md | 9 +++++++-- doc/internals.md | 6 +++--- doc/performance.md | 2 +- doc/performance.zh-cn.md | 2 +- doc/pointer.md | 2 +- doc/pointer.zh-cn.md | 2 +- doc/sax.md | 17 +++++++++++++---- doc/sax.zh-cn.md | 17 +++++++++++++---- doc/schema.md | 4 ++-- doc/schema.zh-cn.md | 4 ++-- doc/tutorial.md | 19 +++++++++++++++++++ doc/tutorial.zh-cn.md | 23 +++++++++++++++++++++-- include/rapidjson/rapidjson.h | 4 ++-- include/rapidjson/writer.h | 2 +- rapidjson.autopkg | 4 ++-- readme.md | 16 +++++++++++++--- readme.zh-cn.md | 18 ++++++++++++++---- 26 files changed, 162 insertions(+), 57 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d1c948a..0205e7b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,9 +4,12 @@ This project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] +## 1.1.0 - 2016-08-25 + ### Added * Add GenericDocument ctor overload to specify JSON type (#369) * Add FAQ (#372, #373, #374, #376) +* Add forward declaration header `fwd.h` * Add @PlatformIO Library Registry manifest file (#400) * Implement assignment operator for BigInteger (#404) * Add comments support (#443) @@ -33,11 +36,15 @@ This project adheres to [Semantic Versioning](http://semver.org/). * Add parse-by-parts example (#556, #562) * Support parse number as string (#564, #589) * Add kFormatSingleLineArray for PrettyWriter (#577) -* Added optional support for trailing commas #584 +* Added optional support for trailing commas (#584) +* Added filterkey and filterkeydom examples (#615) +* Added npm docs (#639) +* Allow options for writing and parsing NaN/Infinity (#641) +* Add std::string overload to PrettyWriter::Key() when RAPIDJSON_HAS_STDSTRING is defined (#698) ### Fixed -* Fix gcc/clang/vc warnings (#350, #394, #397, #444, #447, #473, #515, #582, #589, #595) -* Fix documentation (#482, #511, #550, #557) +* Fix gcc/clang/vc warnings (#350, #394, #397, #444, #447, #473, #515, #582, #589, #595, #667) +* Fix documentation (#482, #511, #550, #557, #614, #635, #660) * Fix emscripten alignment issue (#535) * Fix missing allocator to uses of AddMember in document (#365) * CMake will no longer complain that the minimum CMake version is not specified (#501) @@ -56,6 +63,13 @@ This project adheres to [Semantic Versioning](http://semver.org/). * Fix a crash bug in regex (#605) * Fix schema "required" keyword cannot handle duplicated keys (#609) * Fix cmake CMP0054 warning (#612) +* Added missing include guards in istreamwrapper.h and ostreamwrapper.h (#634) +* Fix undefined behaviour (#646) +* Fix buffer overrun using PutN (#673) +* Fix rapidjson::value::Get() may returns wrong data (#681) +* Add Flush() for all value types (#689) +* Handle malloc() fail in PoolAllocator (#691) +* Fix builds on x32 platform. #703 ### Changed * Clarify problematic JSON license (#392) @@ -63,7 +77,9 @@ This project adheres to [Semantic Versioning](http://semver.org/). * Make whitespace array more compact (#513) * Optimize Writer::WriteString() with SIMD (#544) * x86-64 48-bit pointer optimization for GenericValue (#546) - +* Define RAPIDJSON_HAS_CXX11_RVALUE_REFS directly in clang (#617) +* Make GenericSchemaDocument constructor explicit (#674) +* Optimize FindMember when use std::string (#690) ## [1.0.2] - 2015-05-14 @@ -135,7 +151,8 @@ This project adheres to [Semantic Versioning](http://semver.org/). ## 0.1 - 2011-11-18 -[Unreleased]: https://github.com/miloyip/rapidjson/compare/v1.0.2...HEAD +[Unreleased]: https://github.com/miloyip/rapidjson/compare/v1.1.0...HEAD +[1.1.0]: https://github.com/miloyip/rapidjson/compare/v1.0.2...v1.1.0 [1.0.2]: https://github.com/miloyip/rapidjson/compare/v1.0.1...v1.0.2 [1.0.1]: https://github.com/miloyip/rapidjson/compare/v1.0.0...v1.0.1 [1.0.0]: https://github.com/miloyip/rapidjson/compare/v1.0-beta...v1.0.0 diff --git a/CMakeLists.txt b/CMakeLists.txt index 96bfdc2..ceda71b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,8 +12,8 @@ SET(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/CMakeModules) PROJECT(RapidJSON CXX) set(LIB_MAJOR_VERSION "1") -set(LIB_MINOR_VERSION "0") -set(LIB_PATCH_VERSION "2") +set(LIB_MINOR_VERSION "1") +set(LIB_PATCH_VERSION "0") set(LIB_VERSION_STRING "${LIB_MAJOR_VERSION}.${LIB_MINOR_VERSION}.${LIB_PATCH_VERSION}") # compile in release with debug info mode by default diff --git a/appveyor.yml b/appveyor.yml index 205c670..dfedf9c 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,5 +1,5 @@ os: Visual Studio 2015 CTP -version: 1.0.2.{build} +version: 1.1.0.{build} configuration: - Debug diff --git a/doc/Doxyfile.in b/doc/Doxyfile.in index fcb0926..ca14233 100644 --- a/doc/Doxyfile.in +++ b/doc/Doxyfile.in @@ -765,6 +765,7 @@ WARN_LOGFILE = # Note: If this tag is empty the current directory is searched. INPUT = readme.md \ + CHANGELOG.md \ include/rapidjson/rapidjson.h \ include/ \ doc/features.md \ diff --git a/doc/Doxyfile.zh-cn.in b/doc/Doxyfile.zh-cn.in index 76d828b..87dd866 100644 --- a/doc/Doxyfile.zh-cn.in +++ b/doc/Doxyfile.zh-cn.in @@ -765,6 +765,7 @@ WARN_LOGFILE = # Note: If this tag is empty the current directory is searched. INPUT = readme.zh-cn.md \ + CHANGELOG.md \ include/rapidjson/rapidjson.h \ include/ \ doc/features.zh-cn.md \ diff --git a/doc/dom.zh-cn.md b/doc/dom.zh-cn.md index 13e8c20..d93f603 100644 --- a/doc/dom.zh-cn.md +++ b/doc/dom.zh-cn.md @@ -1,6 +1,6 @@ # DOM -文档对象模型(Document Object Model, DOM)是一种罝于内存中的 JSON 表示方式,以供查询及操作。我们己于 [教程](doc/tutorial.md) 中介绍了 DOM 的基本用法,本节将讲述一些细节及高级用法。 +文档对象模型(Document Object Model, DOM)是一种罝于内存中的 JSON 表示方式,以供查询及操作。我们己于 [教程](doc/tutorial.zh-cn.md) 中介绍了 DOM 的基本用法,本节将讲述一些细节及高级用法。 [TOC] @@ -31,7 +31,7 @@ typedef GenericDocument > Document; ## 编码 {#Encoding} -`Encoding` 参数指明在内存中的 JSON String 使用哪种编码。可行的选项有 `UTF8`、`UTF16`、`UTF32`。要注意这 3 个类型其实也是模板类。`UTF8<>` 等同 `UTF8`,这代表它使用 `char` 来存储字符串。更多细节可以参考 [编码](encoding.md)。 +`Encoding` 参数指明在内存中的 JSON String 使用哪种编码。可行的选项有 `UTF8`、`UTF16`、`UTF32`。要注意这 3 个类型其实也是模板类。`UTF8<>` 等同 `UTF8`,这代表它使用 `char` 来存储字符串。更多细节可以参考 [编码](doc/encoding.zh-cn.md)。 这里是一个例子。假设一个 Windows 应用软件希望查询存储于 JSON 中的本地化字符串。Windows 中含 Unicode 的函数使用 UTF-16(宽字符)编码。无论 JSON 文件使用哪种编码,我们都可以把字符串以 UTF-16 形式存储在内存。 @@ -102,7 +102,7 @@ GenericDocument& GenericDocument::Parse(const Ch* str); GenericDocument& GenericDocument::Parse(const Ch* str); ~~~~~~~~~~ -[教程](tutorial.md) 中的例使用 (8) 去正常解析字符串。而 [流](stream.md) 的例子使用前 3 个函数。我们将稍后介绍原位(*In situ*) 解析。 +[教程](doc/tutorial.zh-cn.md) 中的例使用 (8) 去正常解析字符串。而 [流](doc/stream.zh-cn.md) 的例子使用前 3 个函数。我们将稍后介绍原位(*In situ*) 解析。 `parseFlags` 是以下位标置的组合: @@ -118,6 +118,7 @@ GenericDocument& GenericDocument::Parse(const Ch* str); `kParseCommentsFlag` | 容许单行 `// ...` 及多行 `/* ... */` 注释(放宽的 JSON 语法)。 `kParseNumbersAsStringsFlag` | 把数字类型解析成字符串。 `kParseTrailingCommasFlag` | 容许在对象和数组结束前含有逗号(放宽的 JSON 语法)。 +`kParseNanAndInfFlag` | 容许 `NaN`、`Inf`、`Infinity`、`-Inf` 及 `-Infinity` 作为 `double` 值(放宽的 JSON 语法)。 由于使用了非类型模板参数,而不是函数参数,C++ 编译器能为个别组合生成代码,以改善性能及减少代码尺寸(当只用单种特化)。缺点是需要在编译期决定标志。 @@ -230,9 +231,9 @@ JSON string 会被打上 const-string 的标志。但它们可能并非真正的 ## 转码与校验 {#TranscodingAndValidation} -RapidJSON 内部支持不同 Unicode 格式(正式的术语是 UCS 变换格式)间的转换。在 DOM 解析时,流的来源编码与 DOM 的编码可以不同。例如,来源流可能含有 UTF-8 的 JSON,而 DOM 则使用 UTF-16 编码。在 [EncodedInputStream](doc/stream.md) 一节里有一个例子。 +RapidJSON 内部支持不同 Unicode 格式(正式的术语是 UCS 变换格式)间的转换。在 DOM 解析时,流的来源编码与 DOM 的编码可以不同。例如,来源流可能含有 UTF-8 的 JSON,而 DOM 则使用 UTF-16 编码。在 [EncodedInputStream](doc/stream.zh-cn.md) 一节里有一个例子。 -当从 DOM 输出一个 JSON 至输出流之时,也可以使用转码功能。在 [EncodedOutputStream](doc/stream.md) 一节里有一个例子。 +当从 DOM 输出一个 JSON 至输出流之时,也可以使用转码功能。在 [EncodedOutputStream](doc/stream.zh-cn.md) 一节里有一个例子。 在转码过程中,会把来源 string 解码成 Unicode 码点,然后把码点编码成目标格式。在解码时,它会校验来源 string 的字节序列是否合法。若遇上非合法序列,解析器会停止并返回 `kParseErrorStringInvalidEncoding` 错误。 @@ -256,9 +257,9 @@ d.Accept(writer); 使用者可以创建自定义的处理器,去把 DOM 转换成其它格式。例如,一个把 DOM 转换成 XML 的处理器。 -要知道更多关于 SAX 事件与处理器,可参阅 [SAX](doc/sax.md)。 +要知道更多关于 SAX 事件与处理器,可参阅 [SAX](doc/sax.zh-cn.md)。 -## 使用者缓冲区{ #UserBuffer} +## 使用者缓冲区 {#UserBuffer} 许多应用软件可能需要尽量减少内存分配。 diff --git a/doc/encoding.zh-cn.md b/doc/encoding.zh-cn.md index 163eade..6816923 100644 --- a/doc/encoding.zh-cn.md +++ b/doc/encoding.zh-cn.md @@ -79,7 +79,7 @@ typedef GenericDocument > WDocument; typedef GenericValue > WValue; ~~~~~~~~~~ -可以在 [DOM's Encoding](doc/stream.md) 一节看到更详细的使用例子。 +可以在 [DOM's Encoding](doc/stream.zh-cn.md) 一节看到更详细的使用例子。 ## 字符类型 {#CharacterType} diff --git a/doc/faq.zh-cn.md b/doc/faq.zh-cn.md index cc985e7..ed100e1 100644 --- a/doc/faq.zh-cn.md +++ b/doc/faq.zh-cn.md @@ -88,11 +88,11 @@ 4. 什么是原位(*in situ*)解析? - 原位解析会把 JSON 字符串直接解码至输入的 JSON 中。这是一个优化,可减少内存消耗及提升性能,但输入的 JSON 会被更改。进一步细节请参考 [原位解析](doc/dom.md) 。 + 原位解析会把 JSON 字符串直接解码至输入的 JSON 中。这是一个优化,可减少内存消耗及提升性能,但输入的 JSON 会被更改。进一步细节请参考 [原位解析](doc/dom.zh-cn.md) 。 5. 什么时候会产生解析错误? - 当输入的 JSON 包含非法语法,或不能表示一个值(如 Number 太大),或解析器的处理器中断解析过程,解析器都会产生一个错误。详情请参考 [解析错误](doc/dom.md)。 + 当输入的 JSON 包含非法语法,或不能表示一个值(如 Number 太大),或解析器的处理器中断解析过程,解析器都会产生一个错误。详情请参考 [解析错误](doc/dom.zh-cn.md)。 6. 有什么错误信息? @@ -171,7 +171,7 @@ 2. 怎样去复制一个值? - 有两个 API 可用:含 allocator 的构造函数,以及 `CopyFrom()`。可参考 [深复制 Value](doc/tutorial.md) 里的用例。 + 有两个 API 可用:含 allocator 的构造函数,以及 `CopyFrom()`。可参考 [深复制 Value](doc/tutorial.zh-cn.md) 里的用例。 3. 为什么我需要提供字符串的长度? @@ -239,7 +239,7 @@ [字节顺序标记(byte order mark, BOM)](http://en.wikipedia.org/wiki/Byte_order_mark) 有时会出现于文件/流的开始,以表示其 UTF 编码类型。 - RapidJSON 的 `EncodedInputStream` 可检测/跳过 BOM。`EncodedOutputStream` 可选择是否写入 BOM。可参考 [编码流](doc/stream.md) 中的例子。 + RapidJSON 的 `EncodedInputStream` 可检测/跳过 BOM。`EncodedOutputStream` 可选择是否写入 BOM。可参考 [编码流](doc/stream.zh-cn.md) 中的例子。 5. 为什么会涉及大端/小端? diff --git a/doc/features.md b/doc/features.md index 984c6ab..732fb21 100644 --- a/doc/features.md +++ b/doc/features.md @@ -20,13 +20,16 @@ ## Standard compliance * RapidJSON should be fully RFC4627/ECMA-404 compliance. +* Support JSON Pointer (RFC6901). +* Support JSON Schema Draft v4. * Support Unicode surrogate. * Support null character (`"\u0000"`) * For example, `["Hello\u0000World"]` can be parsed and handled gracefully. There is API for getting/setting lengths of string. * Support optional relaxed syntax. * Single line (`// ...`) and multiple line (`/* ... */`) comments (`kParseCommentsFlag`). * Trailing commas at the end of objects and arrays (`kParseTrailingCommasFlag`). -* [NPM compliant](doc/npm.md). + * `NaN`, `Inf`, `Infinity`, `-Inf` and `-Infinity` as `double` values (`kParseNanAndInfFlag`) +* [NPM compliant](http://github.com/miloyip/rapidjson/blob/master/doc/npm.md). ## Unicode @@ -70,7 +73,7 @@ * Only store pointer instead of copying * Optimization for "short" strings * Store short string in `Value` internally without additional allocation. - * For UTF-8 string: maximum 11 characters in 32-bit, 15 characters in 64-bit. + * For UTF-8 string: maximum 11 characters in 32-bit, 21 characters in 64-bit (13 characters in x86-64). * Optionally support `std::string` (define `RAPIDJSON_HAS_STDSTRING=1`) ## Generation @@ -98,3 +101,4 @@ * Some C++11 support (optional) * Rvalue reference * `noexcept` specifier + * Range-based for loop diff --git a/doc/features.zh-cn.md b/doc/features.zh-cn.md index 623cf62..fd3fd4d 100644 --- a/doc/features.zh-cn.md +++ b/doc/features.zh-cn.md @@ -20,12 +20,16 @@ ## 符合标准 * RapidJSON 应完全符合 RFC4627/ECMA-404 标准。 +* 支持 JSON Pointer (RFC6901). +* 支持 JSON Schema Draft v4. * 支持 Unicod 代理对(surrogate pair)。 * 支持空字符(`"\u0000"`)。 * 例如,可以优雅地解析及处理 `["Hello\u0000World"]`。含读写字符串长度的 API。 -* 支持放宽的可选语法 +* 支持可选的放宽语法 * 单行(`// ...`)及多行(`/* ... */`) 注释 (`kParseCommentsFlag`)。 * 在对象和数组结束前含逗号 (`kParseTrailingCommasFlag`)。 + * `NaN`、`Inf`、`Infinity`、`-Inf` 及 `-Infinity` 作为 `double` 值 (`kParseNanAndInfFlag`) +* [NPM 兼容](https://github.com/miloyip/rapidjson/blob/master/doc/npm.md). ## Unicode @@ -68,7 +72,7 @@ * 只储存指针,不作复制 * 优化“短”字符串 * 在 `Value` 内储存短字符串,无需额外分配。 - * 对 UTF-8 字符串来说,32 位架构下可存储最多 11 字符,64 位下 15 字符。 + * 对 UTF-8 字符串来说,32 位架构下可存储最多 11 字符,64 位下 21 字符(x86-64 下 13 字符)。 * 可选地支持 `std::string`(定义 `RAPIDJSON_HAS_STDSTRING=1`) ## 生成 @@ -96,3 +100,4 @@ * 一些 C++11 的支持(可选) * 右值引用(rvalue reference) * `noexcept` 修饰符 + * 范围 for 循环 diff --git a/doc/internals.md b/doc/internals.md index 174a03a..49802a0 100644 --- a/doc/internals.md +++ b/doc/internals.md @@ -114,7 +114,7 @@ Number is a bit more complicated. For normal integer values, it can contains `kI ## Short-String Optimization {#ShortString} - Kosta (@Kosta-Github) provided a very neat short-string optimization. The optimization idea is given as follow. Excluding the `flags_`, a `Value` has 12 or 16 bytes (32-bit or 64-bit) for storing actual data. Instead of storing a pointer to a string, it is possible to store short strings in these space internally. For encoding with 1-byte character type (e.g. `char`), it can store maximum 11 or 15 characters string inside the `Value` type. + [Kosta](https://github.com/Kosta-Github) provided a very neat short-string optimization. The optimization idea is given as follow. Excluding the `flags_`, a `Value` has 12 or 16 bytes (32-bit or 64-bit) for storing actual data. Instead of storing a pointer to a string, it is possible to store short strings in these space internally. For encoding with 1-byte character type (e.g. `char`), it can store maximum 11 or 15 characters string inside the `Value` type. | ShortString (Ch=char) | |32-bit|64-bit| |---------------------|-------------------------------------|:----:|:----:| @@ -126,7 +126,7 @@ A special technique is applied. Instead of storing the length of string directly This optimization can reduce memory usage for copy-string. It can also improve cache-coherence thus improve runtime performance. -# Allocator {#Allocator} +# Allocator {#InternalAllocator} `Allocator` is a concept in RapidJSON: ~~~cpp @@ -158,7 +158,7 @@ Note that `Malloc()` and `Realloc()` are member functions but `Free()` is static Internally, it allocates chunks of memory from the base allocator (by default `CrtAllocator`) and stores the chunks as a singly linked list. When user requests an allocation, it allocates memory from the following order: -1. User supplied buffer if it is available. (See [User Buffer section in DOM](dom.md)) +1. User supplied buffer if it is available. (See [User Buffer section in DOM](doc/dom.md)) 2. If user supplied buffer is full, use the current memory chunk. 3. If the current block is full, allocate a new block of memory. diff --git a/doc/performance.md b/doc/performance.md index 702ca72..988e799 100644 --- a/doc/performance.md +++ b/doc/performance.md @@ -1,6 +1,6 @@ # Performance -There is a [native JSON benchmark collection] [1] which evaluates speed, memory usage and code size of various operations among 20 JSON libaries. +There is a [native JSON benchmark collection] [1] which evaluates speed, memory usage and code size of various operations among 37 JSON libaries. [1]: https://github.com/miloyip/nativejson-benchmark diff --git a/doc/performance.zh-cn.md b/doc/performance.zh-cn.md index da5d0c6..c20c505 100644 --- a/doc/performance.zh-cn.md +++ b/doc/performance.zh-cn.md @@ -1,6 +1,6 @@ # 性能 -有一个 [native JSON benchmark collection][1] 项目,能评估 20 个 JSON 库在不同操作下的速度、內存用量及代码大小。 +有一个 [native JSON benchmark collection][1] 项目,能评估 37 个 JSON 库在不同操作下的速度、內存用量及代码大小。 [1]: https://github.com/miloyip/nativejson-benchmark diff --git a/doc/pointer.md b/doc/pointer.md index 3927a12..b343d78 100644 --- a/doc/pointer.md +++ b/doc/pointer.md @@ -1,6 +1,6 @@ # Pointer -## Status: experimental, shall be included in v1.1 +(This feature was released in v1.1.0) JSON Pointer is a standardized ([RFC6901]) way to select a value inside a JSON Document (DOM). This can be analogous to XPath for XML document. However, JSON Pointer is much simpler, and a single JSON Pointer only pointed to a single value. diff --git a/doc/pointer.zh-cn.md b/doc/pointer.zh-cn.md index d9bd9c3..f58f55f 100644 --- a/doc/pointer.zh-cn.md +++ b/doc/pointer.zh-cn.md @@ -1,6 +1,6 @@ # Pointer -## 状态: 实验性,应该会合进 v1.1 +(本功能于 v1.1.0 发布) JSON Pointer 是一个标准化([RFC6901])的方式去选取一个 JSON Document(DOM)中的值。这类似于 XML 的 XPath。然而,JSON Pointer 简单得多,而且每个 JSON Pointer 仅指向单个值。 diff --git a/doc/sax.md b/doc/sax.md index 5b36d05..1d4fc2a 100644 --- a/doc/sax.md +++ b/doc/sax.md @@ -159,7 +159,7 @@ Note that, the default character type of `UTF16` is `wchar_t`. So this `reader`n The third template parameter `Allocator` is the allocator type for internal data structure (actually a stack). -## Parsing {#Parsing} +## Parsing {#SaxParsing} The one and only one function of `Reader` is to parse JSON. @@ -244,7 +244,7 @@ Anyway, using `Writer` API is even simpler than generating a JSON by ad hoc meth ~~~~~~~~~~cpp namespace rapidjson { -template, typename TargetEncoding = UTF8<>, typename Allocator = CrtAllocator<> > +template, typename TargetEncoding = UTF8<>, typename Allocator = CrtAllocator<>, unsigned writeFlags = kWriteDefaultFlags> class Writer { public: Writer(OutputStream& os, Allocator* allocator = 0, size_t levelDepth = kDefaultLevelDepth) @@ -260,7 +260,16 @@ The `SourceEncoding` template parameter specifies the encoding to be used in `St The `TargetEncoding` template parameter specifies the encoding in the output stream. -The last one, `Allocator` is the type of allocator, which is used for allocating internal data structure (a stack). +The `Allocator` is the type of allocator, which is used for allocating internal data structure (a stack). + +The `writeFlags` are combination of the following bit-flags: + +Parse flags | Meaning +------------------------------|----------------------------------- +`kWriteNoFlags` | No flag is set. +`kWriteDefaultFlags` | Default write flags. It is equal to macro `RAPIDJSON_WRITE_DEFAULT_FLAGS`, which is defined as `kWriteNoFlags`. +`kWriteValidateEncodingFlag` | Validate encoding of JSON strings. +`kWriteNanAndInfFlag` | Allow writing of `Infinity`, `-Infinity` and `NaN`. Besides, the constructor of `Writer` has a `levelDepth` parameter. This parameter affects the initial memory allocated for storing information per hierarchy level. @@ -278,7 +287,7 @@ A `Writer` can only output a single JSON, which can be any JSON type at the root When a JSON is complete, the `Writer` cannot accept any new events. Otherwise the output will be invalid (i.e. having more than one root). To reuse the `Writer` object, user can call `Writer::Reset(OutputStream& os)` to reset all internal states of the `Writer` with a new output stream. -# Techniques {#Techniques} +# Techniques {#SaxTechniques} ## Parsing JSON to Custom Data Structure {#CustomDataStructure} diff --git a/doc/sax.zh-cn.md b/doc/sax.zh-cn.md index 7b8aabe..b20286d 100644 --- a/doc/sax.zh-cn.md +++ b/doc/sax.zh-cn.md @@ -159,7 +159,7 @@ GenericReader, UTF16<> > reader; 第三个模板参数 `Allocator` 是内部数据结构(实际上是一个堆栈)的分配器类型。 -## 解析 {#Parsing} +## 解析 {#SaxParsing} `Reader` 的唯一功能就是解析 JSON。 @@ -172,7 +172,7 @@ template bool Parse(InputStream& is, Handler& handler); ~~~~~~~~~~ -若在解析中出现错误,它会返回 `false`。使用者可调用 `bool HasParseEror()`, `ParseErrorCode GetParseErrorCode()` 及 `size_t GetErrorOffset()` 获取错误状态。实际上 `Document` 使用这些 `Reader` 函数去获取解析错误。请参考 [DOM](doc/dom.md) 去了解有关解析错误的细节。 +若在解析中出现错误,它会返回 `false`。使用者可调用 `bool HasParseEror()`, `ParseErrorCode GetParseErrorCode()` 及 `size_t GetErrorOffset()` 获取错误状态。实际上 `Document` 使用这些 `Reader` 函数去获取解析错误。请参考 [DOM](doc/dom.zh-cn.md) 去了解有关解析错误的细节。 # Writer {#Writer} @@ -260,7 +260,16 @@ public: `TargetEncoding` 模板参数指定输出流的编码。 -最后一个 `Allocator` 是分配器的类型,用于分配内部数据结构(一个堆栈)。 +`Allocator` 是分配器的类型,用于分配内部数据结构(一个堆栈)。 + +`writeFlags` 是以下位标志的组合: + +写入位标志 | 意义 +------------------------------|----------------------------------- +`kWriteNoFlags` | 没有任何标志。 +`kWriteDefaultFlags` | 缺省的解析选项。它等于 `RAPIDJSON_WRITE_DEFAULT_FLAGS` 宏,此宏定义为 `kWriteNoFlags`。 +`kWriteValidateEncodingFlag` | 校验 JSON 字符串的编码。 +`kWriteNanAndInfFlag` | 容许写入 `Infinity`, `-Infinity` 及 `NaN`。 此外,`Writer` 的构造函数有一 `levelDepth` 参数。存储每层阶信息的初始内存分配量受此参数影响。 @@ -278,7 +287,7 @@ public: 当 JSON 完整时,`Writer` 不能再接受新的事件。不然其输出便会是不合法的(例如有超过一个根节点)。为了重新利用 `Writer` 对象,使用者可调用 `Writer::Reset(OutputStream& os)` 去重置其所有内部状态及设置新的输出流。 -# 技巧 {#Techniques} +# 技巧 {#SaxTechniques} ## 解析 JSON 至自定义结构 {#CustomDataStructure} diff --git a/doc/schema.md b/doc/schema.md index 1fad5fb..a83cebc 100644 --- a/doc/schema.md +++ b/doc/schema.md @@ -1,6 +1,6 @@ # Schema -## Status: experimental, shall be included in v1.1 +(This feature was released in v1.1.0) JSON Schema is a draft standard for describing the format of JSON data. The schema itself is also JSON data. By validating a JSON structure with JSON Schema, your code can safely access the DOM without manually checking types, or whether a key exists, etc. It can also ensure that the serialized JSON conform to a specified schema. @@ -146,7 +146,7 @@ Of course, if your application only needs SAX-style serialization, it can simply ## Remote Schema -JSON Schema supports [`$ref` keyword](http://spacetelescope.github.io/understanding-json-schema/structuring.html), which is a [JSON pointer](pointer.md) referencing to a local or remote schema. Local pointer is prefixed with `#`, while remote pointer is an relative or absolute URI. For example: +JSON Schema supports [`$ref` keyword](http://spacetelescope.github.io/understanding-json-schema/structuring.html), which is a [JSON pointer](doc/pointer.md) referencing to a local or remote schema. Local pointer is prefixed with `#`, while remote pointer is an relative or absolute URI. For example: ~~~js { "$ref": "definitions.json#/address" } diff --git a/doc/schema.zh-cn.md b/doc/schema.zh-cn.md index 345b7c5..a01c1b1 100644 --- a/doc/schema.zh-cn.md +++ b/doc/schema.zh-cn.md @@ -1,6 +1,6 @@ # Schema -## 状态: 实验性,应该会合进 v1.1 +(本功能于 v1.1.0 发布) JSON Schema 是描述 JSON 格式的一个标准草案。一个 schema 本身也是一个 JSON。使用 JSON Schema 去校验 JSON,可以让你的代码安全地访问 DOM,而无须检查类型或键值是否存在等。这也能确保输出的 JSON 是符合指定的 schema。 @@ -146,7 +146,7 @@ if (!d.Accept(validator)) { ## 远程 Schema -JSON Schema 支持 [`$ref` 关键字](http://spacetelescope.github.io/understanding-json-schema/structuring.html),它是一个 [JSON pointer](pointer.md) 引用至一个本地(local)或远程(remote) schema。本地指针的首字符是 `#`,而远程指针是一个相对或绝对 URI。例如: +JSON Schema 支持 [`$ref` 关键字](http://spacetelescope.github.io/understanding-json-schema/structuring.html),它是一个 [JSON pointer](doc/pointer.zh-cn.md) 引用至一个本地(local)或远程(remote) schema。本地指针的首字符是 `#`,而远程指针是一个相对或绝对 URI。例如: ~~~js { "$ref": "definitions.json#/address" } diff --git a/doc/tutorial.md b/doc/tutorial.md index 0da07dc..cb76b4b 100644 --- a/doc/tutorial.md +++ b/doc/tutorial.md @@ -133,6 +133,15 @@ And other familiar query functions: * `SizeType Capacity() const` * `bool Empty() const` +### Range-based For Loop (New in v1.1.0) + +When C++11 is enabled, you can use range-based for loop to access all elements in an array. + +~~~~~~~~~~cpp +for (auto& v : a.GetArray()) + printf("%d ", v.GetInt()); +~~~~~~~~~~ + ## Query Object {#QueryObject} Similar to array, we can access all object members by iterator: @@ -169,6 +178,16 @@ if (itr != document.MemberEnd()) printf("%s\n", itr->value.GetString()); ~~~~~~~~~~ +### Range-based For Loop (New in v1.1.0) + +When C++11 is enabled, you can use range-based for loop to access all members in an object. + +~~~~~~~~~~cpp +for (auto& m : document.GetObject()) + printf("Type of member %s is %s\n", + m.name.GetString(), kTypeNames[m.value.GetType()]); +~~~~~~~~~~ + ## Querying Number {#QueryNumber} JSON provide a single numerical type called Number. Number can be integer or real numbers. RFC 4627 says the range of Number is specified by parser. diff --git a/doc/tutorial.zh-cn.md b/doc/tutorial.zh-cn.md index f5db1ca..61fb0b2 100644 --- a/doc/tutorial.zh-cn.md +++ b/doc/tutorial.zh-cn.md @@ -133,6 +133,15 @@ for (Value::ConstValueIterator itr = a.Begin(); itr != a.End(); ++itr) * `SizeType Capacity() const` * `bool Empty() const` +### 范围 for 循环 (v1.1.0 中的新功能) + +当使用 C++11 功能时,你可使用范围 for 循环去访问 Array 内的所有元素。 + +~~~~~~~~~~cpp +for (auto& v : a.GetArray()) + printf("%d ", v.GetInt()); +~~~~~~~~~~ + ## 查询 Object {#QueryObject} 和 Array 相似,我们可以用迭代器去访问所有 Object 成员: @@ -169,6 +178,16 @@ if (itr != document.MemberEnd()) printf("%s\n", itr->value.GetString()); ~~~~~~~~~~ +### 范围 for 循环 (v1.1.0 中的新功能) + +当使用 C++11 功能时,你可使用范围 for 循环去访问 Object 内的所有成员。 + +~~~~~~~~~~cpp +for (auto& m : document.GetObject()) + printf("Type of member %s is %s\n", + m.name.GetString(), kTypeNames[m.value.GetType()]); +~~~~~~~~~~ + ## 查询 Number {#QueryNumber} JSON 只提供一种数值类型──Number。数字可以是整数或实数。RFC 4627 规定数字的范围由解析器指定。 @@ -510,6 +529,6 @@ assert(b.IsInt()); 3. [DOM](doc/dom.zh-cn.md) 的基本功能已在本教程里介绍。还有更高级的功能,如原位(*in situ*)解析、其他解析选项及高级用法。 4. [SAX](doc/sax.zh-cn.md) 是 RapidJSON 解析/生成功能的基础。学习使用 `Reader`/`Writer` 去实现更高性能的应用程序。也可以使用 `PrettyWriter` 去格式化 JSON。 5. [性能](doc/performance.zh-cn.md) 展示一些我们做的及第三方的性能测试。 -6. [技术内幕](doc/internals.zh-cn.md) 讲述一些 RapidJSON 内部的设计及技术。 +6. [技术内幕](doc/internals.md) 讲述一些 RapidJSON 内部的设计及技术。 -你也可以参考 [常见问题](faq.zh-cn.md)、API 文档、例子及单元测试。 +你也可以参考 [常见问题](doc/faq.zh-cn.md)、API 文档、例子及单元测试。 diff --git a/include/rapidjson/rapidjson.h b/include/rapidjson/rapidjson.h index 4bdaed6..053b2ce 100644 --- a/include/rapidjson/rapidjson.h +++ b/include/rapidjson/rapidjson.h @@ -68,8 +68,8 @@ \brief Version of RapidJSON in ".." string format. */ #define RAPIDJSON_MAJOR_VERSION 1 -#define RAPIDJSON_MINOR_VERSION 0 -#define RAPIDJSON_PATCH_VERSION 2 +#define RAPIDJSON_MINOR_VERSION 1 +#define RAPIDJSON_PATCH_VERSION 0 #define RAPIDJSON_VERSION_STRING \ RAPIDJSON_STRINGIFY(RAPIDJSON_MAJOR_VERSION.RAPIDJSON_MINOR_VERSION.RAPIDJSON_PATCH_VERSION) diff --git a/include/rapidjson/writer.h b/include/rapidjson/writer.h index 112d767..94f22dd 100644 --- a/include/rapidjson/writer.h +++ b/include/rapidjson/writer.h @@ -63,7 +63,7 @@ RAPIDJSON_NAMESPACE_BEGIN enum WriteFlag { kWriteNoFlags = 0, //!< No flags are set. kWriteValidateEncodingFlag = 1, //!< Validate encoding of JSON strings. - kWriteNanAndInfFlag = 2, //!< Allow writing of Inf, -Inf and NaN. + kWriteNanAndInfFlag = 2, //!< Allow writing of Infinity, -Infinity and NaN. kWriteDefaultFlags = RAPIDJSON_WRITE_DEFAULT_FLAGS //!< Default write flags. Can be customized by defining RAPIDJSON_WRITE_DEFAULT_FLAGS }; diff --git a/rapidjson.autopkg b/rapidjson.autopkg index d91aaef..70eb0d8 100644 --- a/rapidjson.autopkg +++ b/rapidjson.autopkg @@ -1,11 +1,11 @@ nuget { - //Usage: Write-NuGetPackage rapidjson.autopkg -defines:MYVERSION=1.0.2 + //Usage: Write-NuGetPackage rapidjson.autopkg -defines:MYVERSION=1.1.0 //Be sure you are running Powershell 3.0 and have the CoApp powershell extensions installed properly. nuspec { id = rapidjson; version : ${MYVERSION}; title: "rapidjson"; - authors: {"https://github.com/miloyip/rapidjson/releases/tag/v1.0.2"}; + authors: {"https://github.com/miloyip/rapidjson/releases/tag/v1.1.0"}; owners: {"@lsantos (github)"}; licenseUrl: "https://github.com/miloyip/rapidjson/blob/master/license.txt"; projectUrl: "https://github.com/miloyip/rapidjson/"; diff --git a/readme.md b/readme.md index fd5d4c6..4a1d64d 100644 --- a/readme.md +++ b/readme.md @@ -1,6 +1,6 @@ ![](doc/logo/rapidjson.png) -![](https://img.shields.io/badge/release-v1.0.2-blue.png) +![](https://img.shields.io/badge/release-v1.1.0-blue.png) ## A fast JSON parser/generator for C++ with both SAX/DOM style API @@ -37,17 +37,27 @@ RapidJSON is a JSON parser and generator for C++. It was inspired by [RapidXml]( * RapidJSON is **self-contained** and **header-only**. It does not depend on external libraries such as BOOST. It even does not depend on STL. -* RapidJSON is **memory-friendly**. Each JSON value occupies exactly 16/20 bytes for most 32/64-bit machines (excluding text string). By default it uses a fast memory allocator, and the parser allocates memory compactly during parsing. +* RapidJSON is **memory-friendly**. Each JSON value occupies exactly 16 bytes for most 32/64-bit machines (excluding text string). By default it uses a fast memory allocator, and the parser allocates memory compactly during parsing. * RapidJSON is **Unicode-friendly**. It supports UTF-8, UTF-16, UTF-32 (LE & BE), and their detection, validation and transcoding internally. For example, you can read a UTF-8 file and let RapidJSON transcode the JSON strings into UTF-16 in the DOM. It also supports surrogates and "\u0000" (null character). More features can be read [here](doc/features.md). -JSON(JavaScript Object Notation) is a light-weight data exchange format. RapidJSON should be in fully compliance with RFC7159/ECMA-404. More information about JSON can be obtained at +JSON(JavaScript Object Notation) is a light-weight data exchange format. RapidJSON should be in fully compliance with RFC7159/ECMA-404, with optional support of relaxed syntax. More information about JSON can be obtained at * [Introducing JSON](http://json.org/) * [RFC7159: The JavaScript Object Notation (JSON) Data Interchange Format](http://www.ietf.org/rfc/rfc7159.txt) * [Standard ECMA-404: The JSON Data Interchange Format](http://www.ecma-international.org/publications/standards/Ecma-404.htm) +## Highlights in v1.1 (2016-8-25) + +* Added [JSON Pointer](doc/pointer.md) +* Added [JSON Schema](doc/schema.md) +* Added [relaxed JSON syntax](doc/dom.md) (comment, trailing comma, NaN/Infinity) +* Iterating array/object with [C++11 Range-based for loop](doc/tutorial.md) +* Reduce memory overhead of each `Value` from 24 bytes to 16 bytes in x86-64 architecture. + +For other changes please refer to [change log](CHANGELOG.md). + ## Compatibility RapidJSON is cross-platform. Some platform/compiler combinations which have been tested are shown as follows. diff --git a/readme.zh-cn.md b/readme.zh-cn.md index 97101d1..74d267c 100644 --- a/readme.zh-cn.md +++ b/readme.zh-cn.md @@ -1,6 +1,6 @@ ![](doc/logo/rapidjson.png) -![](https://img.shields.io/badge/release-v1.0.2-blue.png) +![](https://img.shields.io/badge/release-v1.1.0-blue.png) ## 高效的 C++ JSON 解析/生成器,提供 SAX 及 DOM 风格 API @@ -37,17 +37,27 @@ RapidJSON 是一个 C++ 的 JSON 解析器及生成器。它的灵感来自 [Rap * RapidJSON 独立。它不依赖于 BOOST 等外部库。它甚至不依赖于 STL。 -* RapidJSON 对内存友好。在大部分 32/64 位机器上,每个 JSON 值只占 16 或 20 字节(除字符串外)。它预设使用一个快速的内存分配器,令分析器可以紧凑地分配内存。 +* RapidJSON 对内存友好。在大部分 32/64 位机器上,每个 JSON 值只占 16 字节(除字符串外)。它预设使用一个快速的内存分配器,令分析器可以紧凑地分配内存。 * RapidJSON 对 Unicode 友好。它支持 UTF-8、UTF-16、UTF-32 (大端序/小端序),并内部支持这些编码的检测、校验及转码。例如,RapidJSON 可以在分析一个 UTF-8 文件至 DOM 时,把当中的 JSON 字符串转码至 UTF-16。它也支持代理对(surrogate pair)及 `"\u0000"`(空字符)。 -在 [这里](doc/features.md) 可读取更多特点。 +在 [这里](doc/features.zh-cn.md) 可读取更多特点。 -JSON(JavaScript Object Notation)是一个轻量的数据交换格式。RapidJSON 应该完全遵从 RFC7159/ECMA-404。 关于 JSON 的更多信息可参考: +JSON(JavaScript Object Notation)是一个轻量的数据交换格式。RapidJSON 应该完全遵从 RFC7159/ECMA-404,并支持可选的放宽语法。 关于 JSON 的更多信息可参考: * [Introducing JSON](http://json.org/) * [RFC7159: The JavaScript Object Notation (JSON) Data Interchange Format](http://www.ietf.org/rfc/rfc7159.txt) * [Standard ECMA-404: The JSON Data Interchange Format](http://www.ecma-international.org/publications/standards/Ecma-404.htm) +## v1.1 中的亮点 (2016-8-25) + +* 加入 [JSON Pointer](doc/pointer.zh-cn.md) 功能,可更简单地访问及更改 DOM。 +* 加入 [JSON Schema](doc/schema.zh-cn.md) 功能,可在解析或生成 JSON 时进行校验。 +* 加入 [放宽的 JSON 语法](doc/dom.zh-cn.md) (注释、尾随逗号、NaN/Infinity) +* 使用 [C++11 范围 for 循环](doc/tutorial.zh-cn.md) 去遍历 array 和 object。 +* 在 x86-64 架构下,缩减每个 `Value` 的内存开销从 24 字节至 16 字节。 + +其他改动请参考 [change log](CHANGELOG.md). + ## 兼容性 RapidJSON 是跨平台的。以下是一些曾测试的平台/编译器组合: From 3b2441b87f99ab65f37b141a7b548ebadb607b96 Mon Sep 17 00:00:00 2001 From: Janusz Chorko Date: Fri, 26 Aug 2016 21:17:38 +0200 Subject: [PATCH 118/305] Removed non-compiling assignment operator. Fixed #718 --- include/rapidjson/document.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h index e3e20df..b0f1f70 100644 --- a/include/rapidjson/document.h +++ b/include/rapidjson/document.h @@ -316,8 +316,6 @@ struct GenericStringRef { GenericStringRef(const GenericStringRef& rhs) : s(rhs.s), length(rhs.length) {} - GenericStringRef& operator=(const GenericStringRef& rhs) { s = rhs.s; length = rhs.length; } - //! implicit conversion to plain CharType pointer operator const Ch *() const { return s; } From 862c39be371278a45a88d4d1d75164be57bb7e2d Mon Sep 17 00:00:00 2001 From: Janusz Chorko Date: Fri, 26 Aug 2016 21:26:50 +0200 Subject: [PATCH 119/305] Explicitly disable copy assignment operator --- include/rapidjson/document.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h index b0f1f70..19f5a6a 100644 --- a/include/rapidjson/document.h +++ b/include/rapidjson/document.h @@ -326,6 +326,8 @@ private: //! Disallow construction from non-const array template GenericStringRef(CharType (&str)[N]) /* = delete */; + //! Copy assignment operator not permitted - immutable type + GenericStringRef& operator=(const GenericStringRef& rhs) /* = delete */; }; //! Mark a character pointer as constant string From 0f9dbe0a9c78b6a8163e47a4b5e1c5df7a3360b9 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Mon, 29 Aug 2016 10:17:57 +0800 Subject: [PATCH 120/305] Defer thread creation in parsebypart example --- example/parsebyparts/parsebyparts.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/example/parsebyparts/parsebyparts.cpp b/example/parsebyparts/parsebyparts.cpp index 57eed00..a377efd 100644 --- a/example/parsebyparts/parsebyparts.cpp +++ b/example/parsebyparts/parsebyparts.cpp @@ -21,12 +21,15 @@ public: AsyncDocumentParser(Document& d) : stream_(*this) , d_(d) - , parseThread_(&AsyncDocumentParser::Parse, this) + , parseThread_() , mutex_() , notEmpty_() , finish_() , completed_() - {} + { + // Create and execute thread after all member variables are initialized. + parseThread_ = std::thread(&AsyncDocumentParser::Parse, this); + } ~AsyncDocumentParser() { if (!parseThread_.joinable()) From 250cf666d321b03b3456ace94fdb5647d796fa92 Mon Sep 17 00:00:00 2001 From: niukuo Date: Mon, 29 Aug 2016 21:38:06 +0800 Subject: [PATCH 121/305] fix wrong length in remote schema Change-Id: Ia96ddf5746f1c18968e9e086f17fe4a24b8480d7 --- include/rapidjson/schema.h | 2 +- test/unittest/schematest.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/rapidjson/schema.h b/include/rapidjson/schema.h index b182aa2..8497d30 100644 --- a/include/rapidjson/schema.h +++ b/include/rapidjson/schema.h @@ -1473,7 +1473,7 @@ private: if (i > 0) { // Remote reference, resolve immediately if (remoteProvider_) { - if (const GenericSchemaDocument* remoteDocument = remoteProvider_->GetRemoteDocument(s, i - 1)) { + if (const GenericSchemaDocument* remoteDocument = remoteProvider_->GetRemoteDocument(s, i)) { PointerType pointer(&s[i], len - i, allocator_); if (pointer.IsValid()) { if (const SchemaType* sc = remoteDocument->GetSchema(pointer)) { diff --git a/test/unittest/schematest.cpp b/test/unittest/schematest.cpp index d75b1e5..6a8b685 100644 --- a/test/unittest/schematest.cpp +++ b/test/unittest/schematest.cpp @@ -1101,7 +1101,7 @@ public: }; for (size_t i = 0; i < kCount; i++) - if (strncmp(uri, uris[i], length) == 0) + if (strncmp(uri, uris[i], length) == 0 && strlen(uris[i]) == length) return sd_[i]; return 0; } From 6023ed3a0cb5fe21b2148035619b93713919a004 Mon Sep 17 00:00:00 2001 From: myd7349 Date: Fri, 2 Sep 2016 17:35:40 +0800 Subject: [PATCH 122/305] Fix typo in doc --- doc/features.zh-cn.md | 2 +- readme.zh-cn.md | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/doc/features.zh-cn.md b/doc/features.zh-cn.md index fd3fd4d..19908a8 100644 --- a/doc/features.zh-cn.md +++ b/doc/features.zh-cn.md @@ -22,7 +22,7 @@ * RapidJSON 应完全符合 RFC4627/ECMA-404 标准。 * 支持 JSON Pointer (RFC6901). * 支持 JSON Schema Draft v4. -* 支持 Unicod 代理对(surrogate pair)。 +* 支持 Unicode 代理对(surrogate pair)。 * 支持空字符(`"\u0000"`)。 * 例如,可以优雅地解析及处理 `["Hello\u0000World"]`。含读写字符串长度的 API。 * 支持可选的放宽语法 diff --git a/readme.zh-cn.md b/readme.zh-cn.md index 74d267c..b62b2e1 100644 --- a/readme.zh-cn.md +++ b/readme.zh-cn.md @@ -80,13 +80,13 @@ RapidJSON 依赖于以下软件: 生成测试及例子的步骤: 1. 执行 `git submodule update --init` 去获取 thirdparty submodules (google test)。 -2. 在 rapidjson 目渌下,建立一个 `build` 目录。 +2. 在 rapidjson 目录下,建立一个 `build` 目录。 3. 在 `build` 目录下执行 `cmake ..` 命令以设置生成。Windows 用户可使用 cmake-gui 应用程序。 4. 在 Windows 下,编译生成在 build 目录中的 solution。在 Linux 下,于 build 目录运行 `make`。 成功生成后,你会在 `bin` 的目录下找到编译后的测试及例子可执行文件。而生成的文档将位于 build 下的 `doc/html` 目录。要执行测试,请在 build 下执行 `make test` 或 `ctest`。使用 `ctest -V` 命令可获取详细的输出。 -我们也可以把程序库安装至全系统中,只要在具管理權限下从 build 目录执行 `make install` 命令。这样会按系统的偏好设置安装所有文件。当安装 RapidJSON 后,其他的 CMake 项目需要使用它时,可以通过在 `CMakeLists.txt` 加入一句 `find_package(RapidJSON)`。 +我们也可以把程序库安装至全系统中,只要在具管理权限下从 build 目录执行 `make install` 命令。这样会按系统的偏好设置安装所有文件。当安装 RapidJSON 后,其他的 CMake 项目需要使用它时,可以通过在 `CMakeLists.txt` 加入一句 `find_package(RapidJSON)`。 ## 用法一览 From 3e2172bd52308bc57db0b5930347ce451b6cc0f8 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Sat, 3 Sep 2016 23:37:00 +0800 Subject: [PATCH 123/305] Add preconditions in writer and string functions --- include/rapidjson/internal/strfunc.h | 3 +++ include/rapidjson/prettywriter.h | 8 +++++++- include/rapidjson/writer.h | 8 +++++++- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/include/rapidjson/internal/strfunc.h b/include/rapidjson/internal/strfunc.h index 2edfae5..de41d8f 100644 --- a/include/rapidjson/internal/strfunc.h +++ b/include/rapidjson/internal/strfunc.h @@ -28,6 +28,7 @@ namespace internal { */ template inline SizeType StrLen(const Ch* s) { + RAPIDJSON_ASSERT(s != 0); const Ch* p = s; while (*p) ++p; return SizeType(p - s); @@ -36,6 +37,8 @@ inline SizeType StrLen(const Ch* s) { //! Returns number of code points in a encoded string. template bool CountStringCodePoint(const typename Encoding::Ch* s, SizeType length, SizeType* outCount) { + RAPIDJSON_ASSERT(s != 0); + RAPIDJSON_ASSERT(outCount != 0); GenericStringStream is(s); const typename Encoding::Ch* end = s + length; SizeType count = 0; diff --git a/include/rapidjson/prettywriter.h b/include/rapidjson/prettywriter.h index 0dcb0fe..c6f0216 100644 --- a/include/rapidjson/prettywriter.h +++ b/include/rapidjson/prettywriter.h @@ -91,12 +91,14 @@ public: bool Double(double d) { PrettyPrefix(kNumberType); return Base::WriteDouble(d); } bool RawNumber(const Ch* str, SizeType length, bool copy = false) { + RAPIDJSON_ASSERT(str != 0); (void)copy; PrettyPrefix(kNumberType); return Base::WriteString(str, length); } bool String(const Ch* str, SizeType length, bool copy = false) { + RAPIDJSON_ASSERT(str != 0); (void)copy; PrettyPrefix(kStringType); return Base::WriteString(str, length); @@ -184,7 +186,11 @@ public: \param type Type of the root of json. \note When using PrettyWriter::RawValue(), the result json may not be indented correctly. */ - bool RawValue(const Ch* json, size_t length, Type type) { PrettyPrefix(type); return Base::WriteRawValue(json, length); } + bool RawValue(const Ch* json, size_t length, Type type) { + RAPIDJSON_ASSERT(json != 0); + PrettyPrefix(type); + return Base::WriteRawValue(json, length); + } protected: void PrettyPrefix(Type type) { diff --git a/include/rapidjson/writer.h b/include/rapidjson/writer.h index 94f22dd..c5a3b98 100644 --- a/include/rapidjson/writer.h +++ b/include/rapidjson/writer.h @@ -184,12 +184,14 @@ public: bool Double(double d) { Prefix(kNumberType); return EndValue(WriteDouble(d)); } bool RawNumber(const Ch* str, SizeType length, bool copy = false) { + RAPIDJSON_ASSERT(str != 0); (void)copy; Prefix(kNumberType); return EndValue(WriteString(str, length)); } bool String(const Ch* str, SizeType length, bool copy = false) { + RAPIDJSON_ASSERT(str != 0); (void)copy; Prefix(kStringType); return EndValue(WriteString(str, length)); @@ -249,7 +251,11 @@ public: \param length Length of the json. \param type Type of the root of json. */ - bool RawValue(const Ch* json, size_t length, Type type) { Prefix(type); return EndValue(WriteRawValue(json, length)); } + bool RawValue(const Ch* json, size_t length, Type type) { + RAPIDJSON_ASSERT(json != 0); + Prefix(type); + return EndValue(WriteRawValue(json, length)); + } protected: //! Information for each nested level From 769185d68b0ca3ab8a976b6e625f0c5edff6a3c6 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Fri, 16 Sep 2016 12:13:02 +0800 Subject: [PATCH 124/305] Refactor regex Remove mutable which causes reentrant issue --- include/rapidjson/internal/regex.h | 204 ++++++---- include/rapidjson/schema.h | 3 +- test/unittest/regextest.cpp | 622 ++++++++++++++++------------- 3 files changed, 453 insertions(+), 376 deletions(-) diff --git a/include/rapidjson/internal/regex.h b/include/rapidjson/internal/regex.h index 422a524..8530cd7 100644 --- a/include/rapidjson/internal/regex.h +++ b/include/rapidjson/internal/regex.h @@ -43,12 +43,40 @@ RAPIDJSON_DIAG_OFF(4512) // assignment operator could not be generated RAPIDJSON_NAMESPACE_BEGIN namespace internal { +/////////////////////////////////////////////////////////////////////////////// +// DecodedStream + +template +class DecodedStream { +public: + DecodedStream(SourceStream& ss) : ss_(ss), codepoint_() { Decode(); } + unsigned Peek() { return codepoint_; } + unsigned Take() { + unsigned c = codepoint_; + if (c) // No further decoding when '\0' + Decode(); + return c; + } + +private: + void Decode() { + if (!Encoding::Decode(ss_, &codepoint_)) + codepoint_ = 0; + } + + SourceStream& ss_; + unsigned codepoint_; +}; + /////////////////////////////////////////////////////////////////////////////// // GenericRegex static const SizeType kRegexInvalidState = ~SizeType(0); //!< Represents an invalid index in GenericRegex::State::out, out1 static const SizeType kRegexInvalidRange = ~SizeType(0); +template +class GenericRegexSearch; + //! Regular expression engine with subset of ECMAscript grammar. /*! Supported regular expression syntax: @@ -84,45 +112,25 @@ static const SizeType kRegexInvalidRange = ~SizeType(0); template class GenericRegex { public: + typedef Encoding EncodingType; typedef typename Encoding::Ch Ch; + template friend class GenericRegexSearch; GenericRegex(const Ch* source, Allocator* allocator = 0) : states_(allocator, 256), ranges_(allocator, 256), root_(kRegexInvalidState), stateCount_(), rangeCount_(), - stateSet_(), state0_(allocator, 0), state1_(allocator, 0), anchorBegin_(), anchorEnd_() + anchorBegin_(), anchorEnd_() { GenericStringStream ss(source); - DecodedStream > ds(ss); + DecodedStream, Encoding> ds(ss); Parse(ds); } - ~GenericRegex() { - Allocator::Free(stateSet_); - } + ~GenericRegex() {} bool IsValid() const { return root_ != kRegexInvalidState; } - template - bool Match(InputStream& is) const { - return SearchWithAnchoring(is, true, true); - } - - bool Match(const Ch* s) const { - GenericStringStream is(s); - return Match(is); - } - - template - bool Search(InputStream& is) const { - return SearchWithAnchoring(is, anchorBegin_, anchorEnd_); - } - - bool Search(const Ch* s) const { - GenericStringStream is(s); - return Search(is); - } - private: enum Operator { kZeroOrOne, @@ -157,28 +165,6 @@ private: SizeType minIndex; }; - template - class DecodedStream { - public: - DecodedStream(SourceStream& ss) : ss_(ss), codepoint_() { Decode(); } - unsigned Peek() { return codepoint_; } - unsigned Take() { - unsigned c = codepoint_; - if (c) // No further decoding when '\0' - Decode(); - return c; - } - - private: - void Decode() { - if (!Encoding::Decode(ss_, &codepoint_)) - codepoint_ = 0; - } - - SourceStream& ss_; - unsigned codepoint_; - }; - State& GetState(SizeType index) { RAPIDJSON_ASSERT(index < stateCount_); return states_.template Bottom()[index]; @@ -200,7 +186,7 @@ private: } template - void Parse(DecodedStream& ds) { + void Parse(DecodedStream& ds) { Allocator allocator; Stack operandStack(&allocator, 256); // Frag Stack operatorStack(&allocator, 256); // Operator @@ -327,14 +313,6 @@ private: printf("\n"); #endif } - - // Preallocate buffer for SearchWithAnchoring() - RAPIDJSON_ASSERT(stateSet_ == 0); - if (stateCount_ > 0) { - stateSet_ = static_cast(states_.GetAllocator().Malloc(GetStateSetSize())); - state0_.template Reserve(stateCount_); - state1_.template Reserve(stateCount_); - } } SizeType NewState(SizeType out, SizeType out1, unsigned codepoint) { @@ -483,7 +461,7 @@ private: } template - bool ParseUnsigned(DecodedStream& ds, unsigned* u) { + bool ParseUnsigned(DecodedStream& ds, unsigned* u) { unsigned r = 0; if (ds.Peek() < '0' || ds.Peek() > '9') return false; @@ -497,7 +475,7 @@ private: } template - bool ParseRange(DecodedStream& ds, SizeType* range) { + bool ParseRange(DecodedStream& ds, SizeType* range) { bool isBegin = true; bool negate = false; int step = 0; @@ -575,7 +553,7 @@ private: } template - bool CharacterEscape(DecodedStream& ds, unsigned* escapedCodepoint) { + bool CharacterEscape(DecodedStream& ds, unsigned* escapedCodepoint) { unsigned codepoint; switch (codepoint = ds.Take()) { case '^': @@ -603,34 +581,93 @@ private: } } + Stack states_; + Stack ranges_; + SizeType root_; + SizeType stateCount_; + SizeType rangeCount_; + + static const unsigned kInfinityQuantifier = ~0u; + + // For SearchWithAnchoring() + bool anchorBegin_; + bool anchorEnd_; +}; + +template +class GenericRegexSearch { +public: + typedef typename RegexType::EncodingType Encoding; + typedef typename Encoding::Ch Ch; + + GenericRegexSearch(const RegexType& regex, Allocator* allocator = 0) : + regex_(regex), allocator_(allocator), ownAllocator_(0), + state0_(allocator, 0), state1_(allocator, 0), stateSet_() + { + RAPIDJSON_ASSERT(regex_.IsValid()); + if (!allocator_) + ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator()); + stateSet_ = static_cast(allocator_->Malloc(GetStateSetSize())); + state0_.template Reserve(regex_.stateCount_); + state1_.template Reserve(regex_.stateCount_); + } + + ~GenericRegexSearch() { + Allocator::Free(stateSet_); + RAPIDJSON_DELETE(ownAllocator_); + } + template - bool SearchWithAnchoring(InputStream& is, bool anchorBegin, bool anchorEnd) const { - RAPIDJSON_ASSERT(IsValid()); - DecodedStream ds(is); + bool Match(InputStream& is) { + return SearchWithAnchoring(is, true, true); + } + + bool Match(const Ch* s) { + GenericStringStream is(s); + return Match(is); + } + + template + bool Search(InputStream& is) { + return SearchWithAnchoring(is, regex_.anchorBegin_, regex_.anchorEnd_); + } + + bool Search(const Ch* s) { + GenericStringStream is(s); + return Search(is); + } + +private: + typedef typename RegexType::State State; + typedef typename RegexType::Range Range; + + template + bool SearchWithAnchoring(InputStream& is, bool anchorBegin, bool anchorEnd) { + DecodedStream ds(is); state0_.Clear(); Stack *current = &state0_, *next = &state1_; const size_t stateSetSize = GetStateSetSize(); std::memset(stateSet_, 0, stateSetSize); - bool matched = AddState(*current, root_); + bool matched = AddState(*current, regex_.root_); unsigned codepoint; while (!current->Empty() && (codepoint = ds.Take()) != 0) { std::memset(stateSet_, 0, stateSetSize); next->Clear(); matched = false; for (const SizeType* s = current->template Bottom(); s != current->template End(); ++s) { - const State& sr = GetState(*s); + const State& sr = regex_.GetState(*s); if (sr.codepoint == codepoint || - sr.codepoint == kAnyCharacterClass || - (sr.codepoint == kRangeCharacterClass && MatchRange(sr.rangeStart, codepoint))) + sr.codepoint == RegexType::kAnyCharacterClass || + (sr.codepoint == RegexType::kRangeCharacterClass && MatchRange(sr.rangeStart, codepoint))) { matched = AddState(*next, sr.out) || matched; if (!anchorEnd && matched) return true; } if (!anchorBegin) - AddState(*next, root_); + AddState(*next, regex_.root_); } internal::Swap(current, next); } @@ -639,14 +676,14 @@ private: } size_t GetStateSetSize() const { - return (stateCount_ + 31) / 32 * 4; + return (regex_.stateCount_ + 31) / 32 * 4; } // Return whether the added states is a match state - bool AddState(Stack& l, SizeType index) const { + bool AddState(Stack& l, SizeType index) { RAPIDJSON_ASSERT(index != kRegexInvalidState); - const State& s = GetState(index); + const State& s = regex_.GetState(index); if (s.out1 != kRegexInvalidState) { // Split bool matched = AddState(l, s.out); return AddState(l, s.out1) || matched; @@ -659,33 +696,26 @@ private: } bool MatchRange(SizeType rangeIndex, unsigned codepoint) const { - bool yes = (GetRange(rangeIndex).start & kRangeNegationFlag) == 0; + bool yes = (regex_.GetRange(rangeIndex).start & RegexType::kRangeNegationFlag) == 0; while (rangeIndex != kRegexInvalidRange) { - const Range& r = GetRange(rangeIndex); - if (codepoint >= (r.start & ~kRangeNegationFlag) && codepoint <= r.end) + const Range& r = regex_.GetRange(rangeIndex); + if (codepoint >= (r.start & ~RegexType::kRangeNegationFlag) && codepoint <= r.end) return yes; rangeIndex = r.next; } return !yes; } - Stack states_; - Stack ranges_; - SizeType root_; - SizeType stateCount_; - SizeType rangeCount_; - - static const unsigned kInfinityQuantifier = ~0u; - - // For SearchWithAnchoring() - uint32_t* stateSet_; // allocated by states_.GetAllocator() - mutable Stack state0_; - mutable Stack state1_; - bool anchorBegin_; - bool anchorEnd_; + const RegexType& regex_; + Allocator* allocator_; + Allocator* ownAllocator_; + Stack state0_; + Stack state1_; + uint32_t* stateSet_; }; typedef GenericRegex > Regex; +typedef GenericRegexSearch RegexSearch; } // namespace internal RAPIDJSON_NAMESPACE_END diff --git a/include/rapidjson/schema.h b/include/rapidjson/schema.h index 8497d30..288b93d 100644 --- a/include/rapidjson/schema.h +++ b/include/rapidjson/schema.h @@ -1011,7 +1011,8 @@ private: } static bool IsPatternMatch(const RegexType* pattern, const Ch *str, SizeType) { - return pattern->Search(str); + GenericRegexSearch rs(*pattern); + return rs.Search(str); } #elif RAPIDJSON_SCHEMA_USE_STDREGEX template diff --git a/test/unittest/regextest.cpp b/test/unittest/regextest.cpp index 4fb5b22..cdd3630 100644 --- a/test/unittest/regextest.cpp +++ b/test/unittest/regextest.cpp @@ -20,523 +20,569 @@ using namespace rapidjson::internal; TEST(Regex, Single) { Regex re("a"); ASSERT_TRUE(re.IsValid()); - EXPECT_TRUE(re.Match("a")); - EXPECT_FALSE(re.Match("")); - EXPECT_FALSE(re.Match("b")); + RegexSearch rs(re); + EXPECT_TRUE(rs.Match("a")); + EXPECT_FALSE(rs.Match("")); + EXPECT_FALSE(rs.Match("b")); } TEST(Regex, Concatenation) { Regex re("abc"); ASSERT_TRUE(re.IsValid()); - EXPECT_TRUE(re.Match("abc")); - EXPECT_FALSE(re.Match("")); - EXPECT_FALSE(re.Match("a")); - EXPECT_FALSE(re.Match("b")); - EXPECT_FALSE(re.Match("ab")); - EXPECT_FALSE(re.Match("abcd")); + RegexSearch rs(re); + EXPECT_TRUE(rs.Match("abc")); + EXPECT_FALSE(rs.Match("")); + EXPECT_FALSE(rs.Match("a")); + EXPECT_FALSE(rs.Match("b")); + EXPECT_FALSE(rs.Match("ab")); + EXPECT_FALSE(rs.Match("abcd")); } TEST(Regex, Alternation1) { Regex re("abab|abbb"); ASSERT_TRUE(re.IsValid()); - EXPECT_TRUE(re.Match("abab")); - EXPECT_TRUE(re.Match("abbb")); - EXPECT_FALSE(re.Match("")); - EXPECT_FALSE(re.Match("ab")); - EXPECT_FALSE(re.Match("ababa")); - EXPECT_FALSE(re.Match("abb")); - EXPECT_FALSE(re.Match("abbbb")); + RegexSearch rs(re); + EXPECT_TRUE(rs.Match("abab")); + EXPECT_TRUE(rs.Match("abbb")); + EXPECT_FALSE(rs.Match("")); + EXPECT_FALSE(rs.Match("ab")); + EXPECT_FALSE(rs.Match("ababa")); + EXPECT_FALSE(rs.Match("abb")); + EXPECT_FALSE(rs.Match("abbbb")); } TEST(Regex, Alternation2) { Regex re("a|b|c"); ASSERT_TRUE(re.IsValid()); - EXPECT_TRUE(re.Match("a")); - EXPECT_TRUE(re.Match("b")); - EXPECT_TRUE(re.Match("c")); - EXPECT_FALSE(re.Match("")); - EXPECT_FALSE(re.Match("aa")); - EXPECT_FALSE(re.Match("ab")); + RegexSearch rs(re); + EXPECT_TRUE(rs.Match("a")); + EXPECT_TRUE(rs.Match("b")); + EXPECT_TRUE(rs.Match("c")); + EXPECT_FALSE(rs.Match("")); + EXPECT_FALSE(rs.Match("aa")); + EXPECT_FALSE(rs.Match("ab")); } TEST(Regex, Parenthesis1) { Regex re("(ab)c"); ASSERT_TRUE(re.IsValid()); - EXPECT_TRUE(re.Match("abc")); - EXPECT_FALSE(re.Match("")); - EXPECT_FALSE(re.Match("a")); - EXPECT_FALSE(re.Match("b")); - EXPECT_FALSE(re.Match("ab")); - EXPECT_FALSE(re.Match("abcd")); + RegexSearch rs(re); + EXPECT_TRUE(rs.Match("abc")); + EXPECT_FALSE(rs.Match("")); + EXPECT_FALSE(rs.Match("a")); + EXPECT_FALSE(rs.Match("b")); + EXPECT_FALSE(rs.Match("ab")); + EXPECT_FALSE(rs.Match("abcd")); } TEST(Regex, Parenthesis2) { Regex re("a(bc)"); ASSERT_TRUE(re.IsValid()); - EXPECT_TRUE(re.Match("abc")); - EXPECT_FALSE(re.Match("")); - EXPECT_FALSE(re.Match("a")); - EXPECT_FALSE(re.Match("b")); - EXPECT_FALSE(re.Match("ab")); - EXPECT_FALSE(re.Match("abcd")); + RegexSearch rs(re); + EXPECT_TRUE(rs.Match("abc")); + EXPECT_FALSE(rs.Match("")); + EXPECT_FALSE(rs.Match("a")); + EXPECT_FALSE(rs.Match("b")); + EXPECT_FALSE(rs.Match("ab")); + EXPECT_FALSE(rs.Match("abcd")); } TEST(Regex, Parenthesis3) { Regex re("(a|b)(c|d)"); ASSERT_TRUE(re.IsValid()); - EXPECT_TRUE(re.Match("ac")); - EXPECT_TRUE(re.Match("ad")); - EXPECT_TRUE(re.Match("bc")); - EXPECT_TRUE(re.Match("bd")); - EXPECT_FALSE(re.Match("")); - EXPECT_FALSE(re.Match("ab")); - EXPECT_FALSE(re.Match("cd")); + RegexSearch rs(re); + EXPECT_TRUE(rs.Match("ac")); + EXPECT_TRUE(rs.Match("ad")); + EXPECT_TRUE(rs.Match("bc")); + EXPECT_TRUE(rs.Match("bd")); + EXPECT_FALSE(rs.Match("")); + EXPECT_FALSE(rs.Match("ab")); + EXPECT_FALSE(rs.Match("cd")); } TEST(Regex, ZeroOrOne1) { Regex re("a?"); ASSERT_TRUE(re.IsValid()); - EXPECT_TRUE(re.Match("")); - EXPECT_TRUE(re.Match("a")); - EXPECT_FALSE(re.Match("aa")); + RegexSearch rs(re); + EXPECT_TRUE(rs.Match("")); + EXPECT_TRUE(rs.Match("a")); + EXPECT_FALSE(rs.Match("aa")); } TEST(Regex, ZeroOrOne2) { Regex re("a?b"); ASSERT_TRUE(re.IsValid()); - EXPECT_TRUE(re.Match("b")); - EXPECT_TRUE(re.Match("ab")); - EXPECT_FALSE(re.Match("a")); - EXPECT_FALSE(re.Match("aa")); - EXPECT_FALSE(re.Match("bb")); - EXPECT_FALSE(re.Match("ba")); + RegexSearch rs(re); + EXPECT_TRUE(rs.Match("b")); + EXPECT_TRUE(rs.Match("ab")); + EXPECT_FALSE(rs.Match("a")); + EXPECT_FALSE(rs.Match("aa")); + EXPECT_FALSE(rs.Match("bb")); + EXPECT_FALSE(rs.Match("ba")); } TEST(Regex, ZeroOrOne3) { Regex re("ab?"); ASSERT_TRUE(re.IsValid()); - EXPECT_TRUE(re.Match("a")); - EXPECT_TRUE(re.Match("ab")); - EXPECT_FALSE(re.Match("b")); - EXPECT_FALSE(re.Match("aa")); - EXPECT_FALSE(re.Match("bb")); - EXPECT_FALSE(re.Match("ba")); + RegexSearch rs(re); + EXPECT_TRUE(rs.Match("a")); + EXPECT_TRUE(rs.Match("ab")); + EXPECT_FALSE(rs.Match("b")); + EXPECT_FALSE(rs.Match("aa")); + EXPECT_FALSE(rs.Match("bb")); + EXPECT_FALSE(rs.Match("ba")); } TEST(Regex, ZeroOrOne4) { Regex re("a?b?"); ASSERT_TRUE(re.IsValid()); - EXPECT_TRUE(re.Match("")); - EXPECT_TRUE(re.Match("a")); - EXPECT_TRUE(re.Match("b")); - EXPECT_TRUE(re.Match("ab")); - EXPECT_FALSE(re.Match("aa")); - EXPECT_FALSE(re.Match("bb")); - EXPECT_FALSE(re.Match("ba")); - EXPECT_FALSE(re.Match("abc")); + RegexSearch rs(re); + EXPECT_TRUE(rs.Match("")); + EXPECT_TRUE(rs.Match("a")); + EXPECT_TRUE(rs.Match("b")); + EXPECT_TRUE(rs.Match("ab")); + EXPECT_FALSE(rs.Match("aa")); + EXPECT_FALSE(rs.Match("bb")); + EXPECT_FALSE(rs.Match("ba")); + EXPECT_FALSE(rs.Match("abc")); } TEST(Regex, ZeroOrOne5) { Regex re("a(ab)?b"); ASSERT_TRUE(re.IsValid()); - EXPECT_TRUE(re.Match("ab")); - EXPECT_TRUE(re.Match("aabb")); - EXPECT_FALSE(re.Match("aab")); - EXPECT_FALSE(re.Match("abb")); + RegexSearch rs(re); + EXPECT_TRUE(rs.Match("ab")); + EXPECT_TRUE(rs.Match("aabb")); + EXPECT_FALSE(rs.Match("aab")); + EXPECT_FALSE(rs.Match("abb")); } TEST(Regex, ZeroOrMore1) { Regex re("a*"); ASSERT_TRUE(re.IsValid()); - EXPECT_TRUE(re.Match("")); - EXPECT_TRUE(re.Match("a")); - EXPECT_TRUE(re.Match("aa")); - EXPECT_FALSE(re.Match("b")); - EXPECT_FALSE(re.Match("ab")); + RegexSearch rs(re); + EXPECT_TRUE(rs.Match("")); + EXPECT_TRUE(rs.Match("a")); + EXPECT_TRUE(rs.Match("aa")); + EXPECT_FALSE(rs.Match("b")); + EXPECT_FALSE(rs.Match("ab")); } TEST(Regex, ZeroOrMore2) { Regex re("a*b"); ASSERT_TRUE(re.IsValid()); - EXPECT_TRUE(re.Match("b")); - EXPECT_TRUE(re.Match("ab")); - EXPECT_TRUE(re.Match("aab")); - EXPECT_FALSE(re.Match("")); - EXPECT_FALSE(re.Match("bb")); + RegexSearch rs(re); + EXPECT_TRUE(rs.Match("b")); + EXPECT_TRUE(rs.Match("ab")); + EXPECT_TRUE(rs.Match("aab")); + EXPECT_FALSE(rs.Match("")); + EXPECT_FALSE(rs.Match("bb")); } TEST(Regex, ZeroOrMore3) { Regex re("a*b*"); ASSERT_TRUE(re.IsValid()); - EXPECT_TRUE(re.Match("")); - EXPECT_TRUE(re.Match("a")); - EXPECT_TRUE(re.Match("aa")); - EXPECT_TRUE(re.Match("b")); - EXPECT_TRUE(re.Match("bb")); - EXPECT_TRUE(re.Match("ab")); - EXPECT_TRUE(re.Match("aabb")); - EXPECT_FALSE(re.Match("ba")); + RegexSearch rs(re); + EXPECT_TRUE(rs.Match("")); + EXPECT_TRUE(rs.Match("a")); + EXPECT_TRUE(rs.Match("aa")); + EXPECT_TRUE(rs.Match("b")); + EXPECT_TRUE(rs.Match("bb")); + EXPECT_TRUE(rs.Match("ab")); + EXPECT_TRUE(rs.Match("aabb")); + EXPECT_FALSE(rs.Match("ba")); } TEST(Regex, ZeroOrMore4) { Regex re("a(ab)*b"); ASSERT_TRUE(re.IsValid()); - EXPECT_TRUE(re.Match("ab")); - EXPECT_TRUE(re.Match("aabb")); - EXPECT_TRUE(re.Match("aababb")); - EXPECT_FALSE(re.Match("")); - EXPECT_FALSE(re.Match("aa")); + RegexSearch rs(re); + EXPECT_TRUE(rs.Match("ab")); + EXPECT_TRUE(rs.Match("aabb")); + EXPECT_TRUE(rs.Match("aababb")); + EXPECT_FALSE(rs.Match("")); + EXPECT_FALSE(rs.Match("aa")); } TEST(Regex, OneOrMore1) { Regex re("a+"); ASSERT_TRUE(re.IsValid()); - EXPECT_TRUE(re.Match("a")); - EXPECT_TRUE(re.Match("aa")); - EXPECT_FALSE(re.Match("")); - EXPECT_FALSE(re.Match("b")); - EXPECT_FALSE(re.Match("ab")); + RegexSearch rs(re); + EXPECT_TRUE(rs.Match("a")); + EXPECT_TRUE(rs.Match("aa")); + EXPECT_FALSE(rs.Match("")); + EXPECT_FALSE(rs.Match("b")); + EXPECT_FALSE(rs.Match("ab")); } TEST(Regex, OneOrMore2) { Regex re("a+b"); ASSERT_TRUE(re.IsValid()); - EXPECT_TRUE(re.Match("ab")); - EXPECT_TRUE(re.Match("aab")); - EXPECT_FALSE(re.Match("")); - EXPECT_FALSE(re.Match("b")); + RegexSearch rs(re); + EXPECT_TRUE(rs.Match("ab")); + EXPECT_TRUE(rs.Match("aab")); + EXPECT_FALSE(rs.Match("")); + EXPECT_FALSE(rs.Match("b")); } TEST(Regex, OneOrMore3) { Regex re("a+b+"); ASSERT_TRUE(re.IsValid()); - EXPECT_TRUE(re.Match("ab")); - EXPECT_TRUE(re.Match("aab")); - EXPECT_TRUE(re.Match("abb")); - EXPECT_TRUE(re.Match("aabb")); - EXPECT_FALSE(re.Match("")); - EXPECT_FALSE(re.Match("b")); - EXPECT_FALSE(re.Match("ba")); + RegexSearch rs(re); + EXPECT_TRUE(rs.Match("ab")); + EXPECT_TRUE(rs.Match("aab")); + EXPECT_TRUE(rs.Match("abb")); + EXPECT_TRUE(rs.Match("aabb")); + EXPECT_FALSE(rs.Match("")); + EXPECT_FALSE(rs.Match("b")); + EXPECT_FALSE(rs.Match("ba")); } TEST(Regex, OneOrMore4) { Regex re("a(ab)+b"); ASSERT_TRUE(re.IsValid()); - EXPECT_TRUE(re.Match("aabb")); - EXPECT_TRUE(re.Match("aababb")); - EXPECT_FALSE(re.Match("")); - EXPECT_FALSE(re.Match("ab")); + RegexSearch rs(re); + EXPECT_TRUE(rs.Match("aabb")); + EXPECT_TRUE(rs.Match("aababb")); + EXPECT_FALSE(rs.Match("")); + EXPECT_FALSE(rs.Match("ab")); } TEST(Regex, QuantifierExact1) { Regex re("ab{3}c"); ASSERT_TRUE(re.IsValid()); - EXPECT_TRUE(re.Match("abbbc")); - EXPECT_FALSE(re.Match("ac")); - EXPECT_FALSE(re.Match("abc")); - EXPECT_FALSE(re.Match("abbc")); - EXPECT_FALSE(re.Match("abbbbc")); + RegexSearch rs(re); + EXPECT_TRUE(rs.Match("abbbc")); + EXPECT_FALSE(rs.Match("ac")); + EXPECT_FALSE(rs.Match("abc")); + EXPECT_FALSE(rs.Match("abbc")); + EXPECT_FALSE(rs.Match("abbbbc")); } TEST(Regex, QuantifierExact2) { Regex re("a(bc){3}d"); ASSERT_TRUE(re.IsValid()); - EXPECT_TRUE(re.Match("abcbcbcd")); - EXPECT_FALSE(re.Match("ad")); - EXPECT_FALSE(re.Match("abcd")); - EXPECT_FALSE(re.Match("abcbcd")); - EXPECT_FALSE(re.Match("abcbcbcbcd")); + RegexSearch rs(re); + EXPECT_TRUE(rs.Match("abcbcbcd")); + EXPECT_FALSE(rs.Match("ad")); + EXPECT_FALSE(rs.Match("abcd")); + EXPECT_FALSE(rs.Match("abcbcd")); + EXPECT_FALSE(rs.Match("abcbcbcbcd")); } TEST(Regex, QuantifierExact3) { Regex re("a(b|c){3}d"); ASSERT_TRUE(re.IsValid()); - EXPECT_TRUE(re.Match("abbbd")); - EXPECT_TRUE(re.Match("acccd")); - EXPECT_TRUE(re.Match("abcbd")); - EXPECT_FALSE(re.Match("ad")); - EXPECT_FALSE(re.Match("abbd")); - EXPECT_FALSE(re.Match("accccd")); - EXPECT_FALSE(re.Match("abbbbd")); + RegexSearch rs(re); + EXPECT_TRUE(rs.Match("abbbd")); + EXPECT_TRUE(rs.Match("acccd")); + EXPECT_TRUE(rs.Match("abcbd")); + EXPECT_FALSE(rs.Match("ad")); + EXPECT_FALSE(rs.Match("abbd")); + EXPECT_FALSE(rs.Match("accccd")); + EXPECT_FALSE(rs.Match("abbbbd")); } TEST(Regex, QuantifierMin1) { Regex re("ab{3,}c"); ASSERT_TRUE(re.IsValid()); - EXPECT_TRUE(re.Match("abbbc")); - EXPECT_TRUE(re.Match("abbbbc")); - EXPECT_TRUE(re.Match("abbbbbc")); - EXPECT_FALSE(re.Match("ac")); - EXPECT_FALSE(re.Match("abc")); - EXPECT_FALSE(re.Match("abbc")); + RegexSearch rs(re); + EXPECT_TRUE(rs.Match("abbbc")); + EXPECT_TRUE(rs.Match("abbbbc")); + EXPECT_TRUE(rs.Match("abbbbbc")); + EXPECT_FALSE(rs.Match("ac")); + EXPECT_FALSE(rs.Match("abc")); + EXPECT_FALSE(rs.Match("abbc")); } TEST(Regex, QuantifierMin2) { Regex re("a(bc){3,}d"); ASSERT_TRUE(re.IsValid()); - EXPECT_TRUE(re.Match("abcbcbcd")); - EXPECT_TRUE(re.Match("abcbcbcbcd")); - EXPECT_FALSE(re.Match("ad")); - EXPECT_FALSE(re.Match("abcd")); - EXPECT_FALSE(re.Match("abcbcd")); + RegexSearch rs(re); + EXPECT_TRUE(rs.Match("abcbcbcd")); + EXPECT_TRUE(rs.Match("abcbcbcbcd")); + EXPECT_FALSE(rs.Match("ad")); + EXPECT_FALSE(rs.Match("abcd")); + EXPECT_FALSE(rs.Match("abcbcd")); } TEST(Regex, QuantifierMin3) { Regex re("a(b|c){3,}d"); ASSERT_TRUE(re.IsValid()); - EXPECT_TRUE(re.Match("abbbd")); - EXPECT_TRUE(re.Match("acccd")); - EXPECT_TRUE(re.Match("abcbd")); - EXPECT_TRUE(re.Match("accccd")); - EXPECT_TRUE(re.Match("abbbbd")); - EXPECT_FALSE(re.Match("ad")); - EXPECT_FALSE(re.Match("abbd")); + RegexSearch rs(re); + EXPECT_TRUE(rs.Match("abbbd")); + EXPECT_TRUE(rs.Match("acccd")); + EXPECT_TRUE(rs.Match("abcbd")); + EXPECT_TRUE(rs.Match("accccd")); + EXPECT_TRUE(rs.Match("abbbbd")); + EXPECT_FALSE(rs.Match("ad")); + EXPECT_FALSE(rs.Match("abbd")); } TEST(Regex, QuantifierMinMax1) { Regex re("ab{3,5}c"); ASSERT_TRUE(re.IsValid()); - EXPECT_TRUE(re.Match("abbbc")); - EXPECT_TRUE(re.Match("abbbbc")); - EXPECT_TRUE(re.Match("abbbbbc")); - EXPECT_FALSE(re.Match("ac")); - EXPECT_FALSE(re.Match("abc")); - EXPECT_FALSE(re.Match("abbc")); - EXPECT_FALSE(re.Match("abbbbbbc")); + RegexSearch rs(re); + EXPECT_TRUE(rs.Match("abbbc")); + EXPECT_TRUE(rs.Match("abbbbc")); + EXPECT_TRUE(rs.Match("abbbbbc")); + EXPECT_FALSE(rs.Match("ac")); + EXPECT_FALSE(rs.Match("abc")); + EXPECT_FALSE(rs.Match("abbc")); + EXPECT_FALSE(rs.Match("abbbbbbc")); } TEST(Regex, QuantifierMinMax2) { Regex re("a(bc){3,5}d"); ASSERT_TRUE(re.IsValid()); - EXPECT_TRUE(re.Match("abcbcbcd")); - EXPECT_TRUE(re.Match("abcbcbcbcd")); - EXPECT_TRUE(re.Match("abcbcbcbcbcd")); - EXPECT_FALSE(re.Match("ad")); - EXPECT_FALSE(re.Match("abcd")); - EXPECT_FALSE(re.Match("abcbcd")); - EXPECT_FALSE(re.Match("abcbcbcbcbcbcd")); + RegexSearch rs(re); + EXPECT_TRUE(rs.Match("abcbcbcd")); + EXPECT_TRUE(rs.Match("abcbcbcbcd")); + EXPECT_TRUE(rs.Match("abcbcbcbcbcd")); + EXPECT_FALSE(rs.Match("ad")); + EXPECT_FALSE(rs.Match("abcd")); + EXPECT_FALSE(rs.Match("abcbcd")); + EXPECT_FALSE(rs.Match("abcbcbcbcbcbcd")); } TEST(Regex, QuantifierMinMax3) { Regex re("a(b|c){3,5}d"); ASSERT_TRUE(re.IsValid()); - EXPECT_TRUE(re.Match("abbbd")); - EXPECT_TRUE(re.Match("acccd")); - EXPECT_TRUE(re.Match("abcbd")); - EXPECT_TRUE(re.Match("accccd")); - EXPECT_TRUE(re.Match("abbbbd")); - EXPECT_TRUE(re.Match("acccccd")); - EXPECT_TRUE(re.Match("abbbbbd")); - EXPECT_FALSE(re.Match("ad")); - EXPECT_FALSE(re.Match("abbd")); - EXPECT_FALSE(re.Match("accccccd")); - EXPECT_FALSE(re.Match("abbbbbbd")); + RegexSearch rs(re); + EXPECT_TRUE(rs.Match("abbbd")); + EXPECT_TRUE(rs.Match("acccd")); + EXPECT_TRUE(rs.Match("abcbd")); + EXPECT_TRUE(rs.Match("accccd")); + EXPECT_TRUE(rs.Match("abbbbd")); + EXPECT_TRUE(rs.Match("acccccd")); + EXPECT_TRUE(rs.Match("abbbbbd")); + EXPECT_FALSE(rs.Match("ad")); + EXPECT_FALSE(rs.Match("abbd")); + EXPECT_FALSE(rs.Match("accccccd")); + EXPECT_FALSE(rs.Match("abbbbbbd")); } // Issue538 TEST(Regex, QuantifierMinMax4) { Regex re("a(b|c){0,3}d"); ASSERT_TRUE(re.IsValid()); - EXPECT_TRUE(re.Match("ad")); - EXPECT_TRUE(re.Match("abd")); - EXPECT_TRUE(re.Match("acd")); - EXPECT_TRUE(re.Match("abbd")); - EXPECT_TRUE(re.Match("accd")); - EXPECT_TRUE(re.Match("abcd")); - EXPECT_TRUE(re.Match("abbbd")); - EXPECT_TRUE(re.Match("acccd")); - EXPECT_FALSE(re.Match("abbbbd")); - EXPECT_FALSE(re.Match("add")); - EXPECT_FALSE(re.Match("accccd")); - EXPECT_FALSE(re.Match("abcbcd")); + RegexSearch rs(re); + EXPECT_TRUE(rs.Match("ad")); + EXPECT_TRUE(rs.Match("abd")); + EXPECT_TRUE(rs.Match("acd")); + EXPECT_TRUE(rs.Match("abbd")); + EXPECT_TRUE(rs.Match("accd")); + EXPECT_TRUE(rs.Match("abcd")); + EXPECT_TRUE(rs.Match("abbbd")); + EXPECT_TRUE(rs.Match("acccd")); + EXPECT_FALSE(rs.Match("abbbbd")); + EXPECT_FALSE(rs.Match("add")); + EXPECT_FALSE(rs.Match("accccd")); + EXPECT_FALSE(rs.Match("abcbcd")); } // Issue538 TEST(Regex, QuantifierMinMax5) { Regex re("a(b|c){0,}d"); ASSERT_TRUE(re.IsValid()); - EXPECT_TRUE(re.Match("ad")); - EXPECT_TRUE(re.Match("abd")); - EXPECT_TRUE(re.Match("acd")); - EXPECT_TRUE(re.Match("abbd")); - EXPECT_TRUE(re.Match("accd")); - EXPECT_TRUE(re.Match("abcd")); - EXPECT_TRUE(re.Match("abbbd")); - EXPECT_TRUE(re.Match("acccd")); - EXPECT_TRUE(re.Match("abbbbd")); - EXPECT_TRUE(re.Match("accccd")); - EXPECT_TRUE(re.Match("abcbcd")); - EXPECT_FALSE(re.Match("add")); - EXPECT_FALSE(re.Match("aad")); + RegexSearch rs(re); + EXPECT_TRUE(rs.Match("ad")); + EXPECT_TRUE(rs.Match("abd")); + EXPECT_TRUE(rs.Match("acd")); + EXPECT_TRUE(rs.Match("abbd")); + EXPECT_TRUE(rs.Match("accd")); + EXPECT_TRUE(rs.Match("abcd")); + EXPECT_TRUE(rs.Match("abbbd")); + EXPECT_TRUE(rs.Match("acccd")); + EXPECT_TRUE(rs.Match("abbbbd")); + EXPECT_TRUE(rs.Match("accccd")); + EXPECT_TRUE(rs.Match("abcbcd")); + EXPECT_FALSE(rs.Match("add")); + EXPECT_FALSE(rs.Match("aad")); } -#define EURO "\xE2\x82\xAC" // "\xE2\x82\xAC" is UTF-8 sequence of Euro sign U+20AC +#define EURO "\xE2\x82\xAC" // "\xE2\x82\xAC" is UTF-8 rsquence of Euro sign U+20AC TEST(Regex, Unicode) { Regex re("a" EURO "+b"); ASSERT_TRUE(re.IsValid()); - EXPECT_TRUE(re.Match("a" EURO "b")); - EXPECT_TRUE(re.Match("a" EURO EURO "b")); - EXPECT_FALSE(re.Match("a?b")); - EXPECT_FALSE(re.Match("a" EURO "\xAC" "b")); // unaware of UTF-8 will match + RegexSearch rs(re); + EXPECT_TRUE(rs.Match("a" EURO "b")); + EXPECT_TRUE(rs.Match("a" EURO EURO "b")); + EXPECT_FALSE(rs.Match("a?b")); + EXPECT_FALSE(rs.Match("a" EURO "\xAC" "b")); // unaware of UTF-8 will match } TEST(Regex, AnyCharacter) { Regex re("."); ASSERT_TRUE(re.IsValid()); - EXPECT_TRUE(re.Match("a")); - EXPECT_TRUE(re.Match("b")); - EXPECT_TRUE(re.Match(EURO)); - EXPECT_FALSE(re.Match("")); - EXPECT_FALSE(re.Match("aa")); + RegexSearch rs(re); + EXPECT_TRUE(rs.Match("a")); + EXPECT_TRUE(rs.Match("b")); + EXPECT_TRUE(rs.Match(EURO)); + EXPECT_FALSE(rs.Match("")); + EXPECT_FALSE(rs.Match("aa")); } TEST(Regex, CharacterRange1) { Regex re("[abc]"); ASSERT_TRUE(re.IsValid()); - EXPECT_TRUE(re.Match("a")); - EXPECT_TRUE(re.Match("b")); - EXPECT_TRUE(re.Match("c")); - EXPECT_FALSE(re.Match("")); - EXPECT_FALSE(re.Match("`")); - EXPECT_FALSE(re.Match("d")); - EXPECT_FALSE(re.Match("aa")); + RegexSearch rs(re); + EXPECT_TRUE(rs.Match("a")); + EXPECT_TRUE(rs.Match("b")); + EXPECT_TRUE(rs.Match("c")); + EXPECT_FALSE(rs.Match("")); + EXPECT_FALSE(rs.Match("`")); + EXPECT_FALSE(rs.Match("d")); + EXPECT_FALSE(rs.Match("aa")); } TEST(Regex, CharacterRange2) { Regex re("[^abc]"); ASSERT_TRUE(re.IsValid()); - EXPECT_TRUE(re.Match("`")); - EXPECT_TRUE(re.Match("d")); - EXPECT_FALSE(re.Match("a")); - EXPECT_FALSE(re.Match("b")); - EXPECT_FALSE(re.Match("c")); - EXPECT_FALSE(re.Match("")); - EXPECT_FALSE(re.Match("aa")); + RegexSearch rs(re); + EXPECT_TRUE(rs.Match("`")); + EXPECT_TRUE(rs.Match("d")); + EXPECT_FALSE(rs.Match("a")); + EXPECT_FALSE(rs.Match("b")); + EXPECT_FALSE(rs.Match("c")); + EXPECT_FALSE(rs.Match("")); + EXPECT_FALSE(rs.Match("aa")); } TEST(Regex, CharacterRange3) { Regex re("[a-c]"); ASSERT_TRUE(re.IsValid()); - EXPECT_TRUE(re.Match("a")); - EXPECT_TRUE(re.Match("b")); - EXPECT_TRUE(re.Match("c")); - EXPECT_FALSE(re.Match("")); - EXPECT_FALSE(re.Match("`")); - EXPECT_FALSE(re.Match("d")); - EXPECT_FALSE(re.Match("aa")); + RegexSearch rs(re); + EXPECT_TRUE(rs.Match("a")); + EXPECT_TRUE(rs.Match("b")); + EXPECT_TRUE(rs.Match("c")); + EXPECT_FALSE(rs.Match("")); + EXPECT_FALSE(rs.Match("`")); + EXPECT_FALSE(rs.Match("d")); + EXPECT_FALSE(rs.Match("aa")); } TEST(Regex, CharacterRange4) { Regex re("[^a-c]"); ASSERT_TRUE(re.IsValid()); - EXPECT_TRUE(re.Match("`")); - EXPECT_TRUE(re.Match("d")); - EXPECT_FALSE(re.Match("a")); - EXPECT_FALSE(re.Match("b")); - EXPECT_FALSE(re.Match("c")); - EXPECT_FALSE(re.Match("")); - EXPECT_FALSE(re.Match("aa")); + RegexSearch rs(re); + EXPECT_TRUE(rs.Match("`")); + EXPECT_TRUE(rs.Match("d")); + EXPECT_FALSE(rs.Match("a")); + EXPECT_FALSE(rs.Match("b")); + EXPECT_FALSE(rs.Match("c")); + EXPECT_FALSE(rs.Match("")); + EXPECT_FALSE(rs.Match("aa")); } TEST(Regex, CharacterRange5) { Regex re("[-]"); ASSERT_TRUE(re.IsValid()); - EXPECT_TRUE(re.Match("-")); - EXPECT_FALSE(re.Match("")); - EXPECT_FALSE(re.Match("a")); + RegexSearch rs(re); + EXPECT_TRUE(rs.Match("-")); + EXPECT_FALSE(rs.Match("")); + EXPECT_FALSE(rs.Match("a")); } TEST(Regex, CharacterRange6) { Regex re("[a-]"); ASSERT_TRUE(re.IsValid()); - EXPECT_TRUE(re.Match("a")); - EXPECT_TRUE(re.Match("-")); - EXPECT_FALSE(re.Match("")); - EXPECT_FALSE(re.Match("`")); - EXPECT_FALSE(re.Match("b")); + RegexSearch rs(re); + EXPECT_TRUE(rs.Match("a")); + EXPECT_TRUE(rs.Match("-")); + EXPECT_FALSE(rs.Match("")); + EXPECT_FALSE(rs.Match("`")); + EXPECT_FALSE(rs.Match("b")); } TEST(Regex, CharacterRange7) { Regex re("[-a]"); ASSERT_TRUE(re.IsValid()); - EXPECT_TRUE(re.Match("a")); - EXPECT_TRUE(re.Match("-")); - EXPECT_FALSE(re.Match("")); - EXPECT_FALSE(re.Match("`")); - EXPECT_FALSE(re.Match("b")); + RegexSearch rs(re); + EXPECT_TRUE(rs.Match("a")); + EXPECT_TRUE(rs.Match("-")); + EXPECT_FALSE(rs.Match("")); + EXPECT_FALSE(rs.Match("`")); + EXPECT_FALSE(rs.Match("b")); } TEST(Regex, CharacterRange8) { Regex re("[a-zA-Z0-9]*"); ASSERT_TRUE(re.IsValid()); - EXPECT_TRUE(re.Match("Milo")); - EXPECT_TRUE(re.Match("MT19937")); - EXPECT_TRUE(re.Match("43")); - EXPECT_FALSE(re.Match("a_b")); - EXPECT_FALSE(re.Match("!")); + RegexSearch rs(re); + EXPECT_TRUE(rs.Match("Milo")); + EXPECT_TRUE(rs.Match("MT19937")); + EXPECT_TRUE(rs.Match("43")); + EXPECT_FALSE(rs.Match("a_b")); + EXPECT_FALSE(rs.Match("!")); } TEST(Regex, Search) { Regex re("abc"); ASSERT_TRUE(re.IsValid()); - EXPECT_TRUE(re.Search("abc")); - EXPECT_TRUE(re.Search("_abc")); - EXPECT_TRUE(re.Search("abc_")); - EXPECT_TRUE(re.Search("_abc_")); - EXPECT_TRUE(re.Search("__abc__")); - EXPECT_TRUE(re.Search("abcabc")); - EXPECT_FALSE(re.Search("a")); - EXPECT_FALSE(re.Search("ab")); - EXPECT_FALSE(re.Search("bc")); - EXPECT_FALSE(re.Search("cba")); + RegexSearch rs(re); + EXPECT_TRUE(rs.Search("abc")); + EXPECT_TRUE(rs.Search("_abc")); + EXPECT_TRUE(rs.Search("abc_")); + EXPECT_TRUE(rs.Search("_abc_")); + EXPECT_TRUE(rs.Search("__abc__")); + EXPECT_TRUE(rs.Search("abcabc")); + EXPECT_FALSE(rs.Search("a")); + EXPECT_FALSE(rs.Search("ab")); + EXPECT_FALSE(rs.Search("bc")); + EXPECT_FALSE(rs.Search("cba")); } TEST(Regex, Search_BeginAnchor) { Regex re("^abc"); ASSERT_TRUE(re.IsValid()); - EXPECT_TRUE(re.Search("abc")); - EXPECT_TRUE(re.Search("abc_")); - EXPECT_TRUE(re.Search("abcabc")); - EXPECT_FALSE(re.Search("_abc")); - EXPECT_FALSE(re.Search("_abc_")); - EXPECT_FALSE(re.Search("a")); - EXPECT_FALSE(re.Search("ab")); - EXPECT_FALSE(re.Search("bc")); - EXPECT_FALSE(re.Search("cba")); + RegexSearch rs(re); + EXPECT_TRUE(rs.Search("abc")); + EXPECT_TRUE(rs.Search("abc_")); + EXPECT_TRUE(rs.Search("abcabc")); + EXPECT_FALSE(rs.Search("_abc")); + EXPECT_FALSE(rs.Search("_abc_")); + EXPECT_FALSE(rs.Search("a")); + EXPECT_FALSE(rs.Search("ab")); + EXPECT_FALSE(rs.Search("bc")); + EXPECT_FALSE(rs.Search("cba")); } TEST(Regex, Search_EndAnchor) { Regex re("abc$"); ASSERT_TRUE(re.IsValid()); - EXPECT_TRUE(re.Search("abc")); - EXPECT_TRUE(re.Search("_abc")); - EXPECT_TRUE(re.Search("abcabc")); - EXPECT_FALSE(re.Search("abc_")); - EXPECT_FALSE(re.Search("_abc_")); - EXPECT_FALSE(re.Search("a")); - EXPECT_FALSE(re.Search("ab")); - EXPECT_FALSE(re.Search("bc")); - EXPECT_FALSE(re.Search("cba")); + RegexSearch rs(re); + EXPECT_TRUE(rs.Search("abc")); + EXPECT_TRUE(rs.Search("_abc")); + EXPECT_TRUE(rs.Search("abcabc")); + EXPECT_FALSE(rs.Search("abc_")); + EXPECT_FALSE(rs.Search("_abc_")); + EXPECT_FALSE(rs.Search("a")); + EXPECT_FALSE(rs.Search("ab")); + EXPECT_FALSE(rs.Search("bc")); + EXPECT_FALSE(rs.Search("cba")); } TEST(Regex, Search_BothAnchor) { Regex re("^abc$"); ASSERT_TRUE(re.IsValid()); - EXPECT_TRUE(re.Search("abc")); - EXPECT_FALSE(re.Search("")); - EXPECT_FALSE(re.Search("a")); - EXPECT_FALSE(re.Search("b")); - EXPECT_FALSE(re.Search("ab")); - EXPECT_FALSE(re.Search("abcd")); + RegexSearch rs(re); + EXPECT_TRUE(rs.Search("abc")); + EXPECT_FALSE(rs.Search("")); + EXPECT_FALSE(rs.Search("a")); + EXPECT_FALSE(rs.Search("b")); + EXPECT_FALSE(rs.Search("ab")); + EXPECT_FALSE(rs.Search("abcd")); } TEST(Regex, Escape) { const char* s = "\\^\\$\\|\\(\\)\\?\\*\\+\\.\\[\\]\\{\\}\\\\\\f\\n\\r\\t\\v[\\b][\\[][\\]]"; Regex re(s); ASSERT_TRUE(re.IsValid()); - EXPECT_TRUE(re.Match("^$|()?*+.[]{}\\\x0C\n\r\t\x0B\b[]")); - EXPECT_FALSE(re.Match(s)); // Not escaping + RegexSearch rs(re); + EXPECT_TRUE(rs.Match("^$|()?*+.[]{}\\\x0C\n\r\t\x0B\b[]")); + EXPECT_FALSE(rs.Match(s)); // Not escaping } TEST(Regex, Invalid) { From 307e021606e9bf9f034be6781abf49065bc16c3e Mon Sep 17 00:00:00 2001 From: Tall??di Mih??ly Date: Mon, 19 Sep 2016 18:14:34 +0200 Subject: [PATCH 125/305] Move constructor support for Writer This also requires turning off the c++98 compatibility checks when building with clang. --- include/rapidjson/writer.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/include/rapidjson/writer.h b/include/rapidjson/writer.h index c5a3b98..16e4496 100644 --- a/include/rapidjson/writer.h +++ b/include/rapidjson/writer.h @@ -42,6 +42,7 @@ RAPIDJSON_DIAG_OFF(4127) // conditional expression is constant RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_OFF(padded) RAPIDJSON_DIAG_OFF(unreachable-code) +RAPIDJSON_DIAG_OFF(c++98-compat) #endif RAPIDJSON_NAMESPACE_BEGIN @@ -103,6 +104,13 @@ public: Writer(StackAllocator* allocator = 0, size_t levelDepth = kDefaultLevelDepth) : os_(0), level_stack_(allocator, levelDepth * sizeof(Level)), maxDecimalPlaces_(kDefaultMaxDecimalPlaces), hasRoot_(false) {} +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + Writer(Writer&& rhs) : + os_(rhs.os_), level_stack_(std::move(rhs.level_stack_)), maxDecimalPlaces_(rhs.maxDecimalPlaces_), hasRoot_(rhs.hasRoot_) { + rhs.os_=nullptr; + } +#endif + //! Reset the writer with a new stream. /*! This function reset the writer with a new stream and default settings, From 62dc1077eb61b49121e7613ec13dfbdeabd55653 Mon Sep 17 00:00:00 2001 From: Tall??di Mih??ly Date: Mon, 19 Sep 2016 19:49:58 +0200 Subject: [PATCH 126/305] Move constructor support for PrettyWriter --- include/rapidjson/prettywriter.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/include/rapidjson/prettywriter.h b/include/rapidjson/prettywriter.h index c6f0216..abd964f 100644 --- a/include/rapidjson/prettywriter.h +++ b/include/rapidjson/prettywriter.h @@ -22,6 +22,11 @@ RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_OFF(effc++) #endif +#if defined(__clang__) +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(c++98-compat) +#endif + RAPIDJSON_NAMESPACE_BEGIN //! Combination of PrettyWriter format flags. @@ -57,6 +62,11 @@ public: explicit PrettyWriter(StackAllocator* allocator = 0, size_t levelDepth = Base::kDefaultLevelDepth) : Base(allocator, levelDepth), indentChar_(' '), indentCharCount_(4) {} +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + PrettyWriter(PrettyWriter&& rhs) : + Base(std::forward(rhs)), indentChar_(rhs.indentChar_), indentCharCount_(rhs.indentCharCount_), formatOptions_(rhs.formatOptions_) {} +#endif + //! Set custom indentation. /*! \param indentChar Character for indentation. Must be whitespace character (' ', '\\t', '\\n', '\\r'). \param indentCharCount Number of indent characters for each indentation level. @@ -254,6 +264,10 @@ private: RAPIDJSON_NAMESPACE_END +#if defined(__clang__) +RAPIDJSON_DIAG_POP +#endif + #ifdef __GNUC__ RAPIDJSON_DIAG_POP #endif From 1a64cd099719bc0955beb59124bf50ee305e8485 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tall=C3=B3di=20Mih=C3=A1ly?= Date: Mon, 19 Sep 2016 22:13:33 +0200 Subject: [PATCH 127/305] Tests for Writer, PrettyWriter move constructors --- test/unittest/prettywritertest.cpp | 31 ++++++++++++++++++++++++++++++ test/unittest/writertest.cpp | 27 ++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/test/unittest/prettywritertest.cpp b/test/unittest/prettywritertest.cpp index a372f79..1700c0c 100644 --- a/test/unittest/prettywritertest.cpp +++ b/test/unittest/prettywritertest.cpp @@ -18,6 +18,11 @@ #include "rapidjson/stringbuffer.h" #include "rapidjson/filewritestream.h" +#ifdef __clang__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(c++98-compat) +#endif + using namespace rapidjson; static const char kJson[] = "{\"hello\":\"world\",\"t\":true,\"f\":false,\"n\":null,\"i\":123,\"pi\":3.1416,\"a\":[1,2,3,-1],\"u64\":1234567890123456789,\"i64\":-1234567890123456789}"; @@ -201,3 +206,29 @@ TEST(PrettyWriter, RawValue) { "}", buffer.GetString()); } + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS +TEST(PrettyWriter, MoveCtor) { + StringBuffer buffer; + auto writerGen=[](StringBuffer &target) -> PrettyWriter { + PrettyWriter writer(target); + writer.StartObject(); + writer.Key("a"); + writer.Int(1); + return std::move(writer); + }; + + PrettyWriter writer(writerGen(buffer)); + writer.EndObject(); + EXPECT_TRUE(writer.IsComplete()); + EXPECT_STREQ( + "{\n" + " \"a\": 1\n" + "}", + buffer.GetString()); +} +#endif + +#ifdef __clang__ +RAPIDJSON_DIAG_POP +#endif diff --git a/test/unittest/writertest.cpp b/test/unittest/writertest.cpp index 29f7626..b3124bf 100644 --- a/test/unittest/writertest.cpp +++ b/test/unittest/writertest.cpp @@ -20,6 +20,11 @@ #include "rapidjson/stringbuffer.h" #include "rapidjson/memorybuffer.h" +#ifdef __clang__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(c++98-compat) +#endif + using namespace rapidjson; TEST(Writer, Compact) { @@ -495,3 +500,25 @@ TEST(Writer, RawValue) { EXPECT_TRUE(writer.IsComplete()); EXPECT_STREQ("{\"a\":1,\"raw\":[\"Hello\\nWorld\", 123.456]}", buffer.GetString()); } + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS +TEST(Writer, MoveCtor) { + StringBuffer buffer; + auto writerGen=[](StringBuffer &target) -> Writer { + Writer writer(target); + writer.StartObject(); + writer.Key("a"); + writer.Int(1); + return std::move(writer); + }; + + Writer writer(writerGen(buffer)); + writer.EndObject(); + EXPECT_TRUE(writer.IsComplete()); + EXPECT_STREQ("{\"a\":1}", buffer.GetString()); +} +#endif + +#ifdef __clang__ +RAPIDJSON_DIAG_POP +#endif From f28203c7a12fc12b7a5425846a8f1a0ae94f657b Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Wed, 21 Sep 2016 11:09:04 +0800 Subject: [PATCH 128/305] Fix #741 --- include/rapidjson/writer.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/rapidjson/writer.h b/include/rapidjson/writer.h index 16e4496..8f6e174 100644 --- a/include/rapidjson/writer.h +++ b/include/rapidjson/writer.h @@ -107,7 +107,7 @@ public: #if RAPIDJSON_HAS_CXX11_RVALUE_REFS Writer(Writer&& rhs) : os_(rhs.os_), level_stack_(std::move(rhs.level_stack_)), maxDecimalPlaces_(rhs.maxDecimalPlaces_), hasRoot_(rhs.hasRoot_) { - rhs.os_=nullptr; + rhs.os_ = 0; } #endif From 0761ac126b1aa2144a26c26dfde1c08073eda43a Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Wed, 21 Sep 2016 21:49:49 +0800 Subject: [PATCH 129/305] Remove lambda expression in (pretty)writertest --- test/unittest/prettywritertest.cpp | 19 ++++++++++--------- test/unittest/writertest.cpp | 18 +++++++++--------- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/test/unittest/prettywritertest.cpp b/test/unittest/prettywritertest.cpp index 1700c0c..42ff3f2 100644 --- a/test/unittest/prettywritertest.cpp +++ b/test/unittest/prettywritertest.cpp @@ -208,17 +208,18 @@ TEST(PrettyWriter, RawValue) { } #if RAPIDJSON_HAS_CXX11_RVALUE_REFS + +static PrettyWriter WriterGen(StringBuffer &target) { + PrettyWriter writer(target); + writer.StartObject(); + writer.Key("a"); + writer.Int(1); + return std::move(writer); +} + TEST(PrettyWriter, MoveCtor) { StringBuffer buffer; - auto writerGen=[](StringBuffer &target) -> PrettyWriter { - PrettyWriter writer(target); - writer.StartObject(); - writer.Key("a"); - writer.Int(1); - return std::move(writer); - }; - - PrettyWriter writer(writerGen(buffer)); + PrettyWriter writer(WriterGen(buffer)); writer.EndObject(); EXPECT_TRUE(writer.IsComplete()); EXPECT_STREQ( diff --git a/test/unittest/writertest.cpp b/test/unittest/writertest.cpp index b3124bf..feb4d74 100644 --- a/test/unittest/writertest.cpp +++ b/test/unittest/writertest.cpp @@ -502,17 +502,17 @@ TEST(Writer, RawValue) { } #if RAPIDJSON_HAS_CXX11_RVALUE_REFS +static Writer WriterGen(StringBuffer &target) { + Writer writer(target); + writer.StartObject(); + writer.Key("a"); + writer.Int(1); + return std::move(writer); +} + TEST(Writer, MoveCtor) { StringBuffer buffer; - auto writerGen=[](StringBuffer &target) -> Writer { - Writer writer(target); - writer.StartObject(); - writer.Key("a"); - writer.Int(1); - return std::move(writer); - }; - - Writer writer(writerGen(buffer)); + Writer writer(WriterGen(buffer)); writer.EndObject(); EXPECT_TRUE(writer.IsComplete()); EXPECT_STREQ("{\"a\":1}", buffer.GetString()); From 5cd62c235d0ff1ccc2f9822047a470f62ad947f6 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Thu, 22 Sep 2016 18:11:22 +0800 Subject: [PATCH 130/305] Add StringBuffer::GetLength() Fix #744 --- include/rapidjson/stringbuffer.h | 4 ++++ test/unittest/stringbuffertest.cpp | 24 +++++++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/include/rapidjson/stringbuffer.h b/include/rapidjson/stringbuffer.h index 78f34d2..4e38b82 100644 --- a/include/rapidjson/stringbuffer.h +++ b/include/rapidjson/stringbuffer.h @@ -78,8 +78,12 @@ public: return stack_.template Bottom(); } + //! Get the size of string in bytes in the string buffer. size_t GetSize() const { return stack_.GetSize(); } + //! Get the length of string in Ch in the string buffer. + size_t GetLength() const { return stack_.GetSize() / sizeof(Ch); } + static const size_t kDefaultCapacity = 256; mutable internal::Stack stack_; diff --git a/test/unittest/stringbuffertest.cpp b/test/unittest/stringbuffertest.cpp index ded513c..8a36102 100644 --- a/test/unittest/stringbuffertest.cpp +++ b/test/unittest/stringbuffertest.cpp @@ -26,6 +26,7 @@ using namespace rapidjson; TEST(StringBuffer, InitialSize) { StringBuffer buffer; EXPECT_EQ(0u, buffer.GetSize()); + EXPECT_EQ(0u, buffer.GetLength()); EXPECT_STREQ("", buffer.GetString()); } @@ -34,14 +35,17 @@ TEST(StringBuffer, Put) { buffer.Put('A'); EXPECT_EQ(1u, buffer.GetSize()); + EXPECT_EQ(1u, buffer.GetLength()); EXPECT_STREQ("A", buffer.GetString()); } TEST(StringBuffer, PutN_Issue672) { GenericStringBuffer, MemoryPoolAllocator<> > buffer; EXPECT_EQ(0, buffer.GetSize()); + EXPECT_EQ(0, buffer.GetLength()); rapidjson::PutN(buffer, ' ', 1); EXPECT_EQ(1, buffer.GetSize()); + EXPECT_EQ(1, buffer.GetLength()); } TEST(StringBuffer, Clear) { @@ -52,6 +56,7 @@ TEST(StringBuffer, Clear) { buffer.Clear(); EXPECT_EQ(0u, buffer.GetSize()); + EXPECT_EQ(0u, buffer.GetLength()); EXPECT_STREQ("", buffer.GetString()); } @@ -60,6 +65,7 @@ TEST(StringBuffer, Push) { buffer.Push(5); EXPECT_EQ(5u, buffer.GetSize()); + EXPECT_EQ(5u, buffer.GetLength()); // Causes sudden expansion to make the stack's capacity equal to size buffer.Push(65536u); @@ -76,9 +82,19 @@ TEST(StringBuffer, Pop) { buffer.Pop(3); EXPECT_EQ(2u, buffer.GetSize()); + EXPECT_EQ(2u, buffer.GetLength()); EXPECT_STREQ("AB", buffer.GetString()); } +TEST(StringBuffer, GetLength_Issue744) { + GenericStringBuffer > buffer; + buffer.Put('A'); + buffer.Put('B'); + buffer.Put('C'); + EXPECT_EQ(3u * sizeof(wchar_t), buffer.GetSize()); + EXPECT_EQ(3u, buffer.GetLength()); +} + #if RAPIDJSON_HAS_CXX11_RVALUE_REFS #if 0 // Many old compiler does not support these. Turn it off temporaily. @@ -130,18 +146,23 @@ TEST(StringBuffer, MoveConstructor) { x.Put('D'); EXPECT_EQ(4u, x.GetSize()); + EXPECT_EQ(4u, x.GetLength()); EXPECT_STREQ("ABCD", x.GetString()); // StringBuffer y(x); // does not compile (!is_copy_constructible) StringBuffer y(std::move(x)); EXPECT_EQ(0u, x.GetSize()); + EXPECT_EQ(0u, x.GetLength()); EXPECT_EQ(4u, y.GetSize()); + EXPECT_EQ(4u, y.GetLength()); EXPECT_STREQ("ABCD", y.GetString()); // StringBuffer z = y; // does not compile (!is_copy_assignable) StringBuffer z = std::move(y); EXPECT_EQ(0u, y.GetSize()); + EXPECT_EQ(0u, y.GetLength()); EXPECT_EQ(4u, z.GetSize()); + EXPECT_EQ(4u, z.GetLength()); EXPECT_STREQ("ABCD", z.GetString()); } @@ -153,13 +174,14 @@ TEST(StringBuffer, MoveAssignment) { x.Put('D'); EXPECT_EQ(4u, x.GetSize()); + EXPECT_EQ(4u, x.GetLength()); EXPECT_STREQ("ABCD", x.GetString()); StringBuffer y; // y = x; // does not compile (!is_copy_assignable) y = std::move(x); EXPECT_EQ(0u, x.GetSize()); - EXPECT_EQ(4u, y.GetSize()); + EXPECT_EQ(4u, y.GetLength()); EXPECT_STREQ("ABCD", y.GetString()); } From 51a31ce006f7dfb850771688fe6a2ccdfcf765af Mon Sep 17 00:00:00 2001 From: Sfinktah Bungholio Date: Sun, 25 Sep 2016 20:36:33 +1000 Subject: [PATCH 131/305] Fix for winmindef.h defining min/max macros --- include/rapidjson/document.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h index 19f5a6a..6b9d9ef 100644 --- a/include/rapidjson/document.h +++ b/include/rapidjson/document.h @@ -29,6 +29,14 @@ RAPIDJSON_DIAG_PUSH #ifdef _MSC_VER RAPIDJSON_DIAG_OFF(4127) // conditional expression is constant RAPIDJSON_DIAG_OFF(4244) // conversion from kXxxFlags to 'uint16_t', possible loss of data +#ifdef _MINWINDEF_ // see: http://stackoverflow.com/questions/22744262/cant-call-stdmax-because-minwindef-h-defines-max +#ifndef NOMINMAX +#pragma push_macro("min") +#pragma push_macro("max") +#undef min +#undef max +#endif +#endif #endif #ifdef __clang__ @@ -2570,6 +2578,12 @@ private: }; RAPIDJSON_NAMESPACE_END +#ifdef _MINWINDEF_ // see: http://stackoverflow.com/questions/22744262/cant-call-stdmax-because-minwindef-h-defines-max +#ifndef NOMINMAX +#pragma pop_macro("min") +#pragma pop_macro("max") +#endif +#endif RAPIDJSON_DIAG_POP #endif // RAPIDJSON_DOCUMENT_H_ From 6a15e40b6e66253e4b596d2b4db9fc2caaeb6b43 Mon Sep 17 00:00:00 2001 From: BennyYip Date: Sun, 25 Sep 2016 21:16:26 +0800 Subject: [PATCH 132/305] fix #749 --- doc/faq.zh-cn.md | 4 ++-- doc/tutorial.zh-cn.md | 20 ++++++++++---------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/doc/faq.zh-cn.md b/doc/faq.zh-cn.md index ed100e1..f12d830 100644 --- a/doc/faq.zh-cn.md +++ b/doc/faq.zh-cn.md @@ -163,9 +163,9 @@ ## Document/Value (DOM) -1. 什么是转移语意?为什么? +1. 什么是转移语义?为什么? - `Value` 不用复制语意,而使用了转移语意。这是指,当把来源值赋值于目标值时,来源值的所有权会转移至目标值。 + `Value` 不用复制语义,而使用了转移语义。这是指,当把来源值赋值于目标值时,来源值的所有权会转移至目标值。 由于转移快于复制,此设计决定强迫使用者注意到复制的消耗。 diff --git a/doc/tutorial.zh-cn.md b/doc/tutorial.zh-cn.md index 61fb0b2..ec1315c 100644 --- a/doc/tutorial.zh-cn.md +++ b/doc/tutorial.zh-cn.md @@ -292,7 +292,7 @@ Value o(kObjectType); Value a(kArrayType); ~~~~~~~~~~ -## 转移语意(Move Semantics) {#MoveSemantics} +## 转移语义(Move Semantics) {#MoveSemantics} 在设计 RapidJSON 时有一个非常特别的决定,就是 Value 赋值并不是把来源 Value 复制至目的 Value,而是把把来源 Value 转移(move)至目的 Value。例如: @@ -302,13 +302,13 @@ Value b(456); b = a; // a 变成 Null,b 变成数字 123。 ~~~~~~~~~~ -![使用移动语意赋值。](diagram/move1.png) +![使用移动语义赋值。](diagram/move1.png) -为什么?此语意有何优点? +为什么?此语义有何优点? 最简单的答案就是性能。对于固定大小的 JSON 类型(Number、True、False、Null),复制它们是简单快捷。然而,对于可变大小的 JSON 类型(String、Array、Object),复制它们会产生大量开销,而且这些开销常常不被察觉。尤其是当我们需要创建临时 Object,把它复制至另一变量,然后再析构它。 -例如,若使用正常 * 复制 * 语意: +例如,若使用正常 * 复制 * 语义: ~~~~~~~~~~cpp Value o(kObjectType); @@ -321,15 +321,15 @@ Value o(kObjectType); } ~~~~~~~~~~ -![复制语意产生大量的复制操作。](diagram/move2.png) +![复制语义产生大量的复制操作。](diagram/move2.png) 那个 `o` Object 需要分配一个和 contacts 相同大小的缓冲区,对 conacts 做深度复制,并最终要析构 contacts。这样会产生大量无必要的内存分配/释放,以及内存复制。 有一些方案可避免实质地复制这些数据,例如引用计数(reference counting)、垃圾回收(garbage collection, GC)。 -为了使 RapidJSON 简单及快速,我们选择了对赋值采用 * 转移 * 语意。这方法与 `std::auto_ptr` 相似,都是在赋值时转移拥有权。转移快得多简单得多,只需要析构原来的 Value,把来源 `memcpy()` 至目标,最后把来源设置为 Null 类型。 +为了使 RapidJSON 简单及快速,我们选择了对赋值采用 * 转移 * 语义。这方法与 `std::auto_ptr` 相似,都是在赋值时转移拥有权。转移快得多简单得多,只需要析构原来的 Value,把来源 `memcpy()` 至目标,最后把来源设置为 Null 类型。 -因此,使用转移语意后,上面的例子变成: +因此,使用转移语义后,上面的例子变成: ~~~~~~~~~~cpp Value o(kObjectType); @@ -341,11 +341,11 @@ Value o(kObjectType); } ~~~~~~~~~~ -![转移语意不需复制。](diagram/move3.png) +![转移语义不需复制。](diagram/move3.png) -在 C++11 中这称为转移赋值操作(move assignment operator)。由于 RapidJSON 支持 C++03,它在赋值操作采用转移语意,其它修改形函数如 `AddMember()`, `PushBack()` 也采用转移语意。 +在 C++11 中这称为转移赋值操作(move assignment operator)。由于 RapidJSON 支持 C++03,它在赋值操作采用转移语义,其它修改形函数如 `AddMember()`, `PushBack()` 也采用转移语义。 -### 转移语意及临时值 {#TemporaryValues} +### 转移语义及临时值 {#TemporaryValues} 有时候,我们想直接构造一个 Value 并传递给一个“转移”函数(如 `PushBack()`、`AddMember()`)。由于临时对象是不能转换为正常的 Value 引用,我们加入了一个方便的 `Move()` 函数: From cb017cbf5e4ad79a3e48419269c2739127984c4f Mon Sep 17 00:00:00 2001 From: Adam Majer Date: Fri, 30 Sep 2016 17:10:04 +0200 Subject: [PATCH 133/305] Fix compilation with older GCC versions Older GCC versions fail compiling RapidJSON due to a warning include/rapidjson/reader.h:578: error: suggest a space before ';' or explicit braces around empty body in 'while' statement : warnings being treated as errors --- include/rapidjson/reader.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/rapidjson/reader.h b/include/rapidjson/reader.h index 19f8849..e53bbd2 100644 --- a/include/rapidjson/reader.h +++ b/include/rapidjson/reader.h @@ -575,7 +575,7 @@ private: } } else if (RAPIDJSON_LIKELY(Consume(is, '/'))) - while (is.Peek() != '\0' && is.Take() != '\n'); + while (is.Peek() != '\0' && is.Take() != '\n') {} else RAPIDJSON_PARSE_ERROR(kParseErrorUnspecificSyntaxError, is.Tell()); From 9d8df28c1dd92be8480fae8026fed0aa2c0d8cdd Mon Sep 17 00:00:00 2001 From: Patrick Cheng Date: Fri, 30 Sep 2016 10:47:00 -0700 Subject: [PATCH 134/305] added assertion to help suppress clang warnings --- include/rapidjson/internal/stack.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/rapidjson/internal/stack.h b/include/rapidjson/internal/stack.h index 022c9aa..54ac77a 100644 --- a/include/rapidjson/internal/stack.h +++ b/include/rapidjson/internal/stack.h @@ -113,6 +113,7 @@ public: // Expansion is run very infrequently, so it is moved to another (probably non-inline) function. template RAPIDJSON_FORCEINLINE void Reserve(size_t count = 1) { + RAPIDJSON_ASSERT(stackTop_); // Expand the stack if needed if (RAPIDJSON_UNLIKELY(stackTop_ + sizeof(T) * count > stackEnd_)) Expand(count); @@ -126,6 +127,7 @@ public: template RAPIDJSON_FORCEINLINE T* PushUnsafe(size_t count = 1) { + RAPIDJSON_ASSERT(stackTop_); RAPIDJSON_ASSERT(stackTop_ + sizeof(T) * count <= stackEnd_); T* ret = reinterpret_cast(stackTop_); stackTop_ += sizeof(T) * count; From 91a803d46394a668fd5cb51bd1b4dbea9b4b2fb0 Mon Sep 17 00:00:00 2001 From: Patrick Cheng Date: Fri, 30 Sep 2016 11:12:23 -0700 Subject: [PATCH 135/305] Reserve() is sometimes called when stackTop_ is null. The assert is invalid. --- include/rapidjson/internal/stack.h | 1 - 1 file changed, 1 deletion(-) diff --git a/include/rapidjson/internal/stack.h b/include/rapidjson/internal/stack.h index 54ac77a..26b716d 100644 --- a/include/rapidjson/internal/stack.h +++ b/include/rapidjson/internal/stack.h @@ -113,7 +113,6 @@ public: // Expansion is run very infrequently, so it is moved to another (probably non-inline) function. template RAPIDJSON_FORCEINLINE void Reserve(size_t count = 1) { - RAPIDJSON_ASSERT(stackTop_); // Expand the stack if needed if (RAPIDJSON_UNLIKELY(stackTop_ + sizeof(T) * count > stackEnd_)) Expand(count); From 95224aff7dff65c448dca374b4c7fccc32680e5c Mon Sep 17 00:00:00 2001 From: Patrick Cheng Date: Fri, 30 Sep 2016 13:44:15 -0700 Subject: [PATCH 136/305] When length is 0, the code does nothing, so skip it completely. Previously, os.Push(0) would do nothing as well. But with the newly added assertion, is the stack is empty, it will fail the assertion. --- include/rapidjson/reader.h | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/include/rapidjson/reader.h b/include/rapidjson/reader.h index 19f8849..e3523a0 100644 --- a/include/rapidjson/reader.h +++ b/include/rapidjson/reader.h @@ -948,11 +948,13 @@ private: #else length = static_cast(__builtin_ffs(r) - 1); #endif - char* q = reinterpret_cast(os.Push(length)); - for (size_t i = 0; i < length; i++) - q[i] = p[i]; + if (length != 0) { + char* q = reinterpret_cast(os.Push(length)); + for (size_t i = 0; i < length; i++) + q[i] = p[i]; - p += length; + p += length; + } break; } _mm_storeu_si128(reinterpret_cast<__m128i *>(os.Push(16)), s); From 48f8364f652fbe02075e692742b4a974b0d7bff3 Mon Sep 17 00:00:00 2001 From: Yu Chen Hou Date: Tue, 4 Oct 2016 14:07:50 -0700 Subject: [PATCH 137/305] FIx typo in documentation The use of the vertical bar seems to break the rendering of the table in the documentation here: http://rapidjson.org/md_doc_schema.html Seems like we can fix it by using html encoding for the vertical bars as described in this post: http://stackoverflow.com/questions/17319940/how-to-escape-a-pipe-char-in-a-code-statement-in-a-markdown-table/17320389#17320389 --- doc/schema.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/doc/schema.md b/doc/schema.md index a83cebc..8b4195b 100644 --- a/doc/schema.md +++ b/doc/schema.md @@ -157,7 +157,7 @@ As `SchemaDocument` does not know how to resolve such URI, it needs a user-provi ~~~ class MyRemoteSchemaDocumentProvider : public IRemoteSchemaDocumentProvider { public: - virtual const SchemaDocument* GetRemoteDocument(const char* uri, SizeTyp length) { + virtual const SchemaDocument* GetRemoteDocument(const char* uri, SizeType length) { // Resolve the uri and returns a pointer to that schema. } }; @@ -185,7 +185,7 @@ RapidJSON implemented a simple NFA regular expression engine, which is used by d |Syntax|Description| |------|-----------| |`ab` | Concatenation | -|`a|b` | Alternation | +|a|b | Alternation | |`a?` | Zero or one | |`a*` | Zero or more | |`a+` | One or more | @@ -202,7 +202,7 @@ RapidJSON implemented a simple NFA regular expression engine, which is used by d |`[^abc]` | Negated character classes | |`[^a-c]` | Negated character class range | |`[\b]` | Backspace (U+0008) | -|`\|`, `\\`, ... | Escape characters | +|\\|, `\\`, ... | Escape characters | |`\f` | Form feed (U+000C) | |`\n` | Line feed (U+000A) | |`\r` | Carriage return (U+000D) | From a3300bf4b1e06d126a6820de926b65634864f708 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Wed, 5 Oct 2016 09:21:01 +0800 Subject: [PATCH 138/305] Fix schema.zh-cn.md --- doc/schema.zh-cn.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/doc/schema.zh-cn.md b/doc/schema.zh-cn.md index a01c1b1..fa076de 100644 --- a/doc/schema.zh-cn.md +++ b/doc/schema.zh-cn.md @@ -157,7 +157,7 @@ JSON Schema 支持 [`$ref` 关键字](http://spacetelescope.github.io/understand ~~~ class MyRemoteSchemaDocumentProvider : public IRemoteSchemaDocumentProvider { public: - virtual const SchemaDocument* GetRemoteDocument(const char* uri, SizeTyp length) { + virtual const SchemaDocument* GetRemoteDocument(const char* uri, SizeType length) { // Resolve the uri and returns a pointer to that schema. } }; @@ -185,7 +185,7 @@ RapidJSON 实现了一个简单的 NFA 正则表达式引擎,并预设使用 |语法|描述| |------|-----------| |`ab` | 串联 | -|`a|b` | 交替 | +|a|b | 交替 | |`a?` | 零或一次 | |`a*` | 零或多次 | |`a+` | 一或多次 | @@ -202,7 +202,7 @@ RapidJSON 实现了一个简单的 NFA 正则表达式引擎,并预设使用 |`[^abc]` | 字符组取反 | |`[^a-c]` | 字符组范围取反 | |`[\b]` | 退格符 (U+0008) | -|`\|`, `\\`, ... | 转义字符 | +|\\|, `\\`, ... | 转义字符 | |`\f` | 馈页 (U+000C) | |`\n` | 馈行 (U+000A) | |`\r` | 回车 (U+000D) | From c490d880a3e18d315af0afbe5e8b54c7179d6df0 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Wed, 5 Oct 2016 09:41:56 +0800 Subject: [PATCH 139/305] Another try for fixing schema.md --- doc/schema.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/schema.md b/doc/schema.md index 8b4195b..0ec3243 100644 --- a/doc/schema.md +++ b/doc/schema.md @@ -185,7 +185,7 @@ RapidJSON implemented a simple NFA regular expression engine, which is used by d |Syntax|Description| |------|-----------| |`ab` | Concatenation | -|a|b | Alternation | +|a|b | Alternation | |`a?` | Zero or one | |`a*` | Zero or more | |`a+` | One or more | @@ -202,7 +202,7 @@ RapidJSON implemented a simple NFA regular expression engine, which is used by d |`[^abc]` | Negated character classes | |`[^a-c]` | Negated character class range | |`[\b]` | Backspace (U+0008) | -|\\|, `\\`, ... | Escape characters | +|\\|, `\\`, ... | Escape characters | |`\f` | Form feed (U+000C) | |`\n` | Line feed (U+000A) | |`\r` | Carriage return (U+000D) | From 11df748a3b038ce893349b61d7f6390a10ea6a4b Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Wed, 5 Oct 2016 10:21:45 +0800 Subject: [PATCH 140/305] Revert "Another try for fixing schema.md" This reverts commit c490d880a3e18d315af0afbe5e8b54c7179d6df0. --- doc/schema.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/schema.md b/doc/schema.md index 0ec3243..8b4195b 100644 --- a/doc/schema.md +++ b/doc/schema.md @@ -185,7 +185,7 @@ RapidJSON implemented a simple NFA regular expression engine, which is used by d |Syntax|Description| |------|-----------| |`ab` | Concatenation | -|a|b | Alternation | +|a|b | Alternation | |`a?` | Zero or one | |`a*` | Zero or more | |`a+` | One or more | @@ -202,7 +202,7 @@ RapidJSON implemented a simple NFA regular expression engine, which is used by d |`[^abc]` | Negated character classes | |`[^a-c]` | Negated character class range | |`[\b]` | Backspace (U+0008) | -|\\|, `\\`, ... | Escape characters | +|\\|, `\\`, ... | Escape characters | |`\f` | Form feed (U+000C) | |`\n` | Line feed (U+000A) | |`\r` | Carriage return (U+000D) | From 3f23397596c677f23c9b405a0a93472428947892 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Wed, 5 Oct 2016 15:02:31 +0800 Subject: [PATCH 141/305] Remove unncessary std::move() Fixes #762 --- test/unittest/prettywritertest.cpp | 2 +- test/unittest/writertest.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/unittest/prettywritertest.cpp b/test/unittest/prettywritertest.cpp index 42ff3f2..13d1a8d 100644 --- a/test/unittest/prettywritertest.cpp +++ b/test/unittest/prettywritertest.cpp @@ -214,7 +214,7 @@ static PrettyWriter WriterGen(StringBuffer &target) { writer.StartObject(); writer.Key("a"); writer.Int(1); - return std::move(writer); + return writer; } TEST(PrettyWriter, MoveCtor) { diff --git a/test/unittest/writertest.cpp b/test/unittest/writertest.cpp index feb4d74..d346e0f 100644 --- a/test/unittest/writertest.cpp +++ b/test/unittest/writertest.cpp @@ -507,7 +507,7 @@ static Writer WriterGen(StringBuffer &target) { writer.StartObject(); writer.Key("a"); writer.Int(1); - return std::move(writer); + return writer; } TEST(Writer, MoveCtor) { From 8eaa122c272623ce7963dc7433ba108bc9a9809e Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Thu, 6 Oct 2016 13:32:16 +0800 Subject: [PATCH 142/305] Update dom.zh-cn.md --- doc/dom.zh-cn.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/dom.zh-cn.md b/doc/dom.zh-cn.md index d93f603..b709485 100644 --- a/doc/dom.zh-cn.md +++ b/doc/dom.zh-cn.md @@ -128,7 +128,7 @@ GenericDocument& GenericDocument::Parse(const Ch* str); ## 解析错误 {#ParseError} -当解析过程顺利完成,`Document` 便会含有解析结果。当过程出现错误,原来的 DOM 会 * 维持不便 *。可使用 `bool HasParseError()`、`ParseErrorCode GetParseError()` 及 `size_t GetParseOffset()` 获取解析的错误状态。 +当解析过程顺利完成,`Document` 便会含有解析结果。当过程出现错误,原来的 DOM 会*维持不变*。可使用 `bool HasParseError()`、`ParseErrorCode GetParseError()` 及 `size_t GetParseOffset()` 获取解析的错误状态。 解析错误代号 | 描述 --------------------------------------------|--------------------------------------------------- From 236322797475807ae52502453a51d6640104bc83 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Fri, 14 Oct 2016 22:03:54 +0800 Subject: [PATCH 143/305] Add Value::Value(float) and static_cast for suppressing clang warning --- include/rapidjson/document.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h index 19f5a6a..f4dd25c 100644 --- a/include/rapidjson/document.h +++ b/include/rapidjson/document.h @@ -672,6 +672,9 @@ public: //! Constructor for double value. explicit GenericValue(double d) RAPIDJSON_NOEXCEPT : data_() { data_.n.d = d; data_.f.flags = kNumberDoubleFlag; } + //! Constructor for float value. + explicit GenericValue(float f) RAPIDJSON_NOEXCEPT : data_() { data_.n.d = static_cast(f); data_.f.flags = kNumberDoubleFlag; } + //! Constructor for constant string (i.e. do not make a copy of string) GenericValue(const Ch* s, SizeType length) RAPIDJSON_NOEXCEPT : data_() { SetStringRaw(StringRef(s, length)); } @@ -1671,7 +1674,7 @@ public: GenericValue& SetInt64(int64_t i64) { this->~GenericValue(); new (this) GenericValue(i64); return *this; } GenericValue& SetUint64(uint64_t u64) { this->~GenericValue(); new (this) GenericValue(u64); return *this; } GenericValue& SetDouble(double d) { this->~GenericValue(); new (this) GenericValue(d); return *this; } - GenericValue& SetFloat(float f) { this->~GenericValue(); new (this) GenericValue(f); return *this; } + GenericValue& SetFloat(float f) { this->~GenericValue(); new (this) GenericValue(static_cast(f)); return *this; } //@} From 517dd4dbb8babb5b69e3ed6eabdaedeb177bd977 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Mon, 17 Oct 2016 14:25:24 +0800 Subject: [PATCH 144/305] Fix failing to resolve $ref in allOf causes crash in SchemaValidator::StartObject() --- include/rapidjson/schema.h | 3 +++ test/unittest/schematest.cpp | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/include/rapidjson/schema.h b/include/rapidjson/schema.h index 288b93d..420db62 100644 --- a/include/rapidjson/schema.h +++ b/include/rapidjson/schema.h @@ -1366,6 +1366,9 @@ public: new (schemaMap_.template Push()) SchemaEntry(refEntry->source, const_cast(s), false, allocator_); } } + else if (refEntry->schema) + *refEntry->schema = SchemaType::GetTypeless(); + refEntry->~SchemaRefEntry(); } diff --git a/test/unittest/schematest.cpp b/test/unittest/schematest.cpp index 6a8b685..4780516 100644 --- a/test/unittest/schematest.cpp +++ b/test/unittest/schematest.cpp @@ -1308,6 +1308,14 @@ TEST(SchemaValidator, Issue608) { INVALIDATE(s, "{\"a\" : null, \"a\" : null}", "", "required", ""); } +// Fail to resolve $ref in allOf causes crash in SchemaValidator::StartObject() +TEST(SchemaValidator, Issue728_AllOfRef) { + Document sd; + sd.Parse("{\"allOf\": [{\"$ref\": \"#/abc\"}]}"); + SchemaDocument s(sd); + VALIDATE(s, "{\"key1\": \"abc\", \"key2\": \"def\"}", true); +} + #ifdef __clang__ RAPIDJSON_DIAG_POP #endif From b963eb447bee24a692d4ca718db6252a028f131a Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Mon, 17 Oct 2016 18:30:18 +0800 Subject: [PATCH 145/305] Change SchemaValidator::GetNullHandler() from singleton to instance. --- include/rapidjson/schema.h | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/include/rapidjson/schema.h b/include/rapidjson/schema.h index 420db62..bb5607f 100644 --- a/include/rapidjson/schema.h +++ b/include/rapidjson/schema.h @@ -1368,7 +1368,7 @@ public: } else if (refEntry->schema) *refEntry->schema = SchemaType::GetTypeless(); - + refEntry->~SchemaRefEntry(); } @@ -1579,11 +1579,11 @@ public: : schemaDocument_(&schemaDocument), root_(schemaDocument.GetRoot()), - outputHandler_(GetNullHandler()), stateAllocator_(allocator), ownStateAllocator_(0), schemaStack_(allocator, schemaStackCapacity), documentStack_(allocator, documentStackCapacity), + outputHandler_(CreateNullHandler()), valid_(true) #if RAPIDJSON_SCHEMA_VERBOSE , depth_(0) @@ -1607,11 +1607,12 @@ public: : schemaDocument_(&schemaDocument), root_(schemaDocument.GetRoot()), - outputHandler_(outputHandler), stateAllocator_(allocator), ownStateAllocator_(0), schemaStack_(allocator, schemaStackCapacity), documentStack_(allocator, documentStackCapacity), + outputHandler_(outputHandler), + nullHandler_(0), valid_(true) #if RAPIDJSON_SCHEMA_VERBOSE , depth_(0) @@ -1795,11 +1796,11 @@ private: : schemaDocument_(&schemaDocument), root_(root), - outputHandler_(GetNullHandler()), stateAllocator_(allocator), ownStateAllocator_(0), schemaStack_(allocator, schemaStackCapacity), documentStack_(allocator, documentStackCapacity), + outputHandler_(CreateNullHandler()), valid_(true) #if RAPIDJSON_SCHEMA_VERBOSE , depth_(depth) @@ -1913,20 +1914,20 @@ private: Context& CurrentContext() { return *schemaStack_.template Top(); } const Context& CurrentContext() const { return *schemaStack_.template Top(); } - static OutputHandler& GetNullHandler() { - static OutputHandler nullHandler; - return nullHandler; + OutputHandler& CreateNullHandler() { + return *(nullHandler_ = static_cast(stateAllocator_->Malloc(sizeof(OutputHandler)))); } static const size_t kDefaultSchemaStackCapacity = 1024; static const size_t kDefaultDocumentStackCapacity = 256; const SchemaDocumentType* schemaDocument_; const SchemaType& root_; - OutputHandler& outputHandler_; StateAllocator* stateAllocator_; StateAllocator* ownStateAllocator_; internal::Stack schemaStack_; //!< stack to store the current path of schema (BaseSchemaType *) internal::Stack documentStack_; //!< stack to store the current path of validating document (Ch) + OutputHandler& outputHandler_; + OutputHandler* nullHandler_; bool valid_; #if RAPIDJSON_SCHEMA_VERBOSE unsigned depth_; From ddbd2ef05de8600a297dbbabc6563bd0c65f649b Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Tue, 18 Oct 2016 10:14:00 +0800 Subject: [PATCH 146/305] Restore missing deallocation of GenericSchemaValidator::nullHandler_ --- include/rapidjson/schema.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/include/rapidjson/schema.h b/include/rapidjson/schema.h index bb5607f..af3b621 100644 --- a/include/rapidjson/schema.h +++ b/include/rapidjson/schema.h @@ -1623,6 +1623,10 @@ public: //! Destructor. ~GenericSchemaValidator() { Reset(); + if (nullHandler_) { + nullHandler_->~OutputHandler(); + StateAllocator::Free(nullHandler_); + } RAPIDJSON_DELETE(ownStateAllocator_); } From 7c4e511eb0f00d074237595bc0dc62a3bd266d57 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Tue, 18 Oct 2016 11:37:15 +0800 Subject: [PATCH 147/305] Change Schema::GetTypeless() from singleton to instance Now owned by SchemaDocument::typeless_, and be shared to its Schema::typeless_ --- include/rapidjson/schema.h | 44 ++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/include/rapidjson/schema.h b/include/rapidjson/schema.h index af3b621..6f1611f 100644 --- a/include/rapidjson/schema.h +++ b/include/rapidjson/schema.h @@ -349,6 +349,7 @@ public: Schema(SchemaDocumentType* schemaDocument, const PointerType& p, const ValueType& value, const ValueType& document, AllocatorType* allocator) : allocator_(allocator), + typeless_(schemaDocument->GetTypeless()), enum_(), enumCount_(), not_(), @@ -453,7 +454,7 @@ public: for (SizeType i = 0; i < propertyCount_; i++) { new (&properties_[i]) Property(); properties_[i].name = allProperties[i]; - properties_[i].schema = GetTypeless(); + properties_[i].schema = typeless_; } } } @@ -575,9 +576,7 @@ public: } ~Schema() { - if (allocator_) { - allocator_->Free(enum_); - } + AllocatorType::Free(enum_); if (properties_) { for (SizeType i = 0; i < propertyCount_; i++) properties_[i].~Property(); @@ -592,7 +591,7 @@ public: #if RAPIDJSON_SCHEMA_HAS_REGEX if (pattern_) { pattern_->~RegexType(); - allocator_->Free(pattern_); + AllocatorType::Free(pattern_); } #endif } @@ -610,12 +609,12 @@ public: else if (additionalItemsSchema_) context.valueSchema = additionalItemsSchema_; else if (additionalItems_) - context.valueSchema = GetTypeless(); + context.valueSchema = typeless_; else RAPIDJSON_INVALID_KEYWORD_RETURN(GetItemsString()); } else - context.valueSchema = GetTypeless(); + context.valueSchema = typeless_; context.arrayElementIndex++; } @@ -792,7 +791,7 @@ public: if (FindPropertyIndex(ValueType(str, len).Move(), &index)) { if (context.patternPropertiesSchemaCount > 0) { context.patternPropertiesSchemas[context.patternPropertiesSchemaCount++] = properties_[index].schema; - context.valueSchema = GetTypeless(); + context.valueSchema = typeless_; context.valuePatternValidatorType = Context::kPatternValidatorWithProperty; } else @@ -807,7 +806,7 @@ public: if (additionalPropertiesSchema_) { if (additionalPropertiesSchema_ && context.patternPropertiesSchemaCount > 0) { context.patternPropertiesSchemas[context.patternPropertiesSchemaCount++] = additionalPropertiesSchema_; - context.valueSchema = GetTypeless(); + context.valueSchema = typeless_; context.valuePatternValidatorType = Context::kPatternValidatorWithAdditionalProperty; } else @@ -815,7 +814,7 @@ public: return true; } else if (additionalProperties_) { - context.valueSchema = GetTypeless(); + context.valueSchema = typeless_; return true; } @@ -949,11 +948,6 @@ private: SizeType count; }; - static const SchemaType* GetTypeless() { - static SchemaType typeless(0, PointerType(), ValueType(kObjectType).Move(), ValueType(kObjectType).Move(), 0); - return &typeless; - } - template void AddUniqueElement(V1& a, const V2& v) { for (typename V1::ConstValueIterator itr = a.Begin(); itr != a.End(); ++itr) @@ -1219,6 +1213,7 @@ private: }; AllocatorType* allocator_; + const SchemaType* typeless_; uint64_t* enum_; SizeType enumCount_; SchemaArray allOf_; @@ -1350,6 +1345,9 @@ public: if (!allocator_) ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator()); + typeless_ = static_cast(allocator_->Malloc(sizeof(SchemaType))); + new (typeless_) SchemaType(this, PointerType(), ValueType(kObjectType).Move(), ValueType(kObjectType).Move(), 0); + // Generate root schema, it will call CreateSchema() to create sub-schemas, // And call AddRefSchema() if there are $ref. CreateSchemaRecursive(&root_, PointerType(), document, document); @@ -1367,7 +1365,7 @@ public: } } else if (refEntry->schema) - *refEntry->schema = SchemaType::GetTypeless(); + *refEntry->schema = typeless_; refEntry->~SchemaRefEntry(); } @@ -1384,12 +1382,14 @@ public: allocator_(rhs.allocator_), ownAllocator_(rhs.ownAllocator_), root_(rhs.root_), + typeless_(rhs.typeless_), schemaMap_(std::move(rhs.schemaMap_)), schemaRef_(std::move(rhs.schemaRef_)) { rhs.remoteProvider_ = 0; rhs.allocator_ = 0; rhs.ownAllocator_ = 0; + rhs.typeless_ = 0; } #endif @@ -1398,6 +1398,9 @@ public: while (!schemaMap_.Empty()) schemaMap_.template Pop(1)->~SchemaEntry(); + typeless_->~SchemaType(); + Allocator::Free(typeless_); + RAPIDJSON_DELETE(ownAllocator_); } @@ -1432,7 +1435,7 @@ private: void CreateSchemaRecursive(const SchemaType** schema, const PointerType& pointer, const ValueType& v, const ValueType& document) { if (schema) - *schema = SchemaType::GetTypeless(); + *schema = typeless_; if (v.GetType() == kObjectType) { const SchemaType* s = GetSchema(pointer); @@ -1519,6 +1522,8 @@ private: return PointerType(); } + const SchemaType* GetTypeless() const { return typeless_; } + static const size_t kInitialSchemaMapSize = 64; static const size_t kInitialSchemaRefSize = 64; @@ -1526,6 +1531,7 @@ private: Allocator *allocator_; Allocator *ownAllocator_; const SchemaType* root_; //!< Root schema. + SchemaType* typeless_; internal::Stack schemaMap_; // Stores created Pointer -> Schemas internal::Stack schemaRef_; // Stores Pointer from $ref and schema which holds the $ref }; @@ -1832,8 +1838,8 @@ private: const SchemaType** sa = CurrentContext().patternPropertiesSchemas; typename Context::PatternValidatorType patternValidatorType = CurrentContext().valuePatternValidatorType; bool valueUniqueness = CurrentContext().valueUniqueness; - if (CurrentContext().valueSchema) - PushSchema(*CurrentContext().valueSchema); + RAPIDJSON_ASSERT(CurrentContext().valueSchema); + PushSchema(*CurrentContext().valueSchema); if (count > 0) { CurrentContext().objectPatternValidatorType = patternValidatorType; From 31ace3b7671bcde203315eee3f9b07724d2f1888 Mon Sep 17 00:00:00 2001 From: bluehero Date: Tue, 18 Oct 2016 12:54:42 +0800 Subject: [PATCH 148/305] use _mm_cmpistri --- include/rapidjson/reader.h | 26 ++++++-------------------- 1 file changed, 6 insertions(+), 20 deletions(-) diff --git a/include/rapidjson/reader.h b/include/rapidjson/reader.h index e53bbd2..a8cee36 100644 --- a/include/rapidjson/reader.h +++ b/include/rapidjson/reader.h @@ -299,16 +299,9 @@ inline const char *SkipWhitespace_SIMD(const char* p) { for (;; p += 16) { const __m128i s = _mm_load_si128(reinterpret_cast(p)); - const int r = _mm_cvtsi128_si32(_mm_cmpistrm(w, s, _SIDD_UBYTE_OPS | _SIDD_CMP_EQUAL_ANY | _SIDD_BIT_MASK | _SIDD_NEGATIVE_POLARITY)); - if (r != 0) { // some of characters is non-whitespace -#ifdef _MSC_VER // Find the index of first non-whitespace - unsigned long offset; - _BitScanForward(&offset, r); - return p + offset; -#else - return p + __builtin_ffs(r) - 1; -#endif - } + const int r = _mm_cmpistri(w, s, _SIDD_UBYTE_OPS | _SIDD_CMP_EQUAL_ANY | _SIDD_LEAST_SIGNIFICANT | _SIDD_NEGATIVE_POLARITY); + if (r != 16) // some of characters is non-whitespace + return p + r; } } @@ -325,16 +318,9 @@ inline const char *SkipWhitespace_SIMD(const char* p, const char* end) { for (; p <= end - 16; p += 16) { const __m128i s = _mm_loadu_si128(reinterpret_cast(p)); - const int r = _mm_cvtsi128_si32(_mm_cmpistrm(w, s, _SIDD_UBYTE_OPS | _SIDD_CMP_EQUAL_ANY | _SIDD_BIT_MASK | _SIDD_NEGATIVE_POLARITY)); - if (r != 0) { // some of characters is non-whitespace -#ifdef _MSC_VER // Find the index of first non-whitespace - unsigned long offset; - _BitScanForward(&offset, r); - return p + offset; -#else - return p + __builtin_ffs(r) - 1; -#endif - } + const int r = _mm_cmpistri(w, s, _SIDD_UBYTE_OPS | _SIDD_CMP_EQUAL_ANY | _SIDD_LEAST_SIGNIFICANT | _SIDD_NEGATIVE_POLARITY); + if (r != 16) // some of characters is non-whitespace + return p + r; } return SkipWhitespace(p, end); From bf0cc7bea8f9e8a744098d680bdf521e343dc4db Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Tue, 18 Oct 2016 13:53:00 +0800 Subject: [PATCH 149/305] Fixed a bug for SchemaDocument move constructor --- include/rapidjson/schema.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/include/rapidjson/schema.h b/include/rapidjson/schema.h index 6f1611f..178e91c 100644 --- a/include/rapidjson/schema.h +++ b/include/rapidjson/schema.h @@ -1339,6 +1339,7 @@ public: allocator_(allocator), ownAllocator_(), root_(), + typeless_(), schemaMap_(allocator, kInitialSchemaMapSize), schemaRef_(allocator, kInitialSchemaRefSize) { @@ -1398,8 +1399,10 @@ public: while (!schemaMap_.Empty()) schemaMap_.template Pop(1)->~SchemaEntry(); - typeless_->~SchemaType(); - Allocator::Free(typeless_); + if (typeless_) { + typeless_->~SchemaType(); + Allocator::Free(typeless_); + } RAPIDJSON_DELETE(ownAllocator_); } From 992b7f5f8edaa6dbcb6c298a2c4386356a3ecb4e Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Fri, 21 Oct 2016 12:25:37 +0800 Subject: [PATCH 150/305] Fix nullHandler allocation bug --- include/rapidjson/schema.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/rapidjson/schema.h b/include/rapidjson/schema.h index 178e91c..e7af3cf 100644 --- a/include/rapidjson/schema.h +++ b/include/rapidjson/schema.h @@ -1928,7 +1928,7 @@ private: const Context& CurrentContext() const { return *schemaStack_.template Top(); } OutputHandler& CreateNullHandler() { - return *(nullHandler_ = static_cast(stateAllocator_->Malloc(sizeof(OutputHandler)))); + return *(nullHandler_ = static_cast(GetStateAllocator().Malloc(sizeof(OutputHandler)))); } static const size_t kDefaultSchemaStackCapacity = 1024; From d7dd4106ea62e72c75988da821d0628e84a627b0 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Tue, 25 Oct 2016 18:21:01 +0800 Subject: [PATCH 151/305] Remove empty NumberStream::~NumberStream() Fix #781 --- include/rapidjson/reader.h | 3 --- 1 file changed, 3 deletions(-) diff --git a/include/rapidjson/reader.h b/include/rapidjson/reader.h index a8cee36..71916c0 100644 --- a/include/rapidjson/reader.h +++ b/include/rapidjson/reader.h @@ -1061,7 +1061,6 @@ private: typedef typename InputStream::Ch Ch; NumberStream(GenericReader& reader, InputStream& s) : is(s) { (void)reader; } - ~NumberStream() {} RAPIDJSON_FORCEINLINE Ch Peek() const { return is.Peek(); } RAPIDJSON_FORCEINLINE Ch TakePush() { return is.Take(); } @@ -1083,7 +1082,6 @@ private: typedef NumberStream Base; public: NumberStream(GenericReader& reader, InputStream& is) : Base(reader, is), stackStream(reader.stack_) {} - ~NumberStream() {} RAPIDJSON_FORCEINLINE Ch TakePush() { stackStream.Put(static_cast(Base::is.Peek())); @@ -1110,7 +1108,6 @@ private: typedef NumberStream Base; public: NumberStream(GenericReader& reader, InputStream& is) : Base(reader, is) {} - ~NumberStream() {} RAPIDJSON_FORCEINLINE Ch Take() { return Base::TakePush(); } }; From c4db88a3142548ea342e1fd971bf2a08aa70c7eb Mon Sep 17 00:00:00 2001 From: Wu Zhao Date: Wed, 26 Oct 2016 17:27:54 +0800 Subject: [PATCH 152/305] support IBM PowerPC / ppc64 / ppc64le and XL compiler Avoid POWER platform compiling error and support IBM XL C++ compiler on Linux / AIX. --- CMakeLists.txt | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ceda71b..8ccda4b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -50,7 +50,13 @@ if(CCACHE_FOUND) endif(CCACHE_FOUND) if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native -Wall -Wextra -Werror") + if(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "powerpc" OR ${CMAKE_SYSTEM_PROCESSOR} STREQUAL "ppc64" OR ${CMAKE_SYSTEM_PROCESSOR} STREQUAL "ppc64le") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mcpu=native") + else() + #FIXME: x86 is -march=native, but doesn't mean every arch is this option. To keep original project's compatibility, I leave this except POWER. + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native") + endif() + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Werror") if (RAPIDJSON_BUILD_CXX11) if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "4.7.0") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x") @@ -73,7 +79,13 @@ if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") endif() endif() elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native -Wall -Wextra -Werror -Wno-missing-field-initializers") + if(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "powerpc" OR ${CMAKE_SYSTEM_PROCESSOR} STREQUAL "ppc64" OR ${CMAKE_SYSTEM_PROCESSOR} STREQUAL "ppc64le") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mcpu=native") + else() + #FIXME: x86 is -march=native, but doesn't mean every arch is this option. To keep original project's compatibility, I leave this except POWER. + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native") + endif() + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Werror -Wno-missing-field-initializers") if (RAPIDJSON_BUILD_CXX11) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") endif() @@ -90,6 +102,8 @@ elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang") elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") add_definitions(-D_CRT_SECURE_NO_WARNINGS=1) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc") +elseif (CMAKE_CXX_COMPILER_ID MATCHES "XL") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -qarch=auto") endif() #add extra search paths for libraries and includes From 95b346c3ca6009a5d779533ae2d8d763ee9322d1 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Mon, 31 Oct 2016 18:24:17 +0800 Subject: [PATCH 153/305] Refactor GenericValue deep-clone constructor --- include/rapidjson/document.h | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h index 703c061..2da67a9 100644 --- a/include/rapidjson/document.h +++ b/include/rapidjson/document.h @@ -2419,11 +2419,28 @@ inline GenericValue::GenericValue(const GenericValue& rhs, Allocator& allocator) { switch (rhs.GetType()) { - case kObjectType: - case kArrayType: { // perform deep copy via SAX Handler - GenericDocument d(&allocator); - rhs.Accept(d); - RawAssign(*d.stack_.template Pop(1)); + case kObjectType: { + SizeType count = rhs.data_.o.size; + Member* lm = reinterpret_cast(allocator.Malloc(count * sizeof(Member))); + const typename GenericValue::Member* rm = rhs.GetMembersPointer(); + for (SizeType i = 0; i < count; i++) { + new (&lm[i].name) GenericValue(rm[i].name, allocator); + new (&lm[i].value) GenericValue(rm[i].value, allocator); + } + data_.f.flags = kObjectFlag; + data_.o.size = data_.o.capacity = count; + SetMembersPointer(lm); + } + break; + case kArrayType: { + SizeType count = rhs.data_.a.size; + GenericValue* le = reinterpret_cast(allocator.Malloc(count * sizeof(GenericValue))); + const GenericValue* re = rhs.GetElementsPointer(); + for (SizeType i = 0; i < count; i++) + new (&le[i]) GenericValue(re[i], allocator); + data_.f.flags = kArrayFlag; + data_.a.size = data_.a.capacity = count; + SetElementsPointer(le); } break; case kStringType: From e07d0e94380b41d9cf45734a6af1c6d603b0fc71 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Mon, 31 Oct 2016 18:28:53 +0800 Subject: [PATCH 154/305] Move GenericValue deep-clone constructor into the class declaration. --- include/rapidjson/document.h | 89 +++++++++++++++++------------------- 1 file changed, 41 insertions(+), 48 deletions(-) diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h index 2da67a9..895af88 100644 --- a/include/rapidjson/document.h +++ b/include/rapidjson/document.h @@ -617,8 +617,47 @@ public: \param allocator Allocator for allocating copied elements and buffers. Commonly use GenericDocument::GetAllocator(). \see CopyFrom() */ - template< typename SourceAllocator > - GenericValue(const GenericValue& rhs, Allocator & allocator); + template + GenericValue(const GenericValue& rhs, Allocator& allocator) { + switch (rhs.GetType()) { + case kObjectType: { + SizeType count = rhs.data_.o.size; + Member* lm = reinterpret_cast(allocator.Malloc(count * sizeof(Member))); + const typename GenericValue::Member* rm = rhs.GetMembersPointer(); + for (SizeType i = 0; i < count; i++) { + new (&lm[i].name) GenericValue(rm[i].name, allocator); + new (&lm[i].value) GenericValue(rm[i].value, allocator); + } + data_.f.flags = kObjectFlag; + data_.o.size = data_.o.capacity = count; + SetMembersPointer(lm); + } + break; + case kArrayType: { + SizeType count = rhs.data_.a.size; + GenericValue* le = reinterpret_cast(allocator.Malloc(count * sizeof(GenericValue))); + const GenericValue* re = rhs.GetElementsPointer(); + for (SizeType i = 0; i < count; i++) + new (&le[i]) GenericValue(re[i], allocator); + data_.f.flags = kArrayFlag; + data_.a.size = data_.a.capacity = count; + SetElementsPointer(le); + } + break; + case kStringType: + if (rhs.data_.f.flags == kConstStringFlag) { + data_.f.flags = rhs.data_.f.flags; + data_ = *reinterpret_cast(&rhs.data_); + } + else + SetStringRaw(StringRef(rhs.GetString(), rhs.GetStringLength()), allocator); + break; + default: + data_.f.flags = rhs.data_.f.flags; + data_ = *reinterpret_cast(&rhs.data_); + break; + } + } //! Constructor for boolean value. /*! \param b Boolean value @@ -2412,52 +2451,6 @@ private: //! GenericDocument with UTF8 encoding typedef GenericDocument > Document; -// defined here due to the dependency on GenericDocument -template -template -inline -GenericValue::GenericValue(const GenericValue& rhs, Allocator& allocator) -{ - switch (rhs.GetType()) { - case kObjectType: { - SizeType count = rhs.data_.o.size; - Member* lm = reinterpret_cast(allocator.Malloc(count * sizeof(Member))); - const typename GenericValue::Member* rm = rhs.GetMembersPointer(); - for (SizeType i = 0; i < count; i++) { - new (&lm[i].name) GenericValue(rm[i].name, allocator); - new (&lm[i].value) GenericValue(rm[i].value, allocator); - } - data_.f.flags = kObjectFlag; - data_.o.size = data_.o.capacity = count; - SetMembersPointer(lm); - } - break; - case kArrayType: { - SizeType count = rhs.data_.a.size; - GenericValue* le = reinterpret_cast(allocator.Malloc(count * sizeof(GenericValue))); - const GenericValue* re = rhs.GetElementsPointer(); - for (SizeType i = 0; i < count; i++) - new (&le[i]) GenericValue(re[i], allocator); - data_.f.flags = kArrayFlag; - data_.a.size = data_.a.capacity = count; - SetElementsPointer(le); - } - break; - case kStringType: - if (rhs.data_.f.flags == kConstStringFlag) { - data_.f.flags = rhs.data_.f.flags; - data_ = *reinterpret_cast(&rhs.data_); - } else { - SetStringRaw(StringRef(rhs.GetString(), rhs.GetStringLength()), allocator); - } - break; - default: - data_.f.flags = rhs.data_.f.flags; - data_ = *reinterpret_cast(&rhs.data_); - break; - } -} - //! Helper class for accessing Value of array type. /*! Instance of this helper class is obtained by \c GenericValue::GetArray(). From a077baa9c38a2d4cd95844a1f1ecd81cf1102752 Mon Sep 17 00:00:00 2001 From: SuperSodaSea Date: Sat, 5 Nov 2016 11:55:12 +0800 Subject: [PATCH 155/305] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=8B=BC=E5=86=99?= =?UTF-8?q?=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc/encoding.zh-cn.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/encoding.zh-cn.md b/doc/encoding.zh-cn.md index 6816923..808ba52 100644 --- a/doc/encoding.zh-cn.md +++ b/doc/encoding.zh-cn.md @@ -14,7 +14,7 @@ > (in §6) JSON may be represented using UTF-8, UTF-16, or UTF-32. When JSON is written in UTF-8, JSON is 8bit compatible. When JSON is written in UTF-16 or UTF-32, the binary content-transfer-encoding must be used. > -> 翻译:JSON 可使用 UTF-8、UTF-16 或 UTF-18 表示。当 JSON 以 UTF-8 写入,该 JSON 是 8 位兼容的。当 JSON 以 UTF-16 或 UTF-32 写入,就必须使用二进制的内容传送编码。 +> 翻译:JSON 可使用 UTF-8、UTF-16 或 UTF-32 表示。当 JSON 以 UTF-8 写入,该 JSON 是 8 位兼容的。当 JSON 以 UTF-16 或 UTF-32 写入,就必须使用二进制的内容传送编码。 RapidJSON 支持多种编码。它也能检查 JSON 的编码,以及在不同编码中进行转码。所有这些功能都是在内部实现,无需使用外部的程序库(如 [ICU](http://site.icu-project.org/))。 From bff326fb24fbfeba4bc8cb2f250687d4f6445604 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Mon, 21 Nov 2016 09:37:02 +0800 Subject: [PATCH 156/305] Update sax.md --- doc/sax.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/sax.md b/doc/sax.md index 1d4fc2a..ed6d46a 100644 --- a/doc/sax.md +++ b/doc/sax.md @@ -122,7 +122,7 @@ class Handler { When the `Reader` encounters a JSON number, it chooses a suitable C++ type mapping. And then it calls *one* function out of `Int(int)`, `Uint(unsigned)`, `Int64(int64_t)`, `Uint64(uint64_t)` and `Double(double)`. If `kParseNumbersAsStrings` is enabled, `Reader` will always calls `RawNumber()` instead. -`String(const char* str, SizeType length, bool copy)` is called when the `Reader` encounters a string. The first parameter is pointer to the string. The second parameter is the length of the string (excluding the null terminator). Note that RapidJSON supports null character `'\0'` inside a string. If such situation happens, `strlen(str) < length`. The last `copy` indicates whether the handler needs to make a copy of the string. For normal parsing, `copy = true`. Only when *insitu* parsing is used, `copy = false`. And beware that, the character type depends on the target encoding, which will be explained later. +`String(const char* str, SizeType length, bool copy)` is called when the `Reader` encounters a string. The first parameter is pointer to the string. The second parameter is the length of the string (excluding the null terminator). Note that RapidJSON supports null character `\0` inside a string. If such situation happens, `strlen(str) < length`. The last `copy` indicates whether the handler needs to make a copy of the string. For normal parsing, `copy = true`. Only when *insitu* parsing is used, `copy = false`. And beware that, the character type depends on the target encoding, which will be explained later. When the `Reader` encounters the beginning of an object, it calls `StartObject()`. An object in JSON is a set of name-value pairs. If the object contains members it first calls `Key()` for the name of member, and then calls functions depending on the type of the value. These calls of name-value pairs repeats until calling `EndObject(SizeType memberCount)`. Note that the `memberCount` parameter is just an aid for the handler, user may not need this parameter. From 0024592c239d99b605f9e04a7516d43d3c176f79 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Mon, 21 Nov 2016 09:50:59 +0800 Subject: [PATCH 157/305] Update sax.zh-cn.md --- doc/sax.zh-cn.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/sax.zh-cn.md b/doc/sax.zh-cn.md index b20286d..740c339 100644 --- a/doc/sax.zh-cn.md +++ b/doc/sax.zh-cn.md @@ -122,7 +122,7 @@ class Handler { 当 `Reader` 遇到 JSON number,它会选择一个合适的 C++ 类型映射,然后调用 `Int(int)`、`Uint(unsigned)`、`Int64(int64_t)`、`Uint64(uint64_t)` 及 `Double(double)` 的 * 其中之一个 *。 若开启了 `kParseNumbersAsStrings` 选项,`Reader` 便会改为调用 `RawNumber()`。 -当 `Reader` 遇到 JSON string,它会调用 `String(const char* str, SizeType length, bool copy)`。第一个参数是字符串的指针。第二个参数是字符串的长度(不包含空终止符号)。注意 RapidJSON 支持字串中含有空字符 `'\0'`。若出现这种情况,便会有 `strlen(str) < length`。最后的 `copy` 参数表示处理器是否需要复制该字符串。在正常解析时,`copy = true`。仅当使用原位解析时,`copy = false`。此外,还要注意字符的类型与目标编码相关,我们稍后会再谈这一点。 +当 `Reader` 遇到 JSON string,它会调用 `String(const char* str, SizeType length, bool copy)`。第一个参数是字符串的指针。第二个参数是字符串的长度(不包含空终止符号)。注意 RapidJSON 支持字串中含有空字符 `\0`。若出现这种情况,便会有 `strlen(str) < length`。最后的 `copy` 参数表示处理器是否需要复制该字符串。在正常解析时,`copy = true`。仅当使用原位解析时,`copy = false`。此外,还要注意字符的类型与目标编码相关,我们稍后会再谈这一点。 当 `Reader` 遇到 JSON object 的开始之时,它会调用 `StartObject()`。JSON 的 object 是一个键值对(成员)的集合。若 object 包含成员,它会先为成员的名字调用 `Key()`,然后再按值的类型调用函数。它不断调用这些键值对,直至最终调用 `EndObject(SizeType memberCount)`。注意 `memberCount` 参数对处理器来说只是协助性质,使用者可能不需要此参数。 From ba34c94533b67d81bef4f7eb80d941a768c2496b Mon Sep 17 00:00:00 2001 From: Ivan Kravets Date: Mon, 28 Nov 2016 12:53:24 +0200 Subject: [PATCH 158/305] Update version to 1.1.0 --- library.json | Bin 313 -> 355 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/library.json b/library.json index 47fd352ac7efa35c343de9a2d74ee55ff19ba2eb..21d6bcecf22fd12342f560eae0c540daa713003d 100644 GIT binary patch delta 51 zcmdnV^q6Uaj6hjxQE_H|o|2V Date: Wed, 21 Dec 2016 10:17:25 +0300 Subject: [PATCH 159/305] - replaced RAPIDJSON_NEW macro with variadic varient --- include/rapidjson/allocators.h | 2 +- include/rapidjson/document.h | 4 ++-- include/rapidjson/internal/regex.h | 2 +- include/rapidjson/internal/stack.h | 2 +- include/rapidjson/pointer.h | 4 ++-- include/rapidjson/rapidjson.h | 2 +- include/rapidjson/schema.h | 4 ++-- 7 files changed, 10 insertions(+), 10 deletions(-) diff --git a/include/rapidjson/allocators.h b/include/rapidjson/allocators.h index 98affe0..6405bc3 100644 --- a/include/rapidjson/allocators.h +++ b/include/rapidjson/allocators.h @@ -236,7 +236,7 @@ private: */ bool AddChunk(size_t capacity) { if (!baseAllocator_) - ownBaseAllocator_ = baseAllocator_ = RAPIDJSON_NEW(BaseAllocator()); + ownBaseAllocator_ = baseAllocator_ = RAPIDJSON_NEW(BaseAllocator); if (ChunkHeader* chunk = reinterpret_cast(baseAllocator_->Malloc(RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + capacity))) { chunk->capacity = capacity; chunk->size = 0; diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h index 895af88..5822acc 100644 --- a/include/rapidjson/document.h +++ b/include/rapidjson/document.h @@ -2088,7 +2088,7 @@ public: GenericValue(type), allocator_(allocator), ownAllocator_(0), stack_(stackAllocator, stackCapacity), parseResult_() { if (!allocator_) - ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator()); + ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator); } //! Constructor @@ -2101,7 +2101,7 @@ public: allocator_(allocator), ownAllocator_(0), stack_(stackAllocator, stackCapacity), parseResult_() { if (!allocator_) - ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator()); + ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator); } #if RAPIDJSON_HAS_CXX11_RVALUE_REFS diff --git a/include/rapidjson/internal/regex.h b/include/rapidjson/internal/regex.h index 8530cd7..5001364 100644 --- a/include/rapidjson/internal/regex.h +++ b/include/rapidjson/internal/regex.h @@ -606,7 +606,7 @@ public: { RAPIDJSON_ASSERT(regex_.IsValid()); if (!allocator_) - ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator()); + ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator); stateSet_ = static_cast(allocator_->Malloc(GetStateSetSize())); state0_.template Reserve(regex_.stateCount_); state1_.template Reserve(regex_.stateCount_); diff --git a/include/rapidjson/internal/stack.h b/include/rapidjson/internal/stack.h index 022c9aa..a1b4568 100644 --- a/include/rapidjson/internal/stack.h +++ b/include/rapidjson/internal/stack.h @@ -183,7 +183,7 @@ private: size_t newCapacity; if (stack_ == 0) { if (!allocator_) - ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator()); + ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator); newCapacity = initialCapacity_; } else { newCapacity = GetCapacity(); diff --git a/include/rapidjson/pointer.h b/include/rapidjson/pointer.h index 0206ac1..eab6619 100644 --- a/include/rapidjson/pointer.h +++ b/include/rapidjson/pointer.h @@ -758,7 +758,7 @@ private: */ Ch* CopyFromRaw(const GenericPointer& rhs, size_t extraToken = 0, size_t extraNameBufferSize = 0) { if (!allocator_) // allocator is independently owned. - ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator()); + ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator); size_t nameBufferSize = rhs.tokenCount_; // null terminators for tokens for (Token *t = rhs.tokens_; t != rhs.tokens_ + rhs.tokenCount_; ++t) @@ -806,7 +806,7 @@ private: // Create own allocator if user did not supply. if (!allocator_) - ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator()); + ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator); // Count number of '/' as tokenCount tokenCount_ = 0; diff --git a/include/rapidjson/rapidjson.h b/include/rapidjson/rapidjson.h index 053b2ce..77611ce 100644 --- a/include/rapidjson/rapidjson.h +++ b/include/rapidjson/rapidjson.h @@ -583,7 +583,7 @@ RAPIDJSON_NAMESPACE_END #ifndef RAPIDJSON_NEW ///! customization point for global \c new -#define RAPIDJSON_NEW(x) new x +#define RAPIDJSON_NEW(type, ...) new type(__VA_ARGS__) #endif #ifndef RAPIDJSON_DELETE ///! customization point for global \c delete diff --git a/include/rapidjson/schema.h b/include/rapidjson/schema.h index e7af3cf..a99f1e8 100644 --- a/include/rapidjson/schema.h +++ b/include/rapidjson/schema.h @@ -1344,7 +1344,7 @@ public: schemaRef_(allocator, kInitialSchemaRefSize) { if (!allocator_) - ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator()); + ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator); typeless_ = static_cast(allocator_->Malloc(sizeof(SchemaType))); new (typeless_) SchemaType(this, PointerType(), ValueType(kObjectType).Move(), ValueType(kObjectType).Move(), 0); @@ -1823,7 +1823,7 @@ private: StateAllocator& GetStateAllocator() { if (!stateAllocator_) - stateAllocator_ = ownStateAllocator_ = RAPIDJSON_NEW(StateAllocator()); + stateAllocator_ = ownStateAllocator_ = RAPIDJSON_NEW(StateAllocator); return *stateAllocator_; } From 3f120caeef7362ae0b4f219c5c6cafb5d074d698 Mon Sep 17 00:00:00 2001 From: Andrey Glebov Date: Wed, 21 Dec 2016 10:41:06 +0300 Subject: [PATCH 160/305] - replaced RAPIDJSON_NEW calls in fwdtest.cpp --- test/unittest/fwdtest.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/test/unittest/fwdtest.cpp b/test/unittest/fwdtest.cpp index 4f32684..b9d18e8 100644 --- a/test/unittest/fwdtest.cpp +++ b/test/unittest/fwdtest.cpp @@ -118,23 +118,23 @@ Foo::Foo() : memorypoolallocator(RAPIDJSON_NEW(MemoryPoolAllocator<>)), // stream.h - stringstream(RAPIDJSON_NEW(StringStream(0))), - insitustringstream(RAPIDJSON_NEW(InsituStringStream(0))), + stringstream(RAPIDJSON_NEW(StringStream, NULL)), + insitustringstream(RAPIDJSON_NEW(InsituStringStream, NULL)), // stringbuffer.h stringbuffer(RAPIDJSON_NEW(StringBuffer)), // // filereadstream.h - // filereadstream(RAPIDJSON_NEW(FileReadStream(stdout, buffer, sizeof(buffer)))), + // filereadstream(RAPIDJSON_NEW(FileReadStream, stdout, buffer, sizeof(buffer))), // // filewritestream.h - // filewritestream(RAPIDJSON_NEW(FileWriteStream(stdout, buffer, sizeof(buffer)))), + // filewritestream(RAPIDJSON_NEW(FileWriteStream, stdout, buffer, sizeof(buffer))), // memorybuffer.h memorybuffer(RAPIDJSON_NEW(MemoryBuffer)), // memorystream.h - memorystream(RAPIDJSON_NEW(MemoryStream(0, 0))), + memorystream(RAPIDJSON_NEW(MemoryStream, NULL, 0)), // reader.h basereaderhandler(RAPIDJSON_NEW((BaseReaderHandler, void>))), @@ -154,8 +154,8 @@ Foo::Foo() : pointer(RAPIDJSON_NEW(Pointer)), // schema.h - schemadocument(RAPIDJSON_NEW(SchemaDocument(*document))), - schemavalidator(RAPIDJSON_NEW(SchemaValidator(*schemadocument))) + schemadocument(RAPIDJSON_NEW(SchemaDocument, *document)), + schemavalidator(RAPIDJSON_NEW(SchemaValidator, *schemadocument)) { } From 41ceb8624f2fb46fe5e62b7d2cce471c17db7a5f Mon Sep 17 00:00:00 2001 From: Andrey Glebov Date: Wed, 21 Dec 2016 14:03:53 +0300 Subject: [PATCH 161/305] - replaced RAPIDJSON_NEW with C++98 compatible version --- include/rapidjson/allocators.h | 2 +- include/rapidjson/document.h | 4 ++-- include/rapidjson/internal/regex.h | 2 +- include/rapidjson/internal/stack.h | 2 +- include/rapidjson/pointer.h | 4 ++-- include/rapidjson/rapidjson.h | 2 +- include/rapidjson/schema.h | 4 ++-- test/unittest/fwdtest.cpp | 25 ++++++++++++++----------- 8 files changed, 24 insertions(+), 21 deletions(-) diff --git a/include/rapidjson/allocators.h b/include/rapidjson/allocators.h index 6405bc3..655f4a3 100644 --- a/include/rapidjson/allocators.h +++ b/include/rapidjson/allocators.h @@ -236,7 +236,7 @@ private: */ bool AddChunk(size_t capacity) { if (!baseAllocator_) - ownBaseAllocator_ = baseAllocator_ = RAPIDJSON_NEW(BaseAllocator); + ownBaseAllocator_ = baseAllocator_ = RAPIDJSON_NEW(BaseAllocator)(); if (ChunkHeader* chunk = reinterpret_cast(baseAllocator_->Malloc(RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + capacity))) { chunk->capacity = capacity; chunk->size = 0; diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h index 5822acc..3873b99 100644 --- a/include/rapidjson/document.h +++ b/include/rapidjson/document.h @@ -2088,7 +2088,7 @@ public: GenericValue(type), allocator_(allocator), ownAllocator_(0), stack_(stackAllocator, stackCapacity), parseResult_() { if (!allocator_) - ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator); + ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator)(); } //! Constructor @@ -2101,7 +2101,7 @@ public: allocator_(allocator), ownAllocator_(0), stack_(stackAllocator, stackCapacity), parseResult_() { if (!allocator_) - ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator); + ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator)(); } #if RAPIDJSON_HAS_CXX11_RVALUE_REFS diff --git a/include/rapidjson/internal/regex.h b/include/rapidjson/internal/regex.h index 5001364..936b714 100644 --- a/include/rapidjson/internal/regex.h +++ b/include/rapidjson/internal/regex.h @@ -606,7 +606,7 @@ public: { RAPIDJSON_ASSERT(regex_.IsValid()); if (!allocator_) - ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator); + ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator)(); stateSet_ = static_cast(allocator_->Malloc(GetStateSetSize())); state0_.template Reserve(regex_.stateCount_); state1_.template Reserve(regex_.stateCount_); diff --git a/include/rapidjson/internal/stack.h b/include/rapidjson/internal/stack.h index a1b4568..299e651 100644 --- a/include/rapidjson/internal/stack.h +++ b/include/rapidjson/internal/stack.h @@ -183,7 +183,7 @@ private: size_t newCapacity; if (stack_ == 0) { if (!allocator_) - ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator); + ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator)(); newCapacity = initialCapacity_; } else { newCapacity = GetCapacity(); diff --git a/include/rapidjson/pointer.h b/include/rapidjson/pointer.h index eab6619..4d6391f 100644 --- a/include/rapidjson/pointer.h +++ b/include/rapidjson/pointer.h @@ -758,7 +758,7 @@ private: */ Ch* CopyFromRaw(const GenericPointer& rhs, size_t extraToken = 0, size_t extraNameBufferSize = 0) { if (!allocator_) // allocator is independently owned. - ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator); + ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator)(); size_t nameBufferSize = rhs.tokenCount_; // null terminators for tokens for (Token *t = rhs.tokens_; t != rhs.tokens_ + rhs.tokenCount_; ++t) @@ -806,7 +806,7 @@ private: // Create own allocator if user did not supply. if (!allocator_) - ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator); + ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator)(); // Count number of '/' as tokenCount tokenCount_ = 0; diff --git a/include/rapidjson/rapidjson.h b/include/rapidjson/rapidjson.h index 77611ce..a005257 100644 --- a/include/rapidjson/rapidjson.h +++ b/include/rapidjson/rapidjson.h @@ -583,7 +583,7 @@ RAPIDJSON_NAMESPACE_END #ifndef RAPIDJSON_NEW ///! customization point for global \c new -#define RAPIDJSON_NEW(type, ...) new type(__VA_ARGS__) +#define RAPIDJSON_NEW(TypeName) new TypeName #endif #ifndef RAPIDJSON_DELETE ///! customization point for global \c delete diff --git a/include/rapidjson/schema.h b/include/rapidjson/schema.h index a99f1e8..4760d1b 100644 --- a/include/rapidjson/schema.h +++ b/include/rapidjson/schema.h @@ -1344,7 +1344,7 @@ public: schemaRef_(allocator, kInitialSchemaRefSize) { if (!allocator_) - ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator); + ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator)(); typeless_ = static_cast(allocator_->Malloc(sizeof(SchemaType))); new (typeless_) SchemaType(this, PointerType(), ValueType(kObjectType).Move(), ValueType(kObjectType).Move(), 0); @@ -1823,7 +1823,7 @@ private: StateAllocator& GetStateAllocator() { if (!stateAllocator_) - stateAllocator_ = ownStateAllocator_ = RAPIDJSON_NEW(StateAllocator); + stateAllocator_ = ownStateAllocator_ = RAPIDJSON_NEW(StateAllocator)(); return *stateAllocator_; } diff --git a/test/unittest/fwdtest.cpp b/test/unittest/fwdtest.cpp index b9d18e8..1936d97 100644 --- a/test/unittest/fwdtest.cpp +++ b/test/unittest/fwdtest.cpp @@ -100,6 +100,9 @@ struct Foo { #include "rapidjson/prettywriter.h" #include "rapidjson/schema.h" // -> pointer.h +typedef Transcoder, UTF8<> > TranscoderUtf8ToUtf8; +typedef BaseReaderHandler, void> BaseReaderHandlerUtf8Void; + Foo::Foo() : // encodings.h utf8(RAPIDJSON_NEW(UTF8<>)), @@ -111,40 +114,40 @@ Foo::Foo() : utf32le(RAPIDJSON_NEW(UTF32LE<>)), ascii(RAPIDJSON_NEW(ASCII<>)), autoutf(RAPIDJSON_NEW(AutoUTF)), - transcoder(RAPIDJSON_NEW((Transcoder, UTF8<> >))), + transcoder(RAPIDJSON_NEW(TranscoderUtf8ToUtf8)), // allocators.h crtallocator(RAPIDJSON_NEW(CrtAllocator)), memorypoolallocator(RAPIDJSON_NEW(MemoryPoolAllocator<>)), // stream.h - stringstream(RAPIDJSON_NEW(StringStream, NULL)), - insitustringstream(RAPIDJSON_NEW(InsituStringStream, NULL)), + stringstream(RAPIDJSON_NEW(StringStream)(NULL)), + insitustringstream(RAPIDJSON_NEW(InsituStringStream)(NULL)), // stringbuffer.h stringbuffer(RAPIDJSON_NEW(StringBuffer)), // // filereadstream.h - // filereadstream(RAPIDJSON_NEW(FileReadStream, stdout, buffer, sizeof(buffer))), + // filereadstream(RAPIDJSON_NEW(FileReadStream)(stdout, buffer, sizeof(buffer))), // // filewritestream.h - // filewritestream(RAPIDJSON_NEW(FileWriteStream, stdout, buffer, sizeof(buffer))), + // filewritestream(RAPIDJSON_NEW(FileWriteStream)(stdout, buffer, sizeof(buffer))), // memorybuffer.h memorybuffer(RAPIDJSON_NEW(MemoryBuffer)), // memorystream.h - memorystream(RAPIDJSON_NEW(MemoryStream, NULL, 0)), + memorystream(RAPIDJSON_NEW(MemoryStream)(NULL, 0)), // reader.h - basereaderhandler(RAPIDJSON_NEW((BaseReaderHandler, void>))), + basereaderhandler(RAPIDJSON_NEW(BaseReaderHandlerUtf8Void)), reader(RAPIDJSON_NEW(Reader)), // writer.h - writer(RAPIDJSON_NEW((Writer))), + writer(RAPIDJSON_NEW(Writer)), // prettywriter.h - prettywriter(RAPIDJSON_NEW((PrettyWriter))), + prettywriter(RAPIDJSON_NEW(PrettyWriter)), // document.h value(RAPIDJSON_NEW(Value)), @@ -154,8 +157,8 @@ Foo::Foo() : pointer(RAPIDJSON_NEW(Pointer)), // schema.h - schemadocument(RAPIDJSON_NEW(SchemaDocument, *document)), - schemavalidator(RAPIDJSON_NEW(SchemaValidator, *schemadocument)) + schemadocument(RAPIDJSON_NEW(SchemaDocument)(*document)), + schemavalidator(RAPIDJSON_NEW(SchemaValidator)(*schemadocument)) { } From af4ec9b7e92e1378ff18aa393e6a5dc7f440420a Mon Sep 17 00:00:00 2001 From: SuperSodaSea Date: Fri, 30 Dec 2016 23:12:41 +0800 Subject: [PATCH 162/305] Translate doc/internals.md Part 1 --- doc/internals.zh-cn.md | 365 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 365 insertions(+) create mode 100644 doc/internals.zh-cn.md diff --git a/doc/internals.zh-cn.md b/doc/internals.zh-cn.md new file mode 100644 index 0000000..f30c569 --- /dev/null +++ b/doc/internals.zh-cn.md @@ -0,0 +1,365 @@ +# 内部架构 + +本部分记录了一些设计和实现细节。 + +[TOC] + +# 架构 {#Architecture} + +## SAX 和 DOM + +下面的 UML 图显示了 SAX 和 DOM 的基本关系。 + +![架构 UML 类图](diagram/architecture.png) + +关系的核心是 `Handler` 概念。在 SAX 一边,`Reader` 从流解析 JSON 并将事件发送到 `Handler`。`Writer` 实现了 `Handler` 概念,用于处理相同的事件。在 DOM 一边,`Document` 实现了 `Handler` 概念,用于通过这些时间来构建 DOM。`Value` 支持了 `Value::Accept(Handler&)` 函数,它可以将 DOM 转换为事件进行发送。 + +在这个设计,SAX 是不依赖于 DOM 的。甚至 `Reader` 和 `Writer` 之间也没有依赖。这提供了连接事件发送器和处理器的灵活性。除此之外,`Value` 也是不依赖于 SAX 的。所以,除了将 DOM 序列化为 JSON 之外,用户也可以将其序列化为 XML,或者做任何其他事情。 + +## 工具类 + +SAX 和 DOM API 都依赖于3个额外的概念:`Allocator`、`Encoding` 和 `Stream`。它们的继承层次结构如下图所示。 + +![工具类 UML 类图](diagram/utilityclass.png) + +# 值(Value) {#Value} + +`Value` (实际上被定义为 `GenericValue>`)是 DOM API 的核心。本部分描述了它的设计。 + +## 数据布局 {#DataLayout} + +`Value` 是[可变类型](http://en.wikipedia.org/wiki/Variant_type)。在 RapidJSON 的上下文中,一个 `Value` 的实例可以包含6种 JSON 数据类型之一。通过使用 `union` ,这是可能实现的。每一个 `Value` 包含两个成员:`union Data data_` 和 `unsigned flags_`。`flags_` 表明了 JSON 类型,以及附加的信息。 + +下表显示了所有类型的数据布局。32位/64位列表明了字段所占用的字节数。 + +| Null | | 32位 | 64位 | +|-------------------|----------------------------------|:----:|:----:| +| (未使用) | |4 |8 | +| (未使用) | |4 |4 | +| (未使用) | |4 |4 | +| `unsigned flags_` | `kNullType kNullFlag` |4 |4 | + +| Bool | | 32位 | 64位 | +|-------------------|----------------------------------------------------|:----:|:----:| +| (未使用) | |4 |8 | +| (未使用) | |4 |4 | +| (未使用) | |4 |4 | +| `unsigned flags_` | `kBoolType` (either `kTrueFlag` or `kFalseFlag`) |4 |4 | + +| String | | 32位 | 64位 | +|---------------------|-------------------------------------|:----:|:----:| +| `Ch* str` | 指向字符串的指针(可能拥有所有权) |4 |8 | +| `SizeType length` | 字符串长度 |4 |4 | +| (未使用) | |4 |4 | +| `unsigned flags_` | `kStringType kStringFlag ...` |4 |4 | + +| Object | | 32位 | 64位 | +|---------------------|-------------------------------------|:----:|:----:| +| `Member* members` | 指向成员数组的指针(拥有所有权) |4 |8 | +| `SizeType size` | 成员数量 |4 |4 | +| `SizeType capacity` | 成员容量 |4 |4 | +| `unsigned flags_` | `kObjectType kObjectFlag` |4 |4 | + +| Array | | 32位 | 64位 | +|---------------------|-------------------------------------|:----:|:----:| +| `Value* values` | 指向值数组的指针(拥有所有权) |4 |8 | +| `SizeType size` | 值数量 |4 |4 | +| `SizeType capacity` | 值容量 |4 |4 | +| `unsigned flags_` | `kArrayType kArrayFlag` |4 |4 | + +| Number (Int) | | 32位 | 64位 | +|---------------------|-------------------------------------|:----:|:----:| +| `int i` | 32位有符号整数 |4 |4 | +| (零填充) | 0 |4 |4 | +| (未使用) | |4 |8 | +| `unsigned flags_` | `kNumberType kNumberFlag kIntFlag kInt64Flag ...` |4 |4 | + +| Number (UInt) | | 32位 | 64位 | +|---------------------|-------------------------------------|:----:|:----:| +| `unsigned u` | 32位无符号整数 |4 |4 | +| (零填充) | 0 |4 |4 | +| (未使用) | |4 |8 | +| `unsigned flags_` | `kNumberType kNumberFlag kUIntFlag kUInt64Flag ...` |4 |4 | + +| Number (Int64) | | 32位 | 64位 | +|---------------------|-------------------------------------|:----:|:----:| +| `int64_t i64` | 64位有符号整数 |8 |8 | +| (未使用) | |4 |8 | +| `unsigned flags_` | `kNumberType kNumberFlag kInt64Flag ...` |4 |4 | + +| Number (Uint64) | | 32位 | 64位 | +|---------------------|-------------------------------------|:----:|:----:| +| `uint64_t i64` | 64位无符号整数 |8 |8 | +| (未使用) | |4 |8 | +| `unsigned flags_` | `kNumberType kNumberFlag kInt64Flag ...` |4 |4 | + +| Number (Double) | | 32位 | 64位 | +|---------------------|-------------------------------------|:----:|:----:| +| `uint64_t i64` | 双精度浮点数 |8 |8 | +| (未使用) | |4 |8 | +| `unsigned flags_` |`kNumberType kNumberFlag kDoubleFlag`|4 |4 | + +这里有一些需要注意的地方: +* 为了减少在64位架构上的内存消耗,`SizeType` 被定义为 `unsigned` 而不是 `size_t`。 +* 32位整数的零填充可能被放在实际类型的前面或后面,这依赖于字节序。这使得它可以将32位整数不经过任何转换就可以解释为64位整数。 +* `Int` 永远是 `Int64`,反之不然。 + +## 标志 {#Flags} + +32位的 `flags_` 包含了 JSON 类型和其他信息。如前文中的表所述,每一种 JSON 类型包含了冗余的 `kXXXType` 和 `kXXXFlag`。这个设计是为了优化测试位标志(`IsNumber()`)和获取每一种类型的序列号(`GetType()`)。 + +字符串有两个可选的标志。`kCopyFlag` 表明这个字符串拥有字符串拷贝的所有权。而 `kInlineStrFlag` 意味着使用了[短字符串优化](#ShortString)。 + +数字更加复杂一些。对于普通的整数值,它可以包含 `kIntFlag`、`kUintFlag`、 `kInt64Flag` 和/或 `kUint64Flag`,这由整数的范围决定。带有小数或者超过64位所能表达的范围的整数的数字会被存储为带有 `kDoubleFlag` 的 `double`。 + +## 短字符串优化 {#ShortString} + +[Kosta](https://github.com/Kosta-Github) 提供了很棒的短字符串优化。这个优化的xxx如下所述。除去 `flags_` ,`Value` 有12或16字节(对于32位或64位)来存储实际的数据。这为在其内部直接存储短字符串而不是存储字符串的指针创造了可能。对于1字节的字符类型(例如 `char`),它可以在 `Value` 类型内部存储至多11或15个字符的字符串。 + +|ShortString (Ch=char)| | 32位 | 64位 | +|---------------------|-------------------------------------|:----:|:----:| +| `Ch str[MaxChars]` | 字符串缓冲区 |11 |15 | +| `Ch invLength` | MaxChars - Length |1 |1 | +| `unsigned flags_` | `kStringType kStringFlag ...` |4 |4 | + +这里使用了一项特殊的技术。它存储了 (MaxChars - length) 而不直接存储字符串的长度。这使得存储11个字符并且带有后缀 `\0` 成为可能。 + +这个优化可以减少字符串拷贝内存占用。它也改善了缓存一致性,并进一步提高了运行时性能。 + +# 分配器(Allocator) {#InternalAllocator} + +`Allocator` 是 RapidJSON 中的概念: +~~~cpp +concept Allocator { + static const bool kNeedFree; //!< 表明这个分配器是否需要调用 Free()。 + + // 申请内存块。 + // \param size 内存块的大小,以字节记。 + // \returns 指向内存块的指针。 + void* Malloc(size_t size); + + // 调整内存块的大小。 + // \param originalPtr 当前内存块的指针。空指针是被允许的。 + // \param originalSize 当前大小,以字节记。(设计问题:因为有些分配器可能不会记录它,显示的传递它可以节约内存。) + // \param newSize 新大小,以字节记。 + void* Realloc(void* originalPtr, size_t originalSize, size_t newSize); + + // 释放内存块。 + // \param ptr 指向内存块的指针。空指针是被允许的。 + static void Free(void *ptr); +}; +~~~ + +需要注意的是 `Malloc()` 和 `Realloc()` 是成员函数而 `Free()` 是静态成员函数。 + +## MemoryPoolAllocator {#MemoryPoolAllocator} + +`MemoryPoolAllocator` 是 DOM 的默认内存分配器。它只申请内存而不释放内存。这对于构建 DOM 树非常合适。 + +在它的内部,它从基础的内存分配器申请内存块(默认为 `CrtAllocator`)并将这些内存块存储为单向链表。当用户请求申请内存,它会遵循下列步骤来申请内存: + +1. 如果可用,使用用户提供的缓冲区。(见 [User Buffer section in DOM](doc/dom.md)) +2. 如果用户提供的缓冲区已满,使用当前内存块。 +3. 如果当前内存块已满,申请新的内存块。 + +# 解析优化 {#ParsingOptimization} + +## 使用 SIMD 跳过空格 {#SkipwhitespaceWithSIMD} + +当从流中解析 JSON 时,解析器需要跳过4种空格字符: + +1. 空格 (`U+0020`) +2. 制表符 (`U+000B`) +3. 换行 (`U+000A`) +4. 回车 (`U+000D`) + +这是一份简单的实现: +~~~cpp +void SkipWhitespace(InputStream& s) { + while (s.Peek() == ' ' || s.Peek() == '\n' || s.Peek() == '\r' || s.Peek() == '\t') + s.Take(); +} +~~~ + +但是,这需要对每个字符进行4次比较以及一些分支。这被发现是一个热点。 + +为了加速这一处理,RapidJSON 使用 SIMD 来在一次迭代中比较16个字符和4个空格。目前 RapidJSON 只支持 SSE2 和 SSE4.2 指令。同时它也只会对 UTF-8 内存流启用,包括字符串流或 *原位* 解析。 + +你可以通过在包含 `rapidjson.h` 之前定义 `RAPIDJSON_SSE2` 或 `RAPIDJSON_SSE42` 来启用这个优化。一些编译器可以检测这个设置,如 `perftest.h`: + +~~~cpp +// __SSE2__ 和 __SSE4_2__ 可被 gcc、clang 和 Intel 编译器识别: +// 如果支持的话,我们在 gmake 中使用了 -march=native 来启用 -msse2 和 -msse4.2 +#if defined(__SSE4_2__) +# define RAPIDJSON_SSE42 +#elif defined(__SSE2__) +# define RAPIDJSON_SSE2 +#endif +~~~ + +需要注意的是,这是编译期的设置。在不支持这些指令的机器上运行可执行文件会使它崩溃。 + +### Page boundary issue + +In an early version of RapidJSON, [an issue](https://code.google.com/archive/p/rapidjson/issues/104) reported that the `SkipWhitespace_SIMD()` causes crash very rarely (around 1 in 500,000). After investigation, it is suspected that `_mm_loadu_si128()` accessed bytes after `'\0'`, and across a protected page boundary. + +In [Intel® 64 and IA-32 Architectures Optimization Reference Manual +](http://www.intel.com/content/www/us/en/architecture-and-technology/64-ia-32-architectures-optimization-manual.html), section 10.2.1: + +> To support algorithms requiring unaligned 128-bit SIMD memory accesses, memory buffer allocation by a caller function should consider adding some pad space so that a callee function can safely use the address pointer safely with unaligned 128-bit SIMD memory operations. +> The minimal padding size should be the width of the SIMD register that might be used in conjunction with unaligned SIMD memory access. + +This is not feasible as RapidJSON should not enforce such requirement. + +To fix this issue, currently the routine process bytes up to the next aligned address. After tha, use aligned read to perform SIMD processing. Also see [#85](https://github.com/miloyip/rapidjson/issues/85). + +## Local Stream Copy {#LocalStreamCopy} + +During optimization, it is found that some compilers cannot localize some member data access of streams into local variables or registers. Experimental results show that for some stream types, making a copy of the stream and used it in inner-loop can improve performance. For example, the actual (non-SIMD) implementation of `SkipWhitespace()` is implemented as: + +~~~cpp +template +void SkipWhitespace(InputStream& is) { + internal::StreamLocalCopy copy(is); + InputStream& s(copy.s); + + while (s.Peek() == ' ' || s.Peek() == '\n' || s.Peek() == '\r' || s.Peek() == '\t') + s.Take(); +} +~~~ + +Depending on the traits of stream, `StreamLocalCopy` will make (or not make) a copy of the stream object, use it locally and copy the states of stream back to the original stream. + +## Parsing to Double {#ParsingDouble} + +Parsing string into `double` is difficult. The standard library function `strtod()` can do the job but it is slow. By default, the parsers use normal precision setting. This has has maximum 3 [ULP](http://en.wikipedia.org/wiki/Unit_in_the_last_place) error and implemented in `internal::StrtodNormalPrecision()`. + +When using `kParseFullPrecisionFlag`, the parsers calls `internal::StrtodFullPrecision()` instead, and this function actually implemented 3 versions of conversion methods. +1. [Fast-Path](http://www.exploringbinary.com/fast-path-decimal-to-floating-point-conversion/). +2. Custom DIY-FP implementation as in [double-conversion](https://github.com/floitsch/double-conversion). +3. Big Integer Method as in (Clinger, William D. How to read floating point numbers accurately. Vol. 25. No. 6. ACM, 1990). + +If the first conversion methods fail, it will try the second, and so on. + +# Generation Optimization {#GenerationOptimization} + +## Integer-to-String conversion {#itoa} + +The naive algorithm for integer-to-string conversion involves division per each decimal digit. We have implemented various implementations and evaluated them in [itoa-benchmark](https://github.com/miloyip/itoa-benchmark). + +Although SSE2 version is the fastest but the difference is minor by comparing to the first running-up `branchlut`. And `branchlut` is pure C++ implementation so we adopt `branchlut` in RapidJSON. + +## Double-to-String conversion {#dtoa} + +Originally RapidJSON uses `snprintf(..., ..., "%g")` to achieve double-to-string conversion. This is not accurate as the default precision is 6. Later we also find that this is slow and there is an alternative. + +Google's V8 [double-conversion](https://github.com/floitsch/double-conversion +) implemented a newer, fast algorithm called Grisu3 (Loitsch, Florian. "Printing floating-point numbers quickly and accurately with integers." ACM Sigplan Notices 45.6 (2010): 233-243.). + +However, since it is not header-only so that we implemented a header-only version of Grisu2. This algorithm guarantees that the result is always accurate. And in most of cases it produces the shortest (optimal) string representation. + +The header-only conversion function has been evaluated in [dtoa-benchmark](https://github.com/miloyip/dtoa-benchmark). + +# Parser {#Parser} + +## Iterative Parser {#IterativeParser} + +The iterative parser is a recursive descent LL(1) parser +implemented in a non-recursive manner. + +### Grammar {#IterativeParserGrammar} + +The grammar used for this parser is based on strict JSON syntax: +~~~~~~~~~~ +S -> array | object +array -> [ values ] +object -> { members } +values -> non-empty-values | ε +non-empty-values -> value addition-values +addition-values -> ε | , non-empty-values +members -> non-empty-members | ε +non-empty-members -> member addition-members +addition-members -> ε | , non-empty-members +member -> STRING : value +value -> STRING | NUMBER | NULL | BOOLEAN | object | array +~~~~~~~~~~ + +Note that left factoring is applied to non-terminals `values` and `members` +to make the grammar be LL(1). + +### Parsing Table {#IterativeParserParsingTable} + +Based on the grammar, we can construct the FIRST and FOLLOW set. + +The FIRST set of non-terminals is listed below: + +| NON-TERMINAL | FIRST | +|:-----------------:|:--------------------------------:| +| array | [ | +| object | { | +| values | ε STRING NUMBER NULL BOOLEAN { [ | +| addition-values | ε COMMA | +| members | ε STRING | +| addition-members | ε COMMA | +| member | STRING | +| value | STRING NUMBER NULL BOOLEAN { [ | +| S | [ { | +| non-empty-members | STRING | +| non-empty-values | STRING NUMBER NULL BOOLEAN { [ | + +The FOLLOW set is listed below: + +| NON-TERMINAL | FOLLOW | +|:-----------------:|:-------:| +| S | $ | +| array | , $ } ] | +| object | , $ } ] | +| values | ] | +| non-empty-values | ] | +| addition-values | ] | +| members | } | +| non-empty-members | } | +| addition-members | } | +| member | , } | +| value | , } ] | + +Finally the parsing table can be constructed from FIRST and FOLLOW set: + +| NON-TERMINAL | [ | { | , | : | ] | } | STRING | NUMBER | NULL | BOOLEAN | +|:-----------------:|:---------------------:|:---------------------:|:-------------------:|:-:|:-:|:-:|:-----------------------:|:---------------------:|:---------------------:|:---------------------:| +| S | array | object | | | | | | | | | +| array | [ values ] | | | | | | | | | | +| object | | { members } | | | | | | | | | +| values | non-empty-values | non-empty-values | | | ε | | non-empty-values | non-empty-values | non-empty-values | non-empty-values | +| non-empty-values | value addition-values | value addition-values | | | | | value addition-values | value addition-values | value addition-values | value addition-values | +| addition-values | | | , non-empty-values | | ε | | | | | | +| members | | | | | | ε | non-empty-members | | | | +| non-empty-members | | | | | | | member addition-members | | | | +| addition-members | | | , non-empty-members | | | ε | | | | | +| member | | | | | | | STRING : value | | | | +| value | array | object | | | | | STRING | NUMBER | NULL | BOOLEAN | + +There is a great [tool](http://hackingoff.com/compilers/predict-first-follow-set) for above grammar analysis. + +### Implementation {#IterativeParserImplementation} + +Based on the parsing table, a direct(or conventional) implementation +that pushes the production body in reverse order +while generating a production could work. + +In RapidJSON, several modifications(or adaptations to current design) are made to a direct implementation. + +First, the parsing table is encoded in a state machine in RapidJSON. +States are constructed by the head and body of production. +State transitions are constructed by production rules. +Besides, extra states are added for productions involved with `array` and `object`. +In this way the generation of array values or object members would be a single state transition, +rather than several pop/push operations in the direct implementation. +This also makes the estimation of stack size more easier. + +The state diagram is shown as follows: + +![State Diagram](diagram/iterative-parser-states-diagram.png) + +Second, the iterative parser also keeps track of array's value count and object's member count +in its internal stack, which may be different from a conventional implementation. From dba9816009916e05675d9e504fd4231fbc846382 Mon Sep 17 00:00:00 2001 From: SuperSodaSea Date: Sat, 31 Dec 2016 11:23:05 +0800 Subject: [PATCH 163/305] Translate doc/internals.md Part 2 --- doc/internals.zh-cn.md | 111 ++++++++++++++++++++--------------------- 1 file changed, 53 insertions(+), 58 deletions(-) diff --git a/doc/internals.zh-cn.md b/doc/internals.zh-cn.md index f30c569..ec57959 100644 --- a/doc/internals.zh-cn.md +++ b/doc/internals.zh-cn.md @@ -199,23 +199,23 @@ void SkipWhitespace(InputStream& s) { 需要注意的是,这是编译期的设置。在不支持这些指令的机器上运行可执行文件会使它崩溃。 -### Page boundary issue +### 页面对齐问题 -In an early version of RapidJSON, [an issue](https://code.google.com/archive/p/rapidjson/issues/104) reported that the `SkipWhitespace_SIMD()` causes crash very rarely (around 1 in 500,000). After investigation, it is suspected that `_mm_loadu_si128()` accessed bytes after `'\0'`, and across a protected page boundary. +在 RapidJSON 的早期版本中,被报告了[一个问题](https://code.google.com/archive/p/rapidjson/issues/104):`SkipWhitespace_SIMD()` 会罕见地导致崩溃(约五十万分之一的几率)。在调查之后,怀疑是 `_mm_loadu_si128()` 访问了 `'\0'` 之后的内存,并越过被保护的页面边界。 -In [Intel® 64 and IA-32 Architectures Optimization Reference Manual -](http://www.intel.com/content/www/us/en/architecture-and-technology/64-ia-32-architectures-optimization-manual.html), section 10.2.1: +在 [Intel® 64 and IA-32 Architectures Optimization Reference Manual +](http://www.intel.com/content/www/us/en/architecture-and-technology/64-ia-32-architectures-optimization-manual.html) 中,章节 10.2.1: -> To support algorithms requiring unaligned 128-bit SIMD memory accesses, memory buffer allocation by a caller function should consider adding some pad space so that a callee function can safely use the address pointer safely with unaligned 128-bit SIMD memory operations. -> The minimal padding size should be the width of the SIMD register that might be used in conjunction with unaligned SIMD memory access. +> 为了支持需要费对齐的128位 SIMD 内存访问的算法,调用者的内存缓冲区申请应当考虑添加一些填充空间,这样被调用的函数可以安全地将地址指针用于未对齐的128位 SIMD 内存操作。 +> 在结合非对齐的 SIMD 内存操作中,最小的对齐大小应该等于 SIMD 寄存器的大小。 -This is not feasible as RapidJSON should not enforce such requirement. +对于 RapidJSON 来说,这显然是不可行的,因为 RapidJSON 不应当强迫用户进行内存对齐。 -To fix this issue, currently the routine process bytes up to the next aligned address. After tha, use aligned read to perform SIMD processing. Also see [#85](https://github.com/miloyip/rapidjson/issues/85). +为了修复这个问题,当前的代码会先按字节处理直到下一个对齐的地址。在这之后,使用对齐读取来进行 SIMD 处理。见 [#85](https://github.com/miloyip/rapidjson/issues/85)。 -## Local Stream Copy {#LocalStreamCopy} +## 局部流拷贝 {#LocalStreamCopy} -During optimization, it is found that some compilers cannot localize some member data access of streams into local variables or registers. Experimental results show that for some stream types, making a copy of the stream and used it in inner-loop can improve performance. For example, the actual (non-SIMD) implementation of `SkipWhitespace()` is implemented as: +在优化的过程中,我们发现一些编译器不能将访问流的一些成员数据放入局部变量或者寄存器中。测试结果显示,对于一些流类型,创建流的拷贝并将其用于内层循环中可以改善性能。例如,实际(非 SIMD)的 `SkipWhitespace()` 被实现为: ~~~cpp template @@ -228,48 +228,47 @@ void SkipWhitespace(InputStream& is) { } ~~~ -Depending on the traits of stream, `StreamLocalCopy` will make (or not make) a copy of the stream object, use it locally and copy the states of stream back to the original stream. +基于流的特征,`StreamLocalCopy` 会创建(或不创建)流对象的拷贝,在局部使用它并将流的状态拷贝回原来的流。 -## Parsing to Double {#ParsingDouble} +## 解析为双精度浮点数 {#ParsingDouble} -Parsing string into `double` is difficult. The standard library function `strtod()` can do the job but it is slow. By default, the parsers use normal precision setting. This has has maximum 3 [ULP](http://en.wikipedia.org/wiki/Unit_in_the_last_place) error and implemented in `internal::StrtodNormalPrecision()`. +将字符串解析为 `double` 并不简单。标准库函数 `strtod()` 可以胜任这项工作,但它比较缓慢。默认情况下,解析器使用默认的精度设置。这最多有 3[ULP](http://en.wikipedia.org/wiki/Unit_in_the_last_place) 的误差,并实现在 `internal::StrtodNormalPrecision()` 中。 -When using `kParseFullPrecisionFlag`, the parsers calls `internal::StrtodFullPrecision()` instead, and this function actually implemented 3 versions of conversion methods. -1. [Fast-Path](http://www.exploringbinary.com/fast-path-decimal-to-floating-point-conversion/). -2. Custom DIY-FP implementation as in [double-conversion](https://github.com/floitsch/double-conversion). -3. Big Integer Method as in (Clinger, William D. How to read floating point numbers accurately. Vol. 25. No. 6. ACM, 1990). +当使用 `kParseFullPrecisionFlag` 时,编译器会改为调用 `internal::StrtodFullPrecision()` ,这个函数会自动调用三个版本的转换。 +1. [Fast-Path](http://www.exploringbinary.com/fast-path-decimal-to-floating-point-conversion/)。 +2. [double-conversion](https://github.com/floitsch/double-conversion) 中的自定义 DIY-FP 实现。 +3. (Clinger, William D. How to read floating point numbers accurately. Vol. 25. No. 6. ACM, 1990) 中的大整数算法。 -If the first conversion methods fail, it will try the second, and so on. +如果第一个转换方法失败,则尝试使用第二种方法,以此类推。 -# Generation Optimization {#GenerationOptimization} +# 生成优化 {#GenerationOptimization} -## Integer-to-String conversion {#itoa} +## 整数到字符串的转换 {#itoa} -The naive algorithm for integer-to-string conversion involves division per each decimal digit. We have implemented various implementations and evaluated them in [itoa-benchmark](https://github.com/miloyip/itoa-benchmark). +整数到字符串转换的朴素算法需要对每一个十进制位进行一次处罚。我们实现了若干版本并在 [itoa-benchmark](https://github.com/miloyip/itoa-benchmark) 中对它们进行了评估。 -Although SSE2 version is the fastest but the difference is minor by comparing to the first running-up `branchlut`. And `branchlut` is pure C++ implementation so we adopt `branchlut` in RapidJSON. +虽然 SSE2 版本是最快的,但它和第二快的 `branchlut` 差距不大。而且 `branchlut` 是纯C++实现,所以我们在 RapidJSON 中使用了 `branchlut`。 -## Double-to-String conversion {#dtoa} +## 双精度浮点数到字符串的转换 {#dtoa} -Originally RapidJSON uses `snprintf(..., ..., "%g")` to achieve double-to-string conversion. This is not accurate as the default precision is 6. Later we also find that this is slow and there is an alternative. +原来 RapidJSON 使用 `snprintf(..., ..., "%g")` 来进行双精度浮点数到字符串的转换。这是不准确的,因为默认的精度是6。随后我们发现它很缓慢,而且有其它的替代品。 -Google's V8 [double-conversion](https://github.com/floitsch/double-conversion -) implemented a newer, fast algorithm called Grisu3 (Loitsch, Florian. "Printing floating-point numbers quickly and accurately with integers." ACM Sigplan Notices 45.6 (2010): 233-243.). +Google 的 V8 [double-conversion](https://github.com/floitsch/double-conversion +) 实现了更新的、快速的被称为 Grisu3 的算法(Loitsch, Florian. "Printing floating-point numbers quickly and accurately with integers." ACM Sigplan Notices 45.6 (2010): 233-243.)。 -However, since it is not header-only so that we implemented a header-only version of Grisu2. This algorithm guarantees that the result is always accurate. And in most of cases it produces the shortest (optimal) string representation. +然而,这个实现不是仅头文件的,所以我们实现了一个仅头文件的 Grisu2 版本。这个算法保证了结果永远精确。而且在大多数情况下,它会生成最短的(可选)字符串表示。 -The header-only conversion function has been evaluated in [dtoa-benchmark](https://github.com/miloyip/dtoa-benchmark). +这个仅头文件的转换函数在 [dtoa-benchmark](https://github.com/miloyip/dtoa-benchmark) 中进行评估。 -# Parser {#Parser} +# 解析器 {#Parser} -## Iterative Parser {#IterativeParser} +## 迭代解析 {#IterativeParser} -The iterative parser is a recursive descent LL(1) parser -implemented in a non-recursive manner. +迭代解析器是一个以非递归方式实现的递归下降的 LL(1) 解析器。 -### Grammar {#IterativeParserGrammar} +### 语法 {#IterativeParserGrammar} -The grammar used for this parser is based on strict JSON syntax: +解析器使用的语法是基于严格 JSON 语法的: ~~~~~~~~~~ S -> array | object array -> [ values ] @@ -284,14 +283,13 @@ member -> STRING : value value -> STRING | NUMBER | NULL | BOOLEAN | object | array ~~~~~~~~~~ -Note that left factoring is applied to non-terminals `values` and `members` -to make the grammar be LL(1). +注意到左因子被加入了非终结符的 `values` 和 `members` 来保证语法是 LL(1) 的。 -### Parsing Table {#IterativeParserParsingTable} +### 解析表 {#IterativeParserParsingTable} -Based on the grammar, we can construct the FIRST and FOLLOW set. +基于这份语法,我们可以构造 FIRST 和 FOLLOW 集合。 -The FIRST set of non-terminals is listed below: +非终结符的 FIRST 集合如下所示: | NON-TERMINAL | FIRST | |:-----------------:|:--------------------------------:| @@ -307,7 +305,7 @@ The FIRST set of non-terminals is listed below: | non-empty-members | STRING | | non-empty-values | STRING NUMBER NULL BOOLEAN { [ | -The FOLLOW set is listed below: +FOLLOW 集合如下所示: | NON-TERMINAL | FOLLOW | |:-----------------:|:-------:| @@ -323,7 +321,7 @@ The FOLLOW set is listed below: | member | , } | | value | , } ] | -Finally the parsing table can be constructed from FIRST and FOLLOW set: +最终可以从 FIRST 和 FOLLOW 集合生成解析表: | NON-TERMINAL | [ | { | , | : | ] | } | STRING | NUMBER | NULL | BOOLEAN | |:-----------------:|:---------------------:|:---------------------:|:-------------------:|:-:|:-:|:-:|:-----------------------:|:---------------------:|:---------------------:|:---------------------:| @@ -339,27 +337,24 @@ Finally the parsing table can be constructed from FIRST and FOLLOW set: | member | | | | | | | STRING : value | | | | | value | array | object | | | | | STRING | NUMBER | NULL | BOOLEAN | -There is a great [tool](http://hackingoff.com/compilers/predict-first-follow-set) for above grammar analysis. +对于上面的语法分析,这里有一个很棒的[工具](http://hackingoff.com/compilers/predict-first-follow-set)。 -### Implementation {#IterativeParserImplementation} +### 实现 {#IterativeParserImplementation} -Based on the parsing table, a direct(or conventional) implementation -that pushes the production body in reverse order -while generating a production could work. +基于这份解析表,一个直接的(常规的)将规则反向入栈的实现可以正常工作。 -In RapidJSON, several modifications(or adaptations to current design) are made to a direct implementation. +在 RapidJSON 中,对直接的实现进行了一些修改: -First, the parsing table is encoded in a state machine in RapidJSON. -States are constructed by the head and body of production. -State transitions are constructed by production rules. -Besides, extra states are added for productions involved with `array` and `object`. -In this way the generation of array values or object members would be a single state transition, -rather than several pop/push operations in the direct implementation. -This also makes the estimation of stack size more easier. +首先,在 RapidJSON 中,这份解析表被编码为状态机。 +规则由头部和主体组成。 +状态转换由规则构造。 +除此之外,额外的状态被添加到与 `array` 和 `object` 有关的规则。 +通过这种方式,生成数组值或对象成员可以只用一次状态转移便可完成, +而不需要在直接的实现中的多次出栈/入栈操作。 +这也使得估计栈的大小更加容易。 -The state diagram is shown as follows: +状态图如如下所示: -![State Diagram](diagram/iterative-parser-states-diagram.png) +![状态图](diagram/iterative-parser-states-diagram.png) -Second, the iterative parser also keeps track of array's value count and object's member count -in its internal stack, which may be different from a conventional implementation. +第二,迭代解析器也在内部栈保存了数组的值个数和对象成员的数量,这也与传统的实现不同。 From 835f2f4a79768137ed1d3f262bc02f3aced05ff9 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Sat, 31 Dec 2016 14:51:07 +0800 Subject: [PATCH 164/305] Update Doxyfile.zh-cn.in Change internals from English to Chinese --- doc/Doxyfile.zh-cn.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/Doxyfile.zh-cn.in b/doc/Doxyfile.zh-cn.in index 87dd866..e7fffa6 100644 --- a/doc/Doxyfile.zh-cn.in +++ b/doc/Doxyfile.zh-cn.in @@ -777,7 +777,7 @@ INPUT = readme.zh-cn.md \ doc/sax.zh-cn.md \ doc/schema.zh-cn.md \ doc/performance.zh-cn.md \ - doc/internals.md \ + doc/internals.zh-cn.md \ doc/faq.zh-cn.md # This tag can be used to specify the character encoding of the source files From 3cc77d5d635c2411f327cc4f262f37abb66ff43c Mon Sep 17 00:00:00 2001 From: Zhihao Yuan Date: Wed, 18 Jan 2017 16:16:07 -0600 Subject: [PATCH 165/305] Treat signed-unsigned conversions as errors. --- CMakeLists.txt | 2 ++ example/CMakeLists.txt | 5 ++--- include/rapidjson/encodedstream.h | 2 +- include/rapidjson/encodings.h | 6 +++--- include/rapidjson/internal/dtoa.h | 8 ++++---- include/rapidjson/internal/ieee754.h | 4 ++-- include/rapidjson/internal/regex.h | 4 ++-- include/rapidjson/internal/strtod.h | 14 +++++++------- include/rapidjson/istreamwrapper.h | 2 +- include/rapidjson/pointer.h | 6 +++--- include/rapidjson/schema.h | 2 +- include/rapidjson/writer.h | 2 +- test/perftest/CMakeLists.txt | 2 ++ test/unittest/CMakeLists.txt | 7 +++---- test/unittest/encodingstest.cpp | 2 +- test/unittest/itoatest.cpp | 4 ++-- 16 files changed, 37 insertions(+), 35 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8ccda4b..9257926 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -57,6 +57,7 @@ if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native") endif() set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Werror") + set(EXTRA_CXX_FLAGS -Weffc++ -Wswitch-default -Wfloat-equal -Wconversion -Wsign-conversion) if (RAPIDJSON_BUILD_CXX11) if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "4.7.0") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x") @@ -86,6 +87,7 @@ elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native") endif() set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Werror -Wno-missing-field-initializers") + set(EXTRA_CXX_FLAGS -Weffc++ -Wswitch-default -Wfloat-equal -Wconversion -Wimplicit-fallthrough -Weverything) if (RAPIDJSON_BUILD_CXX11) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") endif() diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index 4d448cc..bec6a8c 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -24,11 +24,10 @@ set(EXAMPLES include_directories("../include/") add_definitions(-D__STDC_FORMAT_MACROS) +set_property(DIRECTORY PROPERTY COMPILE_OPTIONS ${EXTRA_CXX_FLAGS}) if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread -Werror -Wall -Wextra -Weffc++ -Wswitch-default") -elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror -Wall -Wextra -Weffc++ -Wswitch-default -Wfloat-equal -Wimplicit-fallthrough -Weverything") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread") endif() foreach (example ${EXAMPLES}) diff --git a/include/rapidjson/encodedstream.h b/include/rapidjson/encodedstream.h index 1450683..223601c 100644 --- a/include/rapidjson/encodedstream.h +++ b/include/rapidjson/encodedstream.h @@ -200,7 +200,7 @@ private: // xx xx xx xx UTF-8 if (!hasBOM_) { - unsigned pattern = (c[0] ? 1 : 0) | (c[1] ? 2 : 0) | (c[2] ? 4 : 0) | (c[3] ? 8 : 0); + int pattern = (c[0] ? 1 : 0) | (c[1] ? 2 : 0) | (c[2] ? 4 : 0) | (c[3] ? 8 : 0); switch (pattern) { case 0x08: type_ = kUTF32BE; break; case 0x0A: type_ = kUTF16BE; break; diff --git a/include/rapidjson/encodings.h b/include/rapidjson/encodings.h index baa7c2b..ed7d44d 100644 --- a/include/rapidjson/encodings.h +++ b/include/rapidjson/encodings.h @@ -157,7 +157,7 @@ struct UTF8 { if (type >= 32) { *codepoint = 0; } else { - *codepoint = (0xFF >> type) & static_cast(c); + *codepoint = (0xFFu >> type) & static_cast(c); } bool result = true; switch (type) { @@ -283,7 +283,7 @@ struct UTF16 { RAPIDJSON_ASSERT(codepoint <= 0x10FFFF); unsigned v = codepoint - 0x10000; os.Put(static_cast((v >> 10) | 0xD800)); - os.Put((v & 0x3FF) | 0xDC00); + os.Put(static_cast((v & 0x3FF) | 0xDC00)); } } @@ -299,7 +299,7 @@ struct UTF16 { RAPIDJSON_ASSERT(codepoint <= 0x10FFFF); unsigned v = codepoint - 0x10000; PutUnsafe(os, static_cast((v >> 10) | 0xD800)); - PutUnsafe(os, (v & 0x3FF) | 0xDC00); + PutUnsafe(os, static_cast((v & 0x3FF) | 0xDC00)); } } diff --git a/include/rapidjson/internal/dtoa.h b/include/rapidjson/internal/dtoa.h index 8d6350e..bf2e9b2 100644 --- a/include/rapidjson/internal/dtoa.h +++ b/include/rapidjson/internal/dtoa.h @@ -41,7 +41,7 @@ inline void GrisuRound(char* buffer, int len, uint64_t delta, uint64_t rest, uin } } -inline unsigned CountDecimalDigit32(uint32_t n) { +inline int CountDecimalDigit32(uint32_t n) { // Simple pure C++ implementation was faster than __builtin_clz version in this situation. if (n < 10) return 1; if (n < 100) return 2; @@ -63,7 +63,7 @@ inline void DigitGen(const DiyFp& W, const DiyFp& Mp, uint64_t delta, char* buff const DiyFp wp_w = Mp - W; uint32_t p1 = static_cast(Mp.f >> -one.e); uint64_t p2 = Mp.f & (one.f - 1); - unsigned kappa = CountDecimalDigit32(p1); // kappa in [0, 9] + int kappa = CountDecimalDigit32(p1); // kappa in [0, 9] *len = 0; while (kappa > 0) { @@ -102,8 +102,8 @@ inline void DigitGen(const DiyFp& W, const DiyFp& Mp, uint64_t delta, char* buff kappa--; if (p2 < delta) { *K += kappa; - int index = -static_cast(kappa); - GrisuRound(buffer, *len, delta, p2, one.f, wp_w.f * (index < 9 ? kPow10[-static_cast(kappa)] : 0)); + int index = -kappa; + GrisuRound(buffer, *len, delta, p2, one.f, wp_w.f * (index < 9 ? kPow10[index] : 0)); return; } } diff --git a/include/rapidjson/internal/ieee754.h b/include/rapidjson/internal/ieee754.h index 82bb0b9..c2684ba 100644 --- a/include/rapidjson/internal/ieee754.h +++ b/include/rapidjson/internal/ieee754.h @@ -48,13 +48,13 @@ public: int IntegerExponent() const { return (IsNormal() ? Exponent() : kDenormalExponent) - kSignificandSize; } uint64_t ToBias() const { return (u_ & kSignMask) ? ~u_ + 1 : u_ | kSignMask; } - static unsigned EffectiveSignificandSize(int order) { + static int EffectiveSignificandSize(int order) { if (order >= -1021) return 53; else if (order <= -1074) return 0; else - return static_cast(order) + 1074; + return order + 1074; } private: diff --git a/include/rapidjson/internal/regex.h b/include/rapidjson/internal/regex.h index 936b714..1369ea2 100644 --- a/include/rapidjson/internal/regex.h +++ b/include/rapidjson/internal/regex.h @@ -688,8 +688,8 @@ private: bool matched = AddState(l, s.out); return AddState(l, s.out1) || matched; } - else if (!(stateSet_[index >> 5] & (1 << (index & 31)))) { - stateSet_[index >> 5] |= (1 << (index & 31)); + else if (!(stateSet_[index >> 5] & (1u << (index & 31)))) { + stateSet_[index >> 5] |= (1u << (index & 31)); *l.template PushUnsafe() = index; } return s.out == kRegexInvalidState; // by using PushUnsafe() above, we can ensure s is not validated due to reallocation. diff --git a/include/rapidjson/internal/strtod.h b/include/rapidjson/internal/strtod.h index 289c413..adf49e3 100644 --- a/include/rapidjson/internal/strtod.h +++ b/include/rapidjson/internal/strtod.h @@ -140,8 +140,8 @@ inline bool StrtodDiyFp(const char* decimals, size_t length, size_t decimalPosit significand++; size_t remaining = length - i; - const unsigned kUlpShift = 3; - const unsigned kUlp = 1 << kUlpShift; + const int kUlpShift = 3; + const int kUlp = 1 << kUlpShift; int64_t error = (remaining == 0) ? 0 : kUlp / 2; DiyFp v(significand, 0); @@ -177,17 +177,17 @@ inline bool StrtodDiyFp(const char* decimals, size_t length, size_t decimalPosit v = v.Normalize(); error <<= oldExp - v.e; - const unsigned effectiveSignificandSize = Double::EffectiveSignificandSize(64 + v.e); - unsigned precisionSize = 64 - effectiveSignificandSize; + const int effectiveSignificandSize = Double::EffectiveSignificandSize(64 + v.e); + int precisionSize = 64 - effectiveSignificandSize; if (precisionSize + kUlpShift >= 64) { - unsigned scaleExp = (precisionSize + kUlpShift) - 63; + int scaleExp = (precisionSize + kUlpShift) - 63; v.f >>= scaleExp; v.e += scaleExp; - error = (error >> scaleExp) + 1 + static_cast(kUlp); + error = (error >> scaleExp) + 1 + kUlp; precisionSize -= scaleExp; } - DiyFp rounded(v.f >> precisionSize, v.e + static_cast(precisionSize)); + DiyFp rounded(v.f >> precisionSize, v.e + precisionSize); const uint64_t precisionBits = (v.f & ((uint64_t(1) << precisionSize) - 1)) * kUlp; const uint64_t halfWay = (uint64_t(1) << (precisionSize - 1)) * kUlp; if (precisionBits >= halfWay + static_cast(error)) { diff --git a/include/rapidjson/istreamwrapper.h b/include/rapidjson/istreamwrapper.h index f5fe289..8639c8c 100644 --- a/include/rapidjson/istreamwrapper.h +++ b/include/rapidjson/istreamwrapper.h @@ -54,7 +54,7 @@ public: Ch Peek() const { typename StreamType::int_type c = stream_.peek(); - return RAPIDJSON_LIKELY(c != StreamType::traits_type::eof()) ? static_cast(c) : '\0'; + return RAPIDJSON_LIKELY(c != StreamType::traits_type::eof()) ? static_cast(c) : static_cast('\0'); } Ch Take() { diff --git a/include/rapidjson/pointer.h b/include/rapidjson/pointer.h index 4d6391f..bc7acfd 100644 --- a/include/rapidjson/pointer.h +++ b/include/rapidjson/pointer.h @@ -274,7 +274,7 @@ public: else { Ch name[21]; for (size_t i = 0; i <= length; i++) - name[i] = buffer[i]; + name[i] = static_cast(buffer[i]); Token token = { name, length, index }; return Append(token, allocator); } @@ -1029,8 +1029,8 @@ private: unsigned char u = static_cast(c); static const char hexDigits[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; os_.Put('%'); - os_.Put(hexDigits[u >> 4]); - os_.Put(hexDigits[u & 15]); + os_.Put(static_cast(hexDigits[u >> 4])); + os_.Put(static_cast(hexDigits[u & 15])); } private: OutputStream& os_; diff --git a/include/rapidjson/schema.h b/include/rapidjson/schema.h index 4760d1b..3f81d9b 100644 --- a/include/rapidjson/schema.h +++ b/include/rapidjson/schema.h @@ -1263,7 +1263,7 @@ struct TokenHelper { char buffer[21]; size_t length = static_cast((sizeof(SizeType) == 4 ? u32toa(index, buffer) : u64toa(index, buffer)) - buffer); for (size_t i = 0; i < length; i++) - *documentStack.template Push() = buffer[i]; + *documentStack.template Push() = static_cast(buffer[i]); } }; diff --git a/include/rapidjson/writer.h b/include/rapidjson/writer.h index 8f6e174..874c555 100644 --- a/include/rapidjson/writer.h +++ b/include/rapidjson/writer.h @@ -352,7 +352,7 @@ protected: char* end = internal::dtoa(d, buffer, maxDecimalPlaces_); PutReserve(*os_, static_cast(end - buffer)); for (char* p = buffer; p != end; ++p) - PutUnsafe(*os_, static_cast(*p)); + PutUnsafe(*os_, static_cast(*p)); return true; } diff --git a/test/perftest/CMakeLists.txt b/test/perftest/CMakeLists.txt index c33aae4..035e544 100644 --- a/test/perftest/CMakeLists.txt +++ b/test/perftest/CMakeLists.txt @@ -19,6 +19,8 @@ if(CCACHE_FOUND) endif() endif(CCACHE_FOUND) +set_property(DIRECTORY PROPERTY COMPILE_OPTIONS ${EXTRA_CXX_FLAGS}) + IF(NOT (CMAKE_BUILD_TYPE STREQUAL "Debug")) add_test(NAME perftest COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/perftest diff --git a/test/unittest/CMakeLists.txt b/test/unittest/CMakeLists.txt index b3204d6..4e29765 100644 --- a/test/unittest/CMakeLists.txt +++ b/test/unittest/CMakeLists.txt @@ -36,10 +36,9 @@ if(CCACHE_FOUND) endif() endif(CCACHE_FOUND) -if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror -Wall -Wextra -Weffc++ -Wswitch-default -Wfloat-equal") -elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror -Wall -Wextra -Weffc++ -Wswitch-default -Wfloat-equal -Wimplicit-fallthrough -Weverything") +set_property(DIRECTORY PROPERTY COMPILE_OPTIONS ${EXTRA_CXX_FLAGS}) + +if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") # If the user is running a newer version of Clang that includes the # -Wdouble-promotion, we will ignore that warning. if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 3.7) diff --git a/test/unittest/encodingstest.cpp b/test/unittest/encodingstest.cpp index 67b0391..82cf777 100644 --- a/test/unittest/encodingstest.cpp +++ b/test/unittest/encodingstest.cpp @@ -267,7 +267,7 @@ static unsigned inline decode(unsigned* state, unsigned* codep, unsigned byte) { *codep = (*state != UTF8_ACCEPT) ? (byte & 0x3fu) | (*codep << 6) : - (0xff >> type) & (byte); + (0xffu >> type) & (byte); *state = utf8d[256 + *state + type]; return *state; diff --git a/test/unittest/itoatest.cpp b/test/unittest/itoatest.cpp index b752a6a..2f66bed 100644 --- a/test/unittest/itoatest.cpp +++ b/test/unittest/itoatest.cpp @@ -74,7 +74,7 @@ static void Verify(void(*f)(T, char*), char* (*g)(T, char*)) { VerifyValue(std::numeric_limits::max(), f, g); // 2^n - 1, 2^n, 10^n - 1, 10^n until overflow - for (uint32_t power = 2; power <= 10; power += 8) { + for (int power = 2; power <= 10; power += 8) { T i = 1, last; do { VerifyValue(i - 1, f, g); @@ -86,7 +86,7 @@ static void Verify(void(*f)(T, char*), char* (*g)(T, char*)) { last = i; if (i > static_cast(std::numeric_limits::max() / static_cast(power))) break; - i *= power; + i *= static_cast(power); } while (last < i); } } From 265fb6ee8814e81eba849581f676da29b30fb6b9 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Tue, 24 Jan 2017 09:28:52 +0800 Subject: [PATCH 166/305] Fix #831 RAPIDJSON_HAS_CXX11_RANGE_FOR is error defined --- include/rapidjson/rapidjson.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/rapidjson/rapidjson.h b/include/rapidjson/rapidjson.h index a005257..e667d8b 100644 --- a/include/rapidjson/rapidjson.h +++ b/include/rapidjson/rapidjson.h @@ -568,7 +568,7 @@ RAPIDJSON_NAMESPACE_END #ifndef RAPIDJSON_HAS_CXX11_RANGE_FOR #if defined(__clang__) #define RAPIDJSON_HAS_CXX11_RANGE_FOR __has_feature(cxx_range_for) -#elif (defined(RAPIDJSON_GNUC) && (RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4,3,0)) && defined(__GXX_EXPERIMENTAL_CXX0X__)) || \ +#elif (defined(RAPIDJSON_GNUC) && (RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4,6,0)) && defined(__GXX_EXPERIMENTAL_CXX0X__)) || \ (defined(_MSC_VER) && _MSC_VER >= 1700) #define RAPIDJSON_HAS_CXX11_RANGE_FOR 1 #else From 3693e942b72831c82a100ede5f6fbe20dabcdeb7 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Tue, 24 Jan 2017 14:54:50 +0800 Subject: [PATCH 167/305] Fix output character type in writers --- include/rapidjson/prettywriter.h | 2 +- include/rapidjson/writer.h | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/include/rapidjson/prettywriter.h b/include/rapidjson/prettywriter.h index abd964f..d663208 100644 --- a/include/rapidjson/prettywriter.h +++ b/include/rapidjson/prettywriter.h @@ -249,7 +249,7 @@ protected: void WriteIndent() { size_t count = (Base::level_stack_.GetSize() / sizeof(typename Base::Level)) * indentCharCount_; - PutN(*Base::os_, static_cast(indentChar_), count); + PutN(*Base::os_, static_cast(indentChar_), count); } Ch indentChar_; diff --git a/include/rapidjson/writer.h b/include/rapidjson/writer.h index 874c555..5b3004b 100644 --- a/include/rapidjson/writer.h +++ b/include/rapidjson/writer.h @@ -297,7 +297,7 @@ protected: const char* end = internal::i32toa(i, buffer); PutReserve(*os_, static_cast(end - buffer)); for (const char* p = buffer; p != end; ++p) - PutUnsafe(*os_, static_cast(*p)); + PutUnsafe(*os_, static_cast(*p)); return true; } @@ -306,7 +306,7 @@ protected: const char* end = internal::u32toa(u, buffer); PutReserve(*os_, static_cast(end - buffer)); for (const char* p = buffer; p != end; ++p) - PutUnsafe(*os_, static_cast(*p)); + PutUnsafe(*os_, static_cast(*p)); return true; } @@ -315,7 +315,7 @@ protected: const char* end = internal::i64toa(i64, buffer); PutReserve(*os_, static_cast(end - buffer)); for (const char* p = buffer; p != end; ++p) - PutUnsafe(*os_, static_cast(*p)); + PutUnsafe(*os_, static_cast(*p)); return true; } @@ -324,7 +324,7 @@ protected: char* end = internal::u64toa(u64, buffer); PutReserve(*os_, static_cast(end - buffer)); for (char* p = buffer; p != end; ++p) - PutUnsafe(*os_, static_cast(*p)); + PutUnsafe(*os_, static_cast(*p)); return true; } @@ -357,7 +357,7 @@ protected: } bool WriteString(const Ch* str, SizeType length) { - static const typename TargetEncoding::Ch hexDigits[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; + static const typename OutputStream::Ch hexDigits[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; static const char escape[256] = { #define Z16 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 //0 1 2 3 4 5 6 7 8 9 A B C D E F @@ -413,7 +413,7 @@ protected: else if ((sizeof(Ch) == 1 || static_cast(c) < 256) && RAPIDJSON_UNLIKELY(escape[static_cast(c)])) { is.Take(); PutUnsafe(*os_, '\\'); - PutUnsafe(*os_, static_cast(escape[static_cast(c)])); + PutUnsafe(*os_, static_cast(escape[static_cast(c)])); if (escape[static_cast(c)] == 'u') { PutUnsafe(*os_, '0'); PutUnsafe(*os_, '0'); From 738864c53c541944c2e152d79600d4eb0b7e677a Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Tue, 24 Jan 2017 15:08:38 +0800 Subject: [PATCH 168/305] Remove non-ASCII character Fix #824 --- test/unittest/readertest.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/unittest/readertest.cpp b/test/unittest/readertest.cpp index 64a1f9c..ac5a067 100644 --- a/test/unittest/readertest.cpp +++ b/test/unittest/readertest.cpp @@ -245,13 +245,13 @@ static void TestParseDouble() { TEST_DOUBLE(fullPrecision, "0.017976931348623157e+310", 1.7976931348623157e+308); // Max double in another form // Since - // abs((2^-1022 - 2^-1074) - 2.2250738585072012e-308) = 3.109754131239141401123495768877590405345064751974375599... �� 10^-324 - // abs((2^-1022) - 2.2250738585072012e-308) = 1.830902327173324040642192159804623318305533274168872044... �� 10 ^ -324 + // abs((2^-1022 - 2^-1074) - 2.2250738585072012e-308) = 3.109754131239141401123495768877590405345064751974375599... x 10^-324 + // abs((2^-1022) - 2.2250738585072012e-308) = 1.830902327173324040642192159804623318305533274168872044... x 10 ^ -324 // So 2.2250738585072012e-308 should round to 2^-1022 = 2.2250738585072014e-308 TEST_DOUBLE(fullPrecision, "2.2250738585072012e-308", 2.2250738585072014e-308); // http://www.exploringbinary.com/java-hangs-when-converting-2-2250738585072012e-308/ // More closer to normal/subnormal boundary - // boundary = 2^-1022 - 2^-1075 = 2.225073858507201136057409796709131975934819546351645648... �� 10^-308 + // boundary = 2^-1022 - 2^-1075 = 2.225073858507201136057409796709131975934819546351645648... x 10^-308 TEST_DOUBLE(fullPrecision, "2.22507385850720113605740979670913197593481954635164564e-308", 2.2250738585072009e-308); TEST_DOUBLE(fullPrecision, "2.22507385850720113605740979670913197593481954635164565e-308", 2.2250738585072014e-308); From 6769f3e33e5f64ae579f4be3addbe05964393c07 Mon Sep 17 00:00:00 2001 From: StilesCrisis Date: Thu, 2 Feb 2017 23:18:07 -0800 Subject: [PATCH 169/305] Improved reporting MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add “filename” to the error message when JsonChecker reports an error. --- test/unittest/jsoncheckertest.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/unittest/jsoncheckertest.cpp b/test/unittest/jsoncheckertest.cpp index bea788d..e8f8526 100644 --- a/test/unittest/jsoncheckertest.cpp +++ b/test/unittest/jsoncheckertest.cpp @@ -69,10 +69,10 @@ TEST(JsonChecker, Reader) { GenericDocument, CrtAllocator> document; // Use Crt allocator to check exception-safety (no memory leak) document.Parse(json); - EXPECT_TRUE(document.HasParseError()); + EXPECT_TRUE(document.HasParseError()) << filename; document.Parse(json); - EXPECT_TRUE(document.HasParseError()); + EXPECT_TRUE(document.HasParseError()) << filename; free(json); } @@ -89,10 +89,10 @@ TEST(JsonChecker, Reader) { GenericDocument, CrtAllocator> document; // Use Crt allocator to check exception-safety (no memory leak) document.Parse(json); - EXPECT_FALSE(document.HasParseError()); + EXPECT_FALSE(document.HasParseError()) << filename; document.Parse(json); - EXPECT_FALSE(document.HasParseError()); + EXPECT_FALSE(document.HasParseError()) << filename; free(json); } From 20f5caa8f64f3efaf6effb35f17d14dc9fcb3aab Mon Sep 17 00:00:00 2001 From: StilesCrisis Date: Thu, 2 Feb 2017 23:25:29 -0800 Subject: [PATCH 170/305] Token-by-token pull parsing Refactored the iterative parser so that users can parse a single JSON element at a time (invoking the handler one time) and then return control to the calling code. Call IterativeParseInit to start, and then call IterativeParseNext to retrieve one JSON element. Use IterativeParseComplete to check for JSON document completion. --- include/rapidjson/reader.h | 111 +++++++++++++++++++++++++++---------- 1 file changed, 83 insertions(+), 28 deletions(-) diff --git a/include/rapidjson/reader.h b/include/rapidjson/reader.h index dbb5e16..68ef128 100644 --- a/include/rapidjson/reader.h +++ b/include/rapidjson/reader.h @@ -513,6 +513,68 @@ public: return Parse(is, handler); } + //! Initialize JSON text token-by-token parsing + /*! + */ + void IterativeParseInit() { + parseResult_.Clear(); + state_ = IterativeParsingStartState; + } + + //! Parse one token from JSON text + /*! \tparam InputStream Type of input stream, implementing Stream concept + \tparam Handler Type of handler, implementing Handler concept. + \param is Input stream to be parsed. + \param handler The handler to receive events. + \return Whether the parsing is successful. + */ + template + ParseResult IterativeParseNext(InputStream& is, Handler& handler) { + while (is.Peek() != '\0') { + RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_); + SkipWhitespaceAndComments(is); + + Token t = Tokenize(is.Peek()); + IterativeParsingState n = Predict(state_, t); + IterativeParsingState d = Transit(state_, t, n, is, handler); + + if (d == IterativeParsingErrorState) { + HandleError(state_, is); + return parseResult_; + } + + state_ = d; + + // Do not further consume streams if a root JSON has been parsed. + if (state_ == IterativeParsingFinishState) { + // If StopWhenDone is not set, and stray data is found post-root, flag an error. + if (!(parseFlags & kParseStopWhenDoneFlag)) { + SkipWhitespaceAndComments(is); + if (is.Peek() != '\0') + HandleError(state_, is); + } + return parseResult_; + } + + if (!IsIterativeParsingDelimiterState(n)) + return parseResult_; + } + + // Handle the end of file. + if (state_ != IterativeParsingFinishState) + HandleError(state_, is); + + stack_.Clear(); + return parseResult_; + } + + //! Check if token-by-token parsing JSON text is complete + /*! \return Whether the JSON has been fully decoded. + */ + bool IterativeParseComplete() { + return IsIterativeParsingCompleteState(state_); + } + //! Whether a parse error has occured in the last parsing. bool HasParseError() const { return parseResult_.IsError(); } @@ -1803,44 +1865,37 @@ private: } } + RAPIDJSON_FORCEINLINE bool IsIterativeParsingDelimiterState(IterativeParsingState s) { + const unsigned int delimiterStateMask = + (1 << IterativeParsingKeyValueDelimiterState) | + (1 << IterativeParsingMemberDelimiterState) | + (1 << IterativeParsingElementDelimiterState); + + return (1 << s) & delimiterStateMask; + } + + RAPIDJSON_FORCEINLINE bool IsIterativeParsingCompleteState(IterativeParsingState s) { + const unsigned int completeStateMask = + (1 << IterativeParsingFinishState) | + (1 << IterativeParsingErrorState); + + return (1 << s) & completeStateMask; + } + template ParseResult IterativeParse(InputStream& is, Handler& handler) { - parseResult_.Clear(); - ClearStackOnExit scope(*this); - IterativeParsingState state = IterativeParsingStartState; - - SkipWhitespaceAndComments(is); - RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_); - while (is.Peek() != '\0') { - Token t = Tokenize(is.Peek()); - IterativeParsingState n = Predict(state, t); - IterativeParsingState d = Transit(state, t, n, is, handler); - - if (d == IterativeParsingErrorState) { - HandleError(state, is); + IterativeParseInit(); + while (!IterativeParseComplete()) { + if (!IterativeParseNext(is, handler)) break; - } - - state = d; - - // Do not further consume streams if a root JSON has been parsed. - if ((parseFlags & kParseStopWhenDoneFlag) && state == IterativeParsingFinishState) - break; - - SkipWhitespaceAndComments(is); - RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_); } - - // Handle the end of file. - if (state != IterativeParsingFinishState) - HandleError(state, is); - return parseResult_; } static const size_t kDefaultStackCapacity = 256; //!< Default stack capacity in bytes for storing a single decoded string. internal::Stack stack_; //!< A stack for storing decoded string temporarily during non-destructive parsing. ParseResult parseResult_; + IterativeParsingState state_; }; // class GenericReader //! Reader with UTF8 encoding and default allocator. From 1a7c5ea5179601308a46a21a339a146d295af78e Mon Sep 17 00:00:00 2001 From: StilesCrisis Date: Fri, 3 Feb 2017 00:29:43 -0800 Subject: [PATCH 171/305] Fix Dev Studio bool-conversion warning --- include/rapidjson/reader.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/rapidjson/reader.h b/include/rapidjson/reader.h index 68ef128..9babf75 100644 --- a/include/rapidjson/reader.h +++ b/include/rapidjson/reader.h @@ -1871,7 +1871,7 @@ private: (1 << IterativeParsingMemberDelimiterState) | (1 << IterativeParsingElementDelimiterState); - return (1 << s) & delimiterStateMask; + return !!((1 << s) & delimiterStateMask); } RAPIDJSON_FORCEINLINE bool IsIterativeParsingCompleteState(IterativeParsingState s) { @@ -1879,7 +1879,7 @@ private: (1 << IterativeParsingFinishState) | (1 << IterativeParsingErrorState); - return (1 << s) & completeStateMask; + return !!((1 << s) & completeStateMask); } template From 5de7258478433bf76f998fdc6a0f326709aeabc5 Mon Sep 17 00:00:00 2001 From: StilesCrisis Date: Fri, 3 Feb 2017 17:14:14 -0800 Subject: [PATCH 172/305] Improve performance Slight performance improvement over previous submission --- include/rapidjson/reader.h | 174 +++++++++++++++++++------------------ 1 file changed, 90 insertions(+), 84 deletions(-) diff --git a/include/rapidjson/reader.h b/include/rapidjson/reader.h index 9babf75..065772f 100644 --- a/include/rapidjson/reader.h +++ b/include/rapidjson/reader.h @@ -529,9 +529,8 @@ public: \return Whether the parsing is successful. */ template - ParseResult IterativeParseNext(InputStream& is, Handler& handler) { + bool IterativeParseNext(InputStream& is, Handler& handler) { while (is.Peek() != '\0') { - RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_); SkipWhitespaceAndComments(is); Token t = Tokenize(is.Peek()); @@ -540,38 +539,52 @@ public: if (d == IterativeParsingErrorState) { HandleError(state_, is); - return parseResult_; + return false; } state_ = d; - // Do not further consume streams if a root JSON has been parsed. - if (state_ == IterativeParsingFinishState) { - // If StopWhenDone is not set, and stray data is found post-root, flag an error. + // Do not further consume streams if we've parsed a complete object or hit an error. + if (IsIterativeParsingCompleteState(state_)) { + // If we hit an error, we are done. + if (HasParseError()) + return false; + + // If StopWhenDone is not set... if (!(parseFlags & kParseStopWhenDoneFlag)) { + // ... and extra non-whitespace data is found... SkipWhitespaceAndComments(is); - if (is.Peek() != '\0') + if (is.Peek() != '\0') { + // ... this is considered an error. HandleError(state_, is); + return false; + } } - return parseResult_; + + // We are done! + return true; } + // If we found anything other than a delimiter, we invoked the handler, so we can return true now. if (!IsIterativeParsingDelimiterState(n)) - return parseResult_; + return true; } - // Handle the end of file. - if (state_ != IterativeParsingFinishState) - HandleError(state_, is); - + // We reached the end of file. stack_.Clear(); - return parseResult_; + + if (state_ != IterativeParsingFinishState) { + HandleError(state_, is); + return false; + } + + return true; } //! Check if token-by-token parsing JSON text is complete /*! \return Whether the JSON has been fully decoded. */ - bool IterativeParseComplete() { + RAPIDJSON_FORCEINLINE bool IterativeParseComplete() { return IsIterativeParsingCompleteState(state_); } @@ -1455,30 +1468,32 @@ private: // States enum IterativeParsingState { - IterativeParsingStartState = 0, - IterativeParsingFinishState, - IterativeParsingErrorState, + IterativeParsingFinishState = 0, // sink states at top + IterativeParsingErrorState, // sink states at top + IterativeParsingStartState, // Object states IterativeParsingObjectInitialState, IterativeParsingMemberKeyState, - IterativeParsingKeyValueDelimiterState, IterativeParsingMemberValueState, - IterativeParsingMemberDelimiterState, IterativeParsingObjectFinishState, // Array states IterativeParsingArrayInitialState, IterativeParsingElementState, - IterativeParsingElementDelimiterState, IterativeParsingArrayFinishState, // Single value state - IterativeParsingValueState + IterativeParsingValueState, + + // Delimiter states (at bottom) + IterativeParsingElementDelimiterState, + IterativeParsingMemberDelimiterState, + IterativeParsingKeyValueDelimiterState, + + cIterativeParsingStateCount }; - enum { cIterativeParsingStateCount = IterativeParsingValueState + 1 }; - // Tokens enum Token { LeftBracketToken = 0, @@ -1529,6 +1544,18 @@ private: RAPIDJSON_FORCEINLINE IterativeParsingState Predict(IterativeParsingState state, Token token) { // current state x one lookahead token -> new state static const char G[cIterativeParsingStateCount][kTokenCount] = { + // Finish(sink state) + { + IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, + IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, + IterativeParsingErrorState + }, + // Error(sink state) + { + IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, + IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, + IterativeParsingErrorState + }, // Start { IterativeParsingArrayInitialState, // Left bracket @@ -1543,18 +1570,6 @@ private: IterativeParsingValueState, // Null IterativeParsingValueState // Number }, - // Finish(sink state) - { - IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, - IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, - IterativeParsingErrorState - }, - // Error(sink state) - { - IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, - IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, - IterativeParsingErrorState - }, // ObjectInitial { IterativeParsingErrorState, // Left bracket @@ -1583,20 +1598,6 @@ private: IterativeParsingErrorState, // Null IterativeParsingErrorState // Number }, - // KeyValueDelimiter - { - IterativeParsingArrayInitialState, // Left bracket(push MemberValue state) - IterativeParsingErrorState, // Right bracket - IterativeParsingObjectInitialState, // Left curly bracket(push MemberValue state) - IterativeParsingErrorState, // Right curly bracket - IterativeParsingErrorState, // Comma - IterativeParsingErrorState, // Colon - IterativeParsingMemberValueState, // String - IterativeParsingMemberValueState, // False - IterativeParsingMemberValueState, // True - IterativeParsingMemberValueState, // Null - IterativeParsingMemberValueState // Number - }, // MemberValue { IterativeParsingErrorState, // Left bracket @@ -1611,20 +1612,6 @@ private: IterativeParsingErrorState, // Null IterativeParsingErrorState // Number }, - // MemberDelimiter - { - IterativeParsingErrorState, // Left bracket - IterativeParsingErrorState, // Right bracket - IterativeParsingErrorState, // Left curly bracket - IterativeParsingObjectFinishState, // Right curly bracket - IterativeParsingErrorState, // Comma - IterativeParsingErrorState, // Colon - IterativeParsingMemberKeyState, // String - IterativeParsingErrorState, // False - IterativeParsingErrorState, // True - IterativeParsingErrorState, // Null - IterativeParsingErrorState // Number - }, // ObjectFinish(sink state) { IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, @@ -1659,6 +1646,18 @@ private: IterativeParsingErrorState, // Null IterativeParsingErrorState // Number }, + // ArrayFinish(sink state) + { + IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, + IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, + IterativeParsingErrorState + }, + // Single Value (sink state) + { + IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, + IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, + IterativeParsingErrorState + }, // ElementDelimiter { IterativeParsingArrayInitialState, // Left bracket(push Element state) @@ -1673,18 +1672,34 @@ private: IterativeParsingElementState, // Null IterativeParsingElementState // Number }, - // ArrayFinish(sink state) + // MemberDelimiter { - IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, - IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, - IterativeParsingErrorState + IterativeParsingErrorState, // Left bracket + IterativeParsingErrorState, // Right bracket + IterativeParsingErrorState, // Left curly bracket + IterativeParsingObjectFinishState, // Right curly bracket + IterativeParsingErrorState, // Comma + IterativeParsingErrorState, // Colon + IterativeParsingMemberKeyState, // String + IterativeParsingErrorState, // False + IterativeParsingErrorState, // True + IterativeParsingErrorState, // Null + IterativeParsingErrorState // Number }, - // Single Value (sink state) + // KeyValueDelimiter { - IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, - IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, - IterativeParsingErrorState - } + IterativeParsingArrayInitialState, // Left bracket(push MemberValue state) + IterativeParsingErrorState, // Right bracket + IterativeParsingObjectInitialState, // Left curly bracket(push MemberValue state) + IterativeParsingErrorState, // Right curly bracket + IterativeParsingErrorState, // Comma + IterativeParsingErrorState, // Colon + IterativeParsingMemberValueState, // String + IterativeParsingMemberValueState, // False + IterativeParsingMemberValueState, // True + IterativeParsingMemberValueState, // Null + IterativeParsingMemberValueState // Number + }, }; // End of G return static_cast(G[state][token]); @@ -1866,20 +1881,11 @@ private: } RAPIDJSON_FORCEINLINE bool IsIterativeParsingDelimiterState(IterativeParsingState s) { - const unsigned int delimiterStateMask = - (1 << IterativeParsingKeyValueDelimiterState) | - (1 << IterativeParsingMemberDelimiterState) | - (1 << IterativeParsingElementDelimiterState); - - return !!((1 << s) & delimiterStateMask); + return s >= IterativeParsingElementDelimiterState; } RAPIDJSON_FORCEINLINE bool IsIterativeParsingCompleteState(IterativeParsingState s) { - const unsigned int completeStateMask = - (1 << IterativeParsingFinishState) | - (1 << IterativeParsingErrorState); - - return !!((1 << s) & completeStateMask); + return s <= IterativeParsingErrorState; } template From 116f65994b928405149ddf38c4e8d6e1399a1e0b Mon Sep 17 00:00:00 2001 From: StilesCrisis Date: Fri, 3 Feb 2017 18:58:37 -0800 Subject: [PATCH 173/305] Improve coverage and performance Further improvement to perftest and hoping to make coveralls happy. --- include/rapidjson/reader.h | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/include/rapidjson/reader.h b/include/rapidjson/reader.h index 065772f..dcdc8cc 100644 --- a/include/rapidjson/reader.h +++ b/include/rapidjson/reader.h @@ -537,18 +537,17 @@ public: IterativeParsingState n = Predict(state_, t); IterativeParsingState d = Transit(state_, t, n, is, handler); - if (d == IterativeParsingErrorState) { - HandleError(state_, is); - return false; - } - - state_ = d; - - // Do not further consume streams if we've parsed a complete object or hit an error. - if (IsIterativeParsingCompleteState(state_)) { - // If we hit an error, we are done. - if (HasParseError()) + // If we've finished or hit an error... + if (IsIterativeParsingCompleteState(d)) { + // Report errors. + if (d == IterativeParsingErrorState) { + HandleError(state_, is); return false; + } + + // Transition to the finish state. + RAPIDJSON_ASSERT(d == IterativeParsingFinishState); + state_ = d; // If StopWhenDone is not set... if (!(parseFlags & kParseStopWhenDoneFlag)) { @@ -561,11 +560,14 @@ public: } } - // We are done! + // Success! We are done! return true; } - // If we found anything other than a delimiter, we invoked the handler, so we can return true now. + // Transition to the new state. + state_ = d; + + // If we parsed anything other than a delimiter, we invoked the handler, so we can return true now. if (!IsIterativeParsingDelimiterState(n)) return true; } From 82a423db7d9bbc65269ddb35436ea7a46bfd2140 Mon Sep 17 00:00:00 2001 From: StilesCrisis Date: Fri, 3 Feb 2017 21:12:53 -0800 Subject: [PATCH 174/305] Added unit test for pull parsing New unit test which ensures that IterativeParseNext always generates exactly one element at a time, and that calling IterativeParseNext on a complete document is harmless and generates zero events. --- test/unittest/readertest.cpp | 101 +++++++++++++++++++++++++---------- 1 file changed, 73 insertions(+), 28 deletions(-) diff --git a/test/unittest/readertest.cpp b/test/unittest/readertest.cpp index ac5a067..b1c0c31 100644 --- a/test/unittest/readertest.cpp +++ b/test/unittest/readertest.cpp @@ -1157,22 +1157,22 @@ template > struct IterativeParsingReaderHandler { typedef typename Encoding::Ch Ch; - const static int LOG_NULL = -1; - const static int LOG_BOOL = -2; - const static int LOG_INT = -3; - const static int LOG_UINT = -4; - const static int LOG_INT64 = -5; - const static int LOG_UINT64 = -6; - const static int LOG_DOUBLE = -7; - const static int LOG_STRING = -8; - const static int LOG_STARTOBJECT = -9; - const static int LOG_KEY = -10; - const static int LOG_ENDOBJECT = -11; - const static int LOG_STARTARRAY = -12; - const static int LOG_ENDARRAY = -13; + const static uint32_t LOG_NULL = 0x10000000; + const static uint32_t LOG_BOOL = 0x20000000; + const static uint32_t LOG_INT = 0x30000000; + const static uint32_t LOG_UINT = 0x40000000; + const static uint32_t LOG_INT64 = 0x50000000; + const static uint32_t LOG_UINT64 = 0x60000000; + const static uint32_t LOG_DOUBLE = 0x70000000; + const static uint32_t LOG_STRING = 0x80000000; + const static uint32_t LOG_STARTOBJECT = 0x90000000; + const static uint32_t LOG_KEY = 0xA0000000; + const static uint32_t LOG_ENDOBJECT = 0xB0000000; + const static uint32_t LOG_STARTARRAY = 0xC0000000; + const static uint32_t LOG_ENDARRAY = 0xD0000000; const static size_t LogCapacity = 256; - int Logs[LogCapacity]; + uint32_t Logs[LogCapacity]; size_t LogCount; IterativeParsingReaderHandler() : LogCount(0) { @@ -1202,8 +1202,8 @@ struct IterativeParsingReaderHandler { bool EndObject(SizeType c) { RAPIDJSON_ASSERT(LogCount < LogCapacity); - Logs[LogCount++] = LOG_ENDOBJECT; - Logs[LogCount++] = static_cast(c); + RAPIDJSON_ASSERT((static_cast(c) & 0xF0000000) == 0); + Logs[LogCount++] = LOG_ENDOBJECT | static_cast(c); return true; } @@ -1211,8 +1211,8 @@ struct IterativeParsingReaderHandler { bool EndArray(SizeType c) { RAPIDJSON_ASSERT(LogCount < LogCapacity); - Logs[LogCount++] = LOG_ENDARRAY; - Logs[LogCount++] = static_cast(c); + RAPIDJSON_ASSERT((static_cast(c) & 0xF0000000) == 0); + Logs[LogCount++] = LOG_ENDARRAY | static_cast(c); return true; } }; @@ -1228,7 +1228,7 @@ TEST(Reader, IterativeParsing_General) { EXPECT_FALSE(r.IsError()); EXPECT_FALSE(reader.HasParseError()); - int e[] = { + uint32_t e[] = { handler.LOG_STARTARRAY, handler.LOG_INT, handler.LOG_STARTOBJECT, @@ -1236,14 +1236,14 @@ TEST(Reader, IterativeParsing_General) { handler.LOG_STARTARRAY, handler.LOG_INT, handler.LOG_INT, - handler.LOG_ENDARRAY, 2, - handler.LOG_ENDOBJECT, 1, + handler.LOG_ENDARRAY | 2, + handler.LOG_ENDOBJECT | 1, handler.LOG_NULL, handler.LOG_BOOL, handler.LOG_BOOL, handler.LOG_STRING, handler.LOG_DOUBLE, - handler.LOG_ENDARRAY, 7 + handler.LOG_ENDARRAY | 7 }; EXPECT_EQ(sizeof(e) / sizeof(int), handler.LogCount); @@ -1265,20 +1265,20 @@ TEST(Reader, IterativeParsing_Count) { EXPECT_FALSE(r.IsError()); EXPECT_FALSE(reader.HasParseError()); - int e[] = { + uint32_t e[] = { handler.LOG_STARTARRAY, handler.LOG_STARTOBJECT, - handler.LOG_ENDOBJECT, 0, + handler.LOG_ENDOBJECT | 0, handler.LOG_STARTOBJECT, handler.LOG_KEY, handler.LOG_INT, - handler.LOG_ENDOBJECT, 1, + handler.LOG_ENDOBJECT | 1, handler.LOG_STARTARRAY, handler.LOG_INT, - handler.LOG_ENDARRAY, 1, + handler.LOG_ENDARRAY | 1, handler.LOG_STARTARRAY, - handler.LOG_ENDARRAY, 0, - handler.LOG_ENDARRAY, 4 + handler.LOG_ENDARRAY | 0, + handler.LOG_ENDARRAY | 4 }; EXPECT_EQ(sizeof(e) / sizeof(int), handler.LogCount); @@ -1289,6 +1289,51 @@ TEST(Reader, IterativeParsing_Count) { } } +TEST(Reader, IterativePullParsing_General) { + { + IterativeParsingReaderHandler<> handler; + uint32_t e[] = { + handler.LOG_STARTARRAY, + handler.LOG_INT, + handler.LOG_STARTOBJECT, + handler.LOG_KEY, + handler.LOG_STARTARRAY, + handler.LOG_INT, + handler.LOG_INT, + handler.LOG_ENDARRAY | 2, + handler.LOG_ENDOBJECT | 1, + handler.LOG_NULL, + handler.LOG_BOOL, + handler.LOG_BOOL, + handler.LOG_STRING, + handler.LOG_DOUBLE, + handler.LOG_ENDARRAY | 7 + }; + + StringStream is("[1, {\"k\": [1, 2]}, null, false, true, \"string\", 1.2]"); + Reader reader; + + reader.IterativeParseInit(); + while (!reader.IterativeParseComplete()) { + size_t oldLogCount = handler.LogCount; + EXPECT_TRUE(oldLogCount < sizeof(e) / sizeof(int)) << "overrun"; + + EXPECT_TRUE(reader.IterativeParseNext(is, handler)) << "parse fail"; + EXPECT_EQ(handler.LogCount, oldLogCount + 1) << "handler should be invoked exactly once each time"; + EXPECT_EQ(e[oldLogCount], handler.Logs[oldLogCount]) << "wrong event returned"; + } + + EXPECT_FALSE(reader.HasParseError()); + EXPECT_EQ(sizeof(e) / sizeof(int), handler.LogCount) << "handler invoked wrong number of times"; + + // The handler should not be invoked when the JSON has been fully read, but it should not fail + size_t oldLogCount = handler.LogCount; + EXPECT_TRUE(reader.IterativeParseNext(is, handler)) << "parse-next past complete is allowed"; + EXPECT_EQ(handler.LogCount, oldLogCount) << "parse-next past complete should not invoke handler"; + EXPECT_FALSE(reader.HasParseError()) << "parse-next past complete should not generate parse error"; + } +} + // Test iterative parsing on kParseErrorTermination. struct HandlerTerminateAtStartObject : public IterativeParsingReaderHandler<> { bool StartObject() { return false; } From 4394b3bac7a0647f5121a533397c0fe20c89dab8 Mon Sep 17 00:00:00 2001 From: StilesCrisis Date: Sat, 4 Feb 2017 00:05:34 -0800 Subject: [PATCH 175/305] Add LIKELY and UNLIKELY hints MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Doesn’t seem to affect timings in perftest on my machine, but it may help others. --- include/rapidjson/reader.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/rapidjson/reader.h b/include/rapidjson/reader.h index dcdc8cc..df59a1e 100644 --- a/include/rapidjson/reader.h +++ b/include/rapidjson/reader.h @@ -530,7 +530,7 @@ public: */ template bool IterativeParseNext(InputStream& is, Handler& handler) { - while (is.Peek() != '\0') { + while (RAPIDJSON_LIKELY(is.Peek() != '\0')) { SkipWhitespaceAndComments(is); Token t = Tokenize(is.Peek()); @@ -538,7 +538,7 @@ public: IterativeParsingState d = Transit(state_, t, n, is, handler); // If we've finished or hit an error... - if (IsIterativeParsingCompleteState(d)) { + if (RAPIDJSON_UNLIKELY(IsIterativeParsingCompleteState(d))) { // Report errors. if (d == IterativeParsingErrorState) { HandleError(state_, is); From d84d5fe0551634d1057adf6eb163ce8a63d00f1a Mon Sep 17 00:00:00 2001 From: StilesCrisis Date: Sat, 4 Feb 2017 00:41:34 -0800 Subject: [PATCH 176/305] Add example SimplePullHandler code Example code to demonstrate how the token-pulling reader can be used. --- example/simplepullreader/simplepullreader.cpp | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 example/simplepullreader/simplepullreader.cpp diff --git a/example/simplepullreader/simplepullreader.cpp b/example/simplepullreader/simplepullreader.cpp new file mode 100644 index 0000000..0cce08b --- /dev/null +++ b/example/simplepullreader/simplepullreader.cpp @@ -0,0 +1,40 @@ +#include "rapidjson/reader.h" +#include + +using namespace rapidjson; +using namespace std; + +struct MyHandler { + const char* type; + std::string data; + + bool Null() { type = "Null"; data.clear(); return true; } + bool Bool(bool b) { type = "Bool"; data = b? "true": "false"; return true; } + bool Int(int i) { type = "Int"; data = std::to_string(i); return true; } + bool Uint(unsigned u) { type = "Uint"; data = std::to_string(u); return true; } + bool Int64(int64_t i) { type = "Int64"; data = std::to_string(i); return true; } + bool Uint64(uint64_t u) { type = "Uint64"; data = std::to_string(u); return true; } + bool Double(double d) { type = "Double"; data = std::to_string(d); return true; } + bool RawNumber(const char* str, SizeType length, bool) { type = "Number"; data = std::string(str, length); return true; } + bool String(const char* str, SizeType length, bool) { type = "String" data = std::string(str, length); return true; } + bool StartObject() { type = "StartObject"; data.clear(); return true; } + bool Key(const char* str, SizeType length, bool) { type = "Key" data = std::string(str, length); return true; } + bool EndObject(SizeType memberCount) { type = "EndObject"; data = std::to_string(memberCount); return true; } + bool StartArray() { type = "StartArray"; data.clear(); return true; } + bool EndArray(SizeType elementCount) { type = "EndArray"; data = std::to_string(elementCount); return true; } +}; + +int main() { + const char json[] = " { \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3, 4] } "; + + MyHandler handler; + Reader reader; + StringStream ss(json); + reader.IterativeParseInit(); + while (!reader.IterativeParseComplete()) { + reader.IterativeParseNext(ss, handler); + cout << handler.type << ": " << handler.data << endl; + } + + return 0; +} From 4232e407f40a3f2f3769234c1069ebccda43ea51 Mon Sep 17 00:00:00 2001 From: StilesCrisis Date: Sat, 4 Feb 2017 00:47:43 -0800 Subject: [PATCH 177/305] Clean up example code --- example/CMakeLists.txt | 1 + example/simplepullreader/simplepullreader.cpp | 24 +++++++++---------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index bec6a8c..e16e3c9 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -18,6 +18,7 @@ set(EXAMPLES serialize simpledom simplereader + simplepullreader simplewriter tutorial) diff --git a/example/simplepullreader/simplepullreader.cpp b/example/simplepullreader/simplepullreader.cpp index 0cce08b..af8d5a7 100644 --- a/example/simplepullreader/simplepullreader.cpp +++ b/example/simplepullreader/simplepullreader.cpp @@ -9,19 +9,19 @@ struct MyHandler { std::string data; bool Null() { type = "Null"; data.clear(); return true; } - bool Bool(bool b) { type = "Bool"; data = b? "true": "false"; return true; } - bool Int(int i) { type = "Int"; data = std::to_string(i); return true; } - bool Uint(unsigned u) { type = "Uint"; data = std::to_string(u); return true; } - bool Int64(int64_t i) { type = "Int64"; data = std::to_string(i); return true; } - bool Uint64(uint64_t u) { type = "Uint64"; data = std::to_string(u); return true; } - bool Double(double d) { type = "Double"; data = std::to_string(d); return true; } - bool RawNumber(const char* str, SizeType length, bool) { type = "Number"; data = std::string(str, length); return true; } - bool String(const char* str, SizeType length, bool) { type = "String" data = std::string(str, length); return true; } + bool Bool(bool b) { type = "Bool:"; data = b? "true": "false"; return true; } + bool Int(int i) { type = "Int:"; data = std::to_string(i); return true; } + bool Uint(unsigned u) { type = "Uint:"; data = std::to_string(u); return true; } + bool Int64(int64_t i) { type = "Int64:"; data = std::to_string(i); return true; } + bool Uint64(uint64_t u) { type = "Uint64:"; data = std::to_string(u); return true; } + bool Double(double d) { type = "Double:"; data = std::to_string(d); return true; } + bool RawNumber(const char* str, SizeType length, bool) { type = "Number:"; data = std::string(str, length); return true; } + bool String(const char* str, SizeType length, bool) { type = "String:"; data = std::string(str, length); return true; } bool StartObject() { type = "StartObject"; data.clear(); return true; } - bool Key(const char* str, SizeType length, bool) { type = "Key" data = std::string(str, length); return true; } - bool EndObject(SizeType memberCount) { type = "EndObject"; data = std::to_string(memberCount); return true; } + bool Key(const char* str, SizeType length, bool) { type = "Key:"; data = std::string(str, length); return true; } + bool EndObject(SizeType memberCount) { type = "EndObject:"; data = std::to_string(memberCount); return true; } bool StartArray() { type = "StartArray"; data.clear(); return true; } - bool EndArray(SizeType elementCount) { type = "EndArray"; data = std::to_string(elementCount); return true; } + bool EndArray(SizeType elementCount) { type = "EndArray:"; data = std::to_string(elementCount); return true; } }; int main() { @@ -33,7 +33,7 @@ int main() { reader.IterativeParseInit(); while (!reader.IterativeParseComplete()) { reader.IterativeParseNext(ss, handler); - cout << handler.type << ": " << handler.data << endl; + cout << handler.type << handler.data << endl; } return 0; From 6288d95d1e65d6a57c480dea9edc342b4b721507 Mon Sep 17 00:00:00 2001 From: StilesCrisis Date: Sat, 4 Feb 2017 01:07:00 -0800 Subject: [PATCH 178/305] SimplePullReader C++98 support MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit std::to_string can’t be used because it requires C++11. --- example/simplepullreader/simplepullreader.cpp | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/example/simplepullreader/simplepullreader.cpp b/example/simplepullreader/simplepullreader.cpp index af8d5a7..0b11b40 100644 --- a/example/simplepullreader/simplepullreader.cpp +++ b/example/simplepullreader/simplepullreader.cpp @@ -1,27 +1,33 @@ #include "rapidjson/reader.h" #include +#include using namespace rapidjson; using namespace std; +// If you can require C++11, you could use std::to_string here +template std::string stringify(T x) { + return (std::stringstream() << x).str(); +} + struct MyHandler { const char* type; std::string data; bool Null() { type = "Null"; data.clear(); return true; } bool Bool(bool b) { type = "Bool:"; data = b? "true": "false"; return true; } - bool Int(int i) { type = "Int:"; data = std::to_string(i); return true; } - bool Uint(unsigned u) { type = "Uint:"; data = std::to_string(u); return true; } - bool Int64(int64_t i) { type = "Int64:"; data = std::to_string(i); return true; } - bool Uint64(uint64_t u) { type = "Uint64:"; data = std::to_string(u); return true; } - bool Double(double d) { type = "Double:"; data = std::to_string(d); return true; } + bool Int(int i) { type = "Int:"; data = stringify(i); return true; } + bool Uint(unsigned u) { type = "Uint:"; data = stringify(u); return true; } + bool Int64(int64_t i) { type = "Int64:"; data = stringify(i); return true; } + bool Uint64(uint64_t u) { type = "Uint64:"; data = stringify(u); return true; } + bool Double(double d) { type = "Double:"; data = stringify(d); return true; } bool RawNumber(const char* str, SizeType length, bool) { type = "Number:"; data = std::string(str, length); return true; } bool String(const char* str, SizeType length, bool) { type = "String:"; data = std::string(str, length); return true; } bool StartObject() { type = "StartObject"; data.clear(); return true; } bool Key(const char* str, SizeType length, bool) { type = "Key:"; data = std::string(str, length); return true; } - bool EndObject(SizeType memberCount) { type = "EndObject:"; data = std::to_string(memberCount); return true; } + bool EndObject(SizeType memberCount) { type = "EndObject:"; data = stringify(memberCount); return true; } bool StartArray() { type = "StartArray"; data.clear(); return true; } - bool EndArray(SizeType elementCount) { type = "EndArray:"; data = std::to_string(elementCount); return true; } + bool EndArray(SizeType elementCount) { type = "EndArray:"; data = stringify(elementCount); return true; } }; int main() { From a11ec697969eb776973c680d1eadf9ab11ac7e7b Mon Sep 17 00:00:00 2001 From: StilesCrisis Date: Sat, 4 Feb 2017 01:18:46 -0800 Subject: [PATCH 179/305] More C++98 fixes --- example/simplepullreader/simplepullreader.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/example/simplepullreader/simplepullreader.cpp b/example/simplepullreader/simplepullreader.cpp index 0b11b40..98566e6 100644 --- a/example/simplepullreader/simplepullreader.cpp +++ b/example/simplepullreader/simplepullreader.cpp @@ -7,7 +7,9 @@ using namespace std; // If you can require C++11, you could use std::to_string here template std::string stringify(T x) { - return (std::stringstream() << x).str(); + std::stringstream ss; + ss << x; + return ss.str(); } struct MyHandler { From 0f8389e78779cf13e5f0c8f4da2a3a780d097d42 Mon Sep 17 00:00:00 2001 From: John Stiles Date: Tue, 7 Feb 2017 00:02:08 -0800 Subject: [PATCH 180/305] Restored original IterativeParse implementation Runs about 1-2% faster (original speed) by running in a tight loop, at the expense of slight code duplication with IterativeParseNext. --- include/rapidjson/reader.h | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/include/rapidjson/reader.h b/include/rapidjson/reader.h index df59a1e..d92d9fb 100644 --- a/include/rapidjson/reader.h +++ b/include/rapidjson/reader.h @@ -1892,11 +1892,36 @@ private: template ParseResult IterativeParse(InputStream& is, Handler& handler) { - IterativeParseInit(); - while (!IterativeParseComplete()) { - if (!IterativeParseNext(is, handler)) + parseResult_.Clear(); + ClearStackOnExit scope(*this); + IterativeParsingState state = IterativeParsingStartState; + + SkipWhitespaceAndComments(is); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_); + while (is.Peek() != '\0') { + Token t = Tokenize(is.Peek()); + IterativeParsingState n = Predict(state, t); + IterativeParsingState d = Transit(state, t, n, is, handler); + + if (d == IterativeParsingErrorState) { + HandleError(state, is); break; + } + + state = d; + + // Do not further consume streams if a root JSON has been parsed. + if ((parseFlags & kParseStopWhenDoneFlag) && state == IterativeParsingFinishState) + break; + + SkipWhitespaceAndComments(is); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_); } + + // Handle the end of file. + if (state != IterativeParsingFinishState) + HandleError(state, is); + return parseResult_; } From bd4c282d77d4bb6f0034405a721a6fbf477b4955 Mon Sep 17 00:00:00 2001 From: John Stiles Date: Tue, 7 Feb 2017 01:08:51 -0800 Subject: [PATCH 181/305] Test coverage up Add more tests! Good for coverage. --- test/perftest/rapidjsontest.cpp | 29 ++++++++++++++++++++ test/unittest/jsoncheckertest.cpp | 44 +++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) diff --git a/test/perftest/rapidjsontest.cpp b/test/perftest/rapidjsontest.cpp index 675db31..f14e702 100644 --- a/test/perftest/rapidjsontest.cpp +++ b/test/perftest/rapidjsontest.cpp @@ -152,6 +152,35 @@ TEST_F(RapidJson, SIMD_SUFFIX(ReaderParseIterativeInsitu_DummyHandler)) { } } +TEST_F(RapidJson, SIMD_SUFFIX(ReaderParseIterativePull_DummyHandler)) { + for (size_t i = 0; i < kTrialCount; i++) { + StringStream s(json_); + BaseReaderHandler<> h; + Reader reader; + reader.IterativeParseInit(); + while (!reader.IterativeParseComplete()) { + if (!reader.IterativeParseNext(s, h)) + break; + } + EXPECT_FALSE(reader.HasParseError()); + } +} + +TEST_F(RapidJson, SIMD_SUFFIX(ReaderParseIterativePullInsitu_DummyHandler)) { + for (size_t i = 0; i < kTrialCount; i++) { + memcpy(temp_, json_, length_ + 1); + InsituStringStream s(temp_); + BaseReaderHandler<> h; + Reader reader; + reader.IterativeParseInit(); + while (!reader.IterativeParseComplete()) { + if (!reader.IterativeParseNext(s, h)) + break; + } + EXPECT_FALSE(reader.HasParseError()); + } +} + TEST_F(RapidJson, SIMD_SUFFIX(ReaderParse_DummyHandler_ValidateEncoding)) { for (size_t i = 0; i < kTrialCount; i++) { StringStream s(json_); diff --git a/test/unittest/jsoncheckertest.cpp b/test/unittest/jsoncheckertest.cpp index e8f8526..47c2b56 100644 --- a/test/unittest/jsoncheckertest.cpp +++ b/test/unittest/jsoncheckertest.cpp @@ -48,6 +48,24 @@ static char* ReadFile(const char* filename, size_t& length) { return json; } +struct NoOpHandler { + bool Null() { return true; } + bool Bool(bool) { return true; } + bool Int(int) { return true; } + bool Uint(unsigned) { return true; } + bool Int64(int64_t) { return true; } + bool Uint64(uint64_t) { return true; } + bool Double(double) { return true; } + bool RawNumber(const char*, SizeType, bool) { return true; } + bool String(const char*, SizeType, bool) { return true; } + bool StartObject() { return true; } + bool Key(const char*, SizeType, bool) { return true; } + bool EndObject(SizeType) { return true; } + bool StartArray() { return true; } + bool EndArray(SizeType) { return true; } +}; + + TEST(JsonChecker, Reader) { char filename[256]; @@ -67,13 +85,26 @@ TEST(JsonChecker, Reader) { continue; } + // Test stack-based parsing. GenericDocument, CrtAllocator> document; // Use Crt allocator to check exception-safety (no memory leak) document.Parse(json); EXPECT_TRUE(document.HasParseError()) << filename; + // Test iterative parsing. document.Parse(json); EXPECT_TRUE(document.HasParseError()) << filename; + // Test iterative pull-parsing. + Reader reader; + StringStream ss(json); + NoOpHandler h; + reader.IterativeParseInit(); + while (!reader.IterativeParseComplete()) { + if (!reader.IterativeParseNext(ss, h)) + break; + } + EXPECT_TRUE(reader.HasParseError()) << filename; + free(json); } @@ -87,12 +118,25 @@ TEST(JsonChecker, Reader) { continue; } + // Test stack-based parsing. GenericDocument, CrtAllocator> document; // Use Crt allocator to check exception-safety (no memory leak) document.Parse(json); EXPECT_FALSE(document.HasParseError()) << filename; + // Test iterative parsing. document.Parse(json); EXPECT_FALSE(document.HasParseError()) << filename; + + // Test iterative pull-parsing. + Reader reader; + StringStream ss(json); + NoOpHandler h; + reader.IterativeParseInit(); + while (!reader.IterativeParseComplete()) { + if (!reader.IterativeParseNext(ss, h)) + break; + } + EXPECT_FALSE(reader.HasParseError()) << filename; free(json); } From c4117c68ccf45e2f80ea7693766db0c771b6d508 Mon Sep 17 00:00:00 2001 From: StilesCrisis Date: Wed, 22 Feb 2017 21:54:31 -0800 Subject: [PATCH 182/305] Put in unit tests to catch parser failure MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Noticed that the reader could over-consume “NaN” if token terminated in the middle. --- test/unittest/readertest.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/unittest/readertest.cpp b/test/unittest/readertest.cpp index ac5a067..0973791 100644 --- a/test/unittest/readertest.cpp +++ b/test/unittest/readertest.cpp @@ -1832,6 +1832,10 @@ TEST(Reader, ParseNanAndInfinity) { TEST_NAN_INF("Infinity", inf); TEST_NAN_INF("-Inf", -inf); TEST_NAN_INF("-Infinity", -inf); + TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "NInf", 1); + TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "NaInf", 1); + TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "INan", 1); + TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "InNan", 1); TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "nan", 1); TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "-nan", 1); TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "NAN", 1); From 5e785d3db20cdad256a7c0139001b484ea37fab9 Mon Sep 17 00:00:00 2001 From: StilesCrisis Date: Thu, 23 Feb 2017 00:11:12 -0800 Subject: [PATCH 183/305] Fix parsing of NaN/Inf MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A failed half-consume of “NaN” now returns “value invalid” instead of attempting to consume an “Inf”. --- include/rapidjson/reader.h | 27 ++++++++++++++++++--------- test/unittest/readertest.cpp | 4 ++-- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/include/rapidjson/reader.h b/include/rapidjson/reader.h index dbb5e16..c1d10e8 100644 --- a/include/rapidjson/reader.h +++ b/include/rapidjson/reader.h @@ -1170,18 +1170,27 @@ private: } // Parse NaN or Infinity here else if ((parseFlags & kParseNanAndInfFlag) && RAPIDJSON_LIKELY((s.Peek() == 'I' || s.Peek() == 'N'))) { - useNanOrInf = true; - if (RAPIDJSON_LIKELY(Consume(s, 'N') && Consume(s, 'a') && Consume(s, 'N'))) { - d = std::numeric_limits::quiet_NaN(); + if (Consume(s, 'N')) { + if (Consume(s, 'a') && Consume(s, 'N')) { + d = std::numeric_limits::quiet_NaN(); + useNanOrInf = true; + } } - else if (RAPIDJSON_LIKELY(Consume(s, 'I') && Consume(s, 'n') && Consume(s, 'f'))) { - d = (minus ? -std::numeric_limits::infinity() : std::numeric_limits::infinity()); - if (RAPIDJSON_UNLIKELY(s.Peek() == 'i' && !(Consume(s, 'i') && Consume(s, 'n') - && Consume(s, 'i') && Consume(s, 't') && Consume(s, 'y')))) - RAPIDJSON_PARSE_ERROR(kParseErrorValueInvalid, s.Tell()); + else if (RAPIDJSON_LIKELY(Consume(s, 'I'))) { + if (Consume(s, 'n') && Consume(s, 'f')) { + d = (minus ? -std::numeric_limits::infinity() : std::numeric_limits::infinity()); + useNanOrInf = true; + + if (RAPIDJSON_UNLIKELY(s.Peek() == 'i' && !(Consume(s, 'i') && Consume(s, 'n') + && Consume(s, 'i') && Consume(s, 't') && Consume(s, 'y')))) { + RAPIDJSON_PARSE_ERROR(kParseErrorValueInvalid, s.Tell()); + } + } } - else + + if (RAPIDJSON_UNLIKELY(!useNanOrInf)) { RAPIDJSON_PARSE_ERROR(kParseErrorValueInvalid, s.Tell()); + } } else RAPIDJSON_PARSE_ERROR(kParseErrorValueInvalid, s.Tell()); diff --git a/test/unittest/readertest.cpp b/test/unittest/readertest.cpp index 0973791..2217a12 100644 --- a/test/unittest/readertest.cpp +++ b/test/unittest/readertest.cpp @@ -1833,9 +1833,9 @@ TEST(Reader, ParseNanAndInfinity) { TEST_NAN_INF("-Inf", -inf); TEST_NAN_INF("-Infinity", -inf); TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "NInf", 1); - TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "NaInf", 1); + TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "NaInf", 2); TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "INan", 1); - TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "InNan", 1); + TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "InNan", 2); TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "nan", 1); TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "-nan", 1); TEST_NAN_INF_ERROR(kParseErrorValueInvalid, "NAN", 1); From b977fd3c9d21c758d3cf74778458d573d3897b33 Mon Sep 17 00:00:00 2001 From: ylavic Date: Fri, 24 Feb 2017 16:46:53 +0100 Subject: [PATCH 184/305] Missing "internal" namespace for StrLen include/rapidjson/pointer.h:243:40: error: 'StrLen' was not declared in this scope return Append(name, StrLen(name), allocator); --- include/rapidjson/pointer.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/rapidjson/pointer.h b/include/rapidjson/pointer.h index bc7acfd..0f377ef 100644 --- a/include/rapidjson/pointer.h +++ b/include/rapidjson/pointer.h @@ -240,7 +240,7 @@ public: template RAPIDJSON_DISABLEIF_RETURN((internal::NotExpr::Type, Ch> >), (GenericPointer)) Append(T* name, Allocator* allocator = 0) const { - return Append(name, StrLen(name), allocator); + return Append(name, internal::StrLen(name), allocator); } #if RAPIDJSON_HAS_STDSTRING From 5f92c3926b185dcaf95aaa9e524f573adbfeed36 Mon Sep 17 00:00:00 2001 From: oviradoi Date: Fri, 24 Feb 2017 19:50:36 +0200 Subject: [PATCH 185/305] Fix creating the nuget package with Raggles' fork of CoApp --- rapidjson.autopkg | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rapidjson.autopkg b/rapidjson.autopkg index 70eb0d8..486ad14 100644 --- a/rapidjson.autopkg +++ b/rapidjson.autopkg @@ -71,5 +71,7 @@ Changed targets { // We're trying to be standard about these sorts of thing. (Will help with config.h later :D) //Defines += HAS_EQCORE; + // Fix creating the package with Raggles' fork of CoApp + Includes += "$(MSBuildThisFileDirectory)../..${d_include}"; }; } \ No newline at end of file From 97e2f7f16f3b739a262f1391478be484e6cac8ba Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Tue, 28 Feb 2017 09:48:36 +0800 Subject: [PATCH 186/305] Try fixing Error compilation Ubuntu 14.04 #834 --- include/rapidjson/schema.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/rapidjson/schema.h b/include/rapidjson/schema.h index 3f81d9b..c20a838 100644 --- a/include/rapidjson/schema.h +++ b/include/rapidjson/schema.h @@ -880,7 +880,7 @@ public: #define RAPIDJSON_STRING_(name, ...) \ static const ValueType& Get##name##String() {\ static const Ch s[] = { __VA_ARGS__, '\0' };\ - static const ValueType v(s, sizeof(s) / sizeof(Ch) - 1);\ + static const ValueType v(s, static_cast(sizeof(s) / sizeof(Ch) - 1));\ return v;\ } From 595b114216f8899eb72695394bff9cb5856313e6 Mon Sep 17 00:00:00 2001 From: StilesCrisis Date: Mon, 27 Feb 2017 22:36:48 -0800 Subject: [PATCH 187/305] Unit test Add unit tests expecting an assertion when writing an object with a key but no value. --- test/unittest/writertest.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/test/unittest/writertest.cpp b/test/unittest/writertest.cpp index d346e0f..398a63d 100644 --- a/test/unittest/writertest.cpp +++ b/test/unittest/writertest.cpp @@ -442,6 +442,28 @@ TEST(Writer, InvalidEventSequence) { EXPECT_THROW(writer.Int(1), AssertException); EXPECT_FALSE(writer.IsComplete()); } + + // { 'a' } + { + StringBuffer buffer; + Writer writer(buffer); + writer.StartObject(); + writer.Key("a"); + EXPECT_THROW(writer.EndObject(), AssertException); + EXPECT_FALSE(writer.IsComplete()); + } + + // { 'a':'b','c' } + { + StringBuffer buffer; + Writer writer(buffer); + writer.StartObject(); + writer.Key("a"); + writer.String("b"); + writer.Key("c"); + EXPECT_THROW(writer.EndObject(), AssertException); + EXPECT_FALSE(writer.IsComplete()); + } } TEST(Writer, NaN) { From 2e9b7b1ae6ef162b5aa0c04f2b4444be4c8b72cb Mon Sep 17 00:00:00 2001 From: StilesCrisis Date: Mon, 27 Feb 2017 22:44:13 -0800 Subject: [PATCH 188/305] Added assertion Documented existing assertions in EndObject Added new assertion in EndObject to catch error condition where objects are ended with a key but no matching value. --- include/rapidjson/writer.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/include/rapidjson/writer.h b/include/rapidjson/writer.h index 5b3004b..43ec5dc 100644 --- a/include/rapidjson/writer.h +++ b/include/rapidjson/writer.h @@ -221,8 +221,9 @@ public: bool EndObject(SizeType memberCount = 0) { (void)memberCount; - RAPIDJSON_ASSERT(level_stack_.GetSize() >= sizeof(Level)); - RAPIDJSON_ASSERT(!level_stack_.template Top()->inArray); + RAPIDJSON_ASSERT(level_stack_.GetSize() >= sizeof(Level)); // not inside an Object + RAPIDJSON_ASSERT(!level_stack_.template Top()->inArray); // currently inside an Array, not Object + RAPIDJSON_ASSERT(0 == level_stack_.template Top()->valueCount % 2); // Object has a Key without a Value level_stack_.template Pop(1); return EndValue(WriteEndObject()); } From fa84cd18f48294dc94f659c3f7a272b26ea5b1b5 Mon Sep 17 00:00:00 2001 From: StilesCrisis Date: Mon, 27 Feb 2017 22:53:59 -0800 Subject: [PATCH 189/305] Add matching fix for PrettyWriter PrettyWriter EndObject will now also assert if called when a key is missing its matching value. --- include/rapidjson/prettywriter.h | 6 ++-- test/unittest/prettywritertest.cpp | 51 ++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/include/rapidjson/prettywriter.h b/include/rapidjson/prettywriter.h index d663208..b68b687 100644 --- a/include/rapidjson/prettywriter.h +++ b/include/rapidjson/prettywriter.h @@ -136,8 +136,10 @@ public: bool EndObject(SizeType memberCount = 0) { (void)memberCount; - RAPIDJSON_ASSERT(Base::level_stack_.GetSize() >= sizeof(typename Base::Level)); - RAPIDJSON_ASSERT(!Base::level_stack_.template Top()->inArray); + RAPIDJSON_ASSERT(Base::level_stack_.GetSize() >= sizeof(typename Base::Level)); // not inside an Object + RAPIDJSON_ASSERT(!Base::level_stack_.template Top()->inArray); // currently inside an Array, not Object + RAPIDJSON_ASSERT(0 == Base::level_stack_.template Top()->valueCount % 2); // Object has a Key without a Value + bool empty = Base::level_stack_.template Pop(1)->valueCount == 0; if (!empty) { diff --git a/test/unittest/prettywritertest.cpp b/test/unittest/prettywritertest.cpp index 13d1a8d..2891c76 100644 --- a/test/unittest/prettywritertest.cpp +++ b/test/unittest/prettywritertest.cpp @@ -207,6 +207,57 @@ TEST(PrettyWriter, RawValue) { buffer.GetString()); } +TEST(PrettyWriter, InvalidEventSequence) { + // {] + { + StringBuffer buffer; + PrettyWriter writer(buffer); + writer.StartObject(); + EXPECT_THROW(writer.EndArray(), AssertException); + EXPECT_FALSE(writer.IsComplete()); + } + + // [} + { + StringBuffer buffer; + PrettyWriter writer(buffer); + writer.StartArray(); + EXPECT_THROW(writer.EndObject(), AssertException); + EXPECT_FALSE(writer.IsComplete()); + } + + // { 1: + { + StringBuffer buffer; + PrettyWriter writer(buffer); + writer.StartObject(); + EXPECT_THROW(writer.Int(1), AssertException); + EXPECT_FALSE(writer.IsComplete()); + } + + // { 'a' } + { + StringBuffer buffer; + PrettyWriter writer(buffer); + writer.StartObject(); + writer.Key("a"); + EXPECT_THROW(writer.EndObject(), AssertException); + EXPECT_FALSE(writer.IsComplete()); + } + + // { 'a':'b','c' } + { + StringBuffer buffer; + PrettyWriter writer(buffer); + writer.StartObject(); + writer.Key("a"); + writer.String("b"); + writer.Key("c"); + EXPECT_THROW(writer.EndObject(), AssertException); + EXPECT_FALSE(writer.IsComplete()); + } +} + #if RAPIDJSON_HAS_CXX11_RVALUE_REFS static PrettyWriter WriterGen(StringBuffer &target) { From 0ec4e86f14a0b801cee4a707f51245a6b735fef2 Mon Sep 17 00:00:00 2001 From: StilesCrisis Date: Mon, 27 Feb 2017 23:06:05 -0800 Subject: [PATCH 190/305] Unit test Add unit test for Issue 848 (segfault in ~Document) --- test/unittest/schematest.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/unittest/schematest.cpp b/test/unittest/schematest.cpp index 4780516..30b3260 100644 --- a/test/unittest/schematest.cpp +++ b/test/unittest/schematest.cpp @@ -1281,6 +1281,12 @@ TEST(SchemaValidatingWriter, Simple) { EXPECT_TRUE(validator.GetInvalidDocumentPointer() == SchemaDocument::PointerType("")); } +TEST(Schema, Issue848) { + rapidjson::Document d; + rapidjson::SchemaDocument s(d); + rapidjson::GenericSchemaValidator v(s); +} + #if RAPIDJSON_HAS_CXX11_RVALUE_REFS static SchemaDocument ReturnSchemaDocument() { From 4643104b8a0424f8f645b2777fbcdccf9a17acbf Mon Sep 17 00:00:00 2001 From: StilesCrisis Date: Mon, 27 Feb 2017 23:28:25 -0800 Subject: [PATCH 191/305] Fix null handler construction We should not malloc the null-handler object and cast to OutputHandler; we need to actually invoke the constructor via placement new. --- include/rapidjson/schema.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/rapidjson/schema.h b/include/rapidjson/schema.h index c20a838..3dddd3a 100644 --- a/include/rapidjson/schema.h +++ b/include/rapidjson/schema.h @@ -1928,7 +1928,7 @@ private: const Context& CurrentContext() const { return *schemaStack_.template Top(); } OutputHandler& CreateNullHandler() { - return *(nullHandler_ = static_cast(GetStateAllocator().Malloc(sizeof(OutputHandler)))); + return *(nullHandler_ = new (GetStateAllocator().Malloc(sizeof(OutputHandler))) OutputHandler); } static const size_t kDefaultSchemaStackCapacity = 1024; From 5c2bb187725f27de73cb1ca8b26750e0e139046b Mon Sep 17 00:00:00 2001 From: StilesCrisis Date: Tue, 28 Feb 2017 00:06:02 -0800 Subject: [PATCH 192/305] Add IterativeParse docs --- doc/sax.md | 51 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 37 insertions(+), 14 deletions(-) diff --git a/doc/sax.md b/doc/sax.md index ed6d46a..c716d3a 100644 --- a/doc/sax.md +++ b/doc/sax.md @@ -8,7 +8,7 @@ In RapidJSON, `Reader` (typedef of `GenericReader<...>`) is the SAX-style parser # Reader {#Reader} -`Reader` parses a JSON from a stream. While it reads characters from the stream, it analyze the characters according to the syntax of JSON, and publish events to a handler. +`Reader` parses a JSON from a stream. While it reads characters from the stream, it analyzes the characters according to the syntax of JSON, and publishes events to a handler. For example, here is a JSON. @@ -24,7 +24,7 @@ For example, here is a JSON. } ~~~~~~~~~~ -While a `Reader` parses this JSON, it publishes the following events to the handler sequentially: +When a `Reader` parses this JSON, it publishes the following events to the handler sequentially: ~~~~~~~~~~ StartObject() @@ -50,7 +50,7 @@ EndArray(4) EndObject(7) ~~~~~~~~~~ -These events can be easily matched with the JSON, except some event parameters need further explanation. Let's see the `simplereader` example which produces exactly the same output as above: +These events can be easily matched with the JSON, but some event parameters need further explanation. Let's see the `simplereader` example which produces exactly the same output as above: ~~~~~~~~~~cpp #include "rapidjson/reader.h" @@ -91,11 +91,11 @@ void main() { } ~~~~~~~~~~ -Note that, RapidJSON uses template to statically bind the `Reader` type and the handler type, instead of using class with virtual functions. This paradigm can improve the performance by inlining functions. +Note that RapidJSON uses templates to statically bind the `Reader` type and the handler type, instead of using classes with virtual functions. This paradigm can improve performance by inlining functions. ## Handler {#Handler} -As the previous example showed, user needs to implement a handler, which consumes the events (function calls) from `Reader`. The handler must contain the following member functions. +As shown in the previous example, the user needs to implement a handler which consumes the events (via function calls) from the `Reader`. The handler must contain the following member functions. ~~~~~~~~~~cpp class Handler { @@ -122,15 +122,15 @@ class Handler { When the `Reader` encounters a JSON number, it chooses a suitable C++ type mapping. And then it calls *one* function out of `Int(int)`, `Uint(unsigned)`, `Int64(int64_t)`, `Uint64(uint64_t)` and `Double(double)`. If `kParseNumbersAsStrings` is enabled, `Reader` will always calls `RawNumber()` instead. -`String(const char* str, SizeType length, bool copy)` is called when the `Reader` encounters a string. The first parameter is pointer to the string. The second parameter is the length of the string (excluding the null terminator). Note that RapidJSON supports null character `\0` inside a string. If such situation happens, `strlen(str) < length`. The last `copy` indicates whether the handler needs to make a copy of the string. For normal parsing, `copy = true`. Only when *insitu* parsing is used, `copy = false`. And beware that, the character type depends on the target encoding, which will be explained later. +`String(const char* str, SizeType length, bool copy)` is called when the `Reader` encounters a string. The first parameter is pointer to the string. The second parameter is the length of the string (excluding the null terminator). Note that RapidJSON supports null character `\0` inside a string. If such situation happens, `strlen(str) < length`. The last `copy` indicates whether the handler needs to make a copy of the string. For normal parsing, `copy = true`. Only when *insitu* parsing is used, `copy = false`. And be aware that the character type depends on the target encoding, which will be explained later. -When the `Reader` encounters the beginning of an object, it calls `StartObject()`. An object in JSON is a set of name-value pairs. If the object contains members it first calls `Key()` for the name of member, and then calls functions depending on the type of the value. These calls of name-value pairs repeats until calling `EndObject(SizeType memberCount)`. Note that the `memberCount` parameter is just an aid for the handler, user may not need this parameter. +When the `Reader` encounters the beginning of an object, it calls `StartObject()`. An object in JSON is a set of name-value pairs. If the object contains members it first calls `Key()` for the name of member, and then calls functions depending on the type of the value. These calls of name-value pairs repeat until calling `EndObject(SizeType memberCount)`. Note that the `memberCount` parameter is just an aid for the handler; users who do not need this parameter may ignore it. -Array is similar to object but simpler. At the beginning of an array, the `Reader` calls `BeginArary()`. If there is elements, it calls functions according to the types of element. Similarly, in the last call `EndArray(SizeType elementCount)`, the parameter `elementCount` is just an aid for the handler. +Arrays are similar to objects, but simpler. At the beginning of an array, the `Reader` calls `BeginArary()`. If there is elements, it calls functions according to the types of element. Similarly, in the last call `EndArray(SizeType elementCount)`, the parameter `elementCount` is just an aid for the handler. -Every handler functions returns a `bool`. Normally it should returns `true`. If the handler encounters an error, it can return `false` to notify event publisher to stop further processing. +Every handler function returns a `bool`. Normally it should return `true`. If the handler encounters an error, it can return `false` to notify the event publisher to stop further processing. -For example, when we parse a JSON with `Reader` and the handler detected that the JSON does not conform to the required schema, then the handler can return `false` and let the `Reader` stop further parsing. And the `Reader` will be in error state with error code `kParseErrorTermination`. +For example, when we parse a JSON with `Reader` and the handler detects that the JSON does not conform to the required schema, the handler can return `false` and let the `Reader` stop further parsing. This will place the `Reader` in an error state, with error code `kParseErrorTermination`. ## GenericReader {#GenericReader} @@ -149,19 +149,19 @@ typedef GenericReader, UTF8<> > Reader; } // namespace rapidjson ~~~~~~~~~~ -The `Reader` uses UTF-8 as both source and target encoding. The source encoding means the encoding in the JSON stream. The target encoding means the encoding of the `str` parameter in `String()` calls. For example, to parse a UTF-8 stream and outputs UTF-16 string events, you can define a reader by: +The `Reader` uses UTF-8 as both source and target encoding. The source encoding means the encoding in the JSON stream. The target encoding means the encoding of the `str` parameter in `String()` calls. For example, to parse a UTF-8 stream and output UTF-16 string events, you can define a reader by: ~~~~~~~~~~cpp GenericReader, UTF16<> > reader; ~~~~~~~~~~ -Note that, the default character type of `UTF16` is `wchar_t`. So this `reader`needs to call `String(const wchar_t*, SizeType, bool)` of the handler. +Note that, the default character type of `UTF16` is `wchar_t`. So this `reader` needs to call `String(const wchar_t*, SizeType, bool)` of the handler. The third template parameter `Allocator` is the allocator type for internal data structure (actually a stack). ## Parsing {#SaxParsing} -The one and only one function of `Reader` is to parse JSON. +The main function of `Reader` is used to parse JSON. ~~~~~~~~~~cpp template @@ -172,7 +172,30 @@ template bool Parse(InputStream& is, Handler& handler); ~~~~~~~~~~ -If an error occurs during parsing, it will return `false`. User can also calls `bool HasParseEror()`, `ParseErrorCode GetParseErrorCode()` and `size_t GetErrorOffset()` to obtain the error states. Actually `Document` uses these `Reader` functions to obtain parse errors. Please refer to [DOM](doc/dom.md) for details about parse error. +If an error occurs during parsing, it will return `false`. User can also call `bool HasParseError()`, `ParseErrorCode GetParseErrorCode()` and `size_t GetErrorOffset()` to obtain the error states. In fact, `Document` uses these `Reader` functions to obtain parse errors. Please refer to [DOM](doc/dom.md) for details about parse errors. + +## Token-by-Token Parsing {#TokenByTokenParsing} + +Some users may wish to parse a JSON input stream a single token at a time, instead of immediately parsing an entire document without stopping. To parse JSON this way, instead of calling `Parse`, you can use the `IterativeParse` set of functions: + +~~~~~~~~~~cpp + void IterativeParseInit(); + + template + bool IterativeParseNext(InputStream& is, Handler& handler); + + bool IterativeParseComplete(); +~~~~~~~~~~ + +Here is an example of iteratively parsing JSON, token by token: + +~~~~~~~~~~cpp + reader.IterativeParseInit(); + while (!reader.IterativeParseComplete()) { + reader.IterativeParseNext(ss, handler); + // Your handler has been called once. + } +~~~~~~~~~~ # Writer {#Writer} From 0f3bf99d58a4d704246c1bba6183838629960abd Mon Sep 17 00:00:00 2001 From: StilesCrisis Date: Tue, 28 Feb 2017 00:08:30 -0800 Subject: [PATCH 193/305] Tiny fix Make example code var names match API above for consistency --- doc/sax.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/sax.md b/doc/sax.md index c716d3a..4867880 100644 --- a/doc/sax.md +++ b/doc/sax.md @@ -192,7 +192,7 @@ Here is an example of iteratively parsing JSON, token by token: ~~~~~~~~~~cpp reader.IterativeParseInit(); while (!reader.IterativeParseComplete()) { - reader.IterativeParseNext(ss, handler); + reader.IterativeParseNext(is, handler); // Your handler has been called once. } ~~~~~~~~~~ From 6e2e5c7dbe08474249ca18b50da120b2c45ccc36 Mon Sep 17 00:00:00 2001 From: StilesCrisis Date: Tue, 28 Feb 2017 01:11:30 -0800 Subject: [PATCH 194/305] Specialize StrLen for char/wchar_t Compilers generally provide a much smarter strlen than ours. --- include/rapidjson/internal/strfunc.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/include/rapidjson/internal/strfunc.h b/include/rapidjson/internal/strfunc.h index de41d8f..226439a 100644 --- a/include/rapidjson/internal/strfunc.h +++ b/include/rapidjson/internal/strfunc.h @@ -16,6 +16,7 @@ #define RAPIDJSON_INTERNAL_STRFUNC_H_ #include "../stream.h" +#include RAPIDJSON_NAMESPACE_BEGIN namespace internal { @@ -34,6 +35,16 @@ inline SizeType StrLen(const Ch* s) { return SizeType(p - s); } +template <> +inline SizeType StrLen(const char* s) { + return SizeType(std::strlen(s)); +} + +template <> +inline SizeType StrLen(const wchar_t* s) { + return SizeType(std::wcslen(s)); +} + //! Returns number of code points in a encoded string. template bool CountStringCodePoint(const typename Encoding::Ch* s, SizeType length, SizeType* outCount) { From 4b822a41af98974a4ca96b79cd13afe163214d81 Mon Sep 17 00:00:00 2001 From: John Stiles Date: Tue, 28 Feb 2017 19:31:21 -0800 Subject: [PATCH 195/305] Attempt to suppress valgrind wcslen error --- test/unittest/CMakeLists.txt | 2 +- test/valgrind.supp | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 test/valgrind.supp diff --git a/test/unittest/CMakeLists.txt b/test/unittest/CMakeLists.txt index 4e29765..fdf0ad0 100644 --- a/test/unittest/CMakeLists.txt +++ b/test/unittest/CMakeLists.txt @@ -79,7 +79,7 @@ add_test(NAME unittest if(NOT MSVC) # Not running SIMD.* unit test cases for Valgrind add_test(NAME valgrind_unittest - COMMAND valgrind --leak-check=full --error-exitcode=1 ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/unittest --gtest_filter=-SIMD.* + COMMAND valgrind --suppressions=${CMAKE_SOURCE_DIR}/test/valgrind.supp --leak-check=full --error-exitcode=1 ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/unittest --gtest_filter=-SIMD.* WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/bin) if(CMAKE_BUILD_TYPE STREQUAL "Debug") diff --git a/test/valgrind.supp b/test/valgrind.supp new file mode 100644 index 0000000..5a205b7 --- /dev/null +++ b/test/valgrind.supp @@ -0,0 +1,5 @@ +{ + Suppress wcslen valgrind report + Memcheck:Addr8 + fun:__wcslen_sse2 +} From 13e99d8d5ffea627a169734128d38a31a7895b29 Mon Sep 17 00:00:00 2001 From: John Stiles Date: Tue, 28 Feb 2017 22:58:24 -0800 Subject: [PATCH 196/305] Trivial change to re-trigger Travis CI No-op blank line --- test/unittest/writertest.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/test/unittest/writertest.cpp b/test/unittest/writertest.cpp index 398a63d..8fd6eb8 100644 --- a/test/unittest/writertest.cpp +++ b/test/unittest/writertest.cpp @@ -544,3 +544,4 @@ TEST(Writer, MoveCtor) { #ifdef __clang__ RAPIDJSON_DIAG_POP #endif + From 3f9ebfe9e9e9b4ac57f8d58fbce390b4f9a5977e Mon Sep 17 00:00:00 2001 From: John Stiles Date: Thu, 2 Mar 2017 21:24:03 -0800 Subject: [PATCH 197/305] Trivial change to trigger Travis CI --- include/rapidjson/writer.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/rapidjson/writer.h b/include/rapidjson/writer.h index 43ec5dc..12d3145 100644 --- a/include/rapidjson/writer.h +++ b/include/rapidjson/writer.h @@ -623,3 +623,4 @@ RAPIDJSON_DIAG_POP #endif #endif // RAPIDJSON_RAPIDJSON_H_ + From 534f1352619328668e8bc4ffaf9bacb5de697180 Mon Sep 17 00:00:00 2001 From: John Stiles Date: Fri, 3 Mar 2017 00:21:10 -0800 Subject: [PATCH 198/305] Try again to suppress Valgrind --- test/valgrind.supp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/valgrind.supp b/test/valgrind.supp index 5a205b7..8552385 100644 --- a/test/valgrind.supp +++ b/test/valgrind.supp @@ -1,5 +1,5 @@ { Suppress wcslen valgrind report - Memcheck:Addr8 + Memcheck:Cond fun:__wcslen_sse2 } From 6ae50ad6e32030c2d930b313a1c740acbbb0cca6 Mon Sep 17 00:00:00 2001 From: John Stiles Date: Fri, 3 Mar 2017 00:27:47 -0800 Subject: [PATCH 199/305] Once again --- test/valgrind.supp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/test/valgrind.supp b/test/valgrind.supp index 8552385..1fed18b 100644 --- a/test/valgrind.supp +++ b/test/valgrind.supp @@ -1,5 +1,17 @@ { - Suppress wcslen valgrind report + Suppress wcslen valgrind report 1 Memcheck:Cond fun:__wcslen_sse2 } + +{ + Suppress wcslen valgrind report 2 + Memcheck:Addr8 + fun:__wcslen_sse2 +} + +{ + Suppress wcslen valgrind report 3 + Memcheck:Value8 + fun:__wcslen_sse2 +} From db8d3bb4d60aa20c5d6ec4be1f6e71c33d9874b9 Mon Sep 17 00:00:00 2001 From: John Stiles Date: Fri, 3 Mar 2017 00:42:00 -0800 Subject: [PATCH 200/305] Remove unneeded change --- include/rapidjson/writer.h | 1 - 1 file changed, 1 deletion(-) diff --git a/include/rapidjson/writer.h b/include/rapidjson/writer.h index 12d3145..43ec5dc 100644 --- a/include/rapidjson/writer.h +++ b/include/rapidjson/writer.h @@ -623,4 +623,3 @@ RAPIDJSON_DIAG_POP #endif #endif // RAPIDJSON_RAPIDJSON_H_ - From 66b564f385d96d7adbe1ba3073a140f5301e0af6 Mon Sep 17 00:00:00 2001 From: John Stiles Date: Fri, 3 Mar 2017 00:42:21 -0800 Subject: [PATCH 201/305] Remove unneeded change --- test/unittest/writertest.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/test/unittest/writertest.cpp b/test/unittest/writertest.cpp index 8fd6eb8..398a63d 100644 --- a/test/unittest/writertest.cpp +++ b/test/unittest/writertest.cpp @@ -544,4 +544,3 @@ TEST(Writer, MoveCtor) { #ifdef __clang__ RAPIDJSON_DIAG_POP #endif - From d6e9cf5d54ee40dcbe4cc939f33cdf41878a71d7 Mon Sep 17 00:00:00 2001 From: Erik Froseth Date: Fri, 3 Mar 2017 09:48:41 +0100 Subject: [PATCH 202/305] Remove executable bit Remove the executable bit for various .json files --- bin/types/booleans.json | Bin bin/types/floats.json | Bin bin/types/guids.json | Bin bin/types/integers.json | Bin bin/types/mixed.json | Bin bin/types/nulls.json | Bin bin/types/paragraphs.json | Bin 7 files changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 bin/types/booleans.json mode change 100755 => 100644 bin/types/floats.json mode change 100755 => 100644 bin/types/guids.json mode change 100755 => 100644 bin/types/integers.json mode change 100755 => 100644 bin/types/mixed.json mode change 100755 => 100644 bin/types/nulls.json mode change 100755 => 100644 bin/types/paragraphs.json diff --git a/bin/types/booleans.json b/bin/types/booleans.json old mode 100755 new mode 100644 diff --git a/bin/types/floats.json b/bin/types/floats.json old mode 100755 new mode 100644 diff --git a/bin/types/guids.json b/bin/types/guids.json old mode 100755 new mode 100644 diff --git a/bin/types/integers.json b/bin/types/integers.json old mode 100755 new mode 100644 diff --git a/bin/types/mixed.json b/bin/types/mixed.json old mode 100755 new mode 100644 diff --git a/bin/types/nulls.json b/bin/types/nulls.json old mode 100755 new mode 100644 diff --git a/bin/types/paragraphs.json b/bin/types/paragraphs.json old mode 100755 new mode 100644 From dd97ede84d517e2a1d433c5bfc1610d5d769a430 Mon Sep 17 00:00:00 2001 From: John Stiles Date: Sun, 5 Mar 2017 00:27:08 -0800 Subject: [PATCH 203/305] Quoted strings to String() or Key() are auto-sized by template No strlen call needs to be made when templates can auto-deduce the string length. No strlen = faster! Unfortunately this needs a touch of SFINAE to allow multiple overrides to coexist cleanly. --- include/rapidjson/writer.h | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/include/rapidjson/writer.h b/include/rapidjson/writer.h index 43ec5dc..755f483 100644 --- a/include/rapidjson/writer.h +++ b/include/rapidjson/writer.h @@ -16,6 +16,7 @@ #define RAPIDJSON_WRITER_H_ #include "stream.h" +#include "internal/meta.h" #include "internal/stack.h" #include "internal/strfunc.h" #include "internal/dtoa.h" @@ -198,7 +199,8 @@ public: return EndValue(WriteString(str, length)); } - bool String(const Ch* str, SizeType length, bool copy = false) { + template + bool String(const T* str, SizeType length, bool copy = false, RAPIDJSON_ENABLEIF((internal::IsSame))) { RAPIDJSON_ASSERT(str != 0); (void)copy; Prefix(kStringType); @@ -217,7 +219,8 @@ public: return WriteStartObject(); } - bool Key(const Ch* str, SizeType length, bool copy = false) { return String(str, length, copy); } + template + bool Key(const T* str, SizeType length, bool copy = false, RAPIDJSON_ENABLEIF((internal::IsSame))) { return String(str, length, copy); } bool EndObject(SizeType memberCount = 0) { (void)memberCount; @@ -247,8 +250,16 @@ public: //@{ //! Simpler but slower overload. - bool String(const Ch* str) { return String(str, internal::StrLen(str)); } - bool Key(const Ch* str) { return Key(str, internal::StrLen(str)); } + template + bool String(const T* const& str, RAPIDJSON_ENABLEIF((internal::IsSame))) { return String(str, internal::StrLen(str)); } + template + bool Key(const T* const& str, RAPIDJSON_ENABLEIF((internal::IsSame))) { return Key(str, internal::StrLen(str)); } + + //! The compiler can give us the length of quoted strings for free. + template + bool String(const T (&str)[N], RAPIDJSON_ENABLEIF((internal::IsSame))) { return String(str, N-1); } + template + bool Key(const T (&str)[N], RAPIDJSON_ENABLEIF((internal::IsSame))) { return Key(str, N-1); } //@} From 61f8c4ef0df9d91fe6a684bb1b1572e0a537f66e Mon Sep 17 00:00:00 2001 From: John Stiles Date: Sun, 5 Mar 2017 00:38:34 -0800 Subject: [PATCH 204/305] Quoted strings to String() or Key() are auto-sized by template Same fix as previous commit, to prettywriter --- include/rapidjson/prettywriter.h | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/include/rapidjson/prettywriter.h b/include/rapidjson/prettywriter.h index b68b687..64301b8 100644 --- a/include/rapidjson/prettywriter.h +++ b/include/rapidjson/prettywriter.h @@ -107,7 +107,8 @@ public: return Base::WriteString(str, length); } - bool String(const Ch* str, SizeType length, bool copy = false) { + template + bool String(const T* str, SizeType length, bool copy = false, RAPIDJSON_ENABLEIF((internal::IsSame))) { RAPIDJSON_ASSERT(str != 0); (void)copy; PrettyPrefix(kStringType); @@ -126,7 +127,8 @@ public: return Base::WriteStartObject(); } - bool Key(const Ch* str, SizeType length, bool copy = false) { return String(str, length, copy); } + template + bool Key(const T* str, SizeType length, bool copy = false, RAPIDJSON_ENABLEIF((internal::IsSame))) { return String(str, length, copy); } #if RAPIDJSON_HAS_STDSTRING bool Key(const std::basic_string& str) { @@ -184,8 +186,16 @@ public: //@{ //! Simpler but slower overload. - bool String(const Ch* str) { return String(str, internal::StrLen(str)); } - bool Key(const Ch* str) { return Key(str, internal::StrLen(str)); } + template + bool String(const T* const& str, RAPIDJSON_ENABLEIF((internal::IsSame))) { return String(str, internal::StrLen(str)); } + template + bool Key(const T* const& str, RAPIDJSON_ENABLEIF((internal::IsSame))) { return Key(str, internal::StrLen(str)); } + + //! The compiler can give us the length of quoted strings for free. + template + bool String(const T (&str)[N], RAPIDJSON_ENABLEIF((internal::IsSame))) { return String(str, N-1); } + template + bool Key(const T (&str)[N], RAPIDJSON_ENABLEIF((internal::IsSame))) { return Key(str, N-1); } //@} From cdea825a0bc81531d2cd2758362b606106373477 Mon Sep 17 00:00:00 2001 From: John Stiles Date: Sun, 5 Mar 2017 09:23:03 -0800 Subject: [PATCH 205/305] Assert that String() and Key() are given null-terminated strings Assert in case users attempt to pass a char array to String() or Key() that is not null terminated; that is not the intended use of the API. Null terminate your string buffers. --- include/rapidjson/prettywriter.h | 10 ++++++++-- include/rapidjson/writer.h | 10 ++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/include/rapidjson/prettywriter.h b/include/rapidjson/prettywriter.h index 64301b8..abea404 100644 --- a/include/rapidjson/prettywriter.h +++ b/include/rapidjson/prettywriter.h @@ -193,9 +193,15 @@ public: //! The compiler can give us the length of quoted strings for free. template - bool String(const T (&str)[N], RAPIDJSON_ENABLEIF((internal::IsSame))) { return String(str, N-1); } + bool String(const T (&str)[N], RAPIDJSON_ENABLEIF((internal::IsSame))) { + 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 - bool Key(const T (&str)[N], RAPIDJSON_ENABLEIF((internal::IsSame))) { return Key(str, N-1); } + bool Key(const T (&str)[N], RAPIDJSON_ENABLEIF((internal::IsSame))) { + 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); + } //@} diff --git a/include/rapidjson/writer.h b/include/rapidjson/writer.h index 755f483..c438f71 100644 --- a/include/rapidjson/writer.h +++ b/include/rapidjson/writer.h @@ -257,9 +257,15 @@ public: //! The compiler can give us the length of quoted strings for free. template - bool String(const T (&str)[N], RAPIDJSON_ENABLEIF((internal::IsSame))) { return String(str, N-1); } + bool String(const T (&str)[N], RAPIDJSON_ENABLEIF((internal::IsSame))) { + 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 - bool Key(const T (&str)[N], RAPIDJSON_ENABLEIF((internal::IsSame))) { return Key(str, N-1); } + bool Key(const T (&str)[N], RAPIDJSON_ENABLEIF((internal::IsSame))) { + 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); + } //@} From c4e3d6243ce0321b32c9bfc7e3692753b21e46f8 Mon Sep 17 00:00:00 2001 From: John Stiles Date: Sun, 5 Mar 2017 09:50:03 -0800 Subject: [PATCH 206/305] Fix msvc x64 compilation issue Disambiguate by putting the ENABLEIF on the return value instead of in the argument list. --- include/rapidjson/prettywriter.h | 12 ++++++------ include/rapidjson/writer.h | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/include/rapidjson/prettywriter.h b/include/rapidjson/prettywriter.h index abea404..a9d0f02 100644 --- a/include/rapidjson/prettywriter.h +++ b/include/rapidjson/prettywriter.h @@ -108,7 +108,7 @@ public: } template - bool String(const T* str, SizeType length, bool copy = false, RAPIDJSON_ENABLEIF((internal::IsSame))) { + RAPIDJSON_ENABLEIF_RETURN((internal::IsSame), (bool)) String(const T* str, SizeType length, bool copy = false) { RAPIDJSON_ASSERT(str != 0); (void)copy; PrettyPrefix(kStringType); @@ -128,7 +128,7 @@ public: } template - bool Key(const T* str, SizeType length, bool copy = false, RAPIDJSON_ENABLEIF((internal::IsSame))) { return String(str, length, copy); } + RAPIDJSON_ENABLEIF_RETURN((internal::IsSame), (bool)) Key(const T* str, SizeType length, bool copy = false) { return String(str, length, copy); } #if RAPIDJSON_HAS_STDSTRING bool Key(const std::basic_string& str) { @@ -187,18 +187,18 @@ public: //! Simpler but slower overload. template - bool String(const T* const& str, RAPIDJSON_ENABLEIF((internal::IsSame))) { return String(str, internal::StrLen(str)); } + RAPIDJSON_ENABLEIF_RETURN((internal::IsSame), (bool)) String(const T* const& str) { return String(str, internal::StrLen(str)); } template - bool Key(const T* const& str, RAPIDJSON_ENABLEIF((internal::IsSame))) { return Key(str, internal::StrLen(str)); } + RAPIDJSON_ENABLEIF_RETURN((internal::IsSame), (bool)) Key(const T* const& str) { return Key(str, internal::StrLen(str)); } //! The compiler can give us the length of quoted strings for free. template - bool String(const T (&str)[N], RAPIDJSON_ENABLEIF((internal::IsSame))) { + RAPIDJSON_ENABLEIF_RETURN((internal::IsSame), (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 - bool Key(const T (&str)[N], RAPIDJSON_ENABLEIF((internal::IsSame))) { + RAPIDJSON_ENABLEIF_RETURN((internal::IsSame), (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); } diff --git a/include/rapidjson/writer.h b/include/rapidjson/writer.h index c438f71..7a0af39 100644 --- a/include/rapidjson/writer.h +++ b/include/rapidjson/writer.h @@ -200,7 +200,7 @@ public: } template - bool String(const T* str, SizeType length, bool copy = false, RAPIDJSON_ENABLEIF((internal::IsSame))) { + RAPIDJSON_ENABLEIF_RETURN((internal::IsSame), (bool)) String(const T* str, SizeType length, bool copy = false) { RAPIDJSON_ASSERT(str != 0); (void)copy; Prefix(kStringType); @@ -220,7 +220,7 @@ public: } template - bool Key(const T* str, SizeType length, bool copy = false, RAPIDJSON_ENABLEIF((internal::IsSame))) { return String(str, length, copy); } + RAPIDJSON_ENABLEIF_RETURN((internal::IsSame), (bool)) Key(const T* str, SizeType length, bool copy = false) { return String(str, length, copy); } bool EndObject(SizeType memberCount = 0) { (void)memberCount; @@ -251,18 +251,18 @@ public: //! Simpler but slower overload. template - bool String(const T* const& str, RAPIDJSON_ENABLEIF((internal::IsSame))) { return String(str, internal::StrLen(str)); } + RAPIDJSON_ENABLEIF_RETURN((internal::IsSame), (bool)) String(const T* const& str) { return String(str, internal::StrLen(str)); } template - bool Key(const T* const& str, RAPIDJSON_ENABLEIF((internal::IsSame))) { return Key(str, internal::StrLen(str)); } + RAPIDJSON_ENABLEIF_RETURN((internal::IsSame), (bool)) Key(const T* const& str) { return Key(str, internal::StrLen(str)); } //! The compiler can give us the length of quoted strings for free. template - bool String(const T (&str)[N], RAPIDJSON_ENABLEIF((internal::IsSame))) { + RAPIDJSON_ENABLEIF_RETURN((internal::IsSame), (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 - bool Key(const T (&str)[N], RAPIDJSON_ENABLEIF((internal::IsSame))) { + RAPIDJSON_ENABLEIF_RETURN((internal::IsSame), (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); } From c64f378f16f23742b316e99d6fe40a3c14f95698 Mon Sep 17 00:00:00 2001 From: Ted Lyngmo Date: Wed, 8 Mar 2017 06:25:41 +0100 Subject: [PATCH 207/305] Fix -Werror=effc++ errors with GNU 6.3.1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix "'MyHandler::type’ should be initialized in the member initialization list [-Werror=effc++]" errors. https://github.com/miloyip/rapidjson/issues/874 --- example/simplepullreader/simplepullreader.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/example/simplepullreader/simplepullreader.cpp b/example/simplepullreader/simplepullreader.cpp index 98566e6..1401829 100644 --- a/example/simplepullreader/simplepullreader.cpp +++ b/example/simplepullreader/simplepullreader.cpp @@ -16,6 +16,14 @@ struct MyHandler { const char* type; std::string data; + MyHandler() : type(), data() { Null(); } + MyHandler(const MyHandler& cpy) : type(cpy.type),data(cpy.data) {} + MyHandler& operator=(const MyHandler& cpy) { + type = cpy.type; + data = cpy.data; + return *this; + } + bool Null() { type = "Null"; data.clear(); return true; } bool Bool(bool b) { type = "Bool:"; data = b? "true": "false"; return true; } bool Int(int i) { type = "Int:"; data = stringify(i); return true; } From ef22ca17321933eb2bd54ff7975657babab18cdd Mon Sep 17 00:00:00 2001 From: Ted Lyngmo Date: Wed, 8 Mar 2017 06:25:41 +0100 Subject: [PATCH 208/305] Fix -Werror=effc++ errors with GNU 6.3.1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix "'MyHandler::type’ should be initialized in the member initialization list [-Werror=effc++]" errors. https://github.com/miloyip/rapidjson/issues/874 --- example/simplepullreader/simplepullreader.cpp | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/example/simplepullreader/simplepullreader.cpp b/example/simplepullreader/simplepullreader.cpp index 1401829..a4fb116 100644 --- a/example/simplepullreader/simplepullreader.cpp +++ b/example/simplepullreader/simplepullreader.cpp @@ -16,13 +16,7 @@ struct MyHandler { const char* type; std::string data; - MyHandler() : type(), data() { Null(); } - MyHandler(const MyHandler& cpy) : type(cpy.type),data(cpy.data) {} - MyHandler& operator=(const MyHandler& cpy) { - type = cpy.type; - data = cpy.data; - return *this; - } + MyHandler() : type(), data() {} bool Null() { type = "Null"; data.clear(); return true; } bool Bool(bool b) { type = "Bool:"; data = b? "true": "false"; return true; } @@ -38,6 +32,9 @@ struct MyHandler { bool EndObject(SizeType memberCount) { type = "EndObject:"; data = stringify(memberCount); return true; } bool StartArray() { type = "StartArray"; data.clear(); return true; } bool EndArray(SizeType elementCount) { type = "EndArray:"; data = stringify(elementCount); return true; } +private: + MyHandler(const MyHandler& noCopyConstruction); + MyHandler& operator=(const MyHandler& noAssignment); }; int main() { From d4669bbc8e60b5c25097c21097248bd788b5f4b9 Mon Sep 17 00:00:00 2001 From: StilesCrisis Date: Wed, 8 Mar 2017 01:08:41 -0800 Subject: [PATCH 209/305] Add lookahead parser example --- example/CMakeLists.txt | 1 + example/lookaheadparser/lookaheadparser.cpp | 341 ++++++++++++++++++++ 2 files changed, 342 insertions(+) create mode 100644 example/lookaheadparser/lookaheadparser.cpp diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index e16e3c9..e00f77a 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -10,6 +10,7 @@ set(EXAMPLES filterkey filterkeydom jsonx + lookaheadparser messagereader parsebyparts pretty diff --git a/example/lookaheadparser/lookaheadparser.cpp b/example/lookaheadparser/lookaheadparser.cpp new file mode 100644 index 0000000..7c7f387 --- /dev/null +++ b/example/lookaheadparser/lookaheadparser.cpp @@ -0,0 +1,341 @@ +#include "rapidjson/reader.h" +#include "rapidjson/document.h" +#include + +// This example demonstrates JSON token-by-token parsing with an API that is +// more direct; you don't need to design your logic around a handler object and +// callbacks. Instead, you retrieve values from the JSON stream by calling +// GetInt(), GetDouble(), GetString() and GetBool(), traverse into structures +// by calling EnterObject() and EnterArray(), and skip over unwanted data by +// calling SkipValue(). When you know your JSON's structure, this can be quite +// convenient. +// +// If you aren't sure of what's next in the JSON data, you can use PeekType() and +// PeekValue() to look ahead to the next object before reading it. +// +// If you call the wrong retrieval method--e.g. GetInt when the next JSON token is +// not an int, EnterObject or EnterArray when there isn't actually an object or array +// to read--the stream parsing will end immediately and no more data will be delivered. +// +// After calling EnterObject, you retrieve keys via NextObjectKey() and values via +// the normal getters. When NextObjectKey() returns null, you have exited the +// object, or you can call ExitObject() to skip to the end of the object +// immediately. If you fetch the entire object (i.e. NextObjectKey() returned null), +// you should not call ExitObject(). +// +// After calling EnterArray(), you must alternate between calling NextArrayValue() +// to see if the array has more data, and then retrieving values via the normal +// getters. You can call ExitArray() to skip to the end of the array immediately. +// If you fetch the entire array (i.e. NextArrayValue() returned null), +// you should not call ExitArray(). +// +// This parser uses in-situ strings, so the JSON buffer will be altered during the +// parse. + +using namespace rapidjson; + + +class LookaheadParserHandler { +public: + bool Null() { st_ = kHasValue; v_.SetNull(); return true; } + bool Bool(bool b) { st_ = kHasValue; v_.SetBool(b); return true; } + bool Int(int i) { st_ = kHasValue; v_.SetInt(i); return true; } + bool Uint(unsigned u) { st_ = kHasValue; v_.SetUint(u); return true; } + bool Int64(int64_t i) { st_ = kHasValue; v_.SetInt64(i); return true; } + bool Uint64(uint64_t u) { st_ = kHasValue; v_.SetUint64(u); return true; } + bool Double(double d) { st_ = kHasValue; v_.SetDouble(d); return true; } + bool RawNumber(const char*, SizeType, bool) { return false; } + bool String(const char* str, SizeType length, bool) { st_ = kHasValue; v_.SetString(str, length); return true; } + bool StartObject() { st_ = kEnteringObject; return true; } + bool Key(const char* str, SizeType length, bool) { st_ = kHasKey; v_.SetString(str, length); return true; } + bool EndObject(SizeType) { st_ = kExitingObject; return true; } + bool StartArray() { st_ = kEnteringArray; return true; } + bool EndArray(SizeType) { st_ = kExitingArray; return true; } + +protected: + LookaheadParserHandler(char* str); + void ParseNext(); + +protected: + enum LookaheadParsingState { + kError, + kHasValue, + kHasKey, + kEnteringObject, + kExitingObject, + kEnteringArray, + kExitingArray + }; + + Value v_; + LookaheadParsingState st_; + Reader r_; + InsituStringStream ss_; + + static const int parseFlags = kParseDefaultFlags | kParseInsituFlag; +}; + +LookaheadParserHandler::LookaheadParserHandler(char* str) : ss_(str) { + r_.IterativeParseInit(); + ParseNext(); +} + +void LookaheadParserHandler::ParseNext() { + if (r_.HasParseError()) { + st_ = kError; + return; + } + + r_.IterativeParseNext(ss_, *this); +} + +class LookaheadParser : protected LookaheadParserHandler { +public: + LookaheadParser(char* str) : LookaheadParserHandler(str) {} + + void EnterObject(); + void EnterArray(); + void ExitObject(); + void ExitArray(); + const char* NextObjectKey(); + bool NextArrayValue(); + int GetInt(); + double GetDouble(); + const char* GetString(); + bool GetBool(); + void GetNull(); + + void SkipValue(); + Value* PeekValue(); + int PeekType(); // returns a rapidjson::Type, or -1 for no value (at end of object/array) + + bool IsValid() { return st_ != kError; } +}; + +void LookaheadParser::EnterObject() { + if (st_ != kEnteringObject) { + st_ = kError; + return; + } + + ParseNext(); +} + +void LookaheadParser::EnterArray() { + if (st_ != kEnteringArray) { + st_ = kError; + return; + } + + ParseNext(); +} + +void LookaheadParser::ExitObject() { + while (NextObjectKey()) { + SkipValue(); + } +} + +void LookaheadParser::ExitArray() { + while (NextArrayValue()) { + SkipValue(); + } +} + +const char* LookaheadParser::NextObjectKey() { + if (st_ == kExitingObject) { + ParseNext(); + return 0; + } + + if (st_ != kHasKey || !v_.IsString()) { + st_ = kError; + return 0; + } + + const char* result = v_.GetString(); + ParseNext(); + return result; +} + +bool LookaheadParser::NextArrayValue() { + if (st_ == kExitingArray) { + ParseNext(); + return false; + } + + return true; +} + +int LookaheadParser::GetInt() { + if (st_ != kHasValue || !v_.IsInt()) { + st_ = kError; + return 0; + } + + int result = v_.GetInt(); + ParseNext(); + return result; +} + +double LookaheadParser::GetDouble() { + if (st_ != kHasValue || !v_.IsNumber()) { + st_ = kError; + return 0.; + } + + double result = v_.GetDouble(); + ParseNext(); + return result; +} + +bool LookaheadParser::GetBool() { + if (st_ != kHasValue || !v_.IsBool()) { + st_ = kError; + return false; + } + + bool result = v_.GetBool(); + ParseNext(); + return result; +} + +void LookaheadParser::GetNull() { + if (st_ != kHasValue || !v_.IsNull()) { + st_ = kError; + return; + } + + ParseNext(); +} + +const char* LookaheadParser::GetString() { + if (st_ != kHasValue || !v_.IsString()) { + st_ = kError; + return 0; + } + + const char* result = v_.GetString(); + ParseNext(); + return result; +} + +void LookaheadParser::SkipValue() { + int depth = 0; + do { + switch (st_) { + case kEnteringArray: + case kEnteringObject: + ++depth; + break; + + case kExitingArray: + case kExitingObject: + --depth; + break; + + case kError: + return; + + case kHasKey: + case kHasValue: + break; + } + ParseNext(); + } + while (depth > 0); +} + +Value* LookaheadParser::PeekValue() { + if (st_ == kHasValue || st_ == kHasKey) { + return &v_; + } + + return 0; +} + +int LookaheadParser::PeekType() { + switch (st_) { + case kHasValue: + case kHasKey: + return v_.GetType(); + + case kEnteringArray: + return kArrayType; + + case kEnteringObject: + return kObjectType; + + case kExitingArray: + case kExitingObject: + case kError: + return -1; + } +} + +//------------------------------------------------------------------------- + +int main() { + using namespace std; + + char json[] = " { \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null," + "\"i\":123, \"pi\": 3.1416, \"a\":[-1, 2, 3, 4, \"array\", []], \"skipArrays\":[1, 2, [[[3]]]], " + "\"skipObject\":{ \"i\":0, \"t\":true, \"n\":null, \"d\":123.45 }, " + "\"skipNested\":[[[[{\"\":0}, {\"\":[-9.87]}]]], [], []], " + "\"skipString\":\"zzz\", \"reachedEnd\":null, \"t\":true }"; + + LookaheadParser r(json); + + RAPIDJSON_ASSERT(r.PeekType() == kObjectType); + + r.EnterObject(); + while (const char* key = r.NextObjectKey()) { + if (0 == strcmp(key, "hello")) { + RAPIDJSON_ASSERT(r.PeekType() == kStringType); + cout << key << ":" << r.GetString() << endl; + } + else if (0 == strcmp(key, "t") || 0 == strcmp(key, "f")) { + RAPIDJSON_ASSERT(r.PeekType() == kTrueType || r.PeekType() == kFalseType); + cout << key << ":" << r.GetBool() << endl; + continue; + } + else if (0 == strcmp(key, "n")) { + RAPIDJSON_ASSERT(r.PeekType() == kNullType); + r.GetNull(); + cout << key << endl; + continue; + } + else if (0 == strcmp(key, "pi")) { + RAPIDJSON_ASSERT(r.PeekType() == kNumberType); + cout << key << ":" << r.GetDouble() << endl; + continue; + } + else if (0 == strcmp(key, "a")) { + RAPIDJSON_ASSERT(r.PeekType() == kArrayType); + + r.EnterArray(); + + cout << key << ":[ "; + while (r.NextArrayValue()) { + if (r.PeekType() == kNumberType) { + cout << r.GetDouble() << " "; + } + else if (r.PeekType() == kStringType) { + cout << r.GetString() << " "; + } + else { + r.ExitArray(); + break; + } + } + + cout << "]" << endl; + } + else { + cout << key << ":skipped" << endl; + r.SkipValue(); + } + } + + return 0; +} From 8da89f54bd52023ab7dd8169e3aab25518012cb6 Mon Sep 17 00:00:00 2001 From: StilesCrisis Date: Wed, 8 Mar 2017 01:16:19 -0800 Subject: [PATCH 210/305] Fix GCC warning --- example/lookaheadparser/lookaheadparser.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/example/lookaheadparser/lookaheadparser.cpp b/example/lookaheadparser/lookaheadparser.cpp index 7c7f387..f4759c4 100644 --- a/example/lookaheadparser/lookaheadparser.cpp +++ b/example/lookaheadparser/lookaheadparser.cpp @@ -269,6 +269,7 @@ int LookaheadParser::PeekType() { case kExitingArray: case kExitingObject: case kError: + default: return -1; } } From 84a0356608dcef56d8a9e9f7bd2b9a008c1735b9 Mon Sep 17 00:00:00 2001 From: John Stiles Date: Sun, 12 Mar 2017 23:40:54 -0700 Subject: [PATCH 211/305] Add unit test for Issue 889 --- test/unittest/writertest.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/unittest/writertest.cpp b/test/unittest/writertest.cpp index 398a63d..e630bb9 100644 --- a/test/unittest/writertest.cpp +++ b/test/unittest/writertest.cpp @@ -100,6 +100,19 @@ TEST(Writer, String) { #endif } +TEST(Writer, Issue_889) { + char buf[100] = "Hello"; + + StringBuffer buffer; + Writer 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. From 55f8a32020ae45101c709b826818d429c720dff4 Mon Sep 17 00:00:00 2001 From: John Stiles Date: Sun, 12 Mar 2017 23:47:59 -0700 Subject: [PATCH 212/305] Remove broken templatized string length optimization It did not support char arrays. --- include/rapidjson/writer.h | 24 ++++-------------------- 1 file changed, 4 insertions(+), 20 deletions(-) diff --git a/include/rapidjson/writer.h b/include/rapidjson/writer.h index 7a0af39..b83b68e 100644 --- a/include/rapidjson/writer.h +++ b/include/rapidjson/writer.h @@ -199,8 +199,7 @@ public: return EndValue(WriteString(str, length)); } - template - RAPIDJSON_ENABLEIF_RETURN((internal::IsSame), (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); @@ -219,8 +218,7 @@ public: return WriteStartObject(); } - template - RAPIDJSON_ENABLEIF_RETURN((internal::IsSame), (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; @@ -250,23 +248,9 @@ public: //@{ //! Simpler but slower overload. - template - RAPIDJSON_ENABLEIF_RETURN((internal::IsSame), (bool)) String(const T* const& str) { return String(str, internal::StrLen(str)); } - template - RAPIDJSON_ENABLEIF_RETURN((internal::IsSame), (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 - RAPIDJSON_ENABLEIF_RETURN((internal::IsSame), (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 - RAPIDJSON_ENABLEIF_RETURN((internal::IsSame), (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. From e7fd707698334b17d698be6c741990027e4f1378 Mon Sep 17 00:00:00 2001 From: John Stiles Date: Mon, 13 Mar 2017 00:33:10 -0700 Subject: [PATCH 213/305] Improve LookaheadParser MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix clang -Wswitch-enum warnings. Made NextArrayValue() more robust—now handles error state correctly, will enter error state if an unexpected state is reached. Made separate states for each value type to simplify getters. Simplified implementation of skipping arrays and objects. Skipping an object now works whether you’ve retrieved the key or not. --- example/lookaheadparser/lookaheadparser.cpp | 188 ++++++++++++-------- 1 file changed, 111 insertions(+), 77 deletions(-) diff --git a/example/lookaheadparser/lookaheadparser.cpp b/example/lookaheadparser/lookaheadparser.cpp index f4759c4..4d8e13f 100644 --- a/example/lookaheadparser/lookaheadparser.cpp +++ b/example/lookaheadparser/lookaheadparser.cpp @@ -19,15 +19,15 @@ // // After calling EnterObject, you retrieve keys via NextObjectKey() and values via // the normal getters. When NextObjectKey() returns null, you have exited the -// object, or you can call ExitObject() to skip to the end of the object +// object, or you can call SkipObject() to skip to the end of the object // immediately. If you fetch the entire object (i.e. NextObjectKey() returned null), -// you should not call ExitObject(). +// you should not call SkipObject(). // // After calling EnterArray(), you must alternate between calling NextArrayValue() // to see if the array has more data, and then retrieving values via the normal -// getters. You can call ExitArray() to skip to the end of the array immediately. +// getters. You can call SkipArray() to skip to the end of the array immediately. // If you fetch the entire array (i.e. NextArrayValue() returned null), -// you should not call ExitArray(). +// you should not call SkipArray(). // // This parser uses in-situ strings, so the JSON buffer will be altered during the // parse. @@ -37,15 +37,15 @@ using namespace rapidjson; class LookaheadParserHandler { public: - bool Null() { st_ = kHasValue; v_.SetNull(); return true; } - bool Bool(bool b) { st_ = kHasValue; v_.SetBool(b); return true; } - bool Int(int i) { st_ = kHasValue; v_.SetInt(i); return true; } - bool Uint(unsigned u) { st_ = kHasValue; v_.SetUint(u); return true; } - bool Int64(int64_t i) { st_ = kHasValue; v_.SetInt64(i); return true; } - bool Uint64(uint64_t u) { st_ = kHasValue; v_.SetUint64(u); return true; } - bool Double(double d) { st_ = kHasValue; v_.SetDouble(d); return true; } + bool Null() { st_ = kHasNull; v_.SetNull(); return true; } + bool Bool(bool b) { st_ = kHasBool; v_.SetBool(b); return true; } + bool Int(int i) { st_ = kHasNumber; v_.SetInt(i); return true; } + bool Uint(unsigned u) { st_ = kHasNumber; v_.SetUint(u); return true; } + bool Int64(int64_t i) { st_ = kHasNumber; v_.SetInt64(i); return true; } + bool Uint64(uint64_t u) { st_ = kHasNumber; v_.SetUint64(u); return true; } + bool Double(double d) { st_ = kHasNumber; v_.SetDouble(d); return true; } bool RawNumber(const char*, SizeType, bool) { return false; } - bool String(const char* str, SizeType length, bool) { st_ = kHasValue; v_.SetString(str, length); return true; } + bool String(const char* str, SizeType length, bool) { st_ = kHasString; v_.SetString(str, length); return true; } bool StartObject() { st_ = kEnteringObject; return true; } bool Key(const char* str, SizeType length, bool) { st_ = kHasKey; v_.SetString(str, length); return true; } bool EndObject(SizeType) { st_ = kExitingObject; return true; } @@ -59,7 +59,10 @@ protected: protected: enum LookaheadParsingState { kError, - kHasValue, + kHasNull, + kHasBool, + kHasNumber, + kHasString, kHasKey, kEnteringObject, kExitingObject, @@ -93,10 +96,8 @@ class LookaheadParser : protected LookaheadParserHandler { public: LookaheadParser(char* str) : LookaheadParserHandler(str) {} - void EnterObject(); - void EnterArray(); - void ExitObject(); - void ExitArray(); + bool EnterObject(); + bool EnterArray(); const char* NextObjectKey(); bool NextArrayValue(); int GetInt(); @@ -105,70 +106,87 @@ public: bool GetBool(); void GetNull(); + void SkipObject(); + void SkipArray(); void SkipValue(); Value* PeekValue(); int PeekType(); // returns a rapidjson::Type, or -1 for no value (at end of object/array) bool IsValid() { return st_ != kError; } + +protected: + void SkipOut(int depth); }; -void LookaheadParser::EnterObject() { +bool LookaheadParser::EnterObject() { if (st_ != kEnteringObject) { st_ = kError; - return; - } - - ParseNext(); -} - -void LookaheadParser::EnterArray() { - if (st_ != kEnteringArray) { - st_ = kError; - return; - } - - ParseNext(); -} - -void LookaheadParser::ExitObject() { - while (NextObjectKey()) { - SkipValue(); - } -} - -void LookaheadParser::ExitArray() { - while (NextArrayValue()) { - SkipValue(); - } -} - -const char* LookaheadParser::NextObjectKey() { - if (st_ == kExitingObject) { - ParseNext(); - return 0; - } - - if (st_ != kHasKey || !v_.IsString()) { - st_ = kError; - return 0; - } - - const char* result = v_.GetString(); - ParseNext(); - return result; -} - -bool LookaheadParser::NextArrayValue() { - if (st_ == kExitingArray) { - ParseNext(); return false; } + ParseNext(); return true; } +bool LookaheadParser::EnterArray() { + if (st_ != kEnteringArray) { + st_ = kError; + return false; + } + + ParseNext(); + return true; +} + +const char* LookaheadParser::NextObjectKey() { + switch (st_) { + case kHasKey: { + const char* result = v_.GetString(); + ParseNext(); + return result; + } + + case kExitingObject: + ParseNext(); + return 0; + + case kError: + case kHasNull: + case kHasBool: + case kHasNumber: + case kHasString: + case kEnteringObject: + case kEnteringArray: + case kExitingArray: + st_ = kError; + return 0; + } +} + +bool LookaheadParser::NextArrayValue() { + switch (st_) { + case kExitingArray: + ParseNext(); + return false; + + case kError: + case kExitingObject: + case kHasKey: + st_ = kError; + return false; + + case kHasNull: + case kHasBool: + case kHasNumber: + case kHasString: + case kEnteringObject: + case kEnteringArray: + return true; + } +} + int LookaheadParser::GetInt() { - if (st_ != kHasValue || !v_.IsInt()) { + if (st_ != kHasNumber || !v_.IsInt()) { st_ = kError; return 0; } @@ -179,7 +197,7 @@ int LookaheadParser::GetInt() { } double LookaheadParser::GetDouble() { - if (st_ != kHasValue || !v_.IsNumber()) { + if (st_ != kHasNumber || !v_.IsNumber()) { st_ = kError; return 0.; } @@ -190,7 +208,7 @@ double LookaheadParser::GetDouble() { } bool LookaheadParser::GetBool() { - if (st_ != kHasValue || !v_.IsBool()) { + if (st_ != kHasBool) { st_ = kError; return false; } @@ -201,7 +219,7 @@ bool LookaheadParser::GetBool() { } void LookaheadParser::GetNull() { - if (st_ != kHasValue || !v_.IsNull()) { + if (st_ != kHasNull) { st_ = kError; return; } @@ -210,7 +228,7 @@ void LookaheadParser::GetNull() { } const char* LookaheadParser::GetString() { - if (st_ != kHasValue || !v_.IsString()) { + if (st_ != kHasString) { st_ = kError; return 0; } @@ -220,8 +238,7 @@ const char* LookaheadParser::GetString() { return result; } -void LookaheadParser::SkipValue() { - int depth = 0; +void LookaheadParser::SkipOut(int depth) { do { switch (st_) { case kEnteringArray: @@ -237,8 +254,11 @@ void LookaheadParser::SkipValue() { case kError: return; - case kHasKey: - case kHasValue: + case kHasNull: + case kHasBool: + case kHasNumber: + case kHasString: + case kHasKey: break; } ParseNext(); @@ -246,8 +266,20 @@ void LookaheadParser::SkipValue() { while (depth > 0); } +void LookaheadParser::SkipValue() { + SkipOut(0); +} + +void LookaheadParser::SkipArray() { + SkipOut(1); +} + +void LookaheadParser::SkipObject() { + SkipOut(1); +} + Value* LookaheadParser::PeekValue() { - if (st_ == kHasValue || st_ == kHasKey) { + if (st_ >= kHasNull && st_ <= kHasKey) { return &v_; } @@ -256,7 +288,10 @@ Value* LookaheadParser::PeekValue() { int LookaheadParser::PeekType() { switch (st_) { - case kHasValue: + case kHasNull: + case kHasBool: + case kHasNumber: + case kHasString: case kHasKey: return v_.GetType(); @@ -269,7 +304,6 @@ int LookaheadParser::PeekType() { case kExitingArray: case kExitingObject: case kError: - default: return -1; } } @@ -325,7 +359,7 @@ int main() { cout << r.GetString() << " "; } else { - r.ExitArray(); + r.SkipArray(); break; } } From bf19c1a0beafa6a118b1c242d16d0c0cfe0296e2 Mon Sep 17 00:00:00 2001 From: John Stiles Date: Mon, 13 Mar 2017 07:40:51 -0700 Subject: [PATCH 214/305] Remove switch GCC and Clang cannot agree on what constitutes a good switch statement. --- example/lookaheadparser/lookaheadparser.cpp | 61 ++++++++------------- 1 file changed, 22 insertions(+), 39 deletions(-) diff --git a/example/lookaheadparser/lookaheadparser.cpp b/example/lookaheadparser/lookaheadparser.cpp index 4d8e13f..29d9299 100644 --- a/example/lookaheadparser/lookaheadparser.cpp +++ b/example/lookaheadparser/lookaheadparser.cpp @@ -139,50 +139,33 @@ bool LookaheadParser::EnterArray() { } const char* LookaheadParser::NextObjectKey() { - switch (st_) { - case kHasKey: { - const char* result = v_.GetString(); - ParseNext(); - return result; - } - - case kExitingObject: - ParseNext(); - return 0; - - case kError: - case kHasNull: - case kHasBool: - case kHasNumber: - case kHasString: - case kEnteringObject: - case kEnteringArray: - case kExitingArray: - st_ = kError; - return 0; + if (st_ == kHasKey) { + const char* result = v_.GetString(); + ParseNext(); + return result; } + + if (st_ == kExitingObject) { + ParseNext(); + return 0; + } + + st_ = kError; + return 0; } bool LookaheadParser::NextArrayValue() { - switch (st_) { - case kExitingArray: - ParseNext(); - return false; - - case kError: - case kExitingObject: - case kHasKey: - st_ = kError; - return false; - - case kHasNull: - case kHasBool: - case kHasNumber: - case kHasString: - case kEnteringObject: - case kEnteringArray: - return true; + if (st_ == kExitingArray) { + ParseNext(); + return false; } + + if (st_ == kError || st_ == kExitingObject || st_ == kHasKey) { + st_ = kError; + return false; + } + + return true; } int LookaheadParser::GetInt() { From 6723e3296a9ae52aa249bd57395c955d05d81b45 Mon Sep 17 00:00:00 2001 From: John Stiles Date: Mon, 13 Mar 2017 07:43:26 -0700 Subject: [PATCH 215/305] Initialize v_ to placate GCC MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit v_ has a value assigned to it as part of ParseNext() which happens in the constructor, but that’s not soon enough for GCC --- example/lookaheadparser/lookaheadparser.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example/lookaheadparser/lookaheadparser.cpp b/example/lookaheadparser/lookaheadparser.cpp index 29d9299..9ce8432 100644 --- a/example/lookaheadparser/lookaheadparser.cpp +++ b/example/lookaheadparser/lookaheadparser.cpp @@ -78,7 +78,7 @@ protected: static const int parseFlags = kParseDefaultFlags | kParseInsituFlag; }; -LookaheadParserHandler::LookaheadParserHandler(char* str) : ss_(str) { +LookaheadParserHandler::LookaheadParserHandler(char* str) : v_(), ss_(str) { r_.IterativeParseInit(); ParseNext(); } From f0c108b5c9dc4b41a8ea39c8d418ff9a988edc86 Mon Sep 17 00:00:00 2001 From: John Stiles Date: Mon, 13 Mar 2017 07:53:37 -0700 Subject: [PATCH 216/305] Remove all switch --- example/lookaheadparser/lookaheadparser.cpp | 70 ++++++++------------- 1 file changed, 27 insertions(+), 43 deletions(-) diff --git a/example/lookaheadparser/lookaheadparser.cpp b/example/lookaheadparser/lookaheadparser.cpp index 9ce8432..29469ed 100644 --- a/example/lookaheadparser/lookaheadparser.cpp +++ b/example/lookaheadparser/lookaheadparser.cpp @@ -58,6 +58,7 @@ protected: protected: enum LookaheadParsingState { + kInit, kError, kHasNull, kHasBool, @@ -78,7 +79,7 @@ protected: static const int parseFlags = kParseDefaultFlags | kParseInsituFlag; }; -LookaheadParserHandler::LookaheadParserHandler(char* str) : v_(), ss_(str) { +LookaheadParserHandler::LookaheadParserHandler(char* str) : v_(), st_(kInit), r_(), ss_(str) { r_.IterativeParseInit(); ParseNext(); } @@ -145,12 +146,12 @@ const char* LookaheadParser::NextObjectKey() { return result; } - if (st_ == kExitingObject) { - ParseNext(); + if (st_ != kExitingObject) { + st_ = kError; return 0; } - st_ = kError; + ParseNext(); return 0; } @@ -180,7 +181,7 @@ int LookaheadParser::GetInt() { } double LookaheadParser::GetDouble() { - if (st_ != kHasNumber || !v_.IsNumber()) { + if (st_ != kHasNumber) { st_ = kError; return 0.; } @@ -223,27 +224,16 @@ const char* LookaheadParser::GetString() { void LookaheadParser::SkipOut(int depth) { do { - switch (st_) { - case kEnteringArray: - case kEnteringObject: - ++depth; - break; - - case kExitingArray: - case kExitingObject: - --depth; - break; - - case kError: - return; - - case kHasNull: - case kHasBool: - case kHasNumber: - case kHasString: - case kHasKey: - break; + if (st_ == kEnteringArray || st_ == kEnteringObject) { + ++depth; } + else if (st_ == kExitingArray || st_ == kExitingObject) { + --depth; + } + else if (st_ == kError) { + return; + } + ParseNext(); } while (depth > 0); @@ -270,25 +260,19 @@ Value* LookaheadParser::PeekValue() { } int LookaheadParser::PeekType() { - switch (st_) { - case kHasNull: - case kHasBool: - case kHasNumber: - case kHasString: - case kHasKey: - return v_.GetType(); - - case kEnteringArray: - return kArrayType; - - case kEnteringObject: - return kObjectType; - - case kExitingArray: - case kExitingObject: - case kError: - return -1; + if (st_ >= kHasNull && st_ <= kHasKey) { + return v_.GetType(); } + + if (st_ == kEnteringArray) { + return kArrayType; + } + + if (st_ == kEnteringObject) { + return kObjectType; + } + + return -1; } //------------------------------------------------------------------------- From b91c515afea9f0ba6a81fc670889549d77c83db3 Mon Sep 17 00:00:00 2001 From: Clemens Arth Date: Tue, 14 Mar 2017 10:27:36 +0100 Subject: [PATCH 217/305] update to create config file which is independent from actual install location --- CMakeLists.txt | 36 ++++++++++++++++++++++++++++-------- RapidJSONConfig.cmake.in | 18 +++++++++++++++--- 2 files changed, 43 insertions(+), 11 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9257926..d6823a8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -173,15 +173,35 @@ install(DIRECTORY example/ # Provide config and version files to be used by other applications # =============================== -export(PACKAGE ${PROJECT_NAME}) +################################################################################ +# Export package for use from the build tree +EXPORT( PACKAGE ${PROJECT_NAME} ) -# cmake-modules -CONFIGURE_FILE(${PROJECT_NAME}Config.cmake.in - ${PROJECT_NAME}Config.cmake - @ONLY) -CONFIGURE_FILE(${PROJECT_NAME}ConfigVersion.cmake.in - ${PROJECT_NAME}ConfigVersion.cmake - @ONLY) +# Create the RapidJSONConfig.cmake file for other cmake projects. +# ... for the build tree +SET( CONFIG_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}) +SET( CONFIG_DIR ${CMAKE_CURRENT_BINARY_DIR}) +CONFIGURE_FILE( ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}Config.cmake.in + ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake @ONLY ) +CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}ConfigVersion.cmake.in + ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake @ONLY) + +# ... for the install tree +SET( CMAKECONFIG_INSTALL_DIR lib/cmake/${PROJECT_NAME} ) +FILE( RELATIVE_PATH REL_INCLUDE_DIR + "${CMAKE_INSTALL_PREFIX}/${CMAKECONFIG_INSTALL_DIR}" + "${CMAKE_INSTALL_PREFIX}/include" ) + +SET( ${PROJECT_NAME}_INCLUDE_DIR "\${${PROJECT_NAME}_CMAKE_DIR}/${REL_INCLUDE_DIR}" ) +SET( CONFIG_SOURCE_DIR ) +SET( CONFIG_DIR ) +CONFIGURE_FILE( ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}Config.cmake.in + ${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/${PROJECT_NAME}Config.cmake @ONLY ) + +INSTALL(FILES "${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/${PROJECT_NAME}Config.cmake" + DESTINATION ${CMAKECONFIG_INSTALL_DIR} ) + +# Install files INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake diff --git a/RapidJSONConfig.cmake.in b/RapidJSONConfig.cmake.in index 9fa1218..e3c65a5 100644 --- a/RapidJSONConfig.cmake.in +++ b/RapidJSONConfig.cmake.in @@ -1,3 +1,15 @@ -get_filename_component(RAPIDJSON_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH) -set(RAPIDJSON_INCLUDE_DIRS "@INCLUDE_INSTALL_DIR@") -message(STATUS "RapidJSON found. Headers: ${RAPIDJSON_INCLUDE_DIRS}") +################################################################################ +# RapidJSON source dir +set( RapidJSON_SOURCE_DIR "@CONFIG_SOURCE_DIR@") + +################################################################################ +# RapidJSON build dir +set( RapidJSON_DIR "@CONFIG_DIR@") + +################################################################################ +# Compute paths +get_filename_component(RapidJSON_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH) + +set( RapidJSON_INCLUDE_DIR "@RapidJSON_INCLUDE_DIR@" ) +set( RapidJSON_INCLUDE_DIRS "@RapidJSON_INCLUDE_DIR@" ) +message(STATUS "RapidJSON found. Headers: ${RapidJSON_INCLUDE_DIRS}") From 31c6c50ac66e5728d086260fc4a5d0993faaf683 Mon Sep 17 00:00:00 2001 From: John Stiles Date: Tue, 14 Mar 2017 23:28:59 -0700 Subject: [PATCH 218/305] Provide a Flush() API within Writer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is helpful if you’re writing code that needs to control flush behavior and you don’t want to pass around your buffer object to each handler function alongside the writer. Seems like an easy convenience to add. --- include/rapidjson/prettywriter.h | 4 ++-- include/rapidjson/writer.h | 10 +++++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/include/rapidjson/prettywriter.h b/include/rapidjson/prettywriter.h index a9d0f02..2d6a04f 100644 --- a/include/rapidjson/prettywriter.h +++ b/include/rapidjson/prettywriter.h @@ -152,7 +152,7 @@ public: (void)ret; RAPIDJSON_ASSERT(ret == true); if (Base::level_stack_.Empty()) // end of json text - Base::os_->Flush(); + Base::Flush(); return true; } @@ -176,7 +176,7 @@ public: (void)ret; RAPIDJSON_ASSERT(ret == true); if (Base::level_stack_.Empty()) // end of json text - Base::os_->Flush(); + Base::Flush(); return true; } diff --git a/include/rapidjson/writer.h b/include/rapidjson/writer.h index b83b68e..cb7afd5 100644 --- a/include/rapidjson/writer.h +++ b/include/rapidjson/writer.h @@ -267,6 +267,14 @@ public: return EndValue(WriteRawValue(json, length)); } + //! Flush the output stream. + /*! + Allows the user to flush the output stream immediately. + */ + void Flush() { + os_->Flush(); + } + protected: //! Information for each nested level struct Level { @@ -473,7 +481,7 @@ protected: // Flush the value if it is the top level one. bool EndValue(bool ret) { if (RAPIDJSON_UNLIKELY(level_stack_.Empty())) // end of json text - os_->Flush(); + Flush(); return ret; } From d5d18cf6941518a33901a0ce522d38787269b8bb Mon Sep 17 00:00:00 2001 From: John Stiles Date: Tue, 14 Mar 2017 23:48:41 -0700 Subject: [PATCH 219/305] Fix template length optimization issue in PrettyWriter Missed PrettyWriter in the initial fix for Issue #889 --- include/rapidjson/prettywriter.h | 24 ++++-------------------- test/unittest/prettywritertest.cpp | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/include/rapidjson/prettywriter.h b/include/rapidjson/prettywriter.h index a9d0f02..b68b687 100644 --- a/include/rapidjson/prettywriter.h +++ b/include/rapidjson/prettywriter.h @@ -107,8 +107,7 @@ public: return Base::WriteString(str, length); } - template - RAPIDJSON_ENABLEIF_RETURN((internal::IsSame), (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; PrettyPrefix(kStringType); @@ -127,8 +126,7 @@ public: return Base::WriteStartObject(); } - template - RAPIDJSON_ENABLEIF_RETURN((internal::IsSame), (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); } #if RAPIDJSON_HAS_STDSTRING bool Key(const std::basic_string& str) { @@ -186,22 +184,8 @@ public: //@{ //! Simpler but slower overload. - template - RAPIDJSON_ENABLEIF_RETURN((internal::IsSame), (bool)) String(const T* const& str) { return String(str, internal::StrLen(str)); } - template - RAPIDJSON_ENABLEIF_RETURN((internal::IsSame), (bool)) Key(const T* const& str) { return Key(str, internal::StrLen(str)); } - - //! The compiler can give us the length of quoted strings for free. - template - RAPIDJSON_ENABLEIF_RETURN((internal::IsSame), (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 - RAPIDJSON_ENABLEIF_RETURN((internal::IsSame), (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); - } + bool String(const Ch* str) { return String(str, internal::StrLen(str)); } + bool Key(const Ch* str) { return Key(str, internal::StrLen(str)); } //@} diff --git a/test/unittest/prettywritertest.cpp b/test/unittest/prettywritertest.cpp index 2891c76..bfc736f 100644 --- a/test/unittest/prettywritertest.cpp +++ b/test/unittest/prettywritertest.cpp @@ -258,6 +258,20 @@ TEST(PrettyWriter, InvalidEventSequence) { } } +TEST(PrettyWriter, Issue_889) { + char buf[100] = "Hello"; + + StringBuffer buffer; + PrettyWriter writer(buffer); + writer.StartArray(); + writer.String(buf); + writer.EndArray(); + + EXPECT_STREQ("[\n \"Hello\"\n]", buffer.GetString()); + EXPECT_TRUE(writer.IsComplete()); \ +} + + #if RAPIDJSON_HAS_CXX11_RVALUE_REFS static PrettyWriter WriterGen(StringBuffer &target) { From e5635fb27feab7f6e8d7b916aa20ad799045a641 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Thu, 16 Mar 2017 10:46:48 +0800 Subject: [PATCH 220/305] Fix #899 --- include/rapidjson/document.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h index 3873b99..6de441f 100644 --- a/include/rapidjson/document.h +++ b/include/rapidjson/document.h @@ -2564,7 +2564,7 @@ public: GenericObject AddMember(StringRefType name, ValueType& value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; } GenericObject AddMember(StringRefType name, StringRefType value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; } template RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (GenericObject)) AddMember(StringRefType name, T value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; } - void RemoveAllMembers() { return value_.RemoveAllMembers(); } + void RemoveAllMembers() { value_.RemoveAllMembers(); } bool RemoveMember(const Ch* name) const { return value_.RemoveMember(name); } #if RAPIDJSON_HAS_STDSTRING bool RemoveMember(const std::basic_string& name) const { return value_.RemoveMember(name); } From a38104a165e3a7e4e1fc5647c6a7c19b968259b3 Mon Sep 17 00:00:00 2001 From: shadeware Date: Sun, 19 Mar 2017 03:03:36 +0300 Subject: [PATCH 221/305] fix typos in doc code --- doc/schema.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/schema.md b/doc/schema.md index 8b4195b..29ba4f5 100644 --- a/doc/schema.md +++ b/doc/schema.md @@ -20,7 +20,7 @@ Secondly, construct a `SchemaValidator` with the `SchemaDocument`. It is similar // ... Document sd; -if (!sd.Parse(schemaJson).HasParseError()) { +if (sd.Parse(schemaJson).HasParseError()) { // the schema is not a valid JSON. // ... } @@ -28,7 +28,7 @@ SchemaDocument schema(sd); // Compile a Document to SchemaDocument // sd is no longer needed here. Document d; -if (!d.Parse(inputJson).HasParseError()) { +if (d.Parse(inputJson).HasParseError()) { // the input is not a valid JSON. // ... } From 430e8d4c9b6c52e007e57f6f1380c905c1c266df Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Mon, 20 Mar 2017 11:20:04 +0800 Subject: [PATCH 222/305] Update schema.zh-cn.md --- doc/schema.zh-cn.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/schema.zh-cn.md b/doc/schema.zh-cn.md index fa076de..5df1f31 100644 --- a/doc/schema.zh-cn.md +++ b/doc/schema.zh-cn.md @@ -20,7 +20,7 @@ RapidJSON 实现了一个 [JSON Schema Draft v4](http://json-schema.org/document // ... Document sd; -if (!sd.Parse(schemaJson).HasParseError()) { +if (sd.Parse(schemaJson).HasParseError()) { // 此 schema 不是合法的 JSON // ... } @@ -28,7 +28,7 @@ SchemaDocument schema(sd); // 把一个 Document 编译至 SchemaDocument // 之后不再需要 sd Document d; -if (!d.Parse(inputJson).HasParseError()) { +if (d.Parse(inputJson).HasParseError()) { // 输入不是一个合法的 JSON // ... } From da4fd6794c8709667137d667525e5005a091adbb Mon Sep 17 00:00:00 2001 From: Alejandro Martinez Date: Wed, 22 Mar 2017 10:19:54 +0000 Subject: [PATCH 223/305] Fixed bug on space hexadecimal encoding --- include/rapidjson/reader.h | 12 ++++++------ include/rapidjson/writer.h | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/include/rapidjson/reader.h b/include/rapidjson/reader.h index 00ab6a5..6ba3f17 100644 --- a/include/rapidjson/reader.h +++ b/include/rapidjson/reader.h @@ -990,7 +990,7 @@ private: // The rest of string using SIMD static const char dquote[16] = { '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"' }; static const char bslash[16] = { '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\' }; - static const char space[16] = { 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19 }; + static const char space[16] = { 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F }; const __m128i dq = _mm_loadu_si128(reinterpret_cast(&dquote[0])); const __m128i bs = _mm_loadu_si128(reinterpret_cast(&bslash[0])); const __m128i sp = _mm_loadu_si128(reinterpret_cast(&space[0])); @@ -999,7 +999,7 @@ private: const __m128i s = _mm_load_si128(reinterpret_cast(p)); const __m128i t1 = _mm_cmpeq_epi8(s, dq); const __m128i t2 = _mm_cmpeq_epi8(s, bs); - const __m128i t3 = _mm_cmpeq_epi8(_mm_max_epu8(s, sp), sp); // s < 0x20 <=> max(s, 0x19) == 0x19 + const __m128i t3 = _mm_cmpeq_epi8(_mm_max_epu8(s, sp), sp); // s < 0x20 <=> max(s, 0x1F) == 0x1F const __m128i x = _mm_or_si128(_mm_or_si128(t1, t2), t3); unsigned short r = static_cast(_mm_movemask_epi8(x)); if (RAPIDJSON_UNLIKELY(r != 0)) { // some of characters is escaped @@ -1053,7 +1053,7 @@ private: // The rest of string using SIMD static const char dquote[16] = { '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"' }; static const char bslash[16] = { '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\' }; - static const char space[16] = { 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19 }; + static const char space[16] = { 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F }; const __m128i dq = _mm_loadu_si128(reinterpret_cast(&dquote[0])); const __m128i bs = _mm_loadu_si128(reinterpret_cast(&bslash[0])); const __m128i sp = _mm_loadu_si128(reinterpret_cast(&space[0])); @@ -1062,7 +1062,7 @@ private: const __m128i s = _mm_load_si128(reinterpret_cast(p)); const __m128i t1 = _mm_cmpeq_epi8(s, dq); const __m128i t2 = _mm_cmpeq_epi8(s, bs); - const __m128i t3 = _mm_cmpeq_epi8(_mm_max_epu8(s, sp), sp); // s < 0x20 <=> max(s, 0x19) == 0x19 + const __m128i t3 = _mm_cmpeq_epi8(_mm_max_epu8(s, sp), sp); // s < 0x20 <=> max(s, 0x1F) == 0x1F const __m128i x = _mm_or_si128(_mm_or_si128(t1, t2), t3); unsigned short r = static_cast(_mm_movemask_epi8(x)); if (RAPIDJSON_UNLIKELY(r != 0)) { // some of characters is escaped @@ -1101,7 +1101,7 @@ private: // The rest of string using SIMD static const char dquote[16] = { '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"' }; static const char bslash[16] = { '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\' }; - static const char space[16] = { 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19 }; + static const char space[16] = { 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F }; const __m128i dq = _mm_loadu_si128(reinterpret_cast(&dquote[0])); const __m128i bs = _mm_loadu_si128(reinterpret_cast(&bslash[0])); const __m128i sp = _mm_loadu_si128(reinterpret_cast(&space[0])); @@ -1110,7 +1110,7 @@ private: const __m128i s = _mm_load_si128(reinterpret_cast(p)); const __m128i t1 = _mm_cmpeq_epi8(s, dq); const __m128i t2 = _mm_cmpeq_epi8(s, bs); - const __m128i t3 = _mm_cmpeq_epi8(_mm_max_epu8(s, sp), sp); // s < 0x20 <=> max(s, 0x19) == 0x19 + const __m128i t3 = _mm_cmpeq_epi8(_mm_max_epu8(s, sp), sp); // s < 0x20 <=> max(s, 0x1F) == 0x1F const __m128i x = _mm_or_si128(_mm_or_si128(t1, t2), t3); unsigned short r = static_cast(_mm_movemask_epi8(x)); if (RAPIDJSON_UNLIKELY(r != 0)) { // some of characters is escaped diff --git a/include/rapidjson/writer.h b/include/rapidjson/writer.h index cb7afd5..219da5e 100644 --- a/include/rapidjson/writer.h +++ b/include/rapidjson/writer.h @@ -585,7 +585,7 @@ inline bool Writer::ScanWriteUnescapedString(StringStream& is, siz // The rest of string using SIMD static const char dquote[16] = { '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"' }; static const char bslash[16] = { '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\' }; - static const char space[16] = { 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19 }; + static const char space[16] = { 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F }; const __m128i dq = _mm_loadu_si128(reinterpret_cast(&dquote[0])); const __m128i bs = _mm_loadu_si128(reinterpret_cast(&bslash[0])); const __m128i sp = _mm_loadu_si128(reinterpret_cast(&space[0])); @@ -594,7 +594,7 @@ inline bool Writer::ScanWriteUnescapedString(StringStream& is, siz const __m128i s = _mm_load_si128(reinterpret_cast(p)); const __m128i t1 = _mm_cmpeq_epi8(s, dq); const __m128i t2 = _mm_cmpeq_epi8(s, bs); - const __m128i t3 = _mm_cmpeq_epi8(_mm_max_epu8(s, sp), sp); // s < 0x20 <=> max(s, 0x19) == 0x19 + const __m128i t3 = _mm_cmpeq_epi8(_mm_max_epu8(s, sp), sp); // s < 0x20 <=> max(s, 0x1F) == 0x1F const __m128i x = _mm_or_si128(_mm_or_si128(t1, t2), t3); unsigned short r = static_cast(_mm_movemask_epi8(x)); if (RAPIDJSON_UNLIKELY(r != 0)) { // some of characters is escaped From 3c6e2cf0309c4e146c8ed18fd16096136fecbf00 Mon Sep 17 00:00:00 2001 From: Alejandro Martinez Date: Thu, 23 Mar 2017 10:14:17 +0000 Subject: [PATCH 224/305] Added unittests for invalid ascii control chars --- test/unittest/readertest.cpp | 2 ++ test/unittest/writertest.cpp | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/test/unittest/readertest.cpp b/test/unittest/readertest.cpp index 3555f11..5078f52 100644 --- a/test/unittest/readertest.cpp +++ b/test/unittest/readertest.cpp @@ -725,6 +725,8 @@ TEST(Reader, ParseString_Error) { // Malform ASCII sequence TEST_STRINGENCODING_ERROR(ASCII<>, UTF8<>, char, ARRAY('[', '\"', char(0x80u), '\"', ']', '\0')); + TEST_STRINGENCODING_ERROR(ASCII<>, UTF8<>, char, ARRAY('[', '\"', char(0x01u), '\"', ']', '\0')); + TEST_STRINGENCODING_ERROR(ASCII<>, UTF8<>, char, ARRAY('[', '\"', char(0x1Cu), '\"', ']', '\0')); #undef ARRAY #undef TEST_STRINGARRAY_ERROR diff --git a/test/unittest/writertest.cpp b/test/unittest/writertest.cpp index e630bb9..bc28e02 100644 --- a/test/unittest/writertest.cpp +++ b/test/unittest/writertest.cpp @@ -401,6 +401,16 @@ TEST(Writer, InvalidEncoding) { static const UTF32<>::Ch s[] = { 0x110000, 0 }; // Out of U+0000 to U+10FFFF EXPECT_FALSE(writer.String(s)); } + + // Fail in decoding invalid ASCII control bytes + { + GenericStringBuffer > buffer; + Writer >, UTF8<>, UTF16<> > writer(buffer); + writer.StartArray(); + EXPECT_FALSE(writer.String("\x01")); + EXPECT_FALSE(writer.String("\x1C")); + writer.EndArray(); + } } TEST(Writer, ValidateEncoding) { From 85500e8c8f1eabe10bcf7944e71fbb2dbcc893de Mon Sep 17 00:00:00 2001 From: Alejandro Martinez Date: Fri, 24 Mar 2017 13:37:23 +0000 Subject: [PATCH 225/305] Changed error code for invalid special ascii chars, fixed writer tests --- include/rapidjson/reader.h | 2 +- test/unittest/writertest.cpp | 14 +++----------- 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/include/rapidjson/reader.h b/include/rapidjson/reader.h index 6ba3f17..ccc025e 100644 --- a/include/rapidjson/reader.h +++ b/include/rapidjson/reader.h @@ -955,7 +955,7 @@ private: if (c == '\0') RAPIDJSON_PARSE_ERROR(kParseErrorStringMissQuotationMark, is.Tell()); else - RAPIDJSON_PARSE_ERROR(kParseErrorStringEscapeInvalid, is.Tell()); + RAPIDJSON_PARSE_ERROR(kParseErrorStringInvalidEncoding, is.Tell()); } else { size_t offset = is.Tell(); diff --git a/test/unittest/writertest.cpp b/test/unittest/writertest.cpp index bc28e02..b190c6c 100644 --- a/test/unittest/writertest.cpp +++ b/test/unittest/writertest.cpp @@ -401,16 +401,6 @@ TEST(Writer, InvalidEncoding) { static const UTF32<>::Ch s[] = { 0x110000, 0 }; // Out of U+0000 to U+10FFFF EXPECT_FALSE(writer.String(s)); } - - // Fail in decoding invalid ASCII control bytes - { - GenericStringBuffer > buffer; - Writer >, UTF8<>, UTF16<> > writer(buffer); - writer.StartArray(); - EXPECT_FALSE(writer.String("\x01")); - EXPECT_FALSE(writer.String("\x1C")); - writer.EndArray(); - } } TEST(Writer, ValidateEncoding) { @@ -422,8 +412,10 @@ TEST(Writer, ValidateEncoding) { EXPECT_TRUE(writer.String("\xC2\xA2")); // Cents sign U+00A2 EXPECT_TRUE(writer.String("\xE2\x82\xAC")); // Euro sign U+20AC EXPECT_TRUE(writer.String("\xF0\x9D\x84\x9E")); // G clef sign U+1D11E + EXPECT_TRUE(writer.String("\x01")); // SOH control U+0001 + EXPECT_TRUE(writer.String("\x1B")); // Escape control U+001B writer.EndArray(); - EXPECT_STREQ("[\"\x24\",\"\xC2\xA2\",\"\xE2\x82\xAC\",\"\xF0\x9D\x84\x9E\"]", buffer.GetString()); + EXPECT_STREQ("[\"\x24\",\"\xC2\xA2\",\"\xE2\x82\xAC\",\"\xF0\x9D\x84\x9E\",\"\\u0001\",\"\\u001B\"]", buffer.GetString()); } // Fail in decoding invalid UTF-8 sequence http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt From d88be8ef1649eca4602348d1aab5c16c36f83d4f Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Mon, 27 Mar 2017 14:05:03 +0800 Subject: [PATCH 226/305] Fix #905 unable to set writeFlags for PrettyWriter --- include/rapidjson/prettywriter.h | 2 +- test/unittest/prettywritertest.cpp | 43 ++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/include/rapidjson/prettywriter.h b/include/rapidjson/prettywriter.h index ef36a8c..98dfb30 100644 --- a/include/rapidjson/prettywriter.h +++ b/include/rapidjson/prettywriter.h @@ -47,7 +47,7 @@ enum PrettyFormatOptions { template, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator, unsigned writeFlags = kWriteDefaultFlags> class PrettyWriter : public Writer { public: - typedef Writer Base; + typedef Writer Base; typedef typename Base::Ch Ch; //! Constructor diff --git a/test/unittest/prettywritertest.cpp b/test/unittest/prettywritertest.cpp index bfc736f..1e1ca1a 100644 --- a/test/unittest/prettywritertest.cpp +++ b/test/unittest/prettywritertest.cpp @@ -258,6 +258,49 @@ TEST(PrettyWriter, InvalidEventSequence) { } } +TEST(PrettyWriter, NaN) { + double nan = std::numeric_limits::quiet_NaN(); + + EXPECT_TRUE(internal::Double(nan).IsNan()); + StringBuffer buffer; + { + PrettyWriter writer(buffer); + EXPECT_FALSE(writer.Double(nan)); + } + { + PrettyWriter, UTF8<>, CrtAllocator, kWriteNanAndInfFlag> writer(buffer); + EXPECT_TRUE(writer.Double(nan)); + EXPECT_STREQ("NaN", buffer.GetString()); + } + GenericStringBuffer > buffer2; + PrettyWriter > > writer2(buffer2); + EXPECT_FALSE(writer2.Double(nan)); +} + +TEST(PrettyWriter, Inf) { + double inf = std::numeric_limits::infinity(); + + EXPECT_TRUE(internal::Double(inf).IsInf()); + StringBuffer buffer; + { + PrettyWriter writer(buffer); + EXPECT_FALSE(writer.Double(inf)); + } + { + PrettyWriter writer(buffer); + EXPECT_FALSE(writer.Double(-inf)); + } + { + PrettyWriter, UTF8<>, CrtAllocator, kWriteNanAndInfFlag> writer(buffer); + EXPECT_TRUE(writer.Double(inf)); + } + { + PrettyWriter, UTF8<>, CrtAllocator, kWriteNanAndInfFlag> writer(buffer); + EXPECT_TRUE(writer.Double(-inf)); + } + EXPECT_STREQ("Infinity-Infinity", buffer.GetString()); +} + TEST(PrettyWriter, Issue_889) { char buf[100] = "Hello"; From 77f643dc511eaa3a1ce0e9dfa2976282ecc6eede Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Fri, 7 Apr 2017 10:23:30 +0800 Subject: [PATCH 227/305] Fix #910 incorrect casting --- include/rapidjson/document.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h index 6de441f..a2b044c 100644 --- a/include/rapidjson/document.h +++ b/include/rapidjson/document.h @@ -2293,7 +2293,7 @@ public: 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)); + MemoryStream ms(reinterpret_cast(str), length * sizeof(typename SourceEncoding::Ch)); EncodedInputStream is(ms); ParseStream(is); return *this; From ec90588c72ad4192d53b348298b0dd7f790524fa Mon Sep 17 00:00:00 2001 From: Zhihao Yuan Date: Sat, 8 Apr 2017 22:49:13 -0500 Subject: [PATCH 228/305] Fix a non-type template parameter type mismatch This issues a warning in gcc7. --- include/rapidjson/rapidjson.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/rapidjson/rapidjson.h b/include/rapidjson/rapidjson.h index e667d8b..19a3019 100644 --- a/include/rapidjson/rapidjson.h +++ b/include/rapidjson/rapidjson.h @@ -413,7 +413,7 @@ RAPIDJSON_NAMESPACE_END RAPIDJSON_NAMESPACE_BEGIN template struct STATIC_ASSERTION_FAILURE; template <> struct STATIC_ASSERTION_FAILURE { enum { value = 1 }; }; -template struct StaticAssertTest {}; +template struct StaticAssertTest {}; RAPIDJSON_NAMESPACE_END #define RAPIDJSON_JOIN(X, Y) RAPIDJSON_DO_JOIN(X, Y) From f93a29bec2316d3b5d7c33cb6977da6690a29be8 Mon Sep 17 00:00:00 2001 From: "Philipp A. Hartmann" Date: Fri, 14 Apr 2017 20:19:16 +0200 Subject: [PATCH 229/305] RAPIDJSON_STATIC_ASSERT: use C++11 static_assert, if available --- include/rapidjson/rapidjson.h | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/include/rapidjson/rapidjson.h b/include/rapidjson/rapidjson.h index e667d8b..a6281c0 100644 --- a/include/rapidjson/rapidjson.h +++ b/include/rapidjson/rapidjson.h @@ -49,6 +49,11 @@ // token stringification #define RAPIDJSON_STRINGIFY(x) RAPIDJSON_DO_STRINGIFY(x) #define RAPIDJSON_DO_STRINGIFY(x) #x + +// token concatenation +#define RAPIDJSON_JOIN(X, Y) RAPIDJSON_DO_JOIN(X, Y) +#define RAPIDJSON_DO_JOIN(X, Y) RAPIDJSON_DO_JOIN2(X, Y) +#define RAPIDJSON_DO_JOIN2(X, Y) X##Y //!@endcond /*! \def RAPIDJSON_MAJOR_VERSION @@ -405,7 +410,15 @@ RAPIDJSON_NAMESPACE_END /////////////////////////////////////////////////////////////////////////////// // RAPIDJSON_STATIC_ASSERT -// Adopt from boost +// Prefer C++11 static_assert, if available +#ifndef RAPIDJSON_STATIC_ASSERT +#if __cplusplus >= 201103L || ( defined(_MSC_VER) && _MSC_VER >= 1800 ) +#define RAPIDJSON_STATIC_ASSERT(x) \ + static_assert(x, RAPIDJSON_STRINGIFY(x)) +#endif // C++11 +#endif // RAPIDJSON_STATIC_ASSERT + +// Adopt C++03 implementation from boost #ifndef RAPIDJSON_STATIC_ASSERT #ifndef __clang__ //!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN @@ -416,10 +429,6 @@ template <> struct STATIC_ASSERTION_FAILURE { enum { value = 1 }; }; template struct StaticAssertTest {}; RAPIDJSON_NAMESPACE_END -#define RAPIDJSON_JOIN(X, Y) RAPIDJSON_DO_JOIN(X, Y) -#define RAPIDJSON_DO_JOIN(X, Y) RAPIDJSON_DO_JOIN2(X, Y) -#define RAPIDJSON_DO_JOIN2(X, Y) X##Y - #if defined(__GNUC__) #define RAPIDJSON_STATIC_ASSERT_UNUSED_ATTRIBUTE __attribute__((unused)) #else @@ -438,7 +447,7 @@ RAPIDJSON_NAMESPACE_END typedef ::RAPIDJSON_NAMESPACE::StaticAssertTest< \ sizeof(::RAPIDJSON_NAMESPACE::STATIC_ASSERTION_FAILURE)> \ RAPIDJSON_JOIN(StaticAssertTypedef, __LINE__) RAPIDJSON_STATIC_ASSERT_UNUSED_ATTRIBUTE -#endif +#endif // RAPIDJSON_STATIC_ASSERT /////////////////////////////////////////////////////////////////////////////// // RAPIDJSON_LIKELY, RAPIDJSON_UNLIKELY From 2291258bb8adb87e5da30ed2b12fa9929d0e76f8 Mon Sep 17 00:00:00 2001 From: Alejandro Martinez Date: Tue, 11 Apr 2017 02:02:15 +0000 Subject: [PATCH 230/305] Added ARM-Neon support for SIMD.SkipWhitespace* Change-Id: Iaf210d029758723a7eeb7f28fc10cab7467889a9 Signed-off-by: Jun He --- doc/faq.md | 2 +- doc/faq.zh-cn.md | 2 +- doc/internals.md | 7 +- doc/internals.zh-cn.md | 7 +- include/rapidjson/rapidjson.h | 18 ++- include/rapidjson/reader.h | 264 +++++++++++++++++++++++++++++++- include/rapidjson/writer.h | 72 ++++++++- test/perftest/perftest.h | 3 + test/perftest/rapidjsontest.cpp | 2 + test/unittest/simdtest.cpp | 4 + 10 files changed, 365 insertions(+), 16 deletions(-) diff --git a/doc/faq.md b/doc/faq.md index 1b0541c..4946cfe 100644 --- a/doc/faq.md +++ b/doc/faq.md @@ -256,7 +256,7 @@ Alternatively, if we don't want to explicitly refer to the root value of `addres 3. What is SIMD? How it is applied in RapidJSON? - [SIMD](http://en.wikipedia.org/wiki/SIMD) instructions can perform parallel computation in modern CPUs. RapidJSON support Intel's SSE2/SSE4.2 to accelerate whitespace skipping. This improves performance of parsing indent formatted JSON. Define `RAPIDJSON_SSE2` or `RAPIDJSON_SSE42` macro to enable this feature. However, running the executable on a machine without such instruction set support will make it crash. + [SIMD](http://en.wikipedia.org/wiki/SIMD) instructions can perform parallel computation in modern CPUs. RapidJSON support Intel's SSE2/SSE4.2 and ARM's Neon to accelerate whitespace/tabspace/carriage-return/line-feed skipping. This improves performance of parsing indent formatted JSON. Define `RAPIDJSON_SSE2`, `RAPIDJSON_SSE42` or `RAPIDJSON_NEON` macro to enable this feature. However, running the executable on a machine without such instruction set support will make it crash. 4. Does it consume a lot of memory? diff --git a/doc/faq.zh-cn.md b/doc/faq.zh-cn.md index f12d830..307b02f 100644 --- a/doc/faq.zh-cn.md +++ b/doc/faq.zh-cn.md @@ -257,7 +257,7 @@ 3. 什是是 SIMD?它如何用于 RapidJSON? - [SIMD](http://en.wikipedia.org/wiki/SIMD) 指令可以在现代 CPU 中执行并行运算。RapidJSON 支持了 Intel 的 SSE2/SSE4.2 去加速跳过空白字符。在解析含缩进的 JSON 时,这能提升性能。只要定义名为 `RAPIDJSON_SSE2` 或 `RAPIDJSON_SSE42` 的宏,就能启动这个功能。然而,若在不支持这些指令集的机器上执行这些可执行文件,会导致崩溃。 + [SIMD](http://en.wikipedia.org/wiki/SIMD) 指令可以在现代 CPU 中执行并行运算。RapidJSON 支持使用 Intel 的 SSE2/SSE4.2 和 ARM 的 Neon 来加速对空白符、制表符、回车符和换行符的过滤处理。在解析含缩进的 JSON 时,这能提升性能。只要定义名为 `RAPIDJSON_SSE2` ,`RAPIDJSON_SSE42` 或 `RAPIDJSON_NEON` 的宏,就能启动这个功能。然而,若在不支持这些指令集的机器上执行这些可执行文件,会导致崩溃。 4. 它会消耗许多内存么? diff --git a/doc/internals.md b/doc/internals.md index 49802a0..2fff2d9 100644 --- a/doc/internals.md +++ b/doc/internals.md @@ -183,17 +183,20 @@ void SkipWhitespace(InputStream& s) { However, this requires 4 comparisons and a few branching for each character. This was found to be a hot spot. -To accelerate this process, SIMD was applied to compare 16 characters with 4 white spaces for each iteration. Currently RapidJSON only supports SSE2 and SSE4.2 instructions for this. And it is only activated for UTF-8 memory streams, including string stream or *in situ* parsing. +To accelerate this process, SIMD was applied to compare 16 characters with 4 white spaces for each iteration. Currently RapidJSON supports SSE2, SSE4.2 and ARM Neon instructions for this. And it is only activated for UTF-8 memory streams, including string stream or *in situ* parsing. -To enable this optimization, need to define `RAPIDJSON_SSE2` or `RAPIDJSON_SSE42` before including `rapidjson.h`. Some compilers can detect the setting, as in `perftest.h`: +To enable this optimization, need to define `RAPIDJSON_SSE2`, `RAPIDJSON_SSE42` or `RAPIDJSON_NEON` before including `rapidjson.h`. Some compilers can detect the setting, as in `perftest.h`: ~~~cpp // __SSE2__ and __SSE4_2__ are recognized by gcc, clang, and the Intel compiler. // We use -march=native with gmake to enable -msse2 and -msse4.2, if supported. +// Likewise, __ARM_NEON is used to detect Neon. #if defined(__SSE4_2__) # define RAPIDJSON_SSE42 #elif defined(__SSE2__) # define RAPIDJSON_SSE2 +#elif defined(__ARM_NEON) +# define RAPIDJSON_NEON #endif ~~~ diff --git a/doc/internals.zh-cn.md b/doc/internals.zh-cn.md index ec57959..0c8bc06 100644 --- a/doc/internals.zh-cn.md +++ b/doc/internals.zh-cn.md @@ -183,17 +183,20 @@ void SkipWhitespace(InputStream& s) { 但是,这需要对每个字符进行4次比较以及一些分支。这被发现是一个热点。 -为了加速这一处理,RapidJSON 使用 SIMD 来在一次迭代中比较16个字符和4个空格。目前 RapidJSON 只支持 SSE2 和 SSE4.2 指令。同时它也只会对 UTF-8 内存流启用,包括字符串流或 *原位* 解析。 +为了加速这一处理,RapidJSON 使用 SIMD 来在一次迭代中比较16个字符和4个空格。目前 RapidJSON 支持 SSE2 , SSE4.2 和 ARM Neon 指令。同时它也只会对 UTF-8 内存流启用,包括字符串流或 *原位* 解析。 -你可以通过在包含 `rapidjson.h` 之前定义 `RAPIDJSON_SSE2` 或 `RAPIDJSON_SSE42` 来启用这个优化。一些编译器可以检测这个设置,如 `perftest.h`: +你可以通过在包含 `rapidjson.h` 之前定义 `RAPIDJSON_SSE2` , `RAPIDJSON_SSE42` 或 `RAPIDJSON_NEON` 来启用这个优化。一些编译器可以检测这个设置,如 `perftest.h`: ~~~cpp // __SSE2__ 和 __SSE4_2__ 可被 gcc、clang 和 Intel 编译器识别: // 如果支持的话,我们在 gmake 中使用了 -march=native 来启用 -msse2 和 -msse4.2 +// 同样的, __ARM_NEON 被用于识别Neon #if defined(__SSE4_2__) # define RAPIDJSON_SSE42 #elif defined(__SSE2__) # define RAPIDJSON_SSE2 +#elif defined(__ARM_NEON) +# define RAPIDJSON_NEON #endif ~~~ diff --git a/include/rapidjson/rapidjson.h b/include/rapidjson/rapidjson.h index f41bb20..57ab851 100644 --- a/include/rapidjson/rapidjson.h +++ b/include/rapidjson/rapidjson.h @@ -325,17 +325,17 @@ #endif /////////////////////////////////////////////////////////////////////////////// -// RAPIDJSON_SSE2/RAPIDJSON_SSE42/RAPIDJSON_SIMD +// RAPIDJSON_SSE2/RAPIDJSON_SSE42/RAPIDJSON_NEON/RAPIDJSON_SIMD /*! \def RAPIDJSON_SIMD \ingroup RAPIDJSON_CONFIG - \brief Enable SSE2/SSE4.2 optimization. + \brief Enable SSE2/SSE4.2/Neon optimization. RapidJSON supports optimized implementations for some parsing operations - based on the SSE2 or SSE4.2 SIMD extensions on modern Intel-compatible - processors. + based on the SSE2, SSE4.2 or NEon SIMD extensions on modern Intel + or ARM compatible processors. - To enable these optimizations, two different symbols can be defined; + To enable these optimizations, three different symbols can be defined; \code // Enable SSE2 optimization. #define RAPIDJSON_SSE2 @@ -344,13 +344,17 @@ #define RAPIDJSON_SSE42 \endcode - \c RAPIDJSON_SSE42 takes precedence, if both are defined. + // Enable ARM Neon optimization. + #define RAPIDJSON_NEON + \endcode + + \c RAPIDJSON_SSE42 takes precedence over SSE2, if both are defined. If any of these symbols is defined, RapidJSON defines the macro \c RAPIDJSON_SIMD to indicate the availability of the optimized code. */ #if defined(RAPIDJSON_SSE2) || defined(RAPIDJSON_SSE42) \ - || defined(RAPIDJSON_DOXYGEN_RUNNING) + || defined(RAPIDJSON_NEON) || defined(RAPIDJSON_DOXYGEN_RUNNING) #define RAPIDJSON_SIMD #endif diff --git a/include/rapidjson/reader.h b/include/rapidjson/reader.h index ccc025e..120c311 100644 --- a/include/rapidjson/reader.h +++ b/include/rapidjson/reader.h @@ -33,6 +33,8 @@ #include #elif defined(RAPIDJSON_SSE2) #include +#elif defined(RAPIDJSON_NEON) +#include #endif #ifdef _MSC_VER @@ -411,7 +413,92 @@ inline const char *SkipWhitespace_SIMD(const char* p, const char* end) { return SkipWhitespace(p, end); } -#endif // RAPIDJSON_SSE2 +#elif defined(RAPIDJSON_NEON) + +//! Skip whitespace with ARM Neon instructions, testing 16 8-byte characters at once. +inline const char *SkipWhitespace_SIMD(const char* p) { + // Fast return for single non-whitespace + if (*p == ' ' || *p == '\n' || *p == '\r' || *p == '\t') + ++p; + else + return p; + + // 16-byte align to the next boundary + const char* nextAligned = reinterpret_cast((reinterpret_cast(p) + 15) & static_cast(~15)); + while (p != nextAligned) + if (*p == ' ' || *p == '\n' || *p == '\r' || *p == '\t') + ++p; + else + return p; + + const uint8x16_t w0 = vmovq_n_u8(' '); + const uint8x16_t w1 = vmovq_n_u8('\n'); + const uint8x16_t w2 = vmovq_n_u8('\r'); + const uint8x16_t w3 = vmovq_n_u8('\t'); + + for (;; p += 16) { + const uint8x16_t s = vld1q_u8(reinterpret_cast(p)); + uint8x16_t x = vceqq_u8(s, w0); + x = vorrq_u8(x, vceqq_u8(s, w1)); + x = vorrq_u8(x, vceqq_u8(s, w2)); + x = vorrq_u8(x, vceqq_u8(s, w3)); + + x = vmvnq_u8(x); // Negate + x = vrev64q_u8(x); // Rev in 64 + uint64_t low = vgetq_lane_u64(reinterpret_cast(x), 0); // extract + uint64_t high = vgetq_lane_u64(reinterpret_cast(x), 1); // extract + + if (low == 0) { + if (high != 0) { + int lz =__builtin_clzll(high);; + return p + 8 + (lz >> 3); + } + } else { + int lz = __builtin_clzll(low);; + return p + (lz >> 3); + } + } +} + +inline const char *SkipWhitespace_SIMD(const char* p, const char* end) { + // Fast return for single non-whitespace + if (p != end && (*p == ' ' || *p == '\n' || *p == '\r' || *p == '\t')) + ++p; + else + return p; + + const uint8x16_t w0 = vmovq_n_u8(' '); + const uint8x16_t w1 = vmovq_n_u8('\n'); + const uint8x16_t w2 = vmovq_n_u8('\r'); + const uint8x16_t w3 = vmovq_n_u8('\t'); + + for (; p <= end - 16; p += 16) { + const uint8x16_t s = vld1q_u8(reinterpret_cast(p)); + uint8x16_t x = vceqq_u8(s, w0); + x = vorrq_u8(x, vceqq_u8(s, w1)); + x = vorrq_u8(x, vceqq_u8(s, w2)); + x = vorrq_u8(x, vceqq_u8(s, w3)); + + x = vmvnq_u8(x); // Negate + x = vrev64q_u8(x); // Rev in 64 + uint64_t low = vgetq_lane_u64(reinterpret_cast(x), 0); // extract + uint64_t high = vgetq_lane_u64(reinterpret_cast(x), 1); // extract + + if (low == 0) { + if (high != 0) { + int lz = __builtin_clzll(high); + return p + 8 + (lz >> 3); + } + } else { + int lz = __builtin_clzll(low); + return p + (lz >> 3); + } + } + + return SkipWhitespace(p, end); +} + +#endif // RAPIDJSON_NEON #ifdef RAPIDJSON_SIMD //! Template function specialization for InsituStringStream @@ -1129,7 +1216,180 @@ private: is.src_ = is.dst_ = p; } -#endif +#elif defined(RAPIDJSON_NEON) + // StringStream -> StackStream + static RAPIDJSON_FORCEINLINE void ScanCopyUnescapedString(StringStream& is, StackStream& os) { + const char* p = is.src_; + + // Scan one by one until alignment (unaligned load may cross page boundary and cause crash) + const char* nextAligned = reinterpret_cast((reinterpret_cast(p) + 15) & static_cast(~15)); + while (p != nextAligned) + if (RAPIDJSON_UNLIKELY(*p == '\"') || RAPIDJSON_UNLIKELY(*p == '\\') || RAPIDJSON_UNLIKELY(static_cast(*p) < 0x20)) { + is.src_ = p; + return; + } + else + os.Put(*p++); + + // The rest of string using SIMD + const uint8x16_t s0 = vmovq_n_u8('"'); + const uint8x16_t s1 = vmovq_n_u8('\\'); + const uint8x16_t s2 = vmovq_n_u8('\b'); + const uint8x16_t s3 = vmovq_n_u8(32); + + for (;; p += 16) { + const uint8x16_t s = vld1q_u8(reinterpret_cast(p)); + uint8x16_t x = vceqq_u8(s, s0); + x = vorrq_u8(x, vceqq_u8(s, s1)); + x = vorrq_u8(x, vceqq_u8(s, s2)); + x = vorrq_u8(x, vcltq_u8(s, s3)); + + x = vrev64q_u8(x); // Rev in 64 + uint64_t low = vgetq_lane_u64(reinterpret_cast(x), 0); // extract + uint64_t high = vgetq_lane_u64(reinterpret_cast(x), 1); // extract + + SizeType length = 0; + bool escaped = false; + if (low == 0) { + if (high != 0) { + unsigned lz = (unsigned)__builtin_clzll(high);; + length = 8 + (lz >> 3); + escaped = true; + } + } else { + unsigned lz = (unsigned)__builtin_clzll(low);; + length = lz >> 3; + escaped = true; + } + if (RAPIDJSON_UNLIKELY(escaped)) { // some of characters is escaped + if (length != 0) { + char* q = reinterpret_cast(os.Push(length)); + for (size_t i = 0; i < length; i++) + q[i] = p[i]; + + p += length; + } + break; + } + vst1q_u8(reinterpret_cast(os.Push(16)), s); + } + + is.src_ = p; + } + + // InsituStringStream -> InsituStringStream + static RAPIDJSON_FORCEINLINE void ScanCopyUnescapedString(InsituStringStream& is, InsituStringStream& os) { + RAPIDJSON_ASSERT(&is == &os); + (void)os; + + if (is.src_ == is.dst_) { + SkipUnescapedString(is); + return; + } + + char* p = is.src_; + char *q = is.dst_; + + // Scan one by one until alignment (unaligned load may cross page boundary and cause crash) + const char* nextAligned = reinterpret_cast((reinterpret_cast(p) + 15) & static_cast(~15)); + while (p != nextAligned) + if (RAPIDJSON_UNLIKELY(*p == '\"') || RAPIDJSON_UNLIKELY(*p == '\\') || RAPIDJSON_UNLIKELY(static_cast(*p) < 0x20)) { + is.src_ = p; + is.dst_ = q; + return; + } + else + *q++ = *p++; + + // The rest of string using SIMD + const uint8x16_t s0 = vmovq_n_u8('"'); + const uint8x16_t s1 = vmovq_n_u8('\\'); + const uint8x16_t s2 = vmovq_n_u8('\b'); + const uint8x16_t s3 = vmovq_n_u8(32); + + for (;; p += 16, q += 16) { + const uint8x16_t s = vld1q_u8(reinterpret_cast(p)); + uint8x16_t x = vceqq_u8(s, s0); + x = vorrq_u8(x, vceqq_u8(s, s1)); + x = vorrq_u8(x, vceqq_u8(s, s2)); + x = vorrq_u8(x, vcltq_u8(s, s3)); + + x = vrev64q_u8(x); // Rev in 64 + uint64_t low = vgetq_lane_u64(reinterpret_cast(x), 0); // extract + uint64_t high = vgetq_lane_u64(reinterpret_cast(x), 1); // extract + + SizeType length = 0; + bool escaped = false; + if (low == 0) { + if (high != 0) { + unsigned lz = (unsigned)__builtin_clzll(high); + length = 8 + (lz >> 3); + escaped = true; + } + } else { + unsigned lz = (unsigned)__builtin_clzll(low); + length = lz >> 3; + escaped = true; + } + if (RAPIDJSON_UNLIKELY(escaped)) { // some of characters is escaped + for (const char* pend = p + length; p != pend; ) { + *q++ = *p++; + } + break; + } + vst1q_u8(reinterpret_cast(q), s); + } + + is.src_ = p; + is.dst_ = q; + } + + // When read/write pointers are the same for insitu stream, just skip unescaped characters + static RAPIDJSON_FORCEINLINE void SkipUnescapedString(InsituStringStream& is) { + RAPIDJSON_ASSERT(is.src_ == is.dst_); + char* p = is.src_; + + // Scan one by one until alignment (unaligned load may cross page boundary and cause crash) + const char* nextAligned = reinterpret_cast((reinterpret_cast(p) + 15) & static_cast(~15)); + for (; p != nextAligned; p++) + if (RAPIDJSON_UNLIKELY(*p == '\"') || RAPIDJSON_UNLIKELY(*p == '\\') || RAPIDJSON_UNLIKELY(static_cast(*p) < 0x20)) { + is.src_ = is.dst_ = p; + return; + } + + // The rest of string using SIMD + const uint8x16_t s0 = vmovq_n_u8('"'); + const uint8x16_t s1 = vmovq_n_u8('\\'); + const uint8x16_t s2 = vmovq_n_u8('\b'); + const uint8x16_t s3 = vmovq_n_u8(32); + + for (;; p += 16) { + const uint8x16_t s = vld1q_u8(reinterpret_cast(p)); + uint8x16_t x = vceqq_u8(s, s0); + x = vorrq_u8(x, vceqq_u8(s, s1)); + x = vorrq_u8(x, vceqq_u8(s, s2)); + x = vorrq_u8(x, vcltq_u8(s, s3)); + + x = vrev64q_u8(x); // Rev in 64 + uint64_t low = vgetq_lane_u64(reinterpret_cast(x), 0); // extract + uint64_t high = vgetq_lane_u64(reinterpret_cast(x), 1); // extract + + if (low == 0) { + if (high != 0) { + int lz = __builtin_clzll(high); + p += 8 + (lz >> 3); + break; + } + } else { + int lz = __builtin_clzll(low); + p += lz >> 3; + break; + } + } + + is.src_ = is.dst_ = p; + } +#endif // RAPIDJSON_NEON template class NumberStream; diff --git a/include/rapidjson/writer.h b/include/rapidjson/writer.h index 219da5e..61cd070 100644 --- a/include/rapidjson/writer.h +++ b/include/rapidjson/writer.h @@ -32,6 +32,8 @@ #include #elif defined(RAPIDJSON_SSE2) #include +#elif defined(RAPIDJSON_NEON) +#include #endif #ifdef _MSC_VER @@ -619,7 +621,75 @@ inline bool Writer::ScanWriteUnescapedString(StringStream& is, siz is.src_ = p; return RAPIDJSON_LIKELY(is.Tell() < length); } -#endif // defined(RAPIDJSON_SSE2) || defined(RAPIDJSON_SSE42) +#elif defined(RAPIDJSON_NEON) +template<> +inline bool Writer::ScanWriteUnescapedString(StringStream& is, size_t length) { + if (length < 16) + return RAPIDJSON_LIKELY(is.Tell() < length); + + if (!RAPIDJSON_LIKELY(is.Tell() < length)) + return false; + + const char* p = is.src_; + const char* end = is.head_ + length; + const char* nextAligned = reinterpret_cast((reinterpret_cast(p) + 15) & static_cast(~15)); + const char* endAligned = reinterpret_cast(reinterpret_cast(end) & static_cast(~15)); + if (nextAligned > end) + return true; + + while (p != nextAligned) + if (*p < 0x20 || *p == '\"' || *p == '\\') { + is.src_ = p; + return RAPIDJSON_LIKELY(is.Tell() < length); + } + else + os_->PutUnsafe(*p++); + + // The rest of string using SIMD + const uint8x16_t s0 = vmovq_n_u8('"'); + const uint8x16_t s1 = vmovq_n_u8('\\'); + const uint8x16_t s2 = vmovq_n_u8('\b'); + const uint8x16_t s3 = vmovq_n_u8(32); + + for (; p != endAligned; p += 16) { + const uint8x16_t s = vld1q_u8(reinterpret_cast(p)); + uint8x16_t x = vceqq_u8(s, s0); + x = vorrq_u8(x, vceqq_u8(s, s1)); + x = vorrq_u8(x, vceqq_u8(s, s2)); + x = vorrq_u8(x, vcltq_u8(s, s3)); + + x = vrev64q_u8(x); // Rev in 64 + uint64_t low = vgetq_lane_u64(reinterpret_cast(x), 0); // extract + uint64_t high = vgetq_lane_u64(reinterpret_cast(x), 1); // extract + + SizeType len = 0; + bool escaped = false; + if (low == 0) { + if (high != 0) { + unsigned lz = (unsigned)__builtin_clzll(high); + len = 8 + (lz >> 3); + escaped = true; + } + } else { + unsigned lz = (unsigned)__builtin_clzll(low); + len = lz >> 3; + escaped = true; + } + if (RAPIDJSON_UNLIKELY(escaped)) { // some of characters is escaped + char* q = reinterpret_cast(os_->PushUnsafe(len)); + for (size_t i = 0; i < len; i++) + q[i] = p[i]; + + p += len; + break; + } + vst1q_u8(reinterpret_cast(os_->PushUnsafe(16)), s); + } + + is.src_ = p; + return RAPIDJSON_LIKELY(is.Tell() < length); +} +#endif // RAPIDJSON_NEON RAPIDJSON_NAMESPACE_END diff --git a/test/perftest/perftest.h b/test/perftest/perftest.h index b098e41..953f95d 100644 --- a/test/perftest/perftest.h +++ b/test/perftest/perftest.h @@ -24,10 +24,13 @@ // __SSE2__ and __SSE4_2__ are recognized by gcc, clang, and the Intel compiler. // We use -march=native with gmake to enable -msse2 and -msse4.2, if supported. +// Likewise, __ARM_NEON is used to detect Neon. #if defined(__SSE4_2__) # define RAPIDJSON_SSE42 #elif defined(__SSE2__) # define RAPIDJSON_SSE2 +#elif defined(__ARM_NEON) +# define RAPIDJSON_NEON #endif #define RAPIDJSON_HAS_STDSTRING 1 diff --git a/test/perftest/rapidjsontest.cpp b/test/perftest/rapidjsontest.cpp index f14e702..a11a557 100644 --- a/test/perftest/rapidjsontest.cpp +++ b/test/perftest/rapidjsontest.cpp @@ -28,6 +28,8 @@ #define SIMD_SUFFIX(name) name##_SSE2 #elif defined(RAPIDJSON_SSE42) #define SIMD_SUFFIX(name) name##_SSE42 +#elif defined(RAPIDJSON_NEON) +#define SIMD_SUFFIX(name) name##_NEON #else #define SIMD_SUFFIX(name) name #endif diff --git a/test/unittest/simdtest.cpp b/test/unittest/simdtest.cpp index b01b559..7b58cd0 100644 --- a/test/unittest/simdtest.cpp +++ b/test/unittest/simdtest.cpp @@ -21,6 +21,8 @@ # define RAPIDJSON_SSE42 #elif defined(__SSE2__) # define RAPIDJSON_SSE2 +#elif defined(__ARM_NEON) +# define RAPIDJSON_NEON #endif #define RAPIDJSON_NAMESPACE rapidjson_simd @@ -41,6 +43,8 @@ using namespace rapidjson_simd; #define SIMD_SUFFIX(name) name##_SSE2 #elif defined(RAPIDJSON_SSE42) #define SIMD_SUFFIX(name) name##_SSE42 +#elif defined(RAPIDJSON_NEON) +#define SIMD_SUFFIX(name) name##_NEON #else #define SIMD_SUFFIX(name) name #endif From 63423eb6f8aa2b315d810baf8e96e7f2600745d7 Mon Sep 17 00:00:00 2001 From: Oliver Hahm Date: Fri, 21 Apr 2017 14:49:12 +0200 Subject: [PATCH 231/305] fix return values --- include/rapidjson/document.h | 2 +- include/rapidjson/schema.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h index a2b044c..c12820e 100644 --- a/include/rapidjson/document.h +++ b/include/rapidjson/document.h @@ -515,7 +515,7 @@ struct TypeHelper { static bool Is(const ValueType& v) { return v.IsObject(); } static ObjectType Get(ValueType& v) { return v.GetObject(); } static ValueType& Set(ValueType& v, ObjectType data) { return v = data; } - static ValueType& Set(ValueType& v, ObjectType data, typename ValueType::AllocatorType&) { v = data; } + static ValueType& Set(ValueType& v, ObjectType data, typename ValueType::AllocatorType&) { return v = data; } }; template diff --git a/include/rapidjson/schema.h b/include/rapidjson/schema.h index 3dddd3a..44a94f8 100644 --- a/include/rapidjson/schema.h +++ b/include/rapidjson/schema.h @@ -1789,7 +1789,7 @@ RAPIDJSON_MULTILINEMACRO_END } virtual void FreeState(void* p) { - return StateAllocator::Free(p); + StateAllocator::Free(p); } private: From 885b5cd2f9b0fe9596d58ee28663cb6267559f67 Mon Sep 17 00:00:00 2001 From: Oliver Hahm Date: Fri, 21 Apr 2017 14:49:30 +0200 Subject: [PATCH 232/305] common notation of empty if/else case --- include/rapidjson/schema.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/rapidjson/schema.h b/include/rapidjson/schema.h index 44a94f8..348dd37 100644 --- a/include/rapidjson/schema.h +++ b/include/rapidjson/schema.h @@ -1112,8 +1112,8 @@ private: if (exclusiveMaximum_ ? i >= maximum_.GetInt64() : i > maximum_.GetInt64()) RAPIDJSON_INVALID_KEYWORD_RETURN(GetMaximumString()); } - else if (maximum_.IsUint64()) - /* do nothing */; // i <= max(int64_t) < maximum_.GetUint64() + else if (maximum_.IsUint64()) { } + /* do nothing */ // i <= max(int64_t) < maximum_.GetUint64() else if (!CheckDoubleMaximum(context, static_cast(i))) return false; } From 4fe02e15f9f59debf169e1d17fdf660e0ad08065 Mon Sep 17 00:00:00 2001 From: Matthew Early Date: Sat, 29 Apr 2017 16:07:23 -0400 Subject: [PATCH 233/305] typo --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 4a1d64d..cd30218 100644 --- a/readme.md +++ b/readme.md @@ -84,7 +84,7 @@ To generate user documentation and run tests please proceed with the steps below 3. Change to `build` directory and run `cmake ..` command to configure your build. Windows users can do the same with cmake-gui application. 4. On Windows, build the solution found in the build directory. On Linux, run `make` from the build directory. -On successfull build you will find compiled test and example binaries in `bin` +On successful build you will find compiled test and example binaries in `bin` directory. The generated documentation will be available in `doc/html` directory of the build tree. To run tests after finished build please run `make test` or `ctest` from your build tree. You can get detailed output using `ctest From fe19b7b6016d446722621fb407738209d1a911e8 Mon Sep 17 00:00:00 2001 From: Harry Wong Date: Thu, 4 May 2017 10:08:48 +0800 Subject: [PATCH 234/305] Supress implicit fallthrough in GCC --- include/rapidjson/internal/regex.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/rapidjson/internal/regex.h b/include/rapidjson/internal/regex.h index 1369ea2..6d110bd 100644 --- a/include/rapidjson/internal/regex.h +++ b/include/rapidjson/internal/regex.h @@ -29,6 +29,7 @@ RAPIDJSON_DIAG_OFF(implicit-fallthrough) #ifdef __GNUC__ RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_OFF(effc++) +RAPIDJSON_DIAG_OFF(implicit-fallthrough) #endif #ifdef _MSC_VER From cba45fe9de6923b858edb0780e257b7257aa4f7b Mon Sep 17 00:00:00 2001 From: Harry Wong Date: Thu, 4 May 2017 10:32:45 +0800 Subject: [PATCH 235/305] Onley apply to GCC 7 --- include/rapidjson/internal/regex.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/rapidjson/internal/regex.h b/include/rapidjson/internal/regex.h index 6d110bd..e1a2faa 100644 --- a/include/rapidjson/internal/regex.h +++ b/include/rapidjson/internal/regex.h @@ -29,8 +29,10 @@ RAPIDJSON_DIAG_OFF(implicit-fallthrough) #ifdef __GNUC__ RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_OFF(effc++) +#if __GNUC__ >= 7 RAPIDJSON_DIAG_OFF(implicit-fallthrough) #endif +#endif #ifdef _MSC_VER RAPIDJSON_DIAG_PUSH From 568107e178c700fecc7eb3c0da483b1a95a01ece Mon Sep 17 00:00:00 2001 From: Hartwig Date: Wed, 10 May 2017 22:56:01 +0200 Subject: [PATCH 236/305] Add convenience method Key(std::basic_string const&) to Writer --- include/rapidjson/writer.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/include/rapidjson/writer.h b/include/rapidjson/writer.h index 61cd070..e610ebb 100644 --- a/include/rapidjson/writer.h +++ b/include/rapidjson/writer.h @@ -222,6 +222,13 @@ public: bool Key(const Ch* str, SizeType length, bool copy = false) { return String(str, length, copy); } +#if RAPIDJSON_HAS_STDSTRING + bool Key(const std::basic_string& str) + { + return Key(str.data(), SizeType(str.size())); + } +#endif + bool EndObject(SizeType memberCount = 0) { (void)memberCount; RAPIDJSON_ASSERT(level_stack_.GetSize() >= sizeof(Level)); // not inside an Object From b61bbbfe371c51c3ee86103bd1da040bcc5a0779 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Thu, 11 May 2017 16:41:26 +0800 Subject: [PATCH 237/305] Fix #947 -Weffc++ warning --- example/lookaheadparser/lookaheadparser.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/example/lookaheadparser/lookaheadparser.cpp b/example/lookaheadparser/lookaheadparser.cpp index 29469ed..f627f4d 100644 --- a/example/lookaheadparser/lookaheadparser.cpp +++ b/example/lookaheadparser/lookaheadparser.cpp @@ -2,6 +2,11 @@ #include "rapidjson/document.h" #include +RAPIDJSON_DIAG_PUSH +#ifdef __GNUC__ +RAPIDJSON_DIAG_OFF(effc++) +#endif + // This example demonstrates JSON token-by-token parsing with an API that is // more direct; you don't need to design your logic around a handler object and // callbacks. Instead, you retrieve values from the JSON stream by calling @@ -341,3 +346,5 @@ int main() { return 0; } + +RAPIDJSON_DIAG_POP From f8eb7bae89fb38f7ffe3dd69e04e95986839b0d0 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Fri, 12 May 2017 10:32:06 +0800 Subject: [PATCH 238/305] Remove -Weverything See #930 --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d6823a8..8b90c87 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -87,7 +87,7 @@ elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native") endif() set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Werror -Wno-missing-field-initializers") - set(EXTRA_CXX_FLAGS -Weffc++ -Wswitch-default -Wfloat-equal -Wconversion -Wimplicit-fallthrough -Weverything) + set(EXTRA_CXX_FLAGS -Weffc++ -Wswitch-default -Wfloat-equal -Wconversion -Wimplicit-fallthrough) if (RAPIDJSON_BUILD_CXX11) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") endif() From 56b7216efe5014a3936da2fc4d01ad1dc45a2250 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Fri, 12 May 2017 10:32:41 +0800 Subject: [PATCH 239/305] Fix #949 about -Werror=conversion --- test/unittest/pointertest.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/unittest/pointertest.cpp b/test/unittest/pointertest.cpp index dbddbed..eed6fba 100644 --- a/test/unittest/pointertest.cpp +++ b/test/unittest/pointertest.cpp @@ -441,8 +441,8 @@ TEST(Pointer, Stringify) { } // Construct a Pointer with static tokens, no dynamic allocation involved. -#define NAME(s) { s, sizeof(s) / sizeof(s[0]) - 1, kPointerInvalidIndex } -#define INDEX(i) { #i, sizeof(#i) - 1, i } +#define NAME(s) { s, static_cast(sizeof(s) / sizeof(s[0]) - 1), kPointerInvalidIndex } +#define INDEX(i) { #i, static_cast(sizeof(#i) - 1), i } static const Pointer::Token kTokens[] = { NAME("foo"), INDEX(0) }; // equivalent to "/foo/0" From 0033268c115cbfa224937a76685bfb1e55fdb506 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Fri, 12 May 2017 17:30:33 +0800 Subject: [PATCH 240/305] Update tutorial.zh-cn.md typo --- doc/tutorial.zh-cn.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/tutorial.zh-cn.md b/doc/tutorial.zh-cn.md index ec1315c..6b2588f 100644 --- a/doc/tutorial.zh-cn.md +++ b/doc/tutorial.zh-cn.md @@ -343,7 +343,7 @@ Value o(kObjectType); ![转移语义不需复制。](diagram/move3.png) -在 C++11 中这称为转移赋值操作(move assignment operator)。由于 RapidJSON 支持 C++03,它在赋值操作采用转移语义,其它修改形函数如 `AddMember()`, `PushBack()` 也采用转移语义。 +在 C++11 中这称为转移赋值操作(move assignment operator)。由于 RapidJSON 支持 C++03,它在赋值操作采用转移语义,其它修改型函数如 `AddMember()`, `PushBack()` 也采用转移语义。 ### 转移语义及临时值 {#TemporaryValues} From 4ef1ff4fbac491676702cf9e4f300d504d56cee9 Mon Sep 17 00:00:00 2001 From: "Philipp A. Hartmann" Date: Thu, 18 May 2017 19:08:23 +0200 Subject: [PATCH 241/305] GenericValue::CopyFrom: add option to force copying of strings Copying the result of an in-situ parsing into another value/document currently requires that the original buffer - still holding the strings from the parsing, outlives the destination object as well. In order to obtain a "full" copy of a GenericValue, this commit adds an optional flag `copyConstStrings` to `CopyFrom`, which then forces to take a copy of all embedded strings in the source value. This solves the problem discussed in #962. --- include/rapidjson/document.h | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h index c12820e..0d13b60 100644 --- a/include/rapidjson/document.h +++ b/include/rapidjson/document.h @@ -615,10 +615,11 @@ public: \tparam SourceAllocator allocator of \c rhs \param rhs Value to copy from (read-only) \param allocator Allocator for allocating copied elements and buffers. Commonly use GenericDocument::GetAllocator(). + \param copyConstStrings Force copying of constant strings (e.g. referencing an in-situ buffer) \see CopyFrom() */ template - GenericValue(const GenericValue& rhs, Allocator& allocator) { + GenericValue(const GenericValue& rhs, Allocator& allocator, bool copyConstStrings = false) { switch (rhs.GetType()) { case kObjectType: { SizeType count = rhs.data_.o.size; @@ -645,7 +646,7 @@ public: } break; case kStringType: - if (rhs.data_.f.flags == kConstStringFlag) { + if (rhs.data_.f.flags == kConstStringFlag && !copyConstStrings) { data_.f.flags = rhs.data_.f.flags; data_ = *reinterpret_cast(&rhs.data_); } @@ -850,12 +851,13 @@ public: \tparam SourceAllocator Allocator type of \c rhs \param rhs Value to copy from (read-only) \param allocator Allocator to use for copying + \param copyConstStrings Force copying of constant strings (e.g. referencing an in-situ buffer) */ template - GenericValue& CopyFrom(const GenericValue& rhs, Allocator& allocator) { + GenericValue& CopyFrom(const GenericValue& rhs, Allocator& allocator, bool copyConstStrings = false) { RAPIDJSON_ASSERT(static_cast(this) != static_cast(&rhs)); this->~GenericValue(); - new (this) GenericValue(rhs, allocator); + new (this) GenericValue(rhs, allocator, copyConstStrings); return *this; } From 77d2fadfb615687038ffeff9dac5acdfc4d5e327 Mon Sep 17 00:00:00 2001 From: "Tomasz Noczynski (Linux)" Date: Thu, 25 May 2017 13:21:57 +0200 Subject: [PATCH 242/305] If storage class is not specified as first in declaration then Intel C++ Compiler 2017 generates message: message #82: storage class is not first --- include/rapidjson/encodings.h | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/include/rapidjson/encodings.h b/include/rapidjson/encodings.h index ed7d44d..0df1c34 100644 --- a/include/rapidjson/encodings.h +++ b/include/rapidjson/encodings.h @@ -620,28 +620,28 @@ struct AutoUTF { #define RAPIDJSON_ENCODINGS_FUNC(x) UTF8::x, UTF16LE::x, UTF16BE::x, UTF32LE::x, UTF32BE::x template - RAPIDJSON_FORCEINLINE static void Encode(OutputStream& os, unsigned codepoint) { + static RAPIDJSON_FORCEINLINE void Encode(OutputStream& os, unsigned codepoint) { typedef void (*EncodeFunc)(OutputStream&, unsigned); static const EncodeFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Encode) }; (*f[os.GetType()])(os, codepoint); } template - RAPIDJSON_FORCEINLINE static void EncodeUnsafe(OutputStream& os, unsigned codepoint) { + static RAPIDJSON_FORCEINLINE void EncodeUnsafe(OutputStream& os, unsigned codepoint) { typedef void (*EncodeFunc)(OutputStream&, unsigned); static const EncodeFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(EncodeUnsafe) }; (*f[os.GetType()])(os, codepoint); } template - RAPIDJSON_FORCEINLINE static bool Decode(InputStream& is, unsigned* codepoint) { + static RAPIDJSON_FORCEINLINE bool Decode(InputStream& is, unsigned* codepoint) { typedef bool (*DecodeFunc)(InputStream&, unsigned*); static const DecodeFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Decode) }; return (*f[is.GetType()])(is, codepoint); } template - RAPIDJSON_FORCEINLINE static bool Validate(InputStream& is, OutputStream& os) { + static RAPIDJSON_FORCEINLINE bool Validate(InputStream& is, OutputStream& os) { typedef bool (*ValidateFunc)(InputStream&, OutputStream&); static const ValidateFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Validate) }; return (*f[is.GetType()])(is, os); @@ -658,7 +658,7 @@ template struct Transcoder { //! Take one Unicode codepoint from source encoding, convert it to target encoding and put it to the output stream. template - RAPIDJSON_FORCEINLINE static bool Transcode(InputStream& is, OutputStream& os) { + static RAPIDJSON_FORCEINLINE bool Transcode(InputStream& is, OutputStream& os) { unsigned codepoint; if (!SourceEncoding::Decode(is, &codepoint)) return false; @@ -667,7 +667,7 @@ struct Transcoder { } template - RAPIDJSON_FORCEINLINE static bool TranscodeUnsafe(InputStream& is, OutputStream& os) { + static RAPIDJSON_FORCEINLINE bool TranscodeUnsafe(InputStream& is, OutputStream& os) { unsigned codepoint; if (!SourceEncoding::Decode(is, &codepoint)) return false; @@ -677,7 +677,7 @@ struct Transcoder { //! Validate one Unicode codepoint from an encoded stream. template - RAPIDJSON_FORCEINLINE static bool Validate(InputStream& is, OutputStream& os) { + static RAPIDJSON_FORCEINLINE bool Validate(InputStream& is, OutputStream& os) { return Transcode(is, os); // Since source/target encoding is different, must transcode. } }; @@ -690,19 +690,19 @@ inline void PutUnsafe(Stream& stream, typename Stream::Ch c); template struct Transcoder { template - RAPIDJSON_FORCEINLINE static bool Transcode(InputStream& is, OutputStream& os) { + static RAPIDJSON_FORCEINLINE bool Transcode(InputStream& is, OutputStream& os) { os.Put(is.Take()); // Just copy one code unit. This semantic is different from primary template class. return true; } template - RAPIDJSON_FORCEINLINE static bool TranscodeUnsafe(InputStream& is, OutputStream& os) { + static RAPIDJSON_FORCEINLINE bool TranscodeUnsafe(InputStream& is, OutputStream& os) { PutUnsafe(os, is.Take()); // Just copy one code unit. This semantic is different from primary template class. return true; } template - RAPIDJSON_FORCEINLINE static bool Validate(InputStream& is, OutputStream& os) { + static RAPIDJSON_FORCEINLINE bool Validate(InputStream& is, OutputStream& os) { return Encoding::Validate(is, os); // source/target encoding are the same } }; From 294ad93e30abf27e99c037054e8adb1ce853d6e4 Mon Sep 17 00:00:00 2001 From: "Tomasz Noczynski (Linux)" Date: Thu, 25 May 2017 14:14:16 +0200 Subject: [PATCH 243/305] To avoid Intel C++ Compiler #1879 warnings: warning #1879: unimplemented pragma ignored: #pragma intrinsic(_BitScanReverse64) warning #1879: unimplemented pragma ignored: #pragma intrinsic(_umul128) --- include/rapidjson/internal/diyfp.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/rapidjson/internal/diyfp.h b/include/rapidjson/internal/diyfp.h index c9fefdc..29abf80 100644 --- a/include/rapidjson/internal/diyfp.h +++ b/include/rapidjson/internal/diyfp.h @@ -21,7 +21,7 @@ #include "../rapidjson.h" -#if defined(_MSC_VER) && defined(_M_AMD64) +#if defined(_MSC_VER) && defined(_M_AMD64) && !defined(__INTEL_COMPILER) #include #pragma intrinsic(_BitScanReverse64) #pragma intrinsic(_umul128) From 68c96e987bd0eb67ff399904dee6b7c07042ff18 Mon Sep 17 00:00:00 2001 From: "Philipp A. Hartmann" Date: Sat, 27 May 2017 10:26:35 +0200 Subject: [PATCH 244/305] Fixup #964 by forwarding copyConstStrings recursively As reported by @Llerd in #962, the `copyConstStrings` parameter has not been forwarded recursively to the constructors of object members and array elements. --- include/rapidjson/document.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h index 0d13b60..57f0b3c 100644 --- a/include/rapidjson/document.h +++ b/include/rapidjson/document.h @@ -626,8 +626,8 @@ public: Member* lm = reinterpret_cast(allocator.Malloc(count * sizeof(Member))); const typename GenericValue::Member* rm = rhs.GetMembersPointer(); for (SizeType i = 0; i < count; i++) { - new (&lm[i].name) GenericValue(rm[i].name, allocator); - new (&lm[i].value) GenericValue(rm[i].value, allocator); + new (&lm[i].name) GenericValue(rm[i].name, allocator, copyConstStrings); + new (&lm[i].value) GenericValue(rm[i].value, allocator, copyConstStrings); } data_.f.flags = kObjectFlag; data_.o.size = data_.o.capacity = count; @@ -639,7 +639,7 @@ public: GenericValue* le = reinterpret_cast(allocator.Malloc(count * sizeof(GenericValue))); const GenericValue* re = rhs.GetElementsPointer(); for (SizeType i = 0; i < count; i++) - new (&le[i]) GenericValue(re[i], allocator); + new (&le[i]) GenericValue(re[i], allocator, copyConstStrings); data_.f.flags = kArrayFlag; data_.a.size = data_.a.capacity = count; SetElementsPointer(le); From df6362d45060d7fde9772d21f23af969d039e920 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Fri, 9 Jun 2017 10:16:24 +0800 Subject: [PATCH 245/305] Fix patternProperties & additionalProperties lead to ASSERT Fix #825 --- include/rapidjson/schema.h | 4 +++- test/unittest/schematest.cpp | 7 +++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/include/rapidjson/schema.h b/include/rapidjson/schema.h index 348dd37..dd57edb 100644 --- a/include/rapidjson/schema.h +++ b/include/rapidjson/schema.h @@ -783,8 +783,10 @@ public: if (patternProperties_) { context.patternPropertiesSchemaCount = 0; for (SizeType i = 0; i < patternPropertyCount_; i++) - if (patternProperties_[i].pattern && IsPatternMatch(patternProperties_[i].pattern, str, len)) + if (patternProperties_[i].pattern && IsPatternMatch(patternProperties_[i].pattern, str, len)) { context.patternPropertiesSchemas[context.patternPropertiesSchemaCount++] = patternProperties_[i].schema; + context.valueSchema = typeless_; + } } SizeType index; diff --git a/test/unittest/schematest.cpp b/test/unittest/schematest.cpp index 30b3260..e79fec2 100644 --- a/test/unittest/schematest.cpp +++ b/test/unittest/schematest.cpp @@ -1322,6 +1322,13 @@ TEST(SchemaValidator, Issue728_AllOfRef) { VALIDATE(s, "{\"key1\": \"abc\", \"key2\": \"def\"}", true); } +TEST(SchemaValidator, Issue825) { + Document sd; + sd.Parse("{\"type\": \"object\", \"additionalProperties\": false, \"patternProperties\": {\"^i\": { \"type\": \"string\" } } }"); + SchemaDocument s(sd); + VALIDATE(s, "{ \"item\": \"hello\" }", true); +} + #ifdef __clang__ RAPIDJSON_DIAG_POP #endif From 6e81d49b3374c1e8667438056c16ab2f3611c1fd Mon Sep 17 00:00:00 2001 From: kyb Date: Thu, 15 Jun 2017 12:36:20 +0300 Subject: [PATCH 246/305] Fixed #985 : Unittest failed with MinGWx64. And few small improvement were done while looking for mistakes. Problem was because of Windows uses backslashes '\', not Unix '/' --- test/unittest/ostreamwrappertest.cpp | 5 +++-- test/unittest/prettywritertest.cpp | 1 + test/unittest/unittest.h | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/test/unittest/ostreamwrappertest.cpp b/test/unittest/ostreamwrappertest.cpp index b1d1cd8..50f8da6 100644 --- a/test/unittest/ostreamwrappertest.cpp +++ b/test/unittest/ostreamwrappertest.cpp @@ -69,14 +69,15 @@ static void TestFileStream() { const char* s = "Hello World!\n"; { - ofstream ofs(filename, ios::out | ios::binary); - BasicOStreamWrapper osw(ofs); + FileStreamType ofs(filename, ios::out | ios::binary); + BasicOStreamWrapper osw(ofs); for (const char* p = s; *p; p++) osw.Put(*p); osw.Flush(); } fp = fopen(filename, "r"); + ASSERT_TRUE( fp != NULL ); for (const char* p = s; *p; p++) EXPECT_EQ(*p, static_cast(fgetc(fp))); fclose(fp); diff --git a/test/unittest/prettywritertest.cpp b/test/unittest/prettywritertest.cpp index 1e1ca1a..43617a2 100644 --- a/test/unittest/prettywritertest.cpp +++ b/test/unittest/prettywritertest.cpp @@ -167,6 +167,7 @@ TEST(PrettyWriter, OStreamWrapper) { TEST(PrettyWriter, FileWriteStream) { char filename[L_tmpnam]; FILE* fp = TempFile(filename); + ASSERT_TRUE(fp!=NULL); char buffer[16]; FileWriteStream os(fp, buffer, sizeof(buffer)); PrettyWriter writer(os); diff --git a/test/unittest/unittest.h b/test/unittest/unittest.h index e125bf8..5837345 100644 --- a/test/unittest/unittest.h +++ b/test/unittest/unittest.h @@ -78,7 +78,7 @@ inline Ch* StrDup(const Ch* str) { } inline FILE* TempFile(char *filename) { -#ifdef _MSC_VER +#if defined(__WIN32__) || defined(_MSC_VER) filename = tmpnam(filename); // For Visual Studio, tmpnam() adds a backslash in front. Remove it. From a31a380cb81c2f20baf4cd7204815e235ea8d2bd Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Wed, 21 Jun 2017 14:25:47 +0800 Subject: [PATCH 247/305] Improve readme.md Add alt text for images Use https whenever possible Update URLs Use tools.ietf.org for RFC7159 Correct indent for sublists Trim trailing whitespaces --- readme.md | 50 ++++++++++++++++++++++++------------------------- readme.zh-cn.md | 50 ++++++++++++++++++++++++------------------------- 2 files changed, 50 insertions(+), 50 deletions(-) diff --git a/readme.md b/readme.md index cd30218..2937619 100644 --- a/readme.md +++ b/readme.md @@ -1,8 +1,8 @@ -![](doc/logo/rapidjson.png) +![RapidJSON logo](doc/logo/rapidjson.png) -![](https://img.shields.io/badge/release-v1.1.0-blue.png) +![Release version](https://img.shields.io/badge/release-v1.1.0-blue.svg) -## A fast JSON parser/generator for C++ with both SAX/DOM style API +## A fast JSON parser/generator for C++ with both SAX/DOM style API Tencent is pleased to support the open source community by making RapidJSON available. @@ -20,12 +20,12 @@ Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights | :---------------: | :-----------------: | :-------------------: | | ![lin-badge] | ![win-badge] | ![cov-badge] | -[lin-badge]: https://travis-ci.org/miloyip/rapidjson.png?branch=master "Travis build status" +[lin-badge]: https://travis-ci.org/miloyip/rapidjson.svg?branch=master "Travis build status" [lin-link]: https://travis-ci.org/miloyip/rapidjson "Travis build status" -[win-badge]: https://ci.appveyor.com/api/projects/status/u658dcuwxo14a8m9/branch/master "AppVeyor build status" +[win-badge]: https://ci.appveyor.com/api/projects/status/github/miloyip/rapidjson?branch=master&svg=true "AppVeyor build status" [win-link]: https://ci.appveyor.com/project/miloyip/rapidjson/branch/master "AppVeyor build status" -[cov-badge]: https://coveralls.io/repos/miloyip/rapidjson/badge.png?branch=master -[cov-link]: https://coveralls.io/r/miloyip/rapidjson?branch=master +[cov-badge]: https://coveralls.io/repos/miloyip/rapidjson/badge.svg?branch=master "Coveralls coverage" +[cov-link]: https://coveralls.io/r/miloyip/rapidjson?branch=master "Coveralls coverage" ## Introduction @@ -45,8 +45,8 @@ More features can be read [here](doc/features.md). JSON(JavaScript Object Notation) is a light-weight data exchange format. RapidJSON should be in fully compliance with RFC7159/ECMA-404, with optional support of relaxed syntax. More information about JSON can be obtained at * [Introducing JSON](http://json.org/) -* [RFC7159: The JavaScript Object Notation (JSON) Data Interchange Format](http://www.ietf.org/rfc/rfc7159.txt) -* [Standard ECMA-404: The JSON Data Interchange Format](http://www.ecma-international.org/publications/standards/Ecma-404.htm) +* [RFC7159: The JavaScript Object Notation (JSON) Data Interchange Format](https://tools.ietf.org/html/rfc7159) +* [Standard ECMA-404: The JSON Data Interchange Format](https://www.ecma-international.org/publications/standards/Ecma-404.htm) ## Highlights in v1.1 (2016-8-25) @@ -74,8 +74,8 @@ RapidJSON is a header-only C++ library. Just copy the `include/rapidjson` folder RapidJSON uses following software as its dependencies: * [CMake](https://cmake.org/) as a general build tool -* (optional)[Doxygen](http://www.doxygen.org) to build documentation -* (optional)[googletest](https://github.com/google/googletest) for unit and performance testing +* (optional) [Doxygen](http://www.doxygen.org) to build documentation +* (optional) [googletest](https://github.com/google/googletest) for unit and performance testing To generate user documentation and run tests please proceed with the steps below: @@ -139,22 +139,22 @@ The following diagram shows the process. More [examples](https://github.com/miloyip/rapidjson/tree/master/example) are available: * DOM API - * [tutorial](https://github.com/miloyip/rapidjson/blob/master/example/tutorial/tutorial.cpp): Basic usage of DOM API. + * [tutorial](https://github.com/miloyip/rapidjson/blob/master/example/tutorial/tutorial.cpp): Basic usage of DOM API. * SAX API - * [simplereader](https://github.com/miloyip/rapidjson/blob/master/example/simplereader/simplereader.cpp): Dumps all SAX events while parsing a JSON by `Reader`. - * [condense](https://github.com/miloyip/rapidjson/blob/master/example/condense/condense.cpp): A command line tool to rewrite a JSON, with all whitespaces removed. - * [pretty](https://github.com/miloyip/rapidjson/blob/master/example/pretty/pretty.cpp): A command line tool to rewrite a JSON with indents and newlines by `PrettyWriter`. - * [capitalize](https://github.com/miloyip/rapidjson/blob/master/example/capitalize/capitalize.cpp): A command line tool to capitalize strings in JSON. - * [messagereader](https://github.com/miloyip/rapidjson/blob/master/example/messagereader/messagereader.cpp): Parse a JSON message with SAX API. - * [serialize](https://github.com/miloyip/rapidjson/blob/master/example/serialize/serialize.cpp): Serialize a C++ object into JSON with SAX API. - * [jsonx](https://github.com/miloyip/rapidjson/blob/master/example/jsonx/jsonx.cpp): Implements a `JsonxWriter` which stringify SAX events into [JSONx](https://www-01.ibm.com/support/knowledgecenter/SS9H2Y_7.1.0/com.ibm.dp.doc/json_jsonx.html) (a kind of XML) format. The example is a command line tool which converts input JSON into JSONx format. + * [simplereader](https://github.com/miloyip/rapidjson/blob/master/example/simplereader/simplereader.cpp): Dumps all SAX events while parsing a JSON by `Reader`. + * [condense](https://github.com/miloyip/rapidjson/blob/master/example/condense/condense.cpp): A command line tool to rewrite a JSON, with all whitespaces removed. + * [pretty](https://github.com/miloyip/rapidjson/blob/master/example/pretty/pretty.cpp): A command line tool to rewrite a JSON with indents and newlines by `PrettyWriter`. + * [capitalize](https://github.com/miloyip/rapidjson/blob/master/example/capitalize/capitalize.cpp): A command line tool to capitalize strings in JSON. + * [messagereader](https://github.com/miloyip/rapidjson/blob/master/example/messagereader/messagereader.cpp): Parse a JSON message with SAX API. + * [serialize](https://github.com/miloyip/rapidjson/blob/master/example/serialize/serialize.cpp): Serialize a C++ object into JSON with SAX API. + * [jsonx](https://github.com/miloyip/rapidjson/blob/master/example/jsonx/jsonx.cpp): Implements a `JsonxWriter` which stringify SAX events into [JSONx](https://www-01.ibm.com/support/knowledgecenter/SS9H2Y_7.1.0/com.ibm.dp.doc/json_jsonx.html) (a kind of XML) format. The example is a command line tool which converts input JSON into JSONx format. * Schema - * [schemavalidator](https://github.com/miloyip/rapidjson/blob/master/example/schemavalidator/schemavalidator.cpp) : A command line tool to validate a JSON with a JSON schema. - + * [schemavalidator](https://github.com/miloyip/rapidjson/blob/master/example/schemavalidator/schemavalidator.cpp) : A command line tool to validate a JSON with a JSON schema. + * Advanced - * [prettyauto](https://github.com/miloyip/rapidjson/blob/master/example/prettyauto/prettyauto.cpp): A modified version of [pretty](https://github.com/miloyip/rapidjson/blob/master/example/pretty/pretty.cpp) to automatically handle JSON with any UTF encodings. - * [parsebyparts](https://github.com/miloyip/rapidjson/blob/master/example/parsebyparts/parsebyparts.cpp): Implements an `AsyncDocumentParser` which can parse JSON in parts, using C++11 thread. - * [filterkey](https://github.com/miloyip/rapidjson/blob/master/example/filterkey/filterkey.cpp): A command line tool to remove all values with user-specified key. - * [filterkeydom](https://github.com/miloyip/rapidjson/blob/master/example/filterkeydom/filterkeydom.cpp): Same tool as above, but it demonstrates how to use a generator to populate a `Document`. + * [prettyauto](https://github.com/miloyip/rapidjson/blob/master/example/prettyauto/prettyauto.cpp): A modified version of [pretty](https://github.com/miloyip/rapidjson/blob/master/example/pretty/pretty.cpp) to automatically handle JSON with any UTF encodings. + * [parsebyparts](https://github.com/miloyip/rapidjson/blob/master/example/parsebyparts/parsebyparts.cpp): Implements an `AsyncDocumentParser` which can parse JSON in parts, using C++11 thread. + * [filterkey](https://github.com/miloyip/rapidjson/blob/master/example/filterkey/filterkey.cpp): A command line tool to remove all values with user-specified key. + * [filterkeydom](https://github.com/miloyip/rapidjson/blob/master/example/filterkeydom/filterkeydom.cpp): Same tool as above, but it demonstrates how to use a generator to populate a `Document`. diff --git a/readme.zh-cn.md b/readme.zh-cn.md index b62b2e1..81b84bb 100644 --- a/readme.zh-cn.md +++ b/readme.zh-cn.md @@ -1,6 +1,6 @@ -![](doc/logo/rapidjson.png) +![RapidJSON logo](doc/logo/rapidjson.png) -![](https://img.shields.io/badge/release-v1.1.0-blue.png) +![Release version](https://img.shields.io/badge/release-v1.1.0-blue.svg) ## 高效的 C++ JSON 解析/生成器,提供 SAX 及 DOM 风格 API @@ -20,12 +20,12 @@ Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights | :---------------: | :-----------------: | :-------------------: | | ![lin-badge] | ![win-badge] | ![cov-badge] | -[lin-badge]: https://travis-ci.org/miloyip/rapidjson.png?branch=master "Travis build status" +[lin-badge]: https://travis-ci.org/miloyip/rapidjson.svg?branch=master "Travis build status" [lin-link]: https://travis-ci.org/miloyip/rapidjson "Travis build status" -[win-badge]: https://ci.appveyor.com/api/projects/status/u658dcuwxo14a8m9/branch/master "AppVeyor build status" +[win-badge]: https://ci.appveyor.com/api/projects/status/github/miloyip/rapidjson?branch=master&svg=true "AppVeyor build status" [win-link]: https://ci.appveyor.com/project/miloyip/rapidjson/branch/master "AppVeyor build status" -[cov-badge]: https://coveralls.io/repos/miloyip/rapidjson/badge.png?branch=master -[cov-link]: https://coveralls.io/r/miloyip/rapidjson?branch=master +[cov-badge]: https://coveralls.io/repos/miloyip/rapidjson/badge.svg?branch=master "Coveralls coverage" +[cov-link]: https://coveralls.io/r/miloyip/rapidjson?branch=master "Coveralls coverage" ## 简介 @@ -45,8 +45,8 @@ RapidJSON 是一个 C++ 的 JSON 解析器及生成器。它的灵感来自 [Rap JSON(JavaScript Object Notation)是一个轻量的数据交换格式。RapidJSON 应该完全遵从 RFC7159/ECMA-404,并支持可选的放宽语法。 关于 JSON 的更多信息可参考: * [Introducing JSON](http://json.org/) -* [RFC7159: The JavaScript Object Notation (JSON) Data Interchange Format](http://www.ietf.org/rfc/rfc7159.txt) -* [Standard ECMA-404: The JSON Data Interchange Format](http://www.ecma-international.org/publications/standards/Ecma-404.htm) +* [RFC7159: The JavaScript Object Notation (JSON) Data Interchange Format](https://tools.ietf.org/html/rfc7159) +* [Standard ECMA-404: The JSON Data Interchange Format](https://www.ecma-international.org/publications/standards/Ecma-404.htm) ## v1.1 中的亮点 (2016-8-25) @@ -73,9 +73,9 @@ RapidJSON 是跨平台的。以下是一些曾测试的平台/编译器组合 RapidJSON 是只有头文件的 C++ 库。只需把 `include/rapidjson` 目录复制至系统或项目的 include 目录中。 RapidJSON 依赖于以下软件: -* [CMake](http://www.cmake.org) 作为通用生成工具 -* (optional)[Doxygen](http://www.doxygen.org) 用于生成文档 -* (optional)[googletest](https://code.google.com/p/googletest/) 用于单元及性能测试 +* [CMake](https://cmake.org/) 作为通用生成工具 +* (optional) [Doxygen](http://www.doxygen.org) 用于生成文档 +* (optional) [googletest](https://github.com/google/googletest) 用于单元及性能测试 生成测试及例子的步骤: @@ -131,22 +131,22 @@ int main() { 还有许多 [例子](https://github.com/miloyip/rapidjson/tree/master/example) 可供参考: * DOM API - * [tutorial](https://github.com/miloyip/rapidjson/blob/master/example/tutorial/tutorial.cpp): DOM API 的基本使用方法。 + * [tutorial](https://github.com/miloyip/rapidjson/blob/master/example/tutorial/tutorial.cpp): DOM API 的基本使用方法。 * SAX API - * [simplereader](https://github.com/miloyip/rapidjson/blob/master/example/simplereader/simplereader.cpp): 使用 `Reader` 解析 JSON 时,打印所有 SAX 事件。 - * [condense](https://github.com/miloyip/rapidjson/blob/master/example/condense/condense.cpp): 移除 JSON 中所有空白符的命令行工具。 - * [pretty](https://github.com/miloyip/rapidjson/blob/master/example/pretty/pretty.cpp): 为 JSON 加入缩进与换行的命令行工具,当中使用了 `PrettyWriter`。 - * [capitalize](https://github.com/miloyip/rapidjson/blob/master/example/capitalize/capitalize.cpp): 把 JSON 中所有字符串改为大写的命令行工具。 - * [messagereader](https://github.com/miloyip/rapidjson/blob/master/example/messagereader/messagereader.cpp): 使用 SAX API 去解析一个 JSON 报文。 - * [serialize](https://github.com/miloyip/rapidjson/blob/master/example/serialize/serialize.cpp): 使用 SAX API 去序列化 C++ 对象,生成 JSON。 - * [jsonx](https://github.com/miloyip/rapidjson/blob/master/example/jsonx/jsonx.cpp): 实现了一个 `JsonxWriter`,它能把 SAX 事件写成 [JSONx](https://www-01.ibm.com/support/knowledgecenter/SS9H2Y_7.1.0/com.ibm.dp.doc/json_jsonx.html)(一种 XML)格式。这个例子是把 JSON 输入转换成 JSONx 格式的命令行工具。 + * [simplereader](https://github.com/miloyip/rapidjson/blob/master/example/simplereader/simplereader.cpp): 使用 `Reader` 解析 JSON 时,打印所有 SAX 事件。 + * [condense](https://github.com/miloyip/rapidjson/blob/master/example/condense/condense.cpp): 移除 JSON 中所有空白符的命令行工具。 + * [pretty](https://github.com/miloyip/rapidjson/blob/master/example/pretty/pretty.cpp): 为 JSON 加入缩进与换行的命令行工具,当中使用了 `PrettyWriter`。 + * [capitalize](https://github.com/miloyip/rapidjson/blob/master/example/capitalize/capitalize.cpp): 把 JSON 中所有字符串改为大写的命令行工具。 + * [messagereader](https://github.com/miloyip/rapidjson/blob/master/example/messagereader/messagereader.cpp): 使用 SAX API 去解析一个 JSON 报文。 + * [serialize](https://github.com/miloyip/rapidjson/blob/master/example/serialize/serialize.cpp): 使用 SAX API 去序列化 C++ 对象,生成 JSON。 + * [jsonx](https://github.com/miloyip/rapidjson/blob/master/example/jsonx/jsonx.cpp): 实现了一个 `JsonxWriter`,它能把 SAX 事件写成 [JSONx](https://www-01.ibm.com/support/knowledgecenter/SS9H2Y_7.1.0/com.ibm.dp.doc/json_jsonx.html)(一种 XML)格式。这个例子是把 JSON 输入转换成 JSONx 格式的命令行工具。 * Schema API - * [schemavalidator](https://github.com/miloyip/rapidjson/blob/master/example/schemavalidator/schemavalidator.cpp): 使用 JSON Schema 去校验 JSON 的命令行工具。 - + * [schemavalidator](https://github.com/miloyip/rapidjson/blob/master/example/schemavalidator/schemavalidator.cpp): 使用 JSON Schema 去校验 JSON 的命令行工具。 + * 进阶 - * [prettyauto](https://github.com/miloyip/rapidjson/blob/master/example/prettyauto/prettyauto.cpp): [pretty](https://github.com/miloyip/rapidjson/blob/master/example/pretty/pretty.cpp) 的修改版本,可自动处理任何 UTF 编码的 JSON。 - * [parsebyparts](https://github.com/miloyip/rapidjson/blob/master/example/parsebyparts/parsebyparts.cpp): 这例子中的 `AsyncDocumentParser` 类使用 C++ 线程来逐段解析 JSON。 - * [filterkey](https://github.com/miloyip/rapidjson/blob/master/example/filterkey/filterkey.cpp): 移取使用者指定的键值的命令行工具。 - * [filterkeydom](https://github.com/miloyip/rapidjson/blob/master/example/filterkey/filterkey.cpp): 如上的工具,但展示如何使用生成器(generator)去填充一个 `Document`。 \ No newline at end of file + * [prettyauto](https://github.com/miloyip/rapidjson/blob/master/example/prettyauto/prettyauto.cpp): [pretty](https://github.com/miloyip/rapidjson/blob/master/example/pretty/pretty.cpp) 的修改版本,可自动处理任何 UTF 编码的 JSON。 + * [parsebyparts](https://github.com/miloyip/rapidjson/blob/master/example/parsebyparts/parsebyparts.cpp): 这例子中的 `AsyncDocumentParser` 类使用 C++ 线程来逐段解析 JSON。 + * [filterkey](https://github.com/miloyip/rapidjson/blob/master/example/filterkey/filterkey.cpp): 移取使用者指定的键值的命令行工具。 + * [filterkeydom](https://github.com/miloyip/rapidjson/blob/master/example/filterkey/filterkey.cpp): 如上的工具,但展示如何使用生成器(generator)去填充一个 `Document`。 \ No newline at end of file From 0d62f5cd359be662a5b378b8a04862a9bce645f5 Mon Sep 17 00:00:00 2001 From: Leo Mehr Date: Thu, 29 Jun 2017 20:09:13 -0400 Subject: [PATCH 248/305] Tutorial: fix typos in examples and broken links In the move example, the code uses `contacts` when the diagrams use `contact` (no 's') The code in the example: ``` Value contacts(kArrayType); // adding elements to contacts array. // ... o.AddMember("contacts", contacts, d.GetAllocator()); // deep clone contacts (may be with lots of allocations) // destruct contacts. ``` --- doc/diagram/move2.dot | 8 ++++---- doc/diagram/move3.dot | 8 ++++---- doc/tutorial.md | 22 +++++++++++----------- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/doc/diagram/move2.dot b/doc/diagram/move2.dot index 7037ea6..2319871 100644 --- a/doc/diagram/move2.dot +++ b/doc/diagram/move2.dot @@ -18,7 +18,7 @@ digraph { node [shape=Mrecord, style=filled, colorscheme=spectral7] - c1 [label="{contact:array|}", fillcolor=4] + c1 [label="{contacts:array|}", fillcolor=4] c11 [label="{|}"] c12 [label="{|}"] c13 [shape="none", label="...", style="solid"] @@ -41,13 +41,13 @@ digraph { node [shape=Mrecord, style=filled, colorscheme=spectral7] - c2 [label="{contact:array|}", fillcolor=4] + c2 [label="{contacts:array|}", fillcolor=4] c3 [label="{array|}", fillcolor=4] c21 [label="{|}"] c22 [label="{|}"] c23 [shape=none, label="...", style="solid"] o2 [label="{o:object|}", fillcolor=3] - cs [label="{string|\"contact\"}", fillcolor=5] + cs [label="{string|\"contacts\"}", fillcolor=5] c31 [label="{|}"] c32 [label="{|}"] c33 [shape="none", label="...", style="solid"] @@ -59,4 +59,4 @@ digraph { c3 -> { c31; c32; c33 } } ghost -> o2 [style=invis] -} \ No newline at end of file +} diff --git a/doc/diagram/move3.dot b/doc/diagram/move3.dot index c197b99..57adb4f 100644 --- a/doc/diagram/move3.dot +++ b/doc/diagram/move3.dot @@ -19,7 +19,7 @@ digraph { node [shape=Mrecord, style=filled, colorscheme=spectral7] - c1 [label="{contact:array|}", fillcolor=4] + c1 [label="{contacts:array|}", fillcolor=4] c11 [label="{|}"] c12 [label="{|}"] c13 [shape=none, label="...", style="solid"] @@ -42,13 +42,13 @@ digraph { node [shape=Mrecord, style=filled, colorscheme=spectral7] - c2 [label="{contact:null|}", fillcolor=1] + c2 [label="{contacts:null|}", fillcolor=1] c3 [label="{array|}", fillcolor=4] c21 [label="{|}"] c22 [label="{|}"] c23 [shape="none", label="...", style="solid"] o2 [label="{o:object|}", fillcolor=3] - cs [label="{string|\"contact\"}", fillcolor=5] + cs [label="{string|\"contacts\"}", fillcolor=5] c2 -> o2 [style="dashed", constraint=false, label="AddMember", style=invis] edge [arrowhead=vee] @@ -57,4 +57,4 @@ digraph { cs -> c3 [arrowhead=none] } ghost -> o2 [style=invis] -} \ No newline at end of file +} diff --git a/doc/tutorial.md b/doc/tutorial.md index cb76b4b..4a06a83 100644 --- a/doc/tutorial.md +++ b/doc/tutorial.md @@ -2,7 +2,7 @@ This tutorial introduces the basics of the Document Object Model(DOM) API. -As shown in [Usage at a glance](@ref index), a JSON can be parsed into DOM, and then the DOM can be queried and modified easily, and finally be converted back to JSON. +As shown in [Usage at a glance](../readme.md#usage-at-a-glance), a JSON can be parsed into DOM, and then the DOM can be queried and modified easily, and finally be converted back to JSON. [TOC] @@ -55,7 +55,7 @@ printf("hello = %s\n", document["hello"].GetString()); ~~~~~~~~~~ ~~~~~~~~~~ -world +hello = world ~~~~~~~~~~ JSON true/false values are represented as `bool`. @@ -65,7 +65,7 @@ printf("t = %s\n", document["t"].GetBool() ? "true" : "false"); ~~~~~~~~~~ ~~~~~~~~~~ -true +t = true ~~~~~~~~~~ JSON null can be queryed by `IsNull()`. @@ -74,7 +74,7 @@ printf("n = %s\n", document["n"].IsNull() ? "null" : "?"); ~~~~~~~~~~ ~~~~~~~~~~ -null +n = null ~~~~~~~~~~ JSON number type represents all numeric values. However, C++ needs more specific type for manipulation. @@ -526,11 +526,11 @@ Swapping two DOM trees is fast (constant time), despite the complexity of the tr This tutorial shows the basics of DOM tree query and manipulation. There are several important concepts in RapidJSON: -1. [Streams](doc/stream.md) are channels for reading/writing JSON, which can be a in-memory string, or file stream, etc. User can also create their streams. -2. [Encoding](doc/encoding.md) defines which character encoding is used in streams and memory. RapidJSON also provide Unicode conversion/validation internally. -3. [DOM](doc/dom.md)'s basics are already covered in this tutorial. Uncover more advanced features such as *in situ* parsing, other parsing options and advanced usages. -4. [SAX](doc/sax.md) is the foundation of parsing/generating facility in RapidJSON. Learn how to use `Reader`/`Writer` to implement even faster applications. Also try `PrettyWriter` to format the JSON. -5. [Performance](doc/performance.md) shows some in-house and third-party benchmarks. -6. [Internals](doc/internals.md) describes some internal designs and techniques of RapidJSON. +1. [Streams](stream.md) are channels for reading/writing JSON, which can be a in-memory string, or file stream, etc. User can also create their streams. +2. [Encoding](encoding.md) defines which character encoding is used in streams and memory. RapidJSON also provide Unicode conversion/validation internally. +3. [DOM](dom.md)'s basics are already covered in this tutorial. Uncover more advanced features such as *in situ* parsing, other parsing options and advanced usages. +4. [SAX](sax.md) is the foundation of parsing/generating facility in RapidJSON. Learn how to use `Reader`/`Writer` to implement even faster applications. Also try `PrettyWriter` to format the JSON. +5. [Performance](performance.md) shows some in-house and third-party benchmarks. +6. [Internals](internals.md) describes some internal designs and techniques of RapidJSON. -You may also refer to the [FAQ](doc/faq.md), API documentation, examples and unit tests. +You may also refer to the [FAQ](faq.md), API documentation, examples and unit tests. From 3aafe12c91068f5403ec737ea9766eccabf17302 Mon Sep 17 00:00:00 2001 From: Leo Mehr Date: Fri, 30 Jun 2017 12:42:06 -0400 Subject: [PATCH 249/305] undo changes to links and some minor changes to make the readme more easily readable --- doc/tutorial.md | 64 ++++++++++++++++++++++++------------------------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/doc/tutorial.md b/doc/tutorial.md index 4a06a83..167b81d 100644 --- a/doc/tutorial.md +++ b/doc/tutorial.md @@ -2,7 +2,7 @@ This tutorial introduces the basics of the Document Object Model(DOM) API. -As shown in [Usage at a glance](../readme.md#usage-at-a-glance), a JSON can be parsed into DOM, and then the DOM can be queried and modified easily, and finally be converted back to JSON. +As shown in [Usage at a glance](@ref index), JSON can be parsed into a DOM, and then the DOM can be queried and modified easily, and finally be converted back to JSON. [TOC] @@ -14,7 +14,7 @@ Each JSON value is stored in a type called `Value`. A `Document`, representing t In this section, we will use excerpt of `example/tutorial/tutorial.cpp`. -Assumes we have a JSON stored in a C string (`const char* json`): +Assume we have the following JSON stored in a C string (`const char* json`): ~~~~~~~~~~js { "hello": "world", @@ -68,7 +68,7 @@ printf("t = %s\n", document["t"].GetBool() ? "true" : "false"); t = true ~~~~~~~~~~ -JSON null can be queryed by `IsNull()`. +JSON null can be queryed with `IsNull()`. ~~~~~~~~~~cpp printf("n = %s\n", document["n"].IsNull() ? "null" : "?"); ~~~~~~~~~~ @@ -115,15 +115,15 @@ a[3] = 4 Note that, RapidJSON does not automatically convert values between JSON types. If a value is a string, it is invalid to call `GetInt()`, for example. In debug mode it will fail an assertion. In release mode, the behavior is undefined. -In the following, details about querying individual types are discussed. +In the following sections we discuss details about querying individual types. ## Query Array {#QueryArray} -By default, `SizeType` is typedef of `unsigned`. In most systems, array is limited to store up to 2^32-1 elements. +By default, `SizeType` is typedef of `unsigned`. In most systems, an array is limited to store up to 2^32-1 elements. -You may access the elements in array by integer literal, for example, `a[0]`, `a[1]`, `a[2]`. +You may access the elements in an array by integer literal, for example, `a[0]`, `a[1]`, `a[2]`. -Array is similar to `std::vector`, instead of using indices, you may also use iterator to access all the elements. +Array is similar to `std::vector`: instead of using indices, you may also use iterator to access all the elements. ~~~~~~~~~~cpp for (Value::ConstValueIterator itr = a.Begin(); itr != a.End(); ++itr) printf("%d ", itr->GetInt()); @@ -144,7 +144,7 @@ for (auto& v : a.GetArray()) ## Query Object {#QueryObject} -Similar to array, we can access all object members by iterator: +Similar to Array, we can access all object members by iterator: ~~~~~~~~~~cpp static const char* kTypeNames[] = @@ -190,11 +190,11 @@ for (auto& m : document.GetObject()) ## Querying Number {#QueryNumber} -JSON provide a single numerical type called Number. Number can be integer or real numbers. RFC 4627 says the range of Number is specified by parser. +JSON provides a single numerical type called Number. Number can be an integer or a real number. RFC 4627 says the range of Number is specified by the parser implementation. -As C++ provides several integer and floating point number types, the DOM tries to handle these with widest possible range and good performance. +As C++ provides several integer and floating point number types, the DOM tries to handle these with the widest possible range and good performance. -When a Number is parsed, it is stored in the DOM as either one of the following type: +When a Number is parsed, it is stored in the DOM as one of the following types: Type | Description -----------|--------------------------------------- @@ -204,7 +204,7 @@ Type | Description `int64_t` | 64-bit signed integer `double` | 64-bit double precision floating point -When querying a number, you can check whether the number can be obtained as target type: +When querying a number, you can check whether the number can be obtained as the target type: Checking | Obtaining ------------------|--------------------- @@ -215,9 +215,9 @@ Checking | Obtaining `bool IsInt64()` | `int64_t GetInt64()` `bool IsDouble()` | `double GetDouble()` -Note that, an integer value may be obtained in various ways without conversion. For example, A value `x` containing 123 will make `x.IsInt() == x.IsUint() == x.IsInt64() == x.IsUint64() == true`. But a value `y` containing -3000000000 will only makes `x.IsInt64() == true`. +Note that, an integer value may be obtained in various ways without conversion. For example, A value `x` containing 123 will make `x.IsInt() == x.IsUint() == x.IsInt64() == x.IsUint64() == true`. But a value `y` containing -3000000000 will only make `x.IsInt64() == true`. -When obtaining the numeric values, `GetDouble()` will convert internal integer representation to a `double`. Note that, `int` and `unsigned` can be safely convert to `double`, but `int64_t` and `uint64_t` may lose precision (since mantissa of `double` is only 52-bits). +When obtaining the numeric values, `GetDouble()` will convert internal integer representation to a `double`. Note that, `int` and `unsigned` can be safely converted to `double`, but `int64_t` and `uint64_t` may lose precision (since mantissa of `double` is only 52-bits). ## Query String {#QueryString} @@ -225,7 +225,7 @@ In addition to `GetString()`, the `Value` class also contains `GetStringLength() According to RFC 4627, JSON strings can contain Unicode character `U+0000`, which must be escaped as `"\u0000"`. The problem is that, C/C++ often uses null-terminated string, which treats ``\0'` as the terminator symbol. -To conform RFC 4627, RapidJSON supports string containing `U+0000`. If you need to handle this, you can use `GetStringLength()` API to obtain the correct length of string. +To conform RFC 4627, RapidJSON supports string containing `U+0000`. If you need to handle this, you can use `GetStringLength()` to obtain the correct string length. For example, after parsing a the following JSON to `Document d`: @@ -360,14 +360,14 @@ a.PushBack(Value(42).Move(), allocator); // same as above ~~~~~~~~~~ ## Create String {#CreateString} -RapidJSON provide two strategies for storing string. +RapidJSON provides two strategies for storing string. 1. copy-string: allocates a buffer, and then copy the source data into it. 2. const-string: simply store a pointer of string. -Copy-string is always safe because it owns a copy of the data. Const-string can be used for storing string literal, and in-situ parsing which we will mentioned in Document section. +Copy-string is always safe because it owns a copy of the data. Const-string can be used for storing a string literal, and for in-situ parsing which will be mentioned in the DOM section. -To make memory allocation customizable, RapidJSON requires user to pass an instance of allocator, whenever an operation may require allocation. This design is needed to prevent storing a allocator (or Document) pointer per Value. +To make memory allocation customizable, RapidJSON requires users to pass an instance of allocator, whenever an operation may require allocation. This design is needed to prevent storing a allocator (or Document) pointer per Value. Therefore, when we assign a copy-string, we call this overloaded `SetString()` with allocator: @@ -385,7 +385,7 @@ In this example, we get the allocator from a `Document` instance. This is a comm Besides, the above `SetString()` requires length. This can handle null characters within a string. There is another `SetString()` overloaded function without the length parameter. And it assumes the input is null-terminated and calls a `strlen()`-like function to obtain the length. -Finally, for string literal or string with safe life-cycle can use const-string version of `SetString()`, which lacks allocator parameter. For string literals (or constant character arrays), simply passing the literal as parameter is safe and efficient: +Finally, for a string literal or string with a safe life-cycle one can use the const-string version of `SetString()`, which lacks an allocator parameter. For string literals (or constant character arrays), simply passing the literal as parameter is safe and efficient: ~~~~~~~~~~cpp Value s; @@ -393,7 +393,7 @@ s.SetString("rapidjson"); // can contain null character, length derived at co s = "rapidjson"; // shortcut, same as above ~~~~~~~~~~ -For character pointer, the RapidJSON requires to mark it as safe before using it without copying. This can be achieved by using the `StringRef` function: +For a character pointer, RapidJSON requires it to be marked as safe before using it without copying. This can be achieved by using the `StringRef` function: ~~~~~~~~~cpp const char * cstr = getenv("USER"); @@ -408,7 +408,7 @@ s = StringRef(cstr,cstr_len); // shortcut, same as above ~~~~~~~~~ ## Modify Array {#ModifyArray} -Value with array type provides similar APIs as `std::vector`. +Value with array type provides an API similar to `std::vector`. * `Clear()` * `Reserve(SizeType, Allocator&)` @@ -418,7 +418,7 @@ Value with array type provides similar APIs as `std::vector`. * `ValueIterator Erase(ConstValueIterator pos)` * `ValueIterator Erase(ConstValueIterator first, ConstValueIterator last)` -Note that, `Reserve(...)` and `PushBack(...)` may allocate memory for the array elements, therefore require an allocator. +Note that, `Reserve(...)` and `PushBack(...)` may allocate memory for the array elements, therefore requiring an allocator. Here is an example of `PushBack()`: @@ -433,7 +433,7 @@ for (int i = 5; i <= 10; i++) a.PushBack("Lua", allocator).PushBack("Mio", allocator); ~~~~~~~~~~ -Differs from STL, `PushBack()`/`PopBack()` returns the array reference itself. This is called _fluent interface_. +This API differs from STL in that `PushBack()`/`PopBack()` return the array reference itself. This is called _fluent interface_. If you want to add a non-constant string or a string without sufficient lifetime (see [Create String](#CreateString)) to the array, you need to create a string Value by using the copy-string API. To avoid the need for an intermediate variable, you can use a [temporary value](#TemporaryValues) in place: @@ -448,7 +448,7 @@ contact.PushBack(val, document.GetAllocator()); ~~~~~~~~~~ ## Modify Object {#ModifyObject} -Object is a collection of key-value pairs (members). Each key must be a string value. To modify an object, either add or remove members. THe following APIs are for adding members: +The Object class is a collection of key-value pairs (members). Each key must be a string value. To modify an object, either add or remove members. The following API is for adding members: * `Value& AddMember(Value&, Value&, Allocator& allocator)` * `Value& AddMember(StringRefType, Value&, Allocator&)` @@ -462,7 +462,7 @@ contact.AddMember("name", "Milo", document.GetAllocator()); contact.AddMember("married", true, document.GetAllocator()); ~~~~~~~~~~ -The name parameter with `StringRefType` is similar to the interface of `SetString` function for string values. These overloads are used to avoid the need for copying the `name` string, as constant key names are very common in JSON objects. +The name parameter with `StringRefType` is similar to the interface of the `SetString` function for string values. These overloads are used to avoid the need for copying the `name` string, since constant key names are very common in JSON objects. If you need to create a name from a non-constant string or a string without sufficient lifetime (see [Create String](#CreateString)), you need to create a string Value by using the copy-string API. To avoid the need for an intermediate variable, you can use a [temporary value](#TemporaryValues) in place: @@ -526,11 +526,11 @@ Swapping two DOM trees is fast (constant time), despite the complexity of the tr This tutorial shows the basics of DOM tree query and manipulation. There are several important concepts in RapidJSON: -1. [Streams](stream.md) are channels for reading/writing JSON, which can be a in-memory string, or file stream, etc. User can also create their streams. -2. [Encoding](encoding.md) defines which character encoding is used in streams and memory. RapidJSON also provide Unicode conversion/validation internally. -3. [DOM](dom.md)'s basics are already covered in this tutorial. Uncover more advanced features such as *in situ* parsing, other parsing options and advanced usages. -4. [SAX](sax.md) is the foundation of parsing/generating facility in RapidJSON. Learn how to use `Reader`/`Writer` to implement even faster applications. Also try `PrettyWriter` to format the JSON. -5. [Performance](performance.md) shows some in-house and third-party benchmarks. -6. [Internals](internals.md) describes some internal designs and techniques of RapidJSON. +1. [Streams](doc/stream.md) are channels for reading/writing JSON, which can be a in-memory string, or file stream, etc. User can also create their streams. +2. [Encoding](doc/encoding.md) defines which character encoding is used in streams and memory. RapidJSON also provide Unicode conversion/validation internally. +3. [DOM](doc/dom.md)'s basics are already covered in this tutorial. Uncover more advanced features such as *in situ* parsing, other parsing options and advanced usages. +4. [SAX](doc/sax.md) is the foundation of parsing/generating facility in RapidJSON. Learn how to use `Reader`/`Writer` to implement even faster applications. Also try `PrettyWriter` to format the JSON. +5. [Performance](doc/performance.md) shows some in-house and third-party benchmarks. +6. [Internals](doc/internals.md) describes some internal designs and techniques of RapidJSON. -You may also refer to the [FAQ](faq.md), API documentation, examples and unit tests. +You may also refer to the [FAQ](doc/faq.md), API documentation, examples and unit tests. From 14218aeb0a6491130622672495bf5fe7a20ef28a Mon Sep 17 00:00:00 2001 From: "Philipp A. Hartmann" Date: Sun, 9 Jul 2017 11:13:31 +0200 Subject: [PATCH 250/305] ParseResult: improve bool conversion and add operator!= * Use safe-bool idiom for boolean conversion to avoid accidental misuse of ParseResult values (closes #989) * Add operator!= to support more comparison expressions (previously silently/erroneously used operator bool) --- include/rapidjson/error/error.h | 10 ++++++++-- test/unittest/documenttest.cpp | 15 ++++++++++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/include/rapidjson/error/error.h b/include/rapidjson/error/error.h index 95cb31a..9311d2f 100644 --- a/include/rapidjson/error/error.h +++ b/include/rapidjson/error/error.h @@ -104,6 +104,8 @@ enum ParseErrorCode { \see GenericReader::Parse, GenericDocument::Parse */ struct ParseResult { + //!! Unspecified boolean type + typedef bool (ParseResult::*BooleanType)() const; public: //! Default constructor, no error. ParseResult() : code_(kParseErrorNone), offset_(0) {} @@ -115,8 +117,8 @@ public: //! Get the error offset, if \ref IsError(), 0 otherwise. size_t Offset() const { return offset_; } - //! Conversion to \c bool, returns \c true, iff !\ref IsError(). - operator bool() const { return !IsError(); } + //! Explicit conversion to \c bool, returns \c true, iff !\ref IsError(). + operator BooleanType() const { return !IsError() ? &ParseResult::IsError : NULL; } //! Whether the result is an error. bool IsError() const { return code_ != kParseErrorNone; } @@ -124,6 +126,10 @@ public: bool operator==(ParseErrorCode code) const { return code_ == code; } friend bool operator==(ParseErrorCode code, const ParseResult & err) { return code == err.code_; } + bool operator!=(const ParseResult& that) const { return !(*this == that); } + bool operator!=(ParseErrorCode code) const { return !(*this == code); } + friend bool operator!=(ParseErrorCode code, const ParseResult & err) { return err != code; } + //! Reset error code. void Clear() { Set(kParseErrorNone); } //! Update error code and offset. diff --git a/test/unittest/documenttest.cpp b/test/unittest/documenttest.cpp index ecd4b79..0ca5801 100644 --- a/test/unittest/documenttest.cpp +++ b/test/unittest/documenttest.cpp @@ -128,8 +128,14 @@ TEST(Document, UnchangedOnParseError) { Document doc; doc.SetArray().PushBack(0, doc.GetAllocator()); + ParseResult noError; + EXPECT_TRUE(noError); + ParseResult err = doc.Parse("{]"); EXPECT_TRUE(doc.HasParseError()); + EXPECT_NE(err, noError); + EXPECT_NE(err.Code(), noError); + EXPECT_NE(noError, doc.GetParseError()); EXPECT_EQ(err.Code(), doc.GetParseError()); EXPECT_EQ(err.Offset(), doc.GetErrorOffset()); EXPECT_TRUE(doc.IsArray()); @@ -138,6 +144,9 @@ TEST(Document, UnchangedOnParseError) { err = doc.Parse("{}"); EXPECT_FALSE(doc.HasParseError()); EXPECT_FALSE(err.IsError()); + EXPECT_TRUE(err); + EXPECT_EQ(err, noError); + EXPECT_EQ(err.Code(), noError); EXPECT_EQ(err.Code(), doc.GetParseError()); EXPECT_EQ(err.Offset(), doc.GetErrorOffset()); EXPECT_TRUE(doc.IsObject()); @@ -488,15 +497,19 @@ TYPED_TEST(DocumentMove, MoveConstructorParseError) { a.Parse("{ 4 = 4]"); ParseResult error(a.GetParseError(), a.GetErrorOffset()); EXPECT_TRUE(a.HasParseError()); + EXPECT_NE(error, noError); + EXPECT_NE(error.Code(), noError); EXPECT_NE(error.Code(), noError.Code()); EXPECT_NE(error.Offset(), noError.Offset()); D b(std::move(a)); EXPECT_FALSE(a.HasParseError()); EXPECT_TRUE(b.HasParseError()); + EXPECT_EQ(a.GetParseError(), noError); EXPECT_EQ(a.GetParseError(), noError.Code()); - EXPECT_EQ(b.GetParseError(), error.Code()); EXPECT_EQ(a.GetErrorOffset(), noError.Offset()); + EXPECT_EQ(b.GetParseError(), error); + EXPECT_EQ(b.GetParseError(), error.Code()); EXPECT_EQ(b.GetErrorOffset(), error.Offset()); D c(std::move(b)); From eefb618ec947837735d08fe81fb94afdd09948d7 Mon Sep 17 00:00:00 2001 From: "Philipp A. Hartmann" Date: Sun, 9 Jul 2017 11:40:56 +0200 Subject: [PATCH 251/305] Travis: Switch to Ubuntu 14.04 (Trusty) --- .travis.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index f9319f2..38f3a98 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,6 @@ sudo: required -dist: precise +dist: trusty +group: edge language: cpp cache: From f1ba61c7ba5989373880f14f80d2b56f8593eb81 Mon Sep 17 00:00:00 2001 From: "Philipp A. Hartmann" Date: Sun, 9 Jul 2017 14:31:29 +0200 Subject: [PATCH 252/305] unittest.h: change RAPIDJSON_ASSERT to allow usage in expressions --- test/unittest/unittest.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unittest/unittest.h b/test/unittest/unittest.h index 5837345..aa091aa 100644 --- a/test/unittest/unittest.h +++ b/test/unittest/unittest.h @@ -117,7 +117,7 @@ public: #pragma GCC diagnostic pop #endif -#define RAPIDJSON_ASSERT(x) if (!(x)) throw AssertException(RAPIDJSON_STRINGIFY(x)) +#define RAPIDJSON_ASSERT(x) (!(x) ? throw AssertException(RAPIDJSON_STRINGIFY(x)) : (void)0u) class Random { public: From 47c3c1ec9f5c3724c5befb42f8323e760acc1f69 Mon Sep 17 00:00:00 2001 From: "Philipp A. Hartmann" Date: Sun, 9 Jul 2017 14:46:59 +0200 Subject: [PATCH 253/305] Improved handling of NULL strings * Safely assert upon passing NULL string without length (requires usage of RAPIDJSON_ASSERT within an expression) * Allow using a NULL string together with an explicit length 0 (GenericStringRef, GenericValue::SetString, ...), see #817 * Add GenericValue::SetString(StringRefType, Allocator&) overload * Add tests for the various cases --- include/rapidjson/document.h | 26 +++++++++++++++++----- test/unittest/valuetest.cpp | 43 +++++++++++++++++++++++++++++++++--- 2 files changed, 60 insertions(+), 9 deletions(-) diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h index 57f0b3c..06451ad 100644 --- a/include/rapidjson/document.h +++ b/include/rapidjson/document.h @@ -308,7 +308,7 @@ struct GenericStringRef { */ #endif explicit GenericStringRef(const CharType* str) - : s(str), length(internal::StrLen(str)){ RAPIDJSON_ASSERT(s != 0); } + : s(str), length(((RAPIDJSON_ASSERT(str != 0)), internal::StrLen(str))) {} //! Create constant string reference from pointer and length #ifndef __clang__ // -Wdocumentation @@ -320,7 +320,7 @@ struct GenericStringRef { */ #endif GenericStringRef(const CharType* str, SizeType len) - : s(str), length(len) { RAPIDJSON_ASSERT(s != 0); } + : s(RAPIDJSON_LIKELY(str) ? str : emptyString), length(len) { RAPIDJSON_ASSERT(str != 0 || len == 0u); } GenericStringRef(const GenericStringRef& rhs) : s(rhs.s), length(rhs.length) {} @@ -331,6 +331,9 @@ struct GenericStringRef { const SizeType length; //!< length of the string (excluding the trailing NULL terminator) private: + /// Empty string - used when passing in a NULL pointer + static const Ch emptyString[]; + //! Disallow construction from non-const array template GenericStringRef(CharType (&str)[N]) /* = delete */; @@ -338,6 +341,9 @@ private: GenericStringRef& operator=(const GenericStringRef& rhs) /* = delete */; }; +template +const CharType GenericStringRef::emptyString[] = { CharType() }; + //! Mark a character pointer as constant string /*! Mark a plain character pointer as a "string literal". This function can be used to avoid copying a character string to be referenced as a @@ -352,7 +358,7 @@ private: */ template inline GenericStringRef StringRef(const CharType* str) { - return GenericStringRef(str, internal::StrLen(str)); + return GenericStringRef(str); } //! Mark a character pointer as constant string @@ -1762,7 +1768,7 @@ public: \return The value itself for fluent API. \post IsString() == true && GetString() != s && strcmp(GetString(),s) == 0 && GetStringLength() == length */ - GenericValue& SetString(const Ch* s, SizeType length, Allocator& allocator) { this->~GenericValue(); SetStringRaw(StringRef(s, length), allocator); return *this; } + GenericValue& SetString(const Ch* s, SizeType length, Allocator& allocator) { return SetString(StringRef(s, length), allocator); } //! Set this value as a string by copying from source string. /*! \param s source string. @@ -1770,7 +1776,15 @@ public: \return The value itself for fluent API. \post IsString() == true && GetString() != s && strcmp(GetString(),s) == 0 && GetStringLength() == length */ - GenericValue& SetString(const Ch* s, Allocator& allocator) { return SetString(s, internal::StrLen(s), allocator); } + GenericValue& SetString(const Ch* s, Allocator& allocator) { return SetString(StringRef(s), allocator); } + + //! Set this value as a string by copying from source string. + /*! \param s source string reference + \param allocator Allocator for allocating copied buffer. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \post IsString() == true && GetString() != s.s && strcmp(GetString(),s) == 0 && GetStringLength() == length + */ + GenericValue& SetString(StringRefType s, Allocator& allocator) { this->~GenericValue(); SetStringRaw(s, allocator); return *this; } #if RAPIDJSON_HAS_STDSTRING //! Set this value as a string by copying from source string. @@ -1780,7 +1794,7 @@ public: \post IsString() == true && GetString() != s.data() && strcmp(GetString(),s.data() == 0 && GetStringLength() == s.size() \note Requires the definition of the preprocessor symbol \ref RAPIDJSON_HAS_STDSTRING. */ - GenericValue& SetString(const std::basic_string& s, Allocator& allocator) { return SetString(s.data(), SizeType(s.size()), allocator); } + GenericValue& SetString(const std::basic_string& s, Allocator& allocator) { return SetString(StringRef(s), allocator); } #endif //@} diff --git a/test/unittest/valuetest.cpp b/test/unittest/valuetest.cpp index fefc001..307e1b0 100644 --- a/test/unittest/valuetest.cpp +++ b/test/unittest/valuetest.cpp @@ -857,9 +857,46 @@ TEST(Value, String) { } // Issue 226: Value of string type should not point to NULL -TEST(Value, SetStringNullException) { - Value v; - EXPECT_THROW(v.SetString(0, 0), AssertException); +TEST(Value, SetStringNull) { + + MemoryPoolAllocator<> allocator; + const char* nullPtr = 0; + { + // Construction with string type creates empty string + Value v(kStringType); + EXPECT_NE(v.GetString(), nullPtr); // non-null string returned + EXPECT_EQ(v.GetStringLength(), 0u); + + // Construction from/setting to null without length not allowed + EXPECT_THROW(Value(StringRef(nullPtr)), AssertException); + EXPECT_THROW(Value(StringRef(nullPtr), allocator), AssertException); + EXPECT_THROW(v.SetString(nullPtr, allocator), AssertException); + + // Non-empty length with null string is not allowed + EXPECT_THROW(v.SetString(nullPtr, 17u), AssertException); + EXPECT_THROW(v.SetString(nullPtr, 42u, allocator), AssertException); + + // Setting to null string with empty length is allowed + v.SetString(nullPtr, 0u); + EXPECT_NE(v.GetString(), nullPtr); // non-null string returned + EXPECT_EQ(v.GetStringLength(), 0u); + + v.SetNull(); + v.SetString(nullPtr, 0u, allocator); + EXPECT_NE(v.GetString(), nullPtr); // non-null string returned + EXPECT_EQ(v.GetStringLength(), 0u); + } + // Construction with null string and empty length is allowed + { + Value v(nullPtr,0u); + EXPECT_NE(v.GetString(), nullPtr); // non-null string returned + EXPECT_EQ(v.GetStringLength(), 0u); + } + { + Value v(nullPtr, 0u, allocator); + EXPECT_NE(v.GetString(), nullPtr); // non-null string returned + EXPECT_EQ(v.GetStringLength(), 0u); + } } template From 7161894f4069879642aef9eae8b6e63fbd36cfd8 Mon Sep 17 00:00:00 2001 From: "Philipp A. Hartmann" Date: Mon, 10 Jul 2017 21:32:16 +0200 Subject: [PATCH 254/305] travis-doxygen.sh: upgrade to Doxygen 1.8.13 * Upgrade to the latest Doxygen version 1.8.13 * Drop unused variable DOXYGEN_BIN * Reenable --single-branch (travis-ci/travis-ci#5225 is closed) --- travis-doxygen.sh | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/travis-doxygen.sh b/travis-doxygen.sh index 31a50cf..e5c0320 100755 --- a/travis-doxygen.sh +++ b/travis-doxygen.sh @@ -4,10 +4,9 @@ set -e -DOXYGEN_VER=doxygen-1.8.7 +DOXYGEN_VER=doxygen-1.8.13 DOXYGEN_TAR=${DOXYGEN_VER}.linux.bin.tar.gz DOXYGEN_URL="http://ftp.stack.nl/pub/users/dimitri/${DOXYGEN_TAR}" -DOXYGEN_BIN="/usr/local/bin/doxygen" : ${GITHUB_REPO:="miloyip/rapidjson"} GITHUB_HOST="github.com" @@ -66,7 +65,7 @@ gh_pages_prepare() [ ! -d "html" ] || \ abort "Doxygen target directory already exists." git --version - git clone -b gh-pages "${GITHUB_CLONE}" html + git clone --single-branch -b gh-pages "${GITHUB_CLONE}" html cd html # setup git config (with defaults) git config user.name "${GIT_NAME-travis}" From 70171f97903752cdf4c910b94b5ee0e06570bb41 Mon Sep 17 00:00:00 2001 From: "Philipp A. Hartmann" Date: Mon, 10 Jul 2017 22:32:18 +0200 Subject: [PATCH 255/305] GenericStringRef: move assert out of expression --- include/rapidjson/document.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h index 06451ad..3169bd4 100644 --- a/include/rapidjson/document.h +++ b/include/rapidjson/document.h @@ -308,7 +308,7 @@ struct GenericStringRef { */ #endif explicit GenericStringRef(const CharType* str) - : s(str), length(((RAPIDJSON_ASSERT(str != 0)), internal::StrLen(str))) {} + : s(str), length(NotNullStrLen(str)) {} //! Create constant string reference from pointer and length #ifndef __clang__ // -Wdocumentation @@ -331,6 +331,11 @@ struct GenericStringRef { const SizeType length; //!< length of the string (excluding the trailing NULL terminator) private: + SizeType NotNullStrLen(const CharType* str) { + RAPIDJSON_ASSERT(str != 0); + return internal::StrLen(str); + } + /// Empty string - used when passing in a NULL pointer static const Ch emptyString[]; From fcd2e1f60cecc00d414821987b2d3d02dbd593df Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Thu, 13 Jul 2017 16:07:36 +0800 Subject: [PATCH 256/305] Fix #1017 allOf keyword fail with Writer handler Gave up using static binding for null handler, because it cannot be used with arbitrary handler type. Change `OutputHandler handler_` to pointer type. --- include/rapidjson/schema.h | 26 ++++++++------------------ test/unittest/schematest.cpp | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/include/rapidjson/schema.h b/include/rapidjson/schema.h index dd57edb..abcf1a1 100644 --- a/include/rapidjson/schema.h +++ b/include/rapidjson/schema.h @@ -1594,7 +1594,7 @@ public: ownStateAllocator_(0), schemaStack_(allocator, schemaStackCapacity), documentStack_(allocator, documentStackCapacity), - outputHandler_(CreateNullHandler()), + outputHandler_(0), valid_(true) #if RAPIDJSON_SCHEMA_VERBOSE , depth_(0) @@ -1622,8 +1622,7 @@ public: ownStateAllocator_(0), schemaStack_(allocator, schemaStackCapacity), documentStack_(allocator, documentStackCapacity), - outputHandler_(outputHandler), - nullHandler_(0), + outputHandler_(&outputHandler), valid_(true) #if RAPIDJSON_SCHEMA_VERBOSE , depth_(0) @@ -1634,10 +1633,6 @@ public: //! Destructor. ~GenericSchemaValidator() { Reset(); - if (nullHandler_) { - nullHandler_->~OutputHandler(); - StateAllocator::Free(nullHandler_); - } RAPIDJSON_DELETE(ownStateAllocator_); } @@ -1699,7 +1694,7 @@ RAPIDJSON_MULTILINEMACRO_END } #define RAPIDJSON_SCHEMA_HANDLE_END_(method, arg2)\ - return valid_ = EndValue() && outputHandler_.method arg2 + return valid_ = EndValue() && (!outputHandler_ || outputHandler_->method arg2) #define RAPIDJSON_SCHEMA_HANDLE_VALUE_(method, arg1, arg2) \ RAPIDJSON_SCHEMA_HANDLE_BEGIN_ (method, arg1);\ @@ -1721,7 +1716,7 @@ RAPIDJSON_MULTILINEMACRO_END bool StartObject() { RAPIDJSON_SCHEMA_HANDLE_BEGIN_(StartObject, (CurrentContext())); RAPIDJSON_SCHEMA_HANDLE_PARALLEL_(StartObject, ()); - return valid_ = outputHandler_.StartObject(); + return valid_ = !outputHandler_ || outputHandler_->StartObject(); } bool Key(const Ch* str, SizeType len, bool copy) { @@ -1729,7 +1724,7 @@ RAPIDJSON_MULTILINEMACRO_END AppendToken(str, len); if (!CurrentSchema().Key(CurrentContext(), str, len, copy)) return valid_ = false; RAPIDJSON_SCHEMA_HANDLE_PARALLEL_(Key, (str, len, copy)); - return valid_ = outputHandler_.Key(str, len, copy); + return valid_ = !outputHandler_ || outputHandler_->Key(str, len, copy); } bool EndObject(SizeType memberCount) { @@ -1742,7 +1737,7 @@ RAPIDJSON_MULTILINEMACRO_END bool StartArray() { RAPIDJSON_SCHEMA_HANDLE_BEGIN_(StartArray, (CurrentContext())); RAPIDJSON_SCHEMA_HANDLE_PARALLEL_(StartArray, ()); - return valid_ = outputHandler_.StartArray(); + return valid_ = !outputHandler_ || outputHandler_->StartArray(); } bool EndArray(SizeType elementCount) { @@ -1815,7 +1810,7 @@ private: ownStateAllocator_(0), schemaStack_(allocator, schemaStackCapacity), documentStack_(allocator, documentStackCapacity), - outputHandler_(CreateNullHandler()), + outputHandler_(0), valid_(true) #if RAPIDJSON_SCHEMA_VERBOSE , depth_(depth) @@ -1929,10 +1924,6 @@ private: Context& CurrentContext() { return *schemaStack_.template Top(); } const Context& CurrentContext() const { return *schemaStack_.template Top(); } - OutputHandler& CreateNullHandler() { - return *(nullHandler_ = new (GetStateAllocator().Malloc(sizeof(OutputHandler))) OutputHandler); - } - static const size_t kDefaultSchemaStackCapacity = 1024; static const size_t kDefaultDocumentStackCapacity = 256; const SchemaDocumentType* schemaDocument_; @@ -1941,8 +1932,7 @@ private: StateAllocator* ownStateAllocator_; internal::Stack schemaStack_; //!< stack to store the current path of schema (BaseSchemaType *) internal::Stack documentStack_; //!< stack to store the current path of validating document (Ch) - OutputHandler& outputHandler_; - OutputHandler* nullHandler_; + OutputHandler* outputHandler_; bool valid_; #if RAPIDJSON_SCHEMA_VERBOSE unsigned depth_; diff --git a/test/unittest/schematest.cpp b/test/unittest/schematest.cpp index e79fec2..9b99ba8 100644 --- a/test/unittest/schematest.cpp +++ b/test/unittest/schematest.cpp @@ -1329,6 +1329,25 @@ TEST(SchemaValidator, Issue825) { VALIDATE(s, "{ \"item\": \"hello\" }", true); } +TEST(SchemaValidator, Issue1017_allOfHandler) { + Document sd; + sd.Parse("{\"allOf\": [{\"type\": \"object\",\"properties\": {\"cyanArray2\": {\"type\": \"array\",\"items\": { \"type\": \"string\" }}}},{\"type\": \"object\",\"properties\": {\"blackArray\": {\"type\": \"array\",\"items\": { \"type\": \"string\" }}},\"required\": [ \"blackArray\" ]}]}"); + SchemaDocument s(sd); + StringBuffer sb; + Writer writer(sb); + GenericSchemaValidator > validator(s, writer); + EXPECT_TRUE(validator.StartObject()); + EXPECT_TRUE(validator.Key("cyanArray2", 10, false)); + EXPECT_TRUE(validator.StartArray()); + EXPECT_TRUE(validator.EndArray(0)); + EXPECT_TRUE(validator.Key("blackArray", 10, false)); + EXPECT_TRUE(validator.StartArray()); + EXPECT_TRUE(validator.EndArray(0)); + EXPECT_TRUE(validator.EndObject(0)); + EXPECT_TRUE(validator.IsValid()); + EXPECT_STREQ("{\"cyanArray2\":[],\"blackArray\":[]}", sb.GetString()); +} + #ifdef __clang__ RAPIDJSON_DIAG_POP #endif From 707fd36afa54f44d4635fde0107eac1c2f4e8649 Mon Sep 17 00:00:00 2001 From: Bart Muzzin Date: Tue, 25 Jul 2017 22:13:26 -0400 Subject: [PATCH 257/305] Issue #1028: Visual Studio natvis file. --- contrib/natvis/LICENSE | 45 +++++++++++++++++++++++++++++++++ contrib/natvis/README.md | 7 +++++ contrib/natvis/rapidjson.natvis | 38 ++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 contrib/natvis/LICENSE create mode 100644 contrib/natvis/README.md create mode 100644 contrib/natvis/rapidjson.natvis diff --git a/contrib/natvis/LICENSE b/contrib/natvis/LICENSE new file mode 100644 index 0000000..f57da96 --- /dev/null +++ b/contrib/natvis/LICENSE @@ -0,0 +1,45 @@ +The MIT License (MIT) + +Copyright (c) 2017 Bart Muzzin + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +Derived from: + +The MIT License (MIT) + +Copyright (c) 2015 mojmir svoboda + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/contrib/natvis/README.md b/contrib/natvis/README.md new file mode 100644 index 0000000..9685c7f --- /dev/null +++ b/contrib/natvis/README.md @@ -0,0 +1,7 @@ +# rapidjson.natvis + +This file can be used as a [Visual Studio Visualizer](https://docs.microsoft.com/en-gb/visualstudio/debugger/create-custom-views-of-native-objects) to aid in visualizing rapidjson structures within the Visual Studio debugger. Natvis visualizers are supported in Visual Studio 2012 and later. To install, copy the file into this directory: + +`%USERPROFILE%\Documents\Visual Studio 2012\Visualizers` + +Each version of Visual Studio has a similar directory, it must be copied into each directory to be used with that particular version. In Visual Studio 2015 and later, this can be done without restarting Visual Studio (a new debugging session must be started). diff --git a/contrib/natvis/rapidjson.natvis b/contrib/natvis/rapidjson.natvis new file mode 100644 index 0000000..a804b7b --- /dev/null +++ b/contrib/natvis/rapidjson.natvis @@ -0,0 +1,38 @@ + + + + + null + true + false + {data_.ss.str} + {(const char*)((size_t)data_.s.str & 0x0000FFFFFFFFFFFF)} + {data_.n.i.i} + {data_.n.u.u} + {data_.n.i64} + {data_.n.u64} + {data_.n.d} + Object members={data_.o.size} + Array members={data_.a.size} + + data_.o.size + data_.o.capacity + + data_.o.size + + (rapidjson::GenericMember<$T1,$T2>*)(((size_t)data_.o.members) & 0x0000FFFFFFFFFFFF) + + + data_.a.size + data_.a.capacity + + data_.a.size + + (rapidjson::GenericValue<$T1,$T2>*)(((size_t)data_.a.elements) & 0x0000FFFFFFFFFFFF) + + + + + + + From 7c1f20825374390e2a1005e0bc488a3b99c873d0 Mon Sep 17 00:00:00 2001 From: bluehero Date: Sat, 5 Aug 2017 16:53:45 +0800 Subject: [PATCH 258/305] modify --- include/rapidjson/document.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h index 3169bd4..f5c02d6 100644 --- a/include/rapidjson/document.h +++ b/include/rapidjson/document.h @@ -2168,6 +2168,10 @@ public: } #endif + // Allow assignment from ValueType. + // Refer to Effective C++ 3rd Edition/Item 33: Avoid hiding inherited names. + using ValueType::operator=; + //! Exchange the contents of this document with those of another. /*! \param rhs Another document. @@ -2183,6 +2187,10 @@ public: return *this; } + // Allow Swap from ValueType. + // Refer to Effective C++ 3rd Edition/Item 33: Avoid hiding inherited names. + using ValueType::Swap; + //! free-standing swap function helper /*! Helper function to enable support for common swap implementation pattern based on \c std::swap: From 9eb7bf895c124fcf76877b173f6930a40e71e0a8 Mon Sep 17 00:00:00 2001 From: bluehero Date: Sat, 5 Aug 2017 18:12:44 +0800 Subject: [PATCH 259/305] add unittest --- test/unittest/documenttest.cpp | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/test/unittest/documenttest.cpp b/test/unittest/documenttest.cpp index 0ca5801..55d828a 100644 --- a/test/unittest/documenttest.cpp +++ b/test/unittest/documenttest.cpp @@ -290,6 +290,14 @@ TEST(Document, ParseStream_AutoUTFInputStream) { EXPECT_EQ(0, memcmp(bos.GetString(), bos2.GetString(), bos2.GetSize())); } +TEST(Document, Assignment) { + Value x(1234); + Document d; + d = x; + EXPECT_TRUE(x.IsNull()); // move semantic + EXPECT_EQ(1234, d.GetInt()); +} + TEST(Document, Swap) { Document d1; Document::AllocatorType& a = d1.GetAllocator(); @@ -300,7 +308,14 @@ TEST(Document, Swap) { o.SetObject().AddMember("a", 1, a); // Swap between Document and Value - // d1.Swap(o); // doesn't compile + d1.Swap(o); + EXPECT_TRUE(d1.IsObject()); + EXPECT_TRUE(o.IsArray()); + + d1.Swap(o); + EXPECT_TRUE(d1.IsArray()); + EXPECT_TRUE(o.IsObject()); + o.Swap(d1); EXPECT_TRUE(d1.IsObject()); EXPECT_TRUE(o.IsArray()); From 8ba1f84f47a3c5761be86884f77421a73c9a38fe Mon Sep 17 00:00:00 2001 From: bluehero Date: Sat, 5 Aug 2017 20:39:31 +0800 Subject: [PATCH 260/305] modify unittest --- test/unittest/documenttest.cpp | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/test/unittest/documenttest.cpp b/test/unittest/documenttest.cpp index 55d828a..9ff8096 100644 --- a/test/unittest/documenttest.cpp +++ b/test/unittest/documenttest.cpp @@ -290,14 +290,6 @@ TEST(Document, ParseStream_AutoUTFInputStream) { EXPECT_EQ(0, memcmp(bos.GetString(), bos2.GetString(), bos2.GetSize())); } -TEST(Document, Assignment) { - Value x(1234); - Document d; - d = x; - EXPECT_TRUE(x.IsNull()); // move semantic - EXPECT_EQ(1234, d.GetInt()); -} - TEST(Document, Swap) { Document d1; Document::AllocatorType& a = d1.GetAllocator(); @@ -667,13 +659,20 @@ TYPED_TEST(DocumentMove, MoveAssignmentStack) { #endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS -// Issue 22: Memory corruption via operator= +// Issue 22: Memory corruption via operator= from Document // Fixed by making unimplemented assignment operator private. -//TEST(Document, Assignment) { +// Prohibit assignment from Document, But allow assignment from Value +TEST(Document, Assignment) { // Document d1; // Document d2; // d1 = d2; -//} + + Value x(1234); + Document d; + d = x; + EXPECT_TRUE(x.IsNull()); // move semantic + EXPECT_EQ(1234, d.GetInt()); +} #ifdef __clang__ RAPIDJSON_DIAG_POP From 5fb06596a93f62e98fb9900dac2f1c97e5981549 Mon Sep 17 00:00:00 2001 From: bluehero Date: Mon, 7 Aug 2017 11:44:27 +0800 Subject: [PATCH 261/305] modify --- include/rapidjson/document.h | 4 +--- test/unittest/documenttest.cpp | 12 ++++++++++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h index f5c02d6..869667a 100644 --- a/include/rapidjson/document.h +++ b/include/rapidjson/document.h @@ -2168,8 +2168,7 @@ public: } #endif - // Allow assignment from ValueType. - // Refer to Effective C++ 3rd Edition/Item 33: Avoid hiding inherited names. + // Allow assignment like a ValueType. using ValueType::operator=; //! Exchange the contents of this document with those of another. @@ -2188,7 +2187,6 @@ public: } // Allow Swap from ValueType. - // Refer to Effective C++ 3rd Edition/Item 33: Avoid hiding inherited names. using ValueType::Swap; //! free-standing swap function helper diff --git a/test/unittest/documenttest.cpp b/test/unittest/documenttest.cpp index 9ff8096..0d08b25 100644 --- a/test/unittest/documenttest.cpp +++ b/test/unittest/documenttest.cpp @@ -661,17 +661,25 @@ TYPED_TEST(DocumentMove, MoveAssignmentStack) { // Issue 22: Memory corruption via operator= from Document // Fixed by making unimplemented assignment operator private. -// Prohibit assignment from Document, But allow assignment from Value +// Prohibit assignment from Document. +// But allow assignment from ValueType/int/double/..., like a ValueType TEST(Document, Assignment) { // Document d1; // Document d2; // d1 = d2; - Value x(1234); Document d; + + Value x(1234); d = x; EXPECT_TRUE(x.IsNull()); // move semantic EXPECT_EQ(1234, d.GetInt()); + + d = 1; + EXPECT_EQ(1, d.GetInt()); + + d = 12.34; + EXPECT_NEAR(12.34, d.GetDouble(), 0.0); } #ifdef __clang__ From c831675026cc2c0a7b3581d8b0e0fe4eedd8d78f Mon Sep 17 00:00:00 2001 From: bluehero Date: Mon, 7 Aug 2017 11:58:37 +0800 Subject: [PATCH 262/305] modify --- include/rapidjson/document.h | 6 ++---- test/unittest/documenttest.cpp | 21 +++------------------ 2 files changed, 5 insertions(+), 22 deletions(-) diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h index 869667a..f55b7d3 100644 --- a/include/rapidjson/document.h +++ b/include/rapidjson/document.h @@ -2168,9 +2168,6 @@ public: } #endif - // Allow assignment like a ValueType. - using ValueType::operator=; - //! Exchange the contents of this document with those of another. /*! \param rhs Another document. @@ -2186,7 +2183,8 @@ public: return *this; } - // Allow Swap from ValueType. + // Allow Swap with ValueType. + // Refer to Effective C++/Item 33: Avoid hiding inherited names. using ValueType::Swap; //! free-standing swap function helper diff --git a/test/unittest/documenttest.cpp b/test/unittest/documenttest.cpp index 0d08b25..5429802 100644 --- a/test/unittest/documenttest.cpp +++ b/test/unittest/documenttest.cpp @@ -659,28 +659,13 @@ TYPED_TEST(DocumentMove, MoveAssignmentStack) { #endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS -// Issue 22: Memory corruption via operator= from Document +// Issue 22: Memory corruption via operator= // Fixed by making unimplemented assignment operator private. -// Prohibit assignment from Document. -// But allow assignment from ValueType/int/double/..., like a ValueType -TEST(Document, Assignment) { +//TEST(Document, Assignment) { // Document d1; // Document d2; // d1 = d2; - - Document d; - - Value x(1234); - d = x; - EXPECT_TRUE(x.IsNull()); // move semantic - EXPECT_EQ(1234, d.GetInt()); - - d = 1; - EXPECT_EQ(1, d.GetInt()); - - d = 12.34; - EXPECT_NEAR(12.34, d.GetDouble(), 0.0); -} +//} #ifdef __clang__ RAPIDJSON_DIAG_POP From f9004b90c555f9374d3f6e4d462e4abbce3b00a8 Mon Sep 17 00:00:00 2001 From: bluehero Date: Mon, 7 Aug 2017 13:09:22 +0800 Subject: [PATCH 263/305] modify --- include/rapidjson/document.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h index f55b7d3..3133a2f 100644 --- a/include/rapidjson/document.h +++ b/include/rapidjson/document.h @@ -2184,7 +2184,7 @@ public: } // Allow Swap with ValueType. - // Refer to Effective C++/Item 33: Avoid hiding inherited names. + // Refer to Effective C++ 3rd Edition/Item 33: Avoid hiding inherited names. using ValueType::Swap; //! free-standing swap function helper From f91405801f88533c609d95f2fcc2d88811544d35 Mon Sep 17 00:00:00 2001 From: Minmin Gong Date: Thu, 31 Aug 2017 23:16:30 -0700 Subject: [PATCH 264/305] Specifies the endian of msvc ARM64 configuration. --- include/rapidjson/rapidjson.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/rapidjson/rapidjson.h b/include/rapidjson/rapidjson.h index 57ab851..5716fdc 100644 --- a/include/rapidjson/rapidjson.h +++ b/include/rapidjson/rapidjson.h @@ -241,7 +241,7 @@ # define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN # elif defined(__i386__) || defined(__alpha__) || defined(__ia64) || defined(__ia64__) || defined(_M_IX86) || defined(_M_IA64) || defined(_M_ALPHA) || defined(__amd64) || defined(__amd64__) || defined(_M_AMD64) || defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || defined(__bfin__) # define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN -# elif defined(_MSC_VER) && defined(_M_ARM) +# elif defined(_MSC_VER) && (defined(_M_ARM) || defined(_M_ARM64)) # define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN # elif defined(RAPIDJSON_DOXYGEN_RUNNING) # define RAPIDJSON_ENDIAN From 9ce6a7ebb8a467b7e796b010d5acb61da7679ff1 Mon Sep 17 00:00:00 2001 From: Crunkle Date: Sat, 2 Sep 2017 21:03:03 +0100 Subject: [PATCH 265/305] Fix processor check when empty --- CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8b90c87..3ccc374 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -50,7 +50,7 @@ if(CCACHE_FOUND) endif(CCACHE_FOUND) if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") - if(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "powerpc" OR ${CMAKE_SYSTEM_PROCESSOR} STREQUAL "ppc64" OR ${CMAKE_SYSTEM_PROCESSOR} STREQUAL "ppc64le") + if("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "powerpc" OR "${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "ppc64" OR "${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "ppc64le") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mcpu=native") else() #FIXME: x86 is -march=native, but doesn't mean every arch is this option. To keep original project's compatibility, I leave this except POWER. @@ -80,7 +80,7 @@ if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") endif() endif() elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang") - if(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "powerpc" OR ${CMAKE_SYSTEM_PROCESSOR} STREQUAL "ppc64" OR ${CMAKE_SYSTEM_PROCESSOR} STREQUAL "ppc64le") + if("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "powerpc" OR "${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "ppc64" OR "${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "ppc64le") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mcpu=native") else() #FIXME: x86 is -march=native, but doesn't mean every arch is this option. To keep original project's compatibility, I leave this except POWER. From bbdf5d1d4b40891c82e5c1946d32dfc841926066 Mon Sep 17 00:00:00 2001 From: Christopher Warrington Date: Tue, 5 Sep 2017 16:58:09 -0700 Subject: [PATCH 266/305] Fix Windows doc build MSBuild error MSB6001 When using a MSBuild-based CMake generator like 'Visual Studio 15 2017 Win64', the doc build was failing with the error 'MSB6001: Invalid command line switch for "cmd.exe". Illegal characters in path.' This was due to the dependency on Doxyfile*, which wasn't expanded by CMake. The fix is to expand this glob in CMake before specifying the custom command's dependencies. Partial fix for https://github.com/miloyip/rapidjson/issues/622 --- doc/CMakeLists.txt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/doc/CMakeLists.txt b/doc/CMakeLists.txt index c1f165a..c5345ba 100644 --- a/doc/CMakeLists.txt +++ b/doc/CMakeLists.txt @@ -10,11 +10,13 @@ ELSE() CONFIGURE_FILE(Doxyfile.in Doxyfile @ONLY) CONFIGURE_FILE(Doxyfile.zh-cn.in Doxyfile.zh-cn @ONLY) + file(GLOB DOXYFILES ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile*) + add_custom_command(OUTPUT html COMMAND ${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile COMMAND ${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile.zh-cn COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_CURRENT_BINARY_DIR}/html - DEPENDS ${MARKDOWN_DOC} ${SOURCES} ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile* + DEPENDS ${MARKDOWN_DOC} ${SOURCES} ${DOXYFILES} WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/../ ) From 6e38649ec61e5f4f382c257a6b27698bb55eff61 Mon Sep 17 00:00:00 2001 From: Christopher Warrington Date: Tue, 5 Sep 2017 18:23:28 -0700 Subject: [PATCH 267/305] Guard against min/max being macros in document.h Sometimes, particularly when Microsoft's windows.h is included, min/max are defined as macros, interfering with use of std::numeric_limits::min() and the like. To guard against this, the function name is wrapped in an extra set of parenthesis, which inhibits function-style macro expansion. --- include/rapidjson/document.h | 24 +++++------------------- 1 file changed, 5 insertions(+), 19 deletions(-) diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h index 3133a2f..191582e 100644 --- a/include/rapidjson/document.h +++ b/include/rapidjson/document.h @@ -29,14 +29,6 @@ RAPIDJSON_DIAG_PUSH #ifdef _MSC_VER RAPIDJSON_DIAG_OFF(4127) // conditional expression is constant RAPIDJSON_DIAG_OFF(4244) // conversion from kXxxFlags to 'uint16_t', possible loss of data -#ifdef _MINWINDEF_ // see: http://stackoverflow.com/questions/22744262/cant-call-stdmax-because-minwindef-h-defines-max -#ifndef NOMINMAX -#pragma push_macro("min") -#pragma push_macro("max") -#undef min -#undef max -#endif -#endif #endif #ifdef __clang__ @@ -1018,14 +1010,14 @@ public: uint64_t u = GetUint64(); volatile double d = static_cast(u); return (d >= 0.0) - && (d < static_cast(std::numeric_limits::max())) + && (d < static_cast((std::numeric_limits::max)())) && (u == static_cast(d)); } if (IsInt64()) { int64_t i = GetInt64(); volatile double d = static_cast(i); - return (d >= static_cast(std::numeric_limits::min())) - && (d < static_cast(std::numeric_limits::max())) + return (d >= static_cast((std::numeric_limits::min)())) + && (d < static_cast((std::numeric_limits::max)())) && (i == static_cast(d)); } return true; // double, int, uint are always lossless @@ -1042,8 +1034,8 @@ public: bool IsLosslessFloat() const { if (!IsNumber()) return false; double a = GetDouble(); - if (a < static_cast(-std::numeric_limits::max()) - || a > static_cast(std::numeric_limits::max())) + if (a < static_cast(-(std::numeric_limits::max)()) + || a > static_cast((std::numeric_limits::max)())) return false; double b = static_cast(static_cast(a)); return a >= b && a <= b; // Prevent -Wfloat-equal @@ -2616,12 +2608,6 @@ private: }; RAPIDJSON_NAMESPACE_END -#ifdef _MINWINDEF_ // see: http://stackoverflow.com/questions/22744262/cant-call-stdmax-because-minwindef-h-defines-max -#ifndef NOMINMAX -#pragma pop_macro("min") -#pragma pop_macro("max") -#endif -#endif RAPIDJSON_DIAG_POP #endif // RAPIDJSON_DOCUMENT_H_ From e4c0ecf86b7db94014cde331cd43b6443f993be7 Mon Sep 17 00:00:00 2001 From: Christopher Warrington Date: Tue, 5 Sep 2017 18:27:02 -0700 Subject: [PATCH 268/305] Guard against min/max macros in tests too --- test/unittest/itoatest.cpp | 4 ++-- test/unittest/readertest.cpp | 2 +- test/unittest/strtodtest.cpp | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/test/unittest/itoatest.cpp b/test/unittest/itoatest.cpp index 2f66bed..f41edeb 100644 --- a/test/unittest/itoatest.cpp +++ b/test/unittest/itoatest.cpp @@ -70,8 +70,8 @@ template static void Verify(void(*f)(T, char*), char* (*g)(T, char*)) { // Boundary cases VerifyValue(0, f, g); - VerifyValue(std::numeric_limits::min(), f, g); - VerifyValue(std::numeric_limits::max(), f, g); + VerifyValue((std::numeric_limits::min)(), f, g); + VerifyValue((std::numeric_limits::max)(), f, g); // 2^n - 1, 2^n, 10^n - 1, 10^n until overflow for (int power = 2; power <= 10; power += 8) { diff --git a/test/unittest/readertest.cpp b/test/unittest/readertest.cpp index 5078f52..dad33d6 100644 --- a/test/unittest/readertest.cpp +++ b/test/unittest/readertest.cpp @@ -415,7 +415,7 @@ TEST(Reader, ParseNumber_NormalPrecisionError) { uint64_t bias1 = e.ToBias(); uint64_t bias2 = a.ToBias(); double ulp = static_cast(bias1 >= bias2 ? bias1 - bias2 : bias2 - bias1); - ulpMax = std::max(ulpMax, ulp); + ulpMax = (std::max)(ulpMax, ulp); ulpSum += ulp; } printf("ULP Average = %g, Max = %g \n", ulpSum / count, ulpMax); diff --git a/test/unittest/strtodtest.cpp b/test/unittest/strtodtest.cpp index cde836c..807f887 100644 --- a/test/unittest/strtodtest.cpp +++ b/test/unittest/strtodtest.cpp @@ -91,7 +91,7 @@ TEST(Strtod, CheckApproximationCase) { } // Remove common power of two factor from all three scaled values - int common_Exp2 = std::min(dS_Exp2, std::min(bS_Exp2, hS_Exp2)); + int common_Exp2 = (std::min)(dS_Exp2, (std::min)(bS_Exp2, hS_Exp2)); dS_Exp2 -= common_Exp2; bS_Exp2 -= common_Exp2; hS_Exp2 -= common_Exp2; From a683902b2c15e32bfac04afc1d5248466c755c9e Mon Sep 17 00:00:00 2001 From: Christopher Warrington Date: Tue, 5 Sep 2017 16:03:54 -0700 Subject: [PATCH 269/305] Assert Type enum lower bound as well --- include/rapidjson/document.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h index 191582e..93b091f 100644 --- a/include/rapidjson/document.h +++ b/include/rapidjson/document.h @@ -605,7 +605,7 @@ public: kNullFlag, kFalseFlag, kTrueFlag, kObjectFlag, kArrayFlag, kShortStringFlag, kNumberAnyFlag }; - RAPIDJSON_ASSERT(type <= kNumberType); + RAPIDJSON_ASSERT(type >= kNullType && type <= kNumberType); data_.f.flags = defaultFlags[type]; // Use ShortString to store empty string. From 4a2f2729f1313a7150d1275185f4888224a48753 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Wed, 13 Sep 2017 17:03:23 +0800 Subject: [PATCH 270/305] Change from miloyip/rapidjson to Tencent/rapidjson --- .travis.yml | 2 +- CHANGELOG.md | 12 +++++----- RapidJSON.pc.in | 2 +- doc/faq.md | 6 ++--- doc/faq.zh-cn.md | 6 ++--- doc/features.md | 2 +- doc/features.zh-cn.md | 2 +- doc/internals.md | 2 +- doc/internals.zh-cn.md | 2 +- doc/misc/header.html | 2 +- doc/npm.md | 2 +- doc/performance.md | 4 ++-- doc/performance.zh-cn.md | 4 ++-- library.json | Bin 355 -> 355 bytes package.json | Bin 561 -> 561 bytes rapidjson.autopkg | 6 ++--- readme.md | 44 +++++++++++++++++----------------- readme.zh-cn.md | 44 +++++++++++++++++----------------- test/unittest/pointertest.cpp | 2 +- test/unittest/readertest.cpp | 2 +- travis-doxygen.sh | 2 +- 21 files changed, 74 insertions(+), 74 deletions(-) diff --git a/.travis.yml b/.travis.yml index 38f3a98..df821a7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,7 +14,7 @@ env: - CCACHE_MAXSIZE=100M - ARCH_FLAGS_x86='-m32' # #266: don't use SSE on 32-bit - ARCH_FLAGS_x86_64='-msse4.2' # use SSE4.2 on 64-bit - - GITHUB_REPO='miloyip/rapidjson' + - GITHUB_REPO='Tencent/rapidjson' - secure: "HrsaCb+N66EG1HR+LWH1u51SjaJyRwJEDzqJGYMB7LJ/bfqb9mWKF1fLvZGk46W5t7TVaXRDD5KHFx9DPWvKn4gRUVkwTHEy262ah5ORh8M6n/6VVVajeV/AYt2C0sswdkDBDO4Xq+xy5gdw3G8s1A4Inbm73pUh+6vx+7ltBbk=" before_install: diff --git a/CHANGELOG.md b/CHANGELOG.md index 0205e7b..c9d603c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -109,7 +109,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). ## [1.0.0] - 2015-04-22 ### Added -* 100% [Coverall](https://coveralls.io/r/miloyip/rapidjson?branch=master) coverage. +* 100% [Coverall](https://coveralls.io/r/Tencent/rapidjson?branch=master) coverage. * Version macros (#311) ### Fixed @@ -151,8 +151,8 @@ This project adheres to [Semantic Versioning](http://semver.org/). ## 0.1 - 2011-11-18 -[Unreleased]: https://github.com/miloyip/rapidjson/compare/v1.1.0...HEAD -[1.1.0]: https://github.com/miloyip/rapidjson/compare/v1.0.2...v1.1.0 -[1.0.2]: https://github.com/miloyip/rapidjson/compare/v1.0.1...v1.0.2 -[1.0.1]: https://github.com/miloyip/rapidjson/compare/v1.0.0...v1.0.1 -[1.0.0]: https://github.com/miloyip/rapidjson/compare/v1.0-beta...v1.0.0 +[Unreleased]: https://github.com/Tencent/rapidjson/compare/v1.1.0...HEAD +[1.1.0]: https://github.com/Tencent/rapidjson/compare/v1.0.2...v1.1.0 +[1.0.2]: https://github.com/Tencent/rapidjson/compare/v1.0.1...v1.0.2 +[1.0.1]: https://github.com/Tencent/rapidjson/compare/v1.0.0...v1.0.1 +[1.0.0]: https://github.com/Tencent/rapidjson/compare/v1.0-beta...v1.0.0 diff --git a/RapidJSON.pc.in b/RapidJSON.pc.in index 7467f97..6afb079 100644 --- a/RapidJSON.pc.in +++ b/RapidJSON.pc.in @@ -3,5 +3,5 @@ includedir=@INCLUDE_INSTALL_DIR@ Name: @PROJECT_NAME@ Description: A fast JSON parser/generator for C++ with both SAX/DOM style API Version: @LIB_VERSION_STRING@ -URL: https://github.com/miloyip/rapidjson +URL: https://github.com/Tencent/rapidjson Cflags: -I${includedir} diff --git a/doc/faq.md b/doc/faq.md index 4946cfe..74d770d 100644 --- a/doc/faq.md +++ b/doc/faq.md @@ -18,7 +18,7 @@ 4. Is RapidJSON free? - Yes, it is free under MIT license. It can be used in commercial applications. Please check the details in [license.txt](https://github.com/miloyip/rapidjson/blob/master/license.txt). + Yes, it is free under MIT license. It can be used in commercial applications. Please check the details in [license.txt](https://github.com/Tencent/rapidjson/blob/master/license.txt). 5. Is RapidJSON small? What are its dependencies? @@ -44,7 +44,7 @@ 10. How RapidJSON is tested? - RapidJSON contains a unit test suite for automatic testing. [Travis](https://travis-ci.org/miloyip/rapidjson/)(for Linux) and [AppVeyor](https://ci.appveyor.com/project/miloyip/rapidjson/)(for Windows) will compile and run the unit test suite for all modifications. The test process also uses Valgrind (in Linux) to detect memory leaks. + RapidJSON contains a unit test suite for automatic testing. [Travis](https://travis-ci.org/Tencent/rapidjson/)(for Linux) and [AppVeyor](https://ci.appveyor.com/project/Tencent/rapidjson/)(for Windows) will compile and run the unit test suite for all modifications. The test process also uses Valgrind (in Linux) to detect memory leaks. 11. Is RapidJSON well documented? @@ -70,7 +70,7 @@ 3. Does RapidJSON support relaxed syntax? - Currently no. RapidJSON only support the strict standardized format. Support on related syntax is under discussion in this [issue](https://github.com/miloyip/rapidjson/issues/36). + Currently no. RapidJSON only support the strict standardized format. Support on related syntax is under discussion in this [issue](https://github.com/Tencent/rapidjson/issues/36). ## DOM and SAX diff --git a/doc/faq.zh-cn.md b/doc/faq.zh-cn.md index 307b02f..f279acf 100644 --- a/doc/faq.zh-cn.md +++ b/doc/faq.zh-cn.md @@ -18,7 +18,7 @@ 4. RapidJSON 是免费的么? - 是的,它在 MIT 特許條款下免费。它可用于商业软件。详情请参看 [license.txt](https://github.com/miloyip/rapidjson/blob/master/license.txt)。 + 是的,它在 MIT 特許條款下免费。它可用于商业软件。详情请参看 [license.txt](https://github.com/Tencent/rapidjson/blob/master/license.txt)。 5. RapidJSON 很小么?它有何依赖? @@ -44,7 +44,7 @@ 10. RapidJSON 是如何被测试的? - RapidJSON 包含一组单元测试去执行自动测试。[Travis](https://travis-ci.org/miloyip/rapidjson/)(供 Linux 平台)及 [AppVeyor](https://ci.appveyor.com/project/miloyip/rapidjson/)(供 Windows 平台)会对所有修改进行编译及执行单元测试。在 Linux 下还会使用 Valgrind 去检测内存泄漏。 + RapidJSON 包含一组单元测试去执行自动测试。[Travis](https://travis-ci.org/Tencent/rapidjson/)(供 Linux 平台)及 [AppVeyor](https://ci.appveyor.com/project/Tencent/rapidjson/)(供 Windows 平台)会对所有修改进行编译及执行单元测试。在 Linux 下还会使用 Valgrind 去检测内存泄漏。 11. RapidJSON 是否有完整的文档? @@ -70,7 +70,7 @@ 3. RapidJSON 是否支持宽松的语法? - 现时不支持。RapidJSON 只支持严格的标准格式。宽松语法现时在这 [issue](https://github.com/miloyip/rapidjson/issues/36) 中进行讨论。 + 现时不支持。RapidJSON 只支持严格的标准格式。宽松语法现时在这 [issue](https://github.com/Tencent/rapidjson/issues/36) 中进行讨论。 ## DOM 与 SAX diff --git a/doc/features.md b/doc/features.md index 732fb21..0d79e7f 100644 --- a/doc/features.md +++ b/doc/features.md @@ -29,7 +29,7 @@ * Single line (`// ...`) and multiple line (`/* ... */`) comments (`kParseCommentsFlag`). * Trailing commas at the end of objects and arrays (`kParseTrailingCommasFlag`). * `NaN`, `Inf`, `Infinity`, `-Inf` and `-Infinity` as `double` values (`kParseNanAndInfFlag`) -* [NPM compliant](http://github.com/miloyip/rapidjson/blob/master/doc/npm.md). +* [NPM compliant](http://github.com/Tencent/rapidjson/blob/master/doc/npm.md). ## Unicode diff --git a/doc/features.zh-cn.md b/doc/features.zh-cn.md index 19908a8..7662cc1 100644 --- a/doc/features.zh-cn.md +++ b/doc/features.zh-cn.md @@ -29,7 +29,7 @@ * 单行(`// ...`)及多行(`/* ... */`) 注释 (`kParseCommentsFlag`)。 * 在对象和数组结束前含逗号 (`kParseTrailingCommasFlag`)。 * `NaN`、`Inf`、`Infinity`、`-Inf` 及 `-Infinity` 作为 `double` 值 (`kParseNanAndInfFlag`) -* [NPM 兼容](https://github.com/miloyip/rapidjson/blob/master/doc/npm.md). +* [NPM 兼容](https://github.com/Tencent/rapidjson/blob/master/doc/npm.md). ## Unicode diff --git a/doc/internals.md b/doc/internals.md index 2fff2d9..9b94d7f 100644 --- a/doc/internals.md +++ b/doc/internals.md @@ -214,7 +214,7 @@ In [Intel® 64 and IA-32 Architectures Optimization Reference Manual This is not feasible as RapidJSON should not enforce such requirement. -To fix this issue, currently the routine process bytes up to the next aligned address. After tha, use aligned read to perform SIMD processing. Also see [#85](https://github.com/miloyip/rapidjson/issues/85). +To fix this issue, currently the routine process bytes up to the next aligned address. After tha, use aligned read to perform SIMD processing. Also see [#85](https://github.com/Tencent/rapidjson/issues/85). ## Local Stream Copy {#LocalStreamCopy} diff --git a/doc/internals.zh-cn.md b/doc/internals.zh-cn.md index 0c8bc06..ca3d297 100644 --- a/doc/internals.zh-cn.md +++ b/doc/internals.zh-cn.md @@ -214,7 +214,7 @@ void SkipWhitespace(InputStream& s) { 对于 RapidJSON 来说,这显然是不可行的,因为 RapidJSON 不应当强迫用户进行内存对齐。 -为了修复这个问题,当前的代码会先按字节处理直到下一个对齐的地址。在这之后,使用对齐读取来进行 SIMD 处理。见 [#85](https://github.com/miloyip/rapidjson/issues/85)。 +为了修复这个问题,当前的代码会先按字节处理直到下一个对齐的地址。在这之后,使用对齐读取来进行 SIMD 处理。见 [#85](https://github.com/Tencent/rapidjson/issues/85)。 ## 局部流拷贝 {#LocalStreamCopy} diff --git a/doc/misc/header.html b/doc/misc/header.html index 2dbe721..a89ba46 100644 --- a/doc/misc/header.html +++ b/doc/misc/header.html @@ -18,7 +18,7 @@ $extrastylesheet
-
+
$searchbox diff --git a/doc/npm.md b/doc/npm.md index 5efa768..6f4e85a 100644 --- a/doc/npm.md +++ b/doc/npm.md @@ -7,7 +7,7 @@ ... "dependencies": { ... - "rapidjson": "git@github.com:miloyip/rapidjson.git" + "rapidjson": "git@github.com:Tencent/rapidjson.git" }, ... "gypfile": true diff --git a/doc/performance.md b/doc/performance.md index 988e799..7b18730 100644 --- a/doc/performance.md +++ b/doc/performance.md @@ -15,12 +15,12 @@ Additionally, you may refer to the following third-party benchmarks. * [json_spirit](https://github.com/cierelabs/json_spirit) * [jsoncpp](http://jsoncpp.sourceforge.net/) * [libjson](http://sourceforge.net/projects/libjson/) - * [rapidjson](https://github.com/miloyip/rapidjson/) + * [rapidjson](https://github.com/Tencent/rapidjson/) * [QJsonDocument](http://qt-project.org/doc/qt-5.0/qtcore/qjsondocument.html) * [JSON Parser Benchmarking](http://chadaustin.me/2013/01/json-parser-benchmarking/) by Chad Austin (Jan 2013) * [sajson](https://github.com/chadaustin/sajson) - * [rapidjson](https://github.com/miloyip/rapidjson/) + * [rapidjson](https://github.com/Tencent/rapidjson/) * [vjson](https://code.google.com/p/vjson/) * [YAJL](http://lloyd.github.com/yajl/) * [Jansson](http://www.digip.org/jansson/) diff --git a/doc/performance.zh-cn.md b/doc/performance.zh-cn.md index c20c505..2322c9c 100644 --- a/doc/performance.zh-cn.md +++ b/doc/performance.zh-cn.md @@ -15,12 +15,12 @@ RapidJSON 0.1 版本的性能测试文章位于 [这里](https://code.google.com * [json_spirit](https://github.com/cierelabs/json_spirit) * [jsoncpp](http://jsoncpp.sourceforge.net/) * [libjson](http://sourceforge.net/projects/libjson/) - * [rapidjson](https://github.com/miloyip/rapidjson/) + * [rapidjson](https://github.com/Tencent/rapidjson/) * [QJsonDocument](http://qt-project.org/doc/qt-5.0/qtcore/qjsondocument.html) * [JSON Parser Benchmarking](http://chadaustin.me/2013/01/json-parser-benchmarking/) by Chad Austin (Jan 2013) * [sajson](https://github.com/chadaustin/sajson) - * [rapidjson](https://github.com/miloyip/rapidjson/) + * [rapidjson](https://github.com/Tencent/rapidjson/) * [vjson](https://code.google.com/p/vjson/) * [YAJL](http://lloyd.github.com/yajl/) * [Jansson](http://www.digip.org/jansson/) diff --git a/library.json b/library.json index 21d6bcecf22fd12342f560eae0c540daa713003d..2210fcd61735c8cca63c19534e8df591eda60d58 100644 GIT binary patch delta 33 ocmaFN^q6Ub7o%iIYF=_`UWtBDVnJp~R&jow5|@HPEmtiU0MF73BLDyZ delta 33 ocmaFN^q6Ub7o%iuW=?)(W`TZDVnJp~R&jow5|@HPEmtiU0Mu~{RR910 diff --git a/package.json b/package.json index cc6087a5ca36cfd95aacccceb7c07d909f085f4d..129581a633512e5165e985041339c58f990efc24 100644 GIT binary patch delta 60 zcmdnUvXNy&CL?=DYF=_`UdiMF#uPaFHlvjcR9wF(u^=-gt2jSTxhOR;B{x-xtCkA@ Dmf#g| delta 60 zcmdnUvXNy&CL?=pW=?)(X2IkF#uPaFHlvjcR9wF(u^=-gt2jSTxhOR;B{x-xtCkA@ Dt%nu{ diff --git a/rapidjson.autopkg b/rapidjson.autopkg index 486ad14..cbe5258 100644 --- a/rapidjson.autopkg +++ b/rapidjson.autopkg @@ -5,10 +5,10 @@ nuget { id = rapidjson; version : ${MYVERSION}; title: "rapidjson"; - authors: {"https://github.com/miloyip/rapidjson/releases/tag/v1.1.0"}; + authors: {"https://github.com/Tencent/rapidjson/releases/tag/v1.1.0"}; owners: {"@lsantos (github)"}; - licenseUrl: "https://github.com/miloyip/rapidjson/blob/master/license.txt"; - projectUrl: "https://github.com/miloyip/rapidjson/"; + licenseUrl: "https://github.com/Tencent/rapidjson/blob/master/license.txt"; + projectUrl: "https://github.com/Tencent/rapidjson/"; iconUrl: "https://cdn1.iconfinder.com/data/icons/fatcow/32x32/json.png"; requireLicenseAcceptance:false; summary: @"A fast JSON parser/generator for C++ with both SAX/DOM style API"; diff --git a/readme.md b/readme.md index 2937619..c9e9b1a 100644 --- a/readme.md +++ b/readme.md @@ -8,11 +8,11 @@ Tencent is pleased to support the open source community by making RapidJSON avai Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. -* [RapidJSON GitHub](https://github.com/miloyip/rapidjson/) +* [RapidJSON GitHub](https://github.com/Tencent/rapidjson/) * RapidJSON Documentation * [English](http://rapidjson.org/) * [简体中文](http://rapidjson.org/zh-cn/) - * [GitBook](https://www.gitbook.com/book/miloyip/rapidjson/) with downloadable PDF/EPUB/MOBI, without API reference. + * [GitBook](https://www.gitbook.com/book/Tencent/rapidjson/) with downloadable PDF/EPUB/MOBI, without API reference. ## Build status @@ -20,12 +20,12 @@ Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights | :---------------: | :-----------------: | :-------------------: | | ![lin-badge] | ![win-badge] | ![cov-badge] | -[lin-badge]: https://travis-ci.org/miloyip/rapidjson.svg?branch=master "Travis build status" -[lin-link]: https://travis-ci.org/miloyip/rapidjson "Travis build status" -[win-badge]: https://ci.appveyor.com/api/projects/status/github/miloyip/rapidjson?branch=master&svg=true "AppVeyor build status" -[win-link]: https://ci.appveyor.com/project/miloyip/rapidjson/branch/master "AppVeyor build status" -[cov-badge]: https://coveralls.io/repos/miloyip/rapidjson/badge.svg?branch=master "Coveralls coverage" -[cov-link]: https://coveralls.io/r/miloyip/rapidjson?branch=master "Coveralls coverage" +[lin-badge]: https://travis-ci.org/Tencent/rapidjson.svg?branch=master "Travis build status" +[lin-link]: https://travis-ci.org/Tencent/rapidjson "Travis build status" +[win-badge]: https://ci.appveyor.com/api/projects/status/github/Tencent/rapidjson?branch=master&svg=true "AppVeyor build status" +[win-link]: https://ci.appveyor.com/project/Tencent/rapidjson/branch/master "AppVeyor build status" +[cov-badge]: https://coveralls.io/repos/Tencent/rapidjson/badge.svg?branch=master "Coveralls coverage" +[cov-link]: https://coveralls.io/r/Tencent/rapidjson?branch=master "Coveralls coverage" ## Introduction @@ -136,25 +136,25 @@ The following diagram shows the process. ![simpledom](doc/diagram/simpledom.png) -More [examples](https://github.com/miloyip/rapidjson/tree/master/example) are available: +More [examples](https://github.com/Tencent/rapidjson/tree/master/example) are available: * DOM API - * [tutorial](https://github.com/miloyip/rapidjson/blob/master/example/tutorial/tutorial.cpp): Basic usage of DOM API. + * [tutorial](https://github.com/Tencent/rapidjson/blob/master/example/tutorial/tutorial.cpp): Basic usage of DOM API. * SAX API - * [simplereader](https://github.com/miloyip/rapidjson/blob/master/example/simplereader/simplereader.cpp): Dumps all SAX events while parsing a JSON by `Reader`. - * [condense](https://github.com/miloyip/rapidjson/blob/master/example/condense/condense.cpp): A command line tool to rewrite a JSON, with all whitespaces removed. - * [pretty](https://github.com/miloyip/rapidjson/blob/master/example/pretty/pretty.cpp): A command line tool to rewrite a JSON with indents and newlines by `PrettyWriter`. - * [capitalize](https://github.com/miloyip/rapidjson/blob/master/example/capitalize/capitalize.cpp): A command line tool to capitalize strings in JSON. - * [messagereader](https://github.com/miloyip/rapidjson/blob/master/example/messagereader/messagereader.cpp): Parse a JSON message with SAX API. - * [serialize](https://github.com/miloyip/rapidjson/blob/master/example/serialize/serialize.cpp): Serialize a C++ object into JSON with SAX API. - * [jsonx](https://github.com/miloyip/rapidjson/blob/master/example/jsonx/jsonx.cpp): Implements a `JsonxWriter` which stringify SAX events into [JSONx](https://www-01.ibm.com/support/knowledgecenter/SS9H2Y_7.1.0/com.ibm.dp.doc/json_jsonx.html) (a kind of XML) format. The example is a command line tool which converts input JSON into JSONx format. + * [simplereader](https://github.com/Tencent/rapidjson/blob/master/example/simplereader/simplereader.cpp): Dumps all SAX events while parsing a JSON by `Reader`. + * [condense](https://github.com/Tencent/rapidjson/blob/master/example/condense/condense.cpp): A command line tool to rewrite a JSON, with all whitespaces removed. + * [pretty](https://github.com/Tencent/rapidjson/blob/master/example/pretty/pretty.cpp): A command line tool to rewrite a JSON with indents and newlines by `PrettyWriter`. + * [capitalize](https://github.com/Tencent/rapidjson/blob/master/example/capitalize/capitalize.cpp): A command line tool to capitalize strings in JSON. + * [messagereader](https://github.com/Tencent/rapidjson/blob/master/example/messagereader/messagereader.cpp): Parse a JSON message with SAX API. + * [serialize](https://github.com/Tencent/rapidjson/blob/master/example/serialize/serialize.cpp): Serialize a C++ object into JSON with SAX API. + * [jsonx](https://github.com/Tencent/rapidjson/blob/master/example/jsonx/jsonx.cpp): Implements a `JsonxWriter` which stringify SAX events into [JSONx](https://www-01.ibm.com/support/knowledgecenter/SS9H2Y_7.1.0/com.ibm.dp.doc/json_jsonx.html) (a kind of XML) format. The example is a command line tool which converts input JSON into JSONx format. * Schema - * [schemavalidator](https://github.com/miloyip/rapidjson/blob/master/example/schemavalidator/schemavalidator.cpp) : A command line tool to validate a JSON with a JSON schema. + * [schemavalidator](https://github.com/Tencent/rapidjson/blob/master/example/schemavalidator/schemavalidator.cpp) : A command line tool to validate a JSON with a JSON schema. * Advanced - * [prettyauto](https://github.com/miloyip/rapidjson/blob/master/example/prettyauto/prettyauto.cpp): A modified version of [pretty](https://github.com/miloyip/rapidjson/blob/master/example/pretty/pretty.cpp) to automatically handle JSON with any UTF encodings. - * [parsebyparts](https://github.com/miloyip/rapidjson/blob/master/example/parsebyparts/parsebyparts.cpp): Implements an `AsyncDocumentParser` which can parse JSON in parts, using C++11 thread. - * [filterkey](https://github.com/miloyip/rapidjson/blob/master/example/filterkey/filterkey.cpp): A command line tool to remove all values with user-specified key. - * [filterkeydom](https://github.com/miloyip/rapidjson/blob/master/example/filterkeydom/filterkeydom.cpp): Same tool as above, but it demonstrates how to use a generator to populate a `Document`. + * [prettyauto](https://github.com/Tencent/rapidjson/blob/master/example/prettyauto/prettyauto.cpp): A modified version of [pretty](https://github.com/Tencent/rapidjson/blob/master/example/pretty/pretty.cpp) to automatically handle JSON with any UTF encodings. + * [parsebyparts](https://github.com/Tencent/rapidjson/blob/master/example/parsebyparts/parsebyparts.cpp): Implements an `AsyncDocumentParser` which can parse JSON in parts, using C++11 thread. + * [filterkey](https://github.com/Tencent/rapidjson/blob/master/example/filterkey/filterkey.cpp): A command line tool to remove all values with user-specified key. + * [filterkeydom](https://github.com/Tencent/rapidjson/blob/master/example/filterkeydom/filterkeydom.cpp): Same tool as above, but it demonstrates how to use a generator to populate a `Document`. diff --git a/readme.zh-cn.md b/readme.zh-cn.md index 81b84bb..422667b 100644 --- a/readme.zh-cn.md +++ b/readme.zh-cn.md @@ -8,11 +8,11 @@ Tencent is pleased to support the open source community by making RapidJSON avai Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. -* [RapidJSON GitHub](https://github.com/miloyip/rapidjson/) +* [RapidJSON GitHub](https://github.com/Tencent/rapidjson/) * RapidJSON 文档 * [English](http://rapidjson.org/) * [简体中文](http://rapidjson.org/zh-cn/) - * [GitBook](https://www.gitbook.com/book/miloyip/rapidjson/) 可下载 PDF/EPUB/MOBI,但不含 API 参考手册。 + * [GitBook](https://www.gitbook.com/book/Tencent/rapidjson/) 可下载 PDF/EPUB/MOBI,但不含 API 参考手册。 ## Build 状态 @@ -20,12 +20,12 @@ Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights | :---------------: | :-----------------: | :-------------------: | | ![lin-badge] | ![win-badge] | ![cov-badge] | -[lin-badge]: https://travis-ci.org/miloyip/rapidjson.svg?branch=master "Travis build status" -[lin-link]: https://travis-ci.org/miloyip/rapidjson "Travis build status" -[win-badge]: https://ci.appveyor.com/api/projects/status/github/miloyip/rapidjson?branch=master&svg=true "AppVeyor build status" -[win-link]: https://ci.appveyor.com/project/miloyip/rapidjson/branch/master "AppVeyor build status" -[cov-badge]: https://coveralls.io/repos/miloyip/rapidjson/badge.svg?branch=master "Coveralls coverage" -[cov-link]: https://coveralls.io/r/miloyip/rapidjson?branch=master "Coveralls coverage" +[lin-badge]: https://travis-ci.org/Tencent/rapidjson.svg?branch=master "Travis build status" +[lin-link]: https://travis-ci.org/Tencent/rapidjson "Travis build status" +[win-badge]: https://ci.appveyor.com/api/projects/status/github/Tencent/rapidjson?branch=master&svg=true "AppVeyor build status" +[win-link]: https://ci.appveyor.com/project/Tencent/rapidjson/branch/master "AppVeyor build status" +[cov-badge]: https://coveralls.io/repos/Tencent/rapidjson/badge.svg?branch=master "Coveralls coverage" +[cov-link]: https://coveralls.io/r/Tencent/rapidjson?branch=master "Coveralls coverage" ## 简介 @@ -128,25 +128,25 @@ int main() { ![simpledom](doc/diagram/simpledom.png) -还有许多 [例子](https://github.com/miloyip/rapidjson/tree/master/example) 可供参考: +还有许多 [例子](https://github.com/Tencent/rapidjson/tree/master/example) 可供参考: * DOM API - * [tutorial](https://github.com/miloyip/rapidjson/blob/master/example/tutorial/tutorial.cpp): DOM API 的基本使用方法。 + * [tutorial](https://github.com/Tencent/rapidjson/blob/master/example/tutorial/tutorial.cpp): DOM API 的基本使用方法。 * SAX API - * [simplereader](https://github.com/miloyip/rapidjson/blob/master/example/simplereader/simplereader.cpp): 使用 `Reader` 解析 JSON 时,打印所有 SAX 事件。 - * [condense](https://github.com/miloyip/rapidjson/blob/master/example/condense/condense.cpp): 移除 JSON 中所有空白符的命令行工具。 - * [pretty](https://github.com/miloyip/rapidjson/blob/master/example/pretty/pretty.cpp): 为 JSON 加入缩进与换行的命令行工具,当中使用了 `PrettyWriter`。 - * [capitalize](https://github.com/miloyip/rapidjson/blob/master/example/capitalize/capitalize.cpp): 把 JSON 中所有字符串改为大写的命令行工具。 - * [messagereader](https://github.com/miloyip/rapidjson/blob/master/example/messagereader/messagereader.cpp): 使用 SAX API 去解析一个 JSON 报文。 - * [serialize](https://github.com/miloyip/rapidjson/blob/master/example/serialize/serialize.cpp): 使用 SAX API 去序列化 C++ 对象,生成 JSON。 - * [jsonx](https://github.com/miloyip/rapidjson/blob/master/example/jsonx/jsonx.cpp): 实现了一个 `JsonxWriter`,它能把 SAX 事件写成 [JSONx](https://www-01.ibm.com/support/knowledgecenter/SS9H2Y_7.1.0/com.ibm.dp.doc/json_jsonx.html)(一种 XML)格式。这个例子是把 JSON 输入转换成 JSONx 格式的命令行工具。 + * [simplereader](https://github.com/Tencent/rapidjson/blob/master/example/simplereader/simplereader.cpp): 使用 `Reader` 解析 JSON 时,打印所有 SAX 事件。 + * [condense](https://github.com/Tencent/rapidjson/blob/master/example/condense/condense.cpp): 移除 JSON 中所有空白符的命令行工具。 + * [pretty](https://github.com/Tencent/rapidjson/blob/master/example/pretty/pretty.cpp): 为 JSON 加入缩进与换行的命令行工具,当中使用了 `PrettyWriter`。 + * [capitalize](https://github.com/Tencent/rapidjson/blob/master/example/capitalize/capitalize.cpp): 把 JSON 中所有字符串改为大写的命令行工具。 + * [messagereader](https://github.com/Tencent/rapidjson/blob/master/example/messagereader/messagereader.cpp): 使用 SAX API 去解析一个 JSON 报文。 + * [serialize](https://github.com/Tencent/rapidjson/blob/master/example/serialize/serialize.cpp): 使用 SAX API 去序列化 C++ 对象,生成 JSON。 + * [jsonx](https://github.com/Tencent/rapidjson/blob/master/example/jsonx/jsonx.cpp): 实现了一个 `JsonxWriter`,它能把 SAX 事件写成 [JSONx](https://www-01.ibm.com/support/knowledgecenter/SS9H2Y_7.1.0/com.ibm.dp.doc/json_jsonx.html)(一种 XML)格式。这个例子是把 JSON 输入转换成 JSONx 格式的命令行工具。 * Schema API - * [schemavalidator](https://github.com/miloyip/rapidjson/blob/master/example/schemavalidator/schemavalidator.cpp): 使用 JSON Schema 去校验 JSON 的命令行工具。 + * [schemavalidator](https://github.com/Tencent/rapidjson/blob/master/example/schemavalidator/schemavalidator.cpp): 使用 JSON Schema 去校验 JSON 的命令行工具。 * 进阶 - * [prettyauto](https://github.com/miloyip/rapidjson/blob/master/example/prettyauto/prettyauto.cpp): [pretty](https://github.com/miloyip/rapidjson/blob/master/example/pretty/pretty.cpp) 的修改版本,可自动处理任何 UTF 编码的 JSON。 - * [parsebyparts](https://github.com/miloyip/rapidjson/blob/master/example/parsebyparts/parsebyparts.cpp): 这例子中的 `AsyncDocumentParser` 类使用 C++ 线程来逐段解析 JSON。 - * [filterkey](https://github.com/miloyip/rapidjson/blob/master/example/filterkey/filterkey.cpp): 移取使用者指定的键值的命令行工具。 - * [filterkeydom](https://github.com/miloyip/rapidjson/blob/master/example/filterkey/filterkey.cpp): 如上的工具,但展示如何使用生成器(generator)去填充一个 `Document`。 \ No newline at end of file + * [prettyauto](https://github.com/Tencent/rapidjson/blob/master/example/prettyauto/prettyauto.cpp): [pretty](https://github.com/Tencent/rapidjson/blob/master/example/pretty/pretty.cpp) 的修改版本,可自动处理任何 UTF 编码的 JSON。 + * [parsebyparts](https://github.com/Tencent/rapidjson/blob/master/example/parsebyparts/parsebyparts.cpp): 这例子中的 `AsyncDocumentParser` 类使用 C++ 线程来逐段解析 JSON。 + * [filterkey](https://github.com/Tencent/rapidjson/blob/master/example/filterkey/filterkey.cpp): 移取使用者指定的键值的命令行工具。 + * [filterkeydom](https://github.com/Tencent/rapidjson/blob/master/example/filterkey/filterkey.cpp): 如上的工具,但展示如何使用生成器(generator)去填充一个 `Document`。 \ No newline at end of file diff --git a/test/unittest/pointertest.cpp b/test/unittest/pointertest.cpp index eed6fba..d5a688d 100644 --- a/test/unittest/pointertest.cpp +++ b/test/unittest/pointertest.cpp @@ -1488,7 +1488,7 @@ TEST(Pointer, Ambiguity) { } } -// https://github.com/miloyip/rapidjson/issues/483 +// https://github.com/Tencent/rapidjson/issues/483 namespace myjson { class MyAllocator diff --git a/test/unittest/readertest.cpp b/test/unittest/readertest.cpp index dad33d6..e530801 100644 --- a/test/unittest/readertest.cpp +++ b/test/unittest/readertest.cpp @@ -233,7 +233,7 @@ static void TestParseDouble() { TEST_DOUBLE(fullPrecision, "1e-10000", 0.0); // must underflow TEST_DOUBLE(fullPrecision, "18446744073709551616", 18446744073709551616.0); // 2^64 (max of uint64_t + 1, force to use double) TEST_DOUBLE(fullPrecision, "-9223372036854775809", -9223372036854775809.0); // -2^63 - 1(min of int64_t + 1, force to use double) - TEST_DOUBLE(fullPrecision, "0.9868011474609375", 0.9868011474609375); // https://github.com/miloyip/rapidjson/issues/120 + TEST_DOUBLE(fullPrecision, "0.9868011474609375", 0.9868011474609375); // https://github.com/Tencent/rapidjson/issues/120 TEST_DOUBLE(fullPrecision, "123e34", 123e34); // Fast Path Cases In Disguise TEST_DOUBLE(fullPrecision, "45913141877270640000.0", 45913141877270640000.0); TEST_DOUBLE(fullPrecision, "2.2250738585072011e-308", 2.2250738585072011e-308); // http://www.exploringbinary.com/php-hangs-on-numeric-value-2-2250738585072011e-308/ diff --git a/travis-doxygen.sh b/travis-doxygen.sh index e5c0320..33ec6ab 100755 --- a/travis-doxygen.sh +++ b/travis-doxygen.sh @@ -8,7 +8,7 @@ DOXYGEN_VER=doxygen-1.8.13 DOXYGEN_TAR=${DOXYGEN_VER}.linux.bin.tar.gz DOXYGEN_URL="http://ftp.stack.nl/pub/users/dimitri/${DOXYGEN_TAR}" -: ${GITHUB_REPO:="miloyip/rapidjson"} +: ${GITHUB_REPO:="Tencent/rapidjson"} GITHUB_HOST="github.com" GITHUB_CLONE="git://${GITHUB_HOST}/${GITHUB_REPO}" GITHUB_URL="https://${GITHUB_HOST}/${GITHUB_PUSH-${GITHUB_REPO}}" From 4c0f0036b54776dfc48ad76eca685caea0a1dc82 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Thu, 14 Sep 2017 11:55:31 +0800 Subject: [PATCH 271/305] Update appveyor badge and link --- readme.md | 4 ++-- readme.zh-cn.md | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/readme.md b/readme.md index c9e9b1a..be22e20 100644 --- a/readme.md +++ b/readme.md @@ -22,8 +22,8 @@ Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights [lin-badge]: https://travis-ci.org/Tencent/rapidjson.svg?branch=master "Travis build status" [lin-link]: https://travis-ci.org/Tencent/rapidjson "Travis build status" -[win-badge]: https://ci.appveyor.com/api/projects/status/github/Tencent/rapidjson?branch=master&svg=true "AppVeyor build status" -[win-link]: https://ci.appveyor.com/project/Tencent/rapidjson/branch/master "AppVeyor build status" +[win-badge]: https://ci.appveyor.com/api/projects/status/l6qulgqahcayidrf/branch/master?svg=true "AppVeyor build status" +[win-link]: https://ci.appveyor.com/project/miloyip/rapidjson-0fdqj/branch/master "AppVeyor build status" [cov-badge]: https://coveralls.io/repos/Tencent/rapidjson/badge.svg?branch=master "Coveralls coverage" [cov-link]: https://coveralls.io/r/Tencent/rapidjson?branch=master "Coveralls coverage" diff --git a/readme.zh-cn.md b/readme.zh-cn.md index 422667b..bca8897 100644 --- a/readme.zh-cn.md +++ b/readme.zh-cn.md @@ -22,8 +22,8 @@ Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights [lin-badge]: https://travis-ci.org/Tencent/rapidjson.svg?branch=master "Travis build status" [lin-link]: https://travis-ci.org/Tencent/rapidjson "Travis build status" -[win-badge]: https://ci.appveyor.com/api/projects/status/github/Tencent/rapidjson?branch=master&svg=true "AppVeyor build status" -[win-link]: https://ci.appveyor.com/project/Tencent/rapidjson/branch/master "AppVeyor build status" +[win-badge]: https://ci.appveyor.com/api/projects/status/l6qulgqahcayidrf/branch/master?svg=true "AppVeyor build status" +[win-link]: https://ci.appveyor.com/project/miloyip/rapidjson-0fdqj/branch/master "AppVeyor build status" [cov-badge]: https://coveralls.io/repos/Tencent/rapidjson/badge.svg?branch=master "Coveralls coverage" [cov-link]: https://coveralls.io/r/Tencent/rapidjson?branch=master "Coveralls coverage" From 0b8adabab7f32dd9addc517aeb4c32b9da37e411 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Fri, 29 Sep 2017 09:44:05 +0800 Subject: [PATCH 272/305] Fix #1071 gitbook link --- readme.md | 2 +- readme.zh-cn.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/readme.md b/readme.md index be22e20..b833a98 100644 --- a/readme.md +++ b/readme.md @@ -12,7 +12,7 @@ Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights * RapidJSON Documentation * [English](http://rapidjson.org/) * [简体中文](http://rapidjson.org/zh-cn/) - * [GitBook](https://www.gitbook.com/book/Tencent/rapidjson/) with downloadable PDF/EPUB/MOBI, without API reference. + * [GitBook](https://www.gitbook.com/book/miloyip/rapidjson/) with downloadable PDF/EPUB/MOBI, without API reference. ## Build status diff --git a/readme.zh-cn.md b/readme.zh-cn.md index bca8897..f4ddaa8 100644 --- a/readme.zh-cn.md +++ b/readme.zh-cn.md @@ -12,7 +12,7 @@ Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights * RapidJSON 文档 * [English](http://rapidjson.org/) * [简体中文](http://rapidjson.org/zh-cn/) - * [GitBook](https://www.gitbook.com/book/Tencent/rapidjson/) 可下载 PDF/EPUB/MOBI,但不含 API 参考手册。 + * [GitBook](https://www.gitbook.com/book/miloyip/rapidjson/) 可下载 PDF/EPUB/MOBI,但不含 API 参考手册。 ## Build 状态 From 2a0bc6062b38ed40586bd8e1945835698b95a9c1 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Fri, 29 Sep 2017 09:53:00 +0800 Subject: [PATCH 273/305] Update gitbook zh-cn link --- readme.zh-cn.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.zh-cn.md b/readme.zh-cn.md index f4ddaa8..ccf1669 100644 --- a/readme.zh-cn.md +++ b/readme.zh-cn.md @@ -12,7 +12,7 @@ Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights * RapidJSON 文档 * [English](http://rapidjson.org/) * [简体中文](http://rapidjson.org/zh-cn/) - * [GitBook](https://www.gitbook.com/book/miloyip/rapidjson/) 可下载 PDF/EPUB/MOBI,但不含 API 参考手册。 + * [GitBook](https://www.gitbook.com/book/miloyip/rapidjson/details/zh-cn) 可下载 PDF/EPUB/MOBI,但不含 API 参考手册。 ## Build 状态 From b16ff281f854564e2669b2c3f4871793ddc51fc3 Mon Sep 17 00:00:00 2001 From: KaitoHH Date: Tue, 26 Sep 2017 15:39:06 +0800 Subject: [PATCH 274/305] Add feature of locating line and column number of error --- include/rapidjson/document.h | 11 ++++++++- include/rapidjson/error/error.h | 8 +++++++ include/rapidjson/stream.h | 42 +++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 1 deletion(-) diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h index 93b091f..de65740 100644 --- a/include/rapidjson/document.h +++ b/include/rapidjson/document.h @@ -2219,14 +2219,17 @@ public: \return The document itself for fluent API. */ template - GenericDocument& ParseStream(InputStream& is) { + GenericDocument& ParseStream(InputStream& is_) { GenericReader reader( stack_.HasAllocator() ? &stack_.GetAllocator() : 0); ClearStackOnExit scope(*this); + GenericStreamWrapper is(is_); parseResult_ = reader.template Parse(is, *this); if (parseResult_) { RAPIDJSON_ASSERT(stack_.GetSize() == sizeof(ValueType)); // Got one and only one root object ValueType::operator=(*stack_.template Pop(1));// Move value from stack to document + } else { + parseResult_.SetPos(is.line_, is.col_); } return *this; } @@ -2355,6 +2358,12 @@ public: //! Get the position of last parsing error in input, 0 otherwise. size_t GetErrorOffset() const { return parseResult_.Offset(); } + + //! Get the position of last parsing error in input, 0 otherwise. + size_t GetErrorLine() const { return parseResult_.Line(); } + + //! Get the position of last parsing error in input, 0 otherwise. + size_t GetErrorColumn() const { return parseResult_.Col(); } //! Implicit conversion to get the last parse result #ifndef __clang // -Wdocumentation diff --git a/include/rapidjson/error/error.h b/include/rapidjson/error/error.h index 9311d2f..be80579 100644 --- a/include/rapidjson/error/error.h +++ b/include/rapidjson/error/error.h @@ -116,6 +116,10 @@ public: ParseErrorCode Code() const { return code_; } //! Get the error offset, if \ref IsError(), 0 otherwise. size_t Offset() const { return offset_; } + //! Get the position of line number if error exists. + size_t Line() const { return line_; } + //! Get the position of column number if error exists. + size_t Col() const { return col_; } //! Explicit conversion to \c bool, returns \c true, iff !\ref IsError(). operator BooleanType() const { return !IsError() ? &ParseResult::IsError : NULL; } @@ -134,10 +138,14 @@ public: void Clear() { Set(kParseErrorNone); } //! Update error code and offset. void Set(ParseErrorCode code, size_t offset = 0) { code_ = code; offset_ = offset; } + //! Update line number and column number of the error position + void SetPos(size_t line, size_t col) { line_ = line; col_ = col; } private: ParseErrorCode code_; size_t offset_; + size_t line_; + size_t col_; }; //! Function pointer type of GetParseError(). diff --git a/include/rapidjson/stream.h b/include/rapidjson/stream.h index fef82c2..4e4ba80 100644 --- a/include/rapidjson/stream.h +++ b/include/rapidjson/stream.h @@ -100,6 +100,48 @@ inline void PutN(Stream& stream, Ch c, size_t n) { PutUnsafe(stream, c); } +/////////////////////////////////////////////////////////////////////////////// +// GenericStreamWrapper + +//! A Stream Wrapper +/*! \tThis string stream is designed for counting line and column number + \tof the error (if exists) position, while just forwarding any received + \tmessage to the origin stream. + \note implements Stream concept +*/ +template +class GenericStreamWrapper { +public: + typedef typename Encoding::Ch Ch; + size_t line_; + size_t col_; + GenericStreamWrapper(InputStream& is): is_(is), line_(1), col_(0) {} + + Ch Peek() const { return is_.Peek(); } + + // counting line and column number + Ch Take() { + Ch ch = is_.Take(); + if(ch == '\n') { + line_ ++; + col_ = 0; + } else { + col_ ++; + } + return ch; + } + size_t Tell() { return is_.Tell(); } + + Ch* PutBegin() { return is_.PutBegin(); } + void Put(Ch ch) { return is_.Put(ch); } + void Flush() { return is_.Flush(); } + size_t PutEnd(Ch* ch) { is_.PutEnd(ch); } + + const Ch* Peek4() const { is_.Peek4(); } +private: + InputStream& is_; +}; + /////////////////////////////////////////////////////////////////////////////// // StringStream From 79d9c71f98b0f1cfea5fae2fe33595efcbf79028 Mon Sep 17 00:00:00 2001 From: KaitoHH Date: Tue, 26 Sep 2017 16:03:09 +0800 Subject: [PATCH 275/305] fix stream wrapper initializer fix initialization warning add special wrapper for AutoUTFInputStream --- include/rapidjson/error/error.h | 4 ++-- include/rapidjson/stream.h | 16 +++++++++++----- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/include/rapidjson/error/error.h b/include/rapidjson/error/error.h index be80579..618a6cf 100644 --- a/include/rapidjson/error/error.h +++ b/include/rapidjson/error/error.h @@ -108,9 +108,9 @@ struct ParseResult { typedef bool (ParseResult::*BooleanType)() const; public: //! Default constructor, no error. - ParseResult() : code_(kParseErrorNone), offset_(0) {} + ParseResult() : code_(kParseErrorNone), offset_(0), line_(0), col_(0) {} //! Constructor to set an error. - ParseResult(ParseErrorCode code, size_t offset) : code_(code), offset_(offset) {} + ParseResult(ParseErrorCode code, size_t offset) : code_(code), offset_(offset), line_(0), col_(0) {} //! Get the error code. ParseErrorCode Code() const { return code_; } diff --git a/include/rapidjson/stream.h b/include/rapidjson/stream.h index 4e4ba80..a315d3f 100644 --- a/include/rapidjson/stream.h +++ b/include/rapidjson/stream.h @@ -115,7 +115,7 @@ public: typedef typename Encoding::Ch Ch; size_t line_; size_t col_; - GenericStreamWrapper(InputStream& is): is_(is), line_(1), col_(0) {} + GenericStreamWrapper(InputStream& is): line_(1), col_(0), is_(is) {} Ch Peek() const { return is_.Peek(); } @@ -133,11 +133,17 @@ public: size_t Tell() { return is_.Tell(); } Ch* PutBegin() { return is_.PutBegin(); } - void Put(Ch ch) { return is_.Put(ch); } - void Flush() { return is_.Flush(); } - size_t PutEnd(Ch* ch) { is_.PutEnd(ch); } + void Put(Ch ch) { is_.Put(ch); } + void Flush() { is_.Flush(); } + size_t PutEnd(Ch* ch) { return is_.PutEnd(ch); } - const Ch* Peek4() const { is_.Peek4(); } + // wrapper for MemoryStream + const Ch* Peek4() const { return is_.Peek4(); } + + // wrapper for AutoUTFInputStream + UTFType GetType() const { return is_.GetType(); } + bool HasBOM() const { return is_.HasBOM(); } + private: InputStream& is_; }; From 143641c75abaf6f111c79152b76996324ed3ad19 Mon Sep 17 00:00:00 2001 From: KaitoHH Date: Wed, 27 Sep 2017 13:58:16 +0800 Subject: [PATCH 276/305] suppress C4512, C4702 warning --- include/rapidjson/stream.h | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/include/rapidjson/stream.h b/include/rapidjson/stream.h index a315d3f..556c30a 100644 --- a/include/rapidjson/stream.h +++ b/include/rapidjson/stream.h @@ -109,6 +109,12 @@ inline void PutN(Stream& stream, Ch c, size_t n) { \tmessage to the origin stream. \note implements Stream concept */ + +#if defined(_MSC_VER) && _MSC_VER <= 1700 +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(4702) // disable unreachable code +#endif + template class GenericStreamWrapper { public: @@ -135,7 +141,7 @@ public: Ch* PutBegin() { return is_.PutBegin(); } void Put(Ch ch) { is_.Put(ch); } void Flush() { is_.Flush(); } - size_t PutEnd(Ch* ch) { return is_.PutEnd(ch); } + size_t PutEnd(Ch* ch) { return is_.PutEnd(ch); } // wrapper for MemoryStream const Ch* Peek4() const { return is_.Peek4(); } @@ -146,8 +152,17 @@ public: private: InputStream& is_; + + // elimante vs2010-2013 C4512 warning by + // prohibiting copy constructor & assignment operator. + GenericStreamWrapper& operator=(const GenericStreamWrapper &); + GenericStreamWrapper(const GenericStreamWrapper&); }; +#if defined(_MSC_VER) && _MSC_VER <= 1700 +RAPIDJSON_DIAG_POP +#endif + /////////////////////////////////////////////////////////////////////////////// // StringStream From 799fdea9fc05aa74c2ebfb49340943195ac2e1dc Mon Sep 17 00:00:00 2001 From: KaitoHH Date: Thu, 28 Sep 2017 16:57:52 +0800 Subject: [PATCH 277/305] add cursor wrapper --- include/rapidjson/cursorstreamwrapper.h | 59 +++++++++++++++++++++++++ include/rapidjson/document.h | 11 +---- include/rapidjson/error/error.h | 12 +---- include/rapidjson/stream.h | 37 +++++----------- 4 files changed, 72 insertions(+), 47 deletions(-) create mode 100644 include/rapidjson/cursorstreamwrapper.h diff --git a/include/rapidjson/cursorstreamwrapper.h b/include/rapidjson/cursorstreamwrapper.h new file mode 100644 index 0000000..5c752af --- /dev/null +++ b/include/rapidjson/cursorstreamwrapper.h @@ -0,0 +1,59 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_CURSORSTREAMWRAPPER_H_ +#define RAPIDJSON_CURSORSTREAMWRAPPER_H_ + +#include "stream.h" + +RAPIDJSON_NAMESPACE_BEGIN + + +//! Cursor stream wrapper for counting line and column number if error exists. +/*! + \tparam InputStream Any stream that implements Stream Concept +*/ +template > +class CursorStreamWrapper : public GenericStreamWrapper { +public: + typedef typename Encoding::Ch Ch; + + CursorStreamWrapper(InputStream& is): + GenericStreamWrapper(is), line_(1), col_(0) {} + + // counting line and column number + Ch Take() { + Ch ch = this->is_.Take(); + if(ch == '\n') { + line_ ++; + col_ = 0; + } else { + col_ ++; + } + return ch; + } + + //! Get the error line number, if error exists. + size_t GetLine() const { return line_; } + //! Get the error column number, if error exists. + size_t GetColumn() const { return col_; } + +private: + size_t line_; //!< Current Line + size_t col_; //!< Current Column +}; + +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_CURSORSTREAMWRAPPER_H_ diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h index de65740..93b091f 100644 --- a/include/rapidjson/document.h +++ b/include/rapidjson/document.h @@ -2219,17 +2219,14 @@ public: \return The document itself for fluent API. */ template - GenericDocument& ParseStream(InputStream& is_) { + GenericDocument& ParseStream(InputStream& is) { GenericReader reader( stack_.HasAllocator() ? &stack_.GetAllocator() : 0); ClearStackOnExit scope(*this); - GenericStreamWrapper is(is_); parseResult_ = reader.template Parse(is, *this); if (parseResult_) { RAPIDJSON_ASSERT(stack_.GetSize() == sizeof(ValueType)); // Got one and only one root object ValueType::operator=(*stack_.template Pop(1));// Move value from stack to document - } else { - parseResult_.SetPos(is.line_, is.col_); } return *this; } @@ -2358,12 +2355,6 @@ public: //! Get the position of last parsing error in input, 0 otherwise. size_t GetErrorOffset() const { return parseResult_.Offset(); } - - //! Get the position of last parsing error in input, 0 otherwise. - size_t GetErrorLine() const { return parseResult_.Line(); } - - //! Get the position of last parsing error in input, 0 otherwise. - size_t GetErrorColumn() const { return parseResult_.Col(); } //! Implicit conversion to get the last parse result #ifndef __clang // -Wdocumentation diff --git a/include/rapidjson/error/error.h b/include/rapidjson/error/error.h index 618a6cf..9311d2f 100644 --- a/include/rapidjson/error/error.h +++ b/include/rapidjson/error/error.h @@ -108,18 +108,14 @@ struct ParseResult { typedef bool (ParseResult::*BooleanType)() const; public: //! Default constructor, no error. - ParseResult() : code_(kParseErrorNone), offset_(0), line_(0), col_(0) {} + ParseResult() : code_(kParseErrorNone), offset_(0) {} //! Constructor to set an error. - ParseResult(ParseErrorCode code, size_t offset) : code_(code), offset_(offset), line_(0), col_(0) {} + ParseResult(ParseErrorCode code, size_t offset) : code_(code), offset_(offset) {} //! Get the error code. ParseErrorCode Code() const { return code_; } //! Get the error offset, if \ref IsError(), 0 otherwise. size_t Offset() const { return offset_; } - //! Get the position of line number if error exists. - size_t Line() const { return line_; } - //! Get the position of column number if error exists. - size_t Col() const { return col_; } //! Explicit conversion to \c bool, returns \c true, iff !\ref IsError(). operator BooleanType() const { return !IsError() ? &ParseResult::IsError : NULL; } @@ -138,14 +134,10 @@ public: void Clear() { Set(kParseErrorNone); } //! Update error code and offset. void Set(ParseErrorCode code, size_t offset = 0) { code_ = code; offset_ = offset; } - //! Update line number and column number of the error position - void SetPos(size_t line, size_t col) { line_ = line; col_ = col; } private: ParseErrorCode code_; size_t offset_; - size_t line_; - size_t col_; }; //! Function pointer type of GetParseError(). diff --git a/include/rapidjson/stream.h b/include/rapidjson/stream.h index 556c30a..f492797 100644 --- a/include/rapidjson/stream.h +++ b/include/rapidjson/stream.h @@ -104,40 +104,28 @@ inline void PutN(Stream& stream, Ch c, size_t n) { // GenericStreamWrapper //! A Stream Wrapper -/*! \tThis string stream is designed for counting line and column number - \tof the error (if exists) position, while just forwarding any received - \tmessage to the origin stream. +/*! \tThis string stream is a wrapper for any stream by just forwarding any + \treceived message to the origin stream. \note implements Stream concept */ -#if defined(_MSC_VER) && _MSC_VER <= 1700 +#if defined(_MSC_VER) && _MSC_VER <= 1800 RAPIDJSON_DIAG_PUSH -RAPIDJSON_DIAG_OFF(4702) // disable unreachable code +RAPIDJSON_DIAG_OFF(4702) // unreachable code +RAPIDJSON_DIAG_OFF(4512) // assignment operator could not be generated #endif -template +template > class GenericStreamWrapper { public: typedef typename Encoding::Ch Ch; size_t line_; size_t col_; - GenericStreamWrapper(InputStream& is): line_(1), col_(0), is_(is) {} + GenericStreamWrapper(InputStream& is): is_(is) {} Ch Peek() const { return is_.Peek(); } - - // counting line and column number - Ch Take() { - Ch ch = is_.Take(); - if(ch == '\n') { - line_ ++; - col_ = 0; - } else { - col_ ++; - } - return ch; - } + Ch Take() { return is_.Take(); } size_t Tell() { return is_.Tell(); } - Ch* PutBegin() { return is_.PutBegin(); } void Put(Ch ch) { is_.Put(ch); } void Flush() { is_.Flush(); } @@ -150,16 +138,11 @@ public: UTFType GetType() const { return is_.GetType(); } bool HasBOM() const { return is_.HasBOM(); } -private: +protected: InputStream& is_; - - // elimante vs2010-2013 C4512 warning by - // prohibiting copy constructor & assignment operator. - GenericStreamWrapper& operator=(const GenericStreamWrapper &); - GenericStreamWrapper(const GenericStreamWrapper&); }; -#if defined(_MSC_VER) && _MSC_VER <= 1700 +#if defined(_MSC_VER) && _MSC_VER <= 1800 RAPIDJSON_DIAG_POP #endif From 66541b8926c349cea4bee16630a3d38693da4588 Mon Sep 17 00:00:00 2001 From: KaitoHH Date: Fri, 29 Sep 2017 17:24:07 +0800 Subject: [PATCH 278/305] add unit test for cursorstreamwrapper --- test/unittest/CMakeLists.txt | 1 + test/unittest/cursorstreamwrappertest.cpp | 115 ++++++++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 test/unittest/cursorstreamwrappertest.cpp diff --git a/test/unittest/CMakeLists.txt b/test/unittest/CMakeLists.txt index fdf0ad0..072b7b1 100644 --- a/test/unittest/CMakeLists.txt +++ b/test/unittest/CMakeLists.txt @@ -3,6 +3,7 @@ include(CheckCXXCompilerFlag) set(UNITTEST_SOURCES allocatorstest.cpp bigintegertest.cpp + cursorstreamwrappertest.cpp documenttest.cpp dtoatest.cpp encodedstreamtest.cpp diff --git a/test/unittest/cursorstreamwrappertest.cpp b/test/unittest/cursorstreamwrappertest.cpp new file mode 100644 index 0000000..a116248 --- /dev/null +++ b/test/unittest/cursorstreamwrappertest.cpp @@ -0,0 +1,115 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#include "unittest.h" +#include "rapidjson/document.h" +#include "rapidjson/cursorstreamwrapper.h" + +using namespace rapidjson; + +// static const char json[] = "{\"string\"\n\n:\"my string\",\"array\"\n:[\"1\", \"2\", \"3\"]}"; + +bool testJson(const char *json, size_t &line, size_t &col) { + StringStream ss(json); + CursorStreamWrapper csw(ss); + Document document; + document.ParseStream(csw); + bool ret = document.HasParseError(); + if (ret) { + col = csw.GetColumn(); + line = csw.GetLine(); + } + return ret; +} + +TEST(CursorStreamWrapper, MissingFirstBracket) { + const char json[] = "\"string\"\n\n:\"my string\",\"array\"\n:[\"1\", \"2\", \"3\"]}"; + size_t col, line; + bool ret = testJson(json, line, col); + EXPECT_TRUE(ret); + EXPECT_EQ(line, 3); + EXPECT_EQ(col, 0); +} + +TEST(CursorStreamWrapper, MissingQuotes) { + const char json[] = "{\"string\n\n:\"my string\",\"array\"\n:[\"1\", \"2\", \"3\"]}"; + size_t col, line; + bool ret = testJson(json, line, col); + EXPECT_TRUE(ret); + EXPECT_EQ(line, 1); + EXPECT_EQ(col, 8); +} + +TEST(CursorStreamWrapper, MissingColon) { + const char json[] = "{\"string\"\n\n\"my string\",\"array\"\n:[\"1\", \"2\", \"3\"]}"; + size_t col, line; + bool ret = testJson(json, line, col); + EXPECT_TRUE(ret); + EXPECT_EQ(line, 3); + EXPECT_EQ(col, 0); +} + +TEST(CursorStreamWrapper, MissingSecondQuotes) { + const char json[] = "{\"string\"\n\n:my string\",\"array\"\n:[\"1\", \"2\", \"3\"]}"; + size_t col, line; + bool ret = testJson(json, line, col); + EXPECT_TRUE(ret); + EXPECT_EQ(line, 3); + EXPECT_EQ(col, 1); +} + +TEST(CursorStreamWrapper, MissingComma) { + const char json[] = "{\"string\"\n\n:\"my string\"\"array\"\n:[\"1\", \"2\", \"3\"]}"; + size_t col, line; + bool ret = testJson(json, line, col); + EXPECT_TRUE(ret); + EXPECT_EQ(line, 3); + EXPECT_EQ(col, 12); +} + +TEST(CursorStreamWrapper, MissingArrayBracket) { + const char json[] = "{\"string\"\n\n:\"my string\",\"array\"\n:\"1\", \"2\", \"3\"]}"; + size_t col, line; + bool ret = testJson(json, line, col); + EXPECT_TRUE(ret); + EXPECT_EQ(line, 4); + EXPECT_EQ(col, 9); +} + +TEST(CursorStreamWrapper, MissingArrayComma) { + const char json[] = "{\"string\"\n\n:\"my string\",\"array\"\n:[\"1\" \"2\", \"3\"]}"; + size_t col, line; + bool ret = testJson(json, line, col); + EXPECT_TRUE(ret); + EXPECT_EQ(line, 4); + EXPECT_EQ(col, 6); +} + +TEST(CursorStreamWrapper, MissingLastArrayBracket) { + const char json8[] = "{\"string\"\n\n:\"my string\",\"array\"\n:[\"1\", \"2\", \"3\"}"; + size_t col, line; + bool ret = testJson(json8, line, col); + EXPECT_TRUE(ret); + EXPECT_EQ(line, 4); + EXPECT_EQ(col, 15); +} + +TEST(CursorStreamWrapper, MissingLastBracket) { + const char json9[] = "{\"string\"\n\n:\"my string\",\"array\"\n:[\"1\", \"2\", \"3\"]"; + size_t col, line; + bool ret = testJson(json9, line, col); + EXPECT_TRUE(ret); + EXPECT_EQ(line, 4); + EXPECT_EQ(col, 16); +} From 9394b84440fcbf4c2db80049e46c36a78bda04b8 Mon Sep 17 00:00:00 2001 From: KaitoHH Date: Fri, 29 Sep 2017 18:19:41 +0800 Subject: [PATCH 279/305] remove unnecessary code --- include/rapidjson/stream.h | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/include/rapidjson/stream.h b/include/rapidjson/stream.h index f492797..7f2643e 100644 --- a/include/rapidjson/stream.h +++ b/include/rapidjson/stream.h @@ -1,5 +1,5 @@ // Tencent is pleased to support the open source community by making RapidJSON available. -// +// // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. // // Licensed under the MIT License (the "License"); you may not use this file except @@ -7,9 +7,9 @@ // // http://opensource.org/licenses/MIT // -// Unless required by applicable law or agreed to in writing, software distributed -// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR -// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the // specific language governing permissions and limitations under the License. #include "rapidjson.h" @@ -104,7 +104,7 @@ inline void PutN(Stream& stream, Ch c, size_t n) { // GenericStreamWrapper //! A Stream Wrapper -/*! \tThis string stream is a wrapper for any stream by just forwarding any +/*! \tThis string stream is a wrapper for any stream by just forwarding any \treceived message to the origin stream. \note implements Stream concept */ @@ -119,10 +119,8 @@ template > class GenericStreamWrapper { public: typedef typename Encoding::Ch Ch; - size_t line_; - size_t col_; GenericStreamWrapper(InputStream& is): is_(is) {} - + Ch Peek() const { return is_.Peek(); } Ch Take() { return is_.Take(); } size_t Tell() { return is_.Tell(); } @@ -130,10 +128,10 @@ public: void Put(Ch ch) { is_.Put(ch); } void Flush() { is_.Flush(); } size_t PutEnd(Ch* ch) { return is_.PutEnd(ch); } - + // wrapper for MemoryStream const Ch* Peek4() const { return is_.Peek4(); } - + // wrapper for AutoUTFInputStream UTFType GetType() const { return is_.GetType(); } bool HasBOM() const { return is_.HasBOM(); } From 473553bd5ae255217d4176666bff604faa464826 Mon Sep 17 00:00:00 2001 From: KaitoHH Date: Fri, 29 Sep 2017 19:13:29 +0800 Subject: [PATCH 280/305] fix gcc & cl warning --- include/rapidjson/cursorstreamwrapper.h | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/include/rapidjson/cursorstreamwrapper.h b/include/rapidjson/cursorstreamwrapper.h index 5c752af..52c11a7 100644 --- a/include/rapidjson/cursorstreamwrapper.h +++ b/include/rapidjson/cursorstreamwrapper.h @@ -17,6 +17,17 @@ #include "stream.h" +#if defined(__GNUC__) +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(effc++) +#endif + +#if defined(_MSC_VER) && _MSC_VER <= 1800 +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(4702) // unreachable code +RAPIDJSON_DIAG_OFF(4512) // assignment operator could not be generated +#endif + RAPIDJSON_NAMESPACE_BEGIN @@ -29,9 +40,9 @@ class CursorStreamWrapper : public GenericStreamWrapper { public: typedef typename Encoding::Ch Ch; - CursorStreamWrapper(InputStream& is): + CursorStreamWrapper(InputStream& is): GenericStreamWrapper(is), line_(1), col_(0) {} - + // counting line and column number Ch Take() { Ch ch = this->is_.Take(); @@ -54,6 +65,14 @@ private: size_t col_; //!< Current Column }; +#if defined(_MSC_VER) && _MSC_VER <= 1800 +RAPIDJSON_DIAG_POP +#endif + +#if defined(__GNUC__) +RAPIDJSON_DIAG_POP +#endif + RAPIDJSON_NAMESPACE_END #endif // RAPIDJSON_CURSORSTREAMWRAPPER_H_ From 84ca485e51af4fec2ab121c7ddb597dd0f7b3c76 Mon Sep 17 00:00:00 2001 From: Captain Crutches Date: Mon, 2 Oct 2017 20:39:40 -0400 Subject: [PATCH 281/305] Make RapidJSON_INCLUDE_DIR non-blank in Config.cmake --- CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3ccc374..ac1fc25 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -181,6 +181,8 @@ EXPORT( PACKAGE ${PROJECT_NAME} ) # ... for the build tree SET( CONFIG_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}) SET( CONFIG_DIR ${CMAKE_CURRENT_BINARY_DIR}) +SET( ${PROJECT_NAME}_INCLUDE_DIR "\${${PROJECT_NAME}_CMAKE_DIR}/include" ) + CONFIGURE_FILE( ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}Config.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake @ONLY ) CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}ConfigVersion.cmake.in From f0391747e608555b3f70ae6b9c902c5082e80907 Mon Sep 17 00:00:00 2001 From: David Newman Date: Tue, 3 Oct 2017 23:26:19 -0400 Subject: [PATCH 282/305] chore: correct spelling --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index b833a98..78c9540 100644 --- a/readme.md +++ b/readme.md @@ -43,7 +43,7 @@ RapidJSON is a JSON parser and generator for C++. It was inspired by [RapidXml]( More features can be read [here](doc/features.md). -JSON(JavaScript Object Notation) is a light-weight data exchange format. RapidJSON should be in fully compliance with RFC7159/ECMA-404, with optional support of relaxed syntax. More information about JSON can be obtained at +JSON(JavaScript Object Notation) is a light-weight data exchange format. RapidJSON should be in full compliance with RFC7159/ECMA-404, with optional support of relaxed syntax. More information about JSON can be obtained at * [Introducing JSON](http://json.org/) * [RFC7159: The JavaScript Object Notation (JSON) Data Interchange Format](https://tools.ietf.org/html/rfc7159) * [Standard ECMA-404: The JSON Data Interchange Format](https://www.ecma-international.org/publications/standards/Ecma-404.htm) From 6e08e2942509389dbeec137c6079c464b92ba646 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20Dupuis?= Date: Thu, 5 Oct 2017 11:39:21 +0200 Subject: [PATCH 283/305] Initialized regex with schema allocator. --- include/rapidjson/schema.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/rapidjson/schema.h b/include/rapidjson/schema.h index abcf1a1..d884064 100644 --- a/include/rapidjson/schema.h +++ b/include/rapidjson/schema.h @@ -935,7 +935,7 @@ private: }; #if RAPIDJSON_SCHEMA_USE_INTERNALREGEX - typedef internal::GenericRegex RegexType; + typedef internal::GenericRegex RegexType; #elif RAPIDJSON_SCHEMA_USE_STDREGEX typedef std::basic_regex RegexType; #else @@ -995,7 +995,7 @@ private: template RegexType* CreatePattern(const ValueType& value) { if (value.IsString()) { - RegexType* r = new (allocator_->Malloc(sizeof(RegexType))) RegexType(value.GetString()); + RegexType* r = new (allocator_->Malloc(sizeof(RegexType))) RegexType(value.GetString(), allocator_); if (!r->IsValid()) { r->~RegexType(); AllocatorType::Free(r); From b217cc640c6afeadab23e30dbb588245a4cbfde3 Mon Sep 17 00:00:00 2001 From: piotr-kaminski-intel <32583365+piotr-kaminski-intel@users.noreply.github.com> Date: Sat, 7 Oct 2017 00:50:55 +0200 Subject: [PATCH 284/305] Removing Klocwork issues from schema.h Removing Klocwork static code analysis critical issues: line 358: 'this->notValidatorIndex_' might not be initialized in this constructor. line :412 Pointer 'schemaDocument' checked for NULL at line 412 may be passed to function and may be dereferenced there by passing argument this to function 'CreateSchema' at line 419. Also there are 7 similar errors on lines 467 479 511 523 533 538 549. --- include/rapidjson/schema.h | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/include/rapidjson/schema.h b/include/rapidjson/schema.h index abcf1a1..2c5def1 100644 --- a/include/rapidjson/schema.h +++ b/include/rapidjson/schema.h @@ -355,6 +355,7 @@ public: not_(), type_((1 << kTotalSchemaType) - 1), // typeless validatorCount_(), + notValidatorIndex_(), properties_(), additionalPropertiesSchema_(), patternProperties_(), @@ -409,11 +410,9 @@ public: } } - if (schemaDocument) { - AssignIfExist(allOf_, *schemaDocument, p, value, GetAllOfString(), document); - AssignIfExist(anyOf_, *schemaDocument, p, value, GetAnyOfString(), document); - AssignIfExist(oneOf_, *schemaDocument, p, value, GetOneOfString(), document); - } + AssignIfExist(allOf_, *schemaDocument, p, value, GetAllOfString(), document); + AssignIfExist(anyOf_, *schemaDocument, p, value, GetAnyOfString(), document); + AssignIfExist(oneOf_, *schemaDocument, p, value, GetOneOfString(), document); if (const ValueType* v = GetMember(value, GetNotString())) { schemaDocument->CreateSchema(¬_, p.Append(GetNotString(), allocator_), *v, document); From f64b77300735c1648fc695ef5df321b693f4e14b Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Mon, 9 Oct 2017 11:33:48 +0800 Subject: [PATCH 285/305] Partially fix #1077 --- include/rapidjson/encodings.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/rapidjson/encodings.h b/include/rapidjson/encodings.h index 0df1c34..7903e76 100644 --- a/include/rapidjson/encodings.h +++ b/include/rapidjson/encodings.h @@ -384,7 +384,7 @@ struct UTF16BE : UTF16 { static CharType Take(InputByteStream& is) { RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); unsigned c = static_cast(static_cast(is.Take())) << 8; - c |= static_cast(is.Take()); + c |= static_cast(static_cast(is.Take())); return static_cast(c); } From 495266271fb12b6d3ed0a9cf3e251e8b240a6d87 Mon Sep 17 00:00:00 2001 From: Captain Crutches Date: Sun, 8 Oct 2017 23:43:18 -0400 Subject: [PATCH 286/305] Use SOURCE_DIR instead of CMAKE_DIR for build tree --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ac1fc25..846828f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -181,7 +181,7 @@ EXPORT( PACKAGE ${PROJECT_NAME} ) # ... for the build tree SET( CONFIG_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}) SET( CONFIG_DIR ${CMAKE_CURRENT_BINARY_DIR}) -SET( ${PROJECT_NAME}_INCLUDE_DIR "\${${PROJECT_NAME}_CMAKE_DIR}/include" ) +SET( ${PROJECT_NAME}_INCLUDE_DIR "\${${PROJECT_NAME}_SOURCE_DIR}/include" ) CONFIGURE_FILE( ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}Config.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake @ONLY ) From bb99ccb0309bb1cd5b3650af8ee6336325813040 Mon Sep 17 00:00:00 2001 From: piotr-kaminski-intel <32583365+piotr-kaminski-intel@users.noreply.github.com> Date: Tue, 10 Oct 2017 14:09:23 +0200 Subject: [PATCH 287/305] Init variable in the constructor line 358: 'this->notValidatorIndex_' might not be initialized in this constructor. --- include/rapidjson/schema.h | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/include/rapidjson/schema.h b/include/rapidjson/schema.h index 2c5def1..cbcb550 100644 --- a/include/rapidjson/schema.h +++ b/include/rapidjson/schema.h @@ -410,9 +410,11 @@ public: } } - AssignIfExist(allOf_, *schemaDocument, p, value, GetAllOfString(), document); - AssignIfExist(anyOf_, *schemaDocument, p, value, GetAnyOfString(), document); - AssignIfExist(oneOf_, *schemaDocument, p, value, GetOneOfString(), document); + if (schemaDocument) { + AssignIfExist(allOf_, *schemaDocument, p, value, GetAllOfString(), document); + AssignIfExist(anyOf_, *schemaDocument, p, value, GetAnyOfString(), document); + AssignIfExist(oneOf_, *schemaDocument, p, value, GetOneOfString(), document); + } if (const ValueType* v = GetMember(value, GetNotString())) { schemaDocument->CreateSchema(¬_, p.Append(GetNotString(), allocator_), *v, document); From 7bd9b5a1adeb24724ae1e0f1cf048d3f3ba09d85 Mon Sep 17 00:00:00 2001 From: "M.Tayel" <32839716+m-tayel@users.noreply.github.com> Date: Mon, 16 Oct 2017 15:01:27 +0200 Subject: [PATCH 288/305] enable cross compiling by adding option to remove -march/-cpu --- CMakeLists.txt | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 846828f..950d115 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,6 +35,8 @@ option(RAPIDJSON_BUILD_CXX11 "Build rapidjson with C++11 (gcc/clang)" ON) option(RAPIDJSON_BUILD_ASAN "Build rapidjson with address sanitizer (gcc/clang)" OFF) option(RAPIDJSON_BUILD_UBSAN "Build rapidjson with undefined behavior sanitizer (gcc/clang)" OFF) +option(RAPIDJSON_ENABLE_INSTRUMENTATION_OPT "Build rapidjson with -march or -mcpu options" ON) + option(RAPIDJSON_HAS_STDSTRING "" OFF) if(RAPIDJSON_HAS_STDSTRING) add_definitions(-DRAPIDJSON_HAS_STDSTRING) @@ -50,11 +52,13 @@ if(CCACHE_FOUND) endif(CCACHE_FOUND) if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") - if("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "powerpc" OR "${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "ppc64" OR "${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "ppc64le") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mcpu=native") - else() - #FIXME: x86 is -march=native, but doesn't mean every arch is this option. To keep original project's compatibility, I leave this except POWER. - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native") + if(${RAPIDJSON_DISABLE_INSTRUMENTATION_OPT}) + if("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "powerpc" OR "${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "ppc64" OR "${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "ppc64le") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mcpu=native") + else() + #FIXME: x86 is -march=native, but doesn't mean every arch is this option. To keep original project's compatibility, I leave this except POWER. + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native") + endif() endif() set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Werror") set(EXTRA_CXX_FLAGS -Weffc++ -Wswitch-default -Wfloat-equal -Wconversion -Wsign-conversion) From a8e99906039b62ababdab1e709f7e27ccccee5e0 Mon Sep 17 00:00:00 2001 From: h46incon Date: Thu, 19 Oct 2017 20:41:27 +0800 Subject: [PATCH 289/305] Add MemberCapacity() and MemberReserve() interface for object type. --- include/rapidjson/document.h | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h index 93b091f..094a07e 100644 --- a/include/rapidjson/document.h +++ b/include/rapidjson/document.h @@ -1070,6 +1070,9 @@ public: //! Get the number of members in the object. SizeType MemberCount() const { RAPIDJSON_ASSERT(IsObject()); return data_.o.size; } + //! Get the capacity of object. + SizeType MemberCapacity() const { RAPIDJSON_ASSERT(IsObject()); return data_.o.capacity; } + //! Check whether the object is empty. bool ObjectEmpty() const { RAPIDJSON_ASSERT(IsObject()); return data_.o.size == 0; } @@ -1138,6 +1141,21 @@ public: /*! \pre IsObject() == true */ MemberIterator MemberEnd() { RAPIDJSON_ASSERT(IsObject()); return MemberIterator(GetMembersPointer() + data_.o.size); } + //! Request the object to have enough capacity to store members. + /*! \param newCapacity The capacity that the object at least need to have. + \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \note Linear time complexity. + */ + GenericValue& MemberReserve(SizeType newCapacity, Allocator &allocator) { + RAPIDJSON_ASSERT(IsObject()); + if (newCapacity > data_.o.capacity) { + SetMembersPointer(reinterpret_cast(allocator.Realloc(GetMembersPointer(), data_.o.capacity * sizeof(Member), newCapacity * sizeof(Member)))); + data_.o.capacity = newCapacity; + } + return *this; + } + //! Check whether a member exists in the object. /*! \param name Member name to be searched. @@ -1243,17 +1261,8 @@ public: RAPIDJSON_ASSERT(name.IsString()); ObjectData& o = data_.o; - if (o.size >= o.capacity) { - if (o.capacity == 0) { - o.capacity = kDefaultObjectCapacity; - SetMembersPointer(reinterpret_cast(allocator.Malloc(o.capacity * sizeof(Member)))); - } - else { - SizeType oldCapacity = o.capacity; - o.capacity += (oldCapacity + 1) / 2; // grow by factor 1.5 - SetMembersPointer(reinterpret_cast(allocator.Realloc(GetMembersPointer(), oldCapacity * sizeof(Member), o.capacity * sizeof(Member)))); - } - } + if (o.size >= o.capacity) + MemberReserve(o.capacity == 0 ? kDefaultObjectCapacity : (o.capacity + (o.capacity + 1) / 2), allocator); Member* members = GetMembersPointer(); members[o.size].name.RawAssign(name); members[o.size].value.RawAssign(value); @@ -2548,6 +2557,7 @@ public: ~GenericObject() {} SizeType MemberCount() const { return value_.MemberCount(); } + SizeType MemberCapacity() const { return value_.MemberCapacity(); } bool ObjectEmpty() const { return value_.ObjectEmpty(); } template ValueType& operator[](T* name) const { return value_[name]; } template ValueType& operator[](const GenericValue& name) const { return value_[name]; } @@ -2556,6 +2566,7 @@ public: #endif MemberIterator MemberBegin() const { return value_.MemberBegin(); } MemberIterator MemberEnd() const { return value_.MemberEnd(); } + GenericObject MemberReserve(SizeType newCapacity, AllocatorType &allocator) const { value_.MemberReserve(newCapacity, allocator); return *this; } bool HasMember(const Ch* name) const { return value_.HasMember(name); } #if RAPIDJSON_HAS_STDSTRING bool HasMember(const std::basic_string& name) const { return value_.HasMember(name); } From db305dcf217ea1dd009a1355835932de6d866cb5 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Fri, 20 Oct 2017 10:33:37 +0800 Subject: [PATCH 290/305] Fix schema.md TOC --- doc/schema.md | 18 +++++++++--------- doc/schema.zh-cn.md | 18 +++++++++--------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/doc/schema.md b/doc/schema.md index 29ba4f5..dc01626 100644 --- a/doc/schema.md +++ b/doc/schema.md @@ -8,7 +8,7 @@ RapidJSON implemented a JSON Schema validator for [JSON Schema Draft v4](http:// [TOC] -## Basic Usage +# Basic Usage First of all, you need to parse a JSON Schema into `Document`, and then compile the `Document` into a `SchemaDocument`. @@ -52,11 +52,11 @@ Some notes: * One `SchemaDocment` can be referenced by multiple `SchemaValidator`s. It will not be modified by `SchemaValidator`s. * A `SchemaValidator` may be reused to validate multiple documents. To run it for other documents, call `validator.Reset()` first. -## Validation during parsing/serialization +# Validation during parsing/serialization Unlike most JSON Schema validator implementations, RapidJSON provides a SAX-based schema validator. Therefore, you can parse a JSON from a stream while validating it on the fly. If the validator encounters a JSON value that invalidates the supplied schema, the parsing will be terminated immediately. This design is especially useful for parsing large JSON files. -### DOM parsing +## DOM parsing For using DOM in parsing, `Document` needs some preparation and finalizing tasks, in addition to receiving SAX events, thus it needs some work to route the reader, validator and the document. `SchemaValidatingReader` is a helper class that doing such work. @@ -97,7 +97,7 @@ if (!reader.GetParseResult()) { } ~~~ -### SAX parsing +## SAX parsing For using SAX in parsing, it is much simpler. If it only need to validate the JSON without further processing, it is simply: @@ -126,7 +126,7 @@ if (!reader.Parse(ss, validator)) { } ~~~ -### Serialization +## Serialization It is also possible to do validation during serializing. This can ensure the result JSON is valid according to the JSON schema. @@ -144,7 +144,7 @@ if (!d.Accept(validator)) { Of course, if your application only needs SAX-style serialization, it can simply send SAX events to `SchemaValidator` instead of `Writer`. -## Remote Schema +# Remote Schema JSON Schema supports [`$ref` keyword](http://spacetelescope.github.io/understanding-json-schema/structuring.html), which is a [JSON pointer](doc/pointer.md) referencing to a local or remote schema. Local pointer is prefixed with `#`, while remote pointer is an relative or absolute URI. For example: @@ -168,7 +168,7 @@ MyRemoteSchemaDocumentProvider provider; SchemaDocument schema(sd, &provider); ~~~ -## Conformance +# Conformance RapidJSON passed 262 out of 263 tests in [JSON Schema Test Suite](https://github.com/json-schema/JSON-Schema-Test-Suite) (Json Schema draft 4). @@ -176,7 +176,7 @@ The failed test is "changed scope ref invalid" of "change resolution scope" in ` Besides, the `format` schema keyword for string values is ignored, since it is not required by the specification. -### Regular Expression +## Regular Expression The schema keyword `pattern` and `patternProperties` uses regular expression to match the required pattern. @@ -211,7 +211,7 @@ RapidJSON implemented a simple NFA regular expression engine, which is used by d For C++11 compiler, it is also possible to use the `std::regex` by defining `RAPIDJSON_SCHEMA_USE_INTERNALREGEX=0` and `RAPIDJSON_SCHEMA_USE_STDREGEX=1`. If your schemas do not need `pattern` and `patternProperties`, you can set both macros to zero to disable this feature, which will reduce some code size. -## Performance +# Performance Most C++ JSON libraries do not yet support JSON Schema. So we tried to evaluate the performance of RapidJSON's JSON Schema validator according to [json-schema-benchmark](https://github.com/ebdrup/json-schema-benchmark), which tests 11 JavaScript libraries running on Node.js. diff --git a/doc/schema.zh-cn.md b/doc/schema.zh-cn.md index 5df1f31..1dd7e79 100644 --- a/doc/schema.zh-cn.md +++ b/doc/schema.zh-cn.md @@ -8,7 +8,7 @@ RapidJSON 实现了一个 [JSON Schema Draft v4](http://json-schema.org/document [TOC] -## 基本用法 +# 基本用法 首先,你要把 JSON Schema 解析成 `Document`,再把它编译成一个 `SchemaDocument`。 @@ -52,11 +52,11 @@ if (!d.Accept(validator)) { * 一个 `SchemaDocment` 能被多个 `SchemaValidator` 引用。它不会被 `SchemaValidator` 修改。 * 可以重复使用一个 `SchemaValidator` 来校验多个文件。在校验其他文件前,须先调用 `validator.Reset()`。 -## 在解析/生成时进行校验 +# 在解析/生成时进行校验 与大部分 JSON Schema 校验器有所不同,RapidJSON 提供了一个基于 SAX 的 schema 校验器实现。因此,你可以在输入流解析 JSON 的同时进行校验。若校验器遇到一个与 schema 不符的值,就会立即终止解析。这设计对于解析大型 JSON 文件时特别有用。 -### DOM 解析 +## DOM 解析 在使用 DOM 进行解析时,`Document` 除了接收 SAX 事件外,还需做一些准备及结束工作,因此,为了连接 `Reader`、`SchemaValidator` 和 `Document` 要做多一点事情。`SchemaValidatingReader` 是一个辅助类去做那些工作。 @@ -97,7 +97,7 @@ if (!reader.GetParseResult()) { } ~~~ -### SAX 解析 +## SAX 解析 使用 SAX 解析时,情况就简单得多。若只需要校验 JSON 而无需进一步处理,那么仅需要: @@ -126,7 +126,7 @@ if (!reader.Parse(ss, validator)) { } ~~~ -### 生成 +## 生成 我们也可以在生成(serialization)的时候进行校验。这能确保输出的 JSON 符合一个 JSON Schema。 @@ -144,7 +144,7 @@ if (!d.Accept(validator)) { 当然,如果你的应用仅需要 SAX 风格的生成,那么只需要把 SAX 事件由原来发送到 `Writer`,改为发送到 `SchemaValidator`。 -## 远程 Schema +# 远程 Schema JSON Schema 支持 [`$ref` 关键字](http://spacetelescope.github.io/understanding-json-schema/structuring.html),它是一个 [JSON pointer](doc/pointer.zh-cn.md) 引用至一个本地(local)或远程(remote) schema。本地指针的首字符是 `#`,而远程指针是一个相对或绝对 URI。例如: @@ -168,7 +168,7 @@ MyRemoteSchemaDocumentProvider provider; SchemaDocument schema(sd, &provider); ~~~ -## 标准的符合程度 +# 标准的符合程度 RapidJSON 通过了 [JSON Schema Test Suite](https://github.com/json-schema/JSON-Schema-Test-Suite) (Json Schema draft 4) 中 263 个测试的 262 个。 @@ -176,7 +176,7 @@ RapidJSON 通过了 [JSON Schema Test Suite](https://github.com/json-schema/JSON 除此以外,关于字符串类型的 `format` schema 关键字也会被忽略,因为标准中并没需求必须实现。 -### 正则表达式 +## 正则表达式 `pattern` 及 `patternProperties` 这两个 schema 关键字使用了正则表达式去匹配所需的模式。 @@ -211,7 +211,7 @@ RapidJSON 实现了一个简单的 NFA 正则表达式引擎,并预设使用 对于使用 C++11 编译器的使用者,也可使用 `std::regex`,只需定义 `RAPIDJSON_SCHEMA_USE_INTERNALREGEX=0` 及 `RAPIDJSON_SCHEMA_USE_STDREGEX=1`。若你的 schema 无需使用 `pattern` 或 `patternProperties`,可以把两个宏都设为零,以禁用此功能,这样做可节省一些代码体积。 -## 性能 +# 性能 大部分 C++ JSON 库都未支持 JSON Schema。因此我们尝试按照 [json-schema-benchmark](https://github.com/ebdrup/json-schema-benchmark) 去评估 RapidJSON 的 JSON Schema 校验器。该评测测试了 11 个运行在 node.js 上的 JavaScript 库。 From 3c07cecdb85aa3b3a680402bc7c4a19bb942185d Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Fri, 20 Oct 2017 11:16:44 +0800 Subject: [PATCH 291/305] Add anchors to Schema.md --- doc/schema.md | 18 +++++++++--------- doc/schema.zh-cn.md | 18 +++++++++--------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/doc/schema.md b/doc/schema.md index dc01626..5e396ce 100644 --- a/doc/schema.md +++ b/doc/schema.md @@ -8,7 +8,7 @@ RapidJSON implemented a JSON Schema validator for [JSON Schema Draft v4](http:// [TOC] -# Basic Usage +# Basic Usage {#BasicUsage} First of all, you need to parse a JSON Schema into `Document`, and then compile the `Document` into a `SchemaDocument`. @@ -52,11 +52,11 @@ Some notes: * One `SchemaDocment` can be referenced by multiple `SchemaValidator`s. It will not be modified by `SchemaValidator`s. * A `SchemaValidator` may be reused to validate multiple documents. To run it for other documents, call `validator.Reset()` first. -# Validation during parsing/serialization +# Validation during parsing/serialization {#ParsingSerialization} Unlike most JSON Schema validator implementations, RapidJSON provides a SAX-based schema validator. Therefore, you can parse a JSON from a stream while validating it on the fly. If the validator encounters a JSON value that invalidates the supplied schema, the parsing will be terminated immediately. This design is especially useful for parsing large JSON files. -## DOM parsing +## DOM parsing {#DomParsing} For using DOM in parsing, `Document` needs some preparation and finalizing tasks, in addition to receiving SAX events, thus it needs some work to route the reader, validator and the document. `SchemaValidatingReader` is a helper class that doing such work. @@ -97,7 +97,7 @@ if (!reader.GetParseResult()) { } ~~~ -## SAX parsing +## SAX parsing {#SaxParsing} For using SAX in parsing, it is much simpler. If it only need to validate the JSON without further processing, it is simply: @@ -126,7 +126,7 @@ if (!reader.Parse(ss, validator)) { } ~~~ -## Serialization +## Serialization {#Serialization} It is also possible to do validation during serializing. This can ensure the result JSON is valid according to the JSON schema. @@ -144,7 +144,7 @@ if (!d.Accept(validator)) { Of course, if your application only needs SAX-style serialization, it can simply send SAX events to `SchemaValidator` instead of `Writer`. -# Remote Schema +# Remote Schema {#RemoteSchema} JSON Schema supports [`$ref` keyword](http://spacetelescope.github.io/understanding-json-schema/structuring.html), which is a [JSON pointer](doc/pointer.md) referencing to a local or remote schema. Local pointer is prefixed with `#`, while remote pointer is an relative or absolute URI. For example: @@ -168,7 +168,7 @@ MyRemoteSchemaDocumentProvider provider; SchemaDocument schema(sd, &provider); ~~~ -# Conformance +# Conformance {#Conformance} RapidJSON passed 262 out of 263 tests in [JSON Schema Test Suite](https://github.com/json-schema/JSON-Schema-Test-Suite) (Json Schema draft 4). @@ -176,7 +176,7 @@ The failed test is "changed scope ref invalid" of "change resolution scope" in ` Besides, the `format` schema keyword for string values is ignored, since it is not required by the specification. -## Regular Expression +## Regular Expression {#RegEx} The schema keyword `pattern` and `patternProperties` uses regular expression to match the required pattern. @@ -211,7 +211,7 @@ RapidJSON implemented a simple NFA regular expression engine, which is used by d For C++11 compiler, it is also possible to use the `std::regex` by defining `RAPIDJSON_SCHEMA_USE_INTERNALREGEX=0` and `RAPIDJSON_SCHEMA_USE_STDREGEX=1`. If your schemas do not need `pattern` and `patternProperties`, you can set both macros to zero to disable this feature, which will reduce some code size. -# Performance +# Performance {#Performance} Most C++ JSON libraries do not yet support JSON Schema. So we tried to evaluate the performance of RapidJSON's JSON Schema validator according to [json-schema-benchmark](https://github.com/ebdrup/json-schema-benchmark), which tests 11 JavaScript libraries running on Node.js. diff --git a/doc/schema.zh-cn.md b/doc/schema.zh-cn.md index 1dd7e79..c85177f 100644 --- a/doc/schema.zh-cn.md +++ b/doc/schema.zh-cn.md @@ -8,7 +8,7 @@ RapidJSON 实现了一个 [JSON Schema Draft v4](http://json-schema.org/document [TOC] -# 基本用法 +# 基本用法 {#BasicUsage} 首先,你要把 JSON Schema 解析成 `Document`,再把它编译成一个 `SchemaDocument`。 @@ -52,11 +52,11 @@ if (!d.Accept(validator)) { * 一个 `SchemaDocment` 能被多个 `SchemaValidator` 引用。它不会被 `SchemaValidator` 修改。 * 可以重复使用一个 `SchemaValidator` 来校验多个文件。在校验其他文件前,须先调用 `validator.Reset()`。 -# 在解析/生成时进行校验 +# 在解析/生成时进行校验 {#ParsingSerialization} 与大部分 JSON Schema 校验器有所不同,RapidJSON 提供了一个基于 SAX 的 schema 校验器实现。因此,你可以在输入流解析 JSON 的同时进行校验。若校验器遇到一个与 schema 不符的值,就会立即终止解析。这设计对于解析大型 JSON 文件时特别有用。 -## DOM 解析 +## DOM 解析 {#DomParsing} 在使用 DOM 进行解析时,`Document` 除了接收 SAX 事件外,还需做一些准备及结束工作,因此,为了连接 `Reader`、`SchemaValidator` 和 `Document` 要做多一点事情。`SchemaValidatingReader` 是一个辅助类去做那些工作。 @@ -97,7 +97,7 @@ if (!reader.GetParseResult()) { } ~~~ -## SAX 解析 +## SAX 解析 {#SaxParsing} 使用 SAX 解析时,情况就简单得多。若只需要校验 JSON 而无需进一步处理,那么仅需要: @@ -126,7 +126,7 @@ if (!reader.Parse(ss, validator)) { } ~~~ -## 生成 +## 生成 {#Serialization} 我们也可以在生成(serialization)的时候进行校验。这能确保输出的 JSON 符合一个 JSON Schema。 @@ -144,7 +144,7 @@ if (!d.Accept(validator)) { 当然,如果你的应用仅需要 SAX 风格的生成,那么只需要把 SAX 事件由原来发送到 `Writer`,改为发送到 `SchemaValidator`。 -# 远程 Schema +# 远程 Schema {#RemoteSchema} JSON Schema 支持 [`$ref` 关键字](http://spacetelescope.github.io/understanding-json-schema/structuring.html),它是一个 [JSON pointer](doc/pointer.zh-cn.md) 引用至一个本地(local)或远程(remote) schema。本地指针的首字符是 `#`,而远程指针是一个相对或绝对 URI。例如: @@ -168,7 +168,7 @@ MyRemoteSchemaDocumentProvider provider; SchemaDocument schema(sd, &provider); ~~~ -# 标准的符合程度 +# 标准的符合程度 {#Conformance} RapidJSON 通过了 [JSON Schema Test Suite](https://github.com/json-schema/JSON-Schema-Test-Suite) (Json Schema draft 4) 中 263 个测试的 262 个。 @@ -176,7 +176,7 @@ RapidJSON 通过了 [JSON Schema Test Suite](https://github.com/json-schema/JSON 除此以外,关于字符串类型的 `format` schema 关键字也会被忽略,因为标准中并没需求必须实现。 -## 正则表达式 +## 正则表达式 {#RegEx} `pattern` 及 `patternProperties` 这两个 schema 关键字使用了正则表达式去匹配所需的模式。 @@ -211,7 +211,7 @@ RapidJSON 实现了一个简单的 NFA 正则表达式引擎,并预设使用 对于使用 C++11 编译器的使用者,也可使用 `std::regex`,只需定义 `RAPIDJSON_SCHEMA_USE_INTERNALREGEX=0` 及 `RAPIDJSON_SCHEMA_USE_STDREGEX=1`。若你的 schema 无需使用 `pattern` 或 `patternProperties`,可以把两个宏都设为零,以禁用此功能,这样做可节省一些代码体积。 -# 性能 +# 性能 {#Performance} 大部分 C++ JSON 库都未支持 JSON Schema。因此我们尝试按照 [json-schema-benchmark](https://github.com/ebdrup/json-schema-benchmark) 去评估 RapidJSON 的 JSON Schema 校验器。该评测测试了 11 个运行在 node.js 上的 JavaScript 库。 From f4b1f761f31352348bf142095643d61e84795dc8 Mon Sep 17 00:00:00 2001 From: "M.Tayel" <32839716+m-tayel@users.noreply.github.com> Date: Tue, 24 Oct 2017 12:25:47 +0200 Subject: [PATCH 292/305] Fixed typo in CMake file --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 950d115..8d69855 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -52,7 +52,7 @@ if(CCACHE_FOUND) endif(CCACHE_FOUND) if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") - if(${RAPIDJSON_DISABLE_INSTRUMENTATION_OPT}) + if(${RAPIDJSON_ENABLE_INSTRUMENTATION_OPT}) if("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "powerpc" OR "${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "ppc64" OR "${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "ppc64le") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mcpu=native") else() From 1be14d04a05fc8bcb7ad2fdb72fb7f71f50a337f Mon Sep 17 00:00:00 2001 From: clach04 Date: Thu, 26 Oct 2017 21:19:54 -0700 Subject: [PATCH 293/305] Fix issue #1104 Solaris compilation errors fread()/fwrite() Explicit std name space for fread() and fwrite(). --- include/rapidjson/filereadstream.h | 2 +- include/rapidjson/filewritestream.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/rapidjson/filereadstream.h b/include/rapidjson/filereadstream.h index b56ea13..f1bfb7d 100644 --- a/include/rapidjson/filereadstream.h +++ b/include/rapidjson/filereadstream.h @@ -68,7 +68,7 @@ private: ++current_; else if (!eof_) { count_ += readCount_; - readCount_ = fread(buffer_, 1, bufferSize_, fp_); + readCount_ = std::fread(buffer_, 1, bufferSize_, fp_); bufferLast_ = buffer_ + readCount_ - 1; current_ = buffer_; diff --git a/include/rapidjson/filewritestream.h b/include/rapidjson/filewritestream.h index 6378dd6..3811f8b 100644 --- a/include/rapidjson/filewritestream.h +++ b/include/rapidjson/filewritestream.h @@ -62,7 +62,7 @@ public: void Flush() { if (current_ != buffer_) { - size_t result = fwrite(buffer_, 1, static_cast(current_ - buffer_), fp_); + size_t result = std::fwrite(buffer_, 1, static_cast(current_ - buffer_), fp_); if (result < static_cast(current_ - buffer_)) { // failure deliberately ignored at this time // added to avoid warn_unused_result build errors From 8684c9960de4e2b028ea0f51a0d5dd8e68ff4345 Mon Sep 17 00:00:00 2001 From: Martin Lindhe Date: Sat, 4 Nov 2017 10:32:02 +0100 Subject: [PATCH 294/305] fix some typos --- CHANGELOG.md | 2 +- doc/dom.md | 2 +- doc/encoding.md | 2 +- doc/faq.md | 16 ++++++++-------- doc/performance.md | 2 +- doc/pointer.md | 2 +- doc/sax.md | 2 +- doc/schema.md | 2 +- doc/tutorial.md | 2 +- 9 files changed, 16 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c9d603c..1c580bd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -140,7 +140,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). * Redo all documentation (English, Simplified Chinese) ### Changed -* Copyright ownership transfered to THL A29 Limited (a Tencent company). +* Copyright ownership transferred to THL A29 Limited (a Tencent company). * Migrating from Premake to CMAKE (#192) * Resolve all warning reports diff --git a/doc/dom.md b/doc/dom.md index 6c541fe..25ffbd2 100644 --- a/doc/dom.md +++ b/doc/dom.md @@ -241,7 +241,7 @@ Some techniques about using DOM API is discussed here. ## DOM as SAX Event Publisher -In RapidJSON, stringifying a DOM with `Writer` may be look a little bit weired. +In RapidJSON, stringifying a DOM with `Writer` may be look a little bit weird. ~~~~~~~~~~cpp // ... diff --git a/doc/encoding.md b/doc/encoding.md index 8f8ff7f..e663aea 100644 --- a/doc/encoding.md +++ b/doc/encoding.md @@ -10,7 +10,7 @@ The earlier [RFC4627](http://www.ietf.org/rfc/rfc4627.txt) stated that, > (in §6) JSON may be represented using UTF-8, UTF-16, or UTF-32. When JSON is written in UTF-8, JSON is 8bit compatible. When JSON is written in UTF-16 or UTF-32, the binary content-transfer-encoding must be used. -RapidJSON supports various encodings. It can also validate the encodings of JSON, and transconding JSON among encodings. All these features are implemented internally, without the need for external libraries (e.g. [ICU](http://site.icu-project.org/)). +RapidJSON supports various encodings. It can also validate the encodings of JSON, and transcoding JSON among encodings. All these features are implemented internally, without the need for external libraries (e.g. [ICU](http://site.icu-project.org/)). [TOC] diff --git a/doc/faq.md b/doc/faq.md index 74d770d..d5697ff 100644 --- a/doc/faq.md +++ b/doc/faq.md @@ -116,7 +116,7 @@ ~~~~~~~~~~cpp Value(kObjectType).Swap(d); ~~~~~~~~~~ - or equivalent, but sightly longer to type: + or equivalent, but slightly longer to type: ~~~~~~~~~~cpp d.Swap(Value(kObjectType).Move()); ~~~~~~~~~~ @@ -140,11 +140,11 @@ } ~~~~~~~~~~ - The most important requirement to take care of document and value life-cycle as well as consistent memory managent using the right allocator during the value transfer. + The most important requirement to take care of document and value life-cycle as well as consistent memory management using the right allocator during the value transfer. Simple yet most efficient way to achieve that is to modify the `address` definition above to initialize it with allocator of the `person` document, then we just add the root member of the value: ~~~~~~~~~~cpp - Documnet address(person.GetAllocator()); + Document address(person.GetAllocator()); ... person["person"].AddMember("address", address["address"], person.GetAllocator()); ~~~~~~~~~~ @@ -174,7 +174,7 @@ Alternatively, if we don't want to explicitly refer to the root value of `addres 3. Why do I need to provide the length of string? - Since C string is null-terminated, the length of string needs to be computed via `strlen()`, with linear runtime complexity. This incurs an unncessary overhead of many operations, if the user already knows the length of string. + Since C string is null-terminated, the length of string needs to be computed via `strlen()`, with linear runtime complexity. This incurs an unnecessary overhead of many operations, if the user already knows the length of string. Also, RapidJSON can handle `\u0000` (null character) within a string. If a string contains null characters, `strlen()` cannot return the true length of it. In such case user must provide the length of string explicitly. @@ -204,7 +204,7 @@ Alternatively, if we don't want to explicitly refer to the root value of `addres 2. Can it validate the encoding? - Yes, just pass `kParseValidateEncodingFlag` to `Parse()`. If there is invalid encoding in the stream, it wil generate `kParseErrorStringInvalidEncoding` error. + Yes, just pass `kParseValidateEncodingFlag` to `Parse()`. If there is invalid encoding in the stream, it will generate `kParseErrorStringInvalidEncoding` error. 3. What is surrogate pair? Does RapidJSON support it? @@ -248,7 +248,7 @@ Alternatively, if we don't want to explicitly refer to the root value of `addres 1. Is RapidJSON really fast? - Yes. It may be the fastest open source JSON library. There is a [benchmark](https://github.com/miloyip/nativejson-benchmark) for evaluating performance of C/C++ JSON libaries. + Yes. It may be the fastest open source JSON library. There is a [benchmark](https://github.com/miloyip/nativejson-benchmark) for evaluating performance of C/C++ JSON libraries. 2. Why is it fast? @@ -262,13 +262,13 @@ Alternatively, if we don't want to explicitly refer to the root value of `addres The design of RapidJSON aims at reducing memory footprint. - In the SAX API, `Reader` consumes memory portional to maximum depth of JSON tree, plus maximum length of JSON string. + In the SAX API, `Reader` consumes memory proportional to maximum depth of JSON tree, plus maximum length of JSON string. In the DOM API, each `Value` consumes exactly 16/24 bytes for 32/64-bit architecture respectively. RapidJSON also uses a special memory allocator to minimize overhead of allocations. 5. What is the purpose of being high performance? - Some applications need to process very large JSON files. Some server-side applications need to process huge amount of JSONs. Being high performance can improve both latency and throuput. In a broad sense, it will also save energy. + Some applications need to process very large JSON files. Some server-side applications need to process huge amount of JSONs. Being high performance can improve both latency and throughput. In a broad sense, it will also save energy. ## Gossip diff --git a/doc/performance.md b/doc/performance.md index 7b18730..6f9e1bf 100644 --- a/doc/performance.md +++ b/doc/performance.md @@ -1,6 +1,6 @@ # Performance -There is a [native JSON benchmark collection] [1] which evaluates speed, memory usage and code size of various operations among 37 JSON libaries. +There is a [native JSON benchmark collection] [1] which evaluates speed, memory usage and code size of various operations among 37 JSON libraries. [1]: https://github.com/miloyip/nativejson-benchmark diff --git a/doc/pointer.md b/doc/pointer.md index b343d78..9a0e5ca 100644 --- a/doc/pointer.md +++ b/doc/pointer.md @@ -211,7 +211,7 @@ p.Stringify(sb); std::cout << sb.GetString() << std::endl; ~~~ -It can also stringify to URI fragment reprsentation by `StringifyUriFragment()`. +It can also stringify to URI fragment representation by `StringifyUriFragment()`. # User-Supplied Tokens {#UserSuppliedTokens} diff --git a/doc/sax.md b/doc/sax.md index 4867880..874361f 100644 --- a/doc/sax.md +++ b/doc/sax.md @@ -126,7 +126,7 @@ When the `Reader` encounters a JSON number, it chooses a suitable C++ type mappi When the `Reader` encounters the beginning of an object, it calls `StartObject()`. An object in JSON is a set of name-value pairs. If the object contains members it first calls `Key()` for the name of member, and then calls functions depending on the type of the value. These calls of name-value pairs repeat until calling `EndObject(SizeType memberCount)`. Note that the `memberCount` parameter is just an aid for the handler; users who do not need this parameter may ignore it. -Arrays are similar to objects, but simpler. At the beginning of an array, the `Reader` calls `BeginArary()`. If there is elements, it calls functions according to the types of element. Similarly, in the last call `EndArray(SizeType elementCount)`, the parameter `elementCount` is just an aid for the handler. +Arrays are similar to objects, but simpler. At the beginning of an array, the `Reader` calls `BeginArray()`. If there is elements, it calls functions according to the types of element. Similarly, in the last call `EndArray(SizeType elementCount)`, the parameter `elementCount` is just an aid for the handler. Every handler function returns a `bool`. Normally it should return `true`. If the handler encounters an error, it can return `false` to notify the event publisher to stop further processing. diff --git a/doc/schema.md b/doc/schema.md index 5e396ce..b454225 100644 --- a/doc/schema.md +++ b/doc/schema.md @@ -49,7 +49,7 @@ if (!d.Accept(validator)) { Some notes: -* One `SchemaDocment` can be referenced by multiple `SchemaValidator`s. It will not be modified by `SchemaValidator`s. +* One `SchemaDocument` can be referenced by multiple `SchemaValidator`s. It will not be modified by `SchemaValidator`s. * A `SchemaValidator` may be reused to validate multiple documents. To run it for other documents, call `validator.Reset()` first. # Validation during parsing/serialization {#ParsingSerialization} diff --git a/doc/tutorial.md b/doc/tutorial.md index 167b81d..3fa63c9 100644 --- a/doc/tutorial.md +++ b/doc/tutorial.md @@ -68,7 +68,7 @@ printf("t = %s\n", document["t"].GetBool() ? "true" : "false"); t = true ~~~~~~~~~~ -JSON null can be queryed with `IsNull()`. +JSON null can be queried with `IsNull()`. ~~~~~~~~~~cpp printf("n = %s\n", document["n"].IsNull() ? "null" : "?"); ~~~~~~~~~~ From 25c1b78f3c905284d7194430f083b0381d7aff83 Mon Sep 17 00:00:00 2001 From: Matthis Thorade Date: Mon, 4 Dec 2017 13:15:01 +0100 Subject: [PATCH 295/305] ignore DS_Store files --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index e7e8fba..1d3073f 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,6 @@ Doxyfile Doxyfile.zh-cn DartConfiguration.tcl *.nupkg + +# Files created by OS +*.DS_Store From 195dc90d27bf6552cab4503cc842b2d3bd3b3828 Mon Sep 17 00:00:00 2001 From: Matthis Thorade Date: Mon, 4 Dec 2017 13:18:51 +0100 Subject: [PATCH 296/305] Delete .DS_Store --- bin/jsonschema/remotes/.DS_Store | Bin 6148 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 bin/jsonschema/remotes/.DS_Store diff --git a/bin/jsonschema/remotes/.DS_Store b/bin/jsonschema/remotes/.DS_Store deleted file mode 100644 index 1d098a4103d67f2b3b336934f3d7f42b462861a1..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHKOHRWu5S@X7Ds|H(?0kjZAgaO%dI3btXOYNNp?e>&D;7NuZ$2PWB6c8zW+eOB z^Ks%A#p59&UhngYXh}qKG(ncZgot|5bmq<%K-M+xX_ue7{;rgMVxhmNl6SwP2P)K4 zrjz#{8T!Z7rYpnNcX53hIFz={`93>*C>7{`CI$;>GS&XK|+FoU?3O>27-Z~ zU;sH=WWF$rJ{SlFf`JbPv%iZPLM Date: Mon, 4 Dec 2017 13:19:05 +0100 Subject: [PATCH 297/305] Delete .DS_Store --- bin/jsonschema/tests/.DS_Store | Bin 6148 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 bin/jsonschema/tests/.DS_Store diff --git a/bin/jsonschema/tests/.DS_Store b/bin/jsonschema/tests/.DS_Store deleted file mode 100644 index dae9b18efac169b4f44a57c7926495a005607a85..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHKJ8r`;3?&nz2$02NM_r*n4j=^133`F1H98qEkT$!goU2Fc<7bHBbW4X8j{x-~ zicf;xV45PL`*Z&!(u&9iZYUQUmS+3r3tMGGfpDDhDZBpTZFn8WVUc}1VB81kpAHP(0stF?-7xoF z0$3~ntcg<~A}|dqFsPa>h6Ww+l6f_83JkhvHXoWdYj!B=x8wZc>7q4|BNdLa}rlnfC~I81+?j&yFH$iwRQ10tF;CG0=JwmxEbb7!QkZ>=;as-E60zX b6nVww*sqCGpwkg|I*>mDrVEV<{I&w$e{~fm From 79d5e236739c72851d66b3d5cb671caaa6585ea1 Mon Sep 17 00:00:00 2001 From: Matthis Thorade Date: Mon, 4 Dec 2017 13:19:14 +0100 Subject: [PATCH 298/305] Delete .DS_Store --- bin/jsonschema/tests/draft4/.DS_Store | Bin 6148 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 bin/jsonschema/tests/draft4/.DS_Store diff --git a/bin/jsonschema/tests/draft4/.DS_Store b/bin/jsonschema/tests/draft4/.DS_Store deleted file mode 100644 index ef142295ea00d1d19fc3d30cc4e82dd207530825..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHK%}T^D5T4N$3SRc8x4go>L0IYo$i9Huf(orExO?vd?#YAC<2OGpmKHn+A~FM+ zFPY3tnh%;}h={j`c0;r#q6$rrrL!PnUYt5}=L;ZfjzTYVPhI=kbPI|8qDj8JqCx}h z=^1$X{)bX@53|YcakFbmKiF=rZcj9aqIv5BBrVO0ha4q-$4St!$ zB7YhZqhKHy_-738s@~OGY|8J}+4khFO=x#$BH}kn2ZH|O5rBc5BUd_U^GW*f%Z{U= TWD&cD1LGl}goFwPeu04xBO^7X From d75bb90a5dd8c5946ce7496748d1cea842aabc0f Mon Sep 17 00:00:00 2001 From: "Philipp A. Hartmann" Date: Tue, 12 Dec 2017 21:16:07 +0100 Subject: [PATCH 299/305] Avoid inheritance from std::iterator Instead of inheriting from the deprecated std::iterator template, define the member typedefs needed for std::iterator_traits directly. Closes #1131. --- include/rapidjson/document.h | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h index 094a07e..eb6d7dc 100644 --- a/include/rapidjson/document.h +++ b/include/rapidjson/document.h @@ -45,7 +45,7 @@ RAPIDJSON_DIAG_OFF(terminate) // ignore throwing RAPIDJSON_ASSERT in RAPIDJSON_N #endif // __GNUC__ #ifndef RAPIDJSON_NOMEMBERITERATORCLASS -#include // std::iterator, std::random_access_iterator_tag +#include // std::random_access_iterator_tag #endif #if RAPIDJSON_HAS_CXX11_RVALUE_REFS @@ -98,16 +98,13 @@ struct GenericMember { \see GenericMember, GenericValue::MemberIterator, GenericValue::ConstMemberIterator */ template -class GenericMemberIterator - : public std::iterator >::Type> { +class GenericMemberIterator { friend class GenericValue; template friend class GenericMemberIterator; typedef GenericMember PlainType; typedef typename internal::MaybeAddConst::Type ValueType; - typedef std::iterator BaseType; public: //! Iterator type itself @@ -117,12 +114,21 @@ public: //! Non-constant iterator type typedef GenericMemberIterator NonConstIterator; + /** \name std::iterator_traits support */ + //@{ + typedef ValueType value_type; + typedef ValueType * pointer; + typedef ValueType & reference; + typedef std::ptrdiff_t difference_type; + typedef std::random_access_iterator_tag iterator_category; + //@} + //! Pointer to (const) GenericMember - typedef typename BaseType::pointer Pointer; + typedef pointer Pointer; //! Reference to (const) GenericMember - typedef typename BaseType::reference Reference; + typedef reference Reference; //! Signed integer type (e.g. \c ptrdiff_t) - typedef typename BaseType::difference_type DifferenceType; + typedef difference_type DifferenceType; //! Default constructor (singular value) /*! Creates an iterator pointing to no element. From f2a28ee4720f34efae516c04eccb97e6820644b2 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Wed, 13 Dec 2017 21:53:18 +0800 Subject: [PATCH 300/305] Add archiver example A simple (de)serialization framework using DOM and SAX API --- example/CMakeLists.txt | 2 + example/archiver/archiver.cpp | 292 ++++++++++++++++++++++++++++++ example/archiver/archiver.h | 139 ++++++++++++++ example/archiver/archivertest.cpp | 281 ++++++++++++++++++++++++++++ 4 files changed, 714 insertions(+) create mode 100644 example/archiver/archiver.cpp create mode 100644 example/archiver/archiver.h create mode 100644 example/archiver/archivertest.cpp diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index e00f77a..ff54199 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -32,6 +32,8 @@ if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread") endif() +add_executable(archivertest archiver/archiver.cpp archiver/archivertest.cpp) + foreach (example ${EXAMPLES}) add_executable(${example} ${example}/${example}.cpp) endforeach() diff --git a/example/archiver/archiver.cpp b/example/archiver/archiver.cpp new file mode 100644 index 0000000..59ae4c4 --- /dev/null +++ b/example/archiver/archiver.cpp @@ -0,0 +1,292 @@ +#include "archiver.h" +#include +#include +#include "rapidjson/document.h" +#include "rapidjson/prettywriter.h" +#include "rapidjson/stringbuffer.h" + +using namespace rapidjson; + +struct JsonReaderStackItem { + enum State { + BeforeStart, //!< An object/array is in the stack but it is not yet called by StartObject()/StartArray(). + Started, //!< An object/array is called by StartObject()/StartArray(). + Closed //!< An array is closed after read all element, but before EndArray(). + }; + + JsonReaderStackItem(const Value* value, State state) : value(value), state(state), index() {} + + const Value* value; + State state; + SizeType index; // For array iteration +}; + +typedef std::stack JsonReaderStack; + +#define DOCUMENT reinterpret_cast(mDocument) +#define STACK (reinterpret_cast(mStack)) +#define TOP (STACK->top()) +#define CURRENT (*TOP.value) + +JsonReader::JsonReader(const char* json) : mDocument(), mStack(), mError(false) { + mDocument = new Document; + DOCUMENT->Parse(json); + if (DOCUMENT->HasParseError()) + mError = true; + else { + mStack = new JsonReaderStack; + STACK->push(JsonReaderStackItem(DOCUMENT, JsonReaderStackItem::BeforeStart)); + } +} + +JsonReader::~JsonReader() { + delete DOCUMENT; + delete STACK; +} + +// Archive concept +JsonReader& JsonReader::StartObject() { + if (!mError) { + if (CURRENT.IsObject() && TOP.state == JsonReaderStackItem::BeforeStart) + TOP.state = JsonReaderStackItem::Started; + else + mError = true; + } + return *this; +} + +JsonReader& JsonReader::EndObject() { + if (!mError) { + if (CURRENT.IsObject() && TOP.state == JsonReaderStackItem::Started) + Next(); + else + mError = true; + } + return *this; +} + +JsonReader& JsonReader::Member(const char* name) { + if (!mError) { + if (CURRENT.IsObject() && TOP.state == JsonReaderStackItem::Started) { + Value::ConstMemberIterator memberItr = CURRENT.FindMember(name); + if (memberItr != CURRENT.MemberEnd()) + STACK->push(JsonReaderStackItem(&memberItr->value, JsonReaderStackItem::BeforeStart)); + else + mError = true; + } + else + mError = true; + } + return *this; +} + +bool JsonReader::HasMember(const char* name) const { + if (!mError && CURRENT.IsObject() && TOP.state == JsonReaderStackItem::Started) + return CURRENT.HasMember(name); + return false; +} + +JsonReader& JsonReader::StartArray(size_t* size) { + if (!mError) { + if (CURRENT.IsArray() && TOP.state == JsonReaderStackItem::BeforeStart) { + TOP.state = JsonReaderStackItem::Started; + if (size) + *size = CURRENT.Size(); + + if (!CURRENT.Empty()) { + const Value* value = &CURRENT[TOP.index]; + STACK->push(JsonReaderStackItem(value, JsonReaderStackItem::BeforeStart)); + } + else + TOP.state = JsonReaderStackItem::Closed; + } + else + mError = true; + } + return *this; +} + +JsonReader& JsonReader::EndArray() { + if (!mError) { + if (CURRENT.IsArray() && TOP.state == JsonReaderStackItem::Closed) + Next(); + else + mError = true; + } + return *this; +} + +JsonReader& JsonReader::operator&(bool& b) { + if (!mError) { + if (CURRENT.IsBool()) { + b = CURRENT.GetBool(); + Next(); + } + else + mError = true; + } + return *this; +} + +JsonReader& JsonReader::operator&(unsigned& u) { + if (!mError) { + if (CURRENT.IsUint()) { + u = CURRENT.GetUint(); + Next(); + } + else + mError = true; + } + return *this; +} + +JsonReader& JsonReader::operator&(int& i) { + if (!mError) { + if (CURRENT.IsInt()) { + i = CURRENT.GetInt(); + Next(); + } + else + mError = true; + } + return *this; +} + +JsonReader& JsonReader::operator&(double& d) { + if (!mError) { + if (CURRENT.IsNumber()) { + d = CURRENT.GetDouble(); + Next(); + } + else + mError = true; + } + return *this; +} + +JsonReader& JsonReader::operator&(std::string& s) { + if (!mError) { + if (CURRENT.IsString()) { + s = CURRENT.GetString(); + Next(); + } + else + mError = true; + } + return *this; +} + +JsonReader& JsonReader::SetNull() { + // This function is for JsonWriter only. + mError = true; + return *this; +} + +void JsonReader::Next() { + if (!mError) { + assert(!STACK->empty()); + STACK->pop(); + + if (!STACK->empty() && CURRENT.IsArray()) { + if (TOP.state == JsonReaderStackItem::Started) { // Otherwise means reading array item pass end + if (TOP.index < CURRENT.Size() - 1) { + const Value* value = &CURRENT[++TOP.index]; + STACK->push(JsonReaderStackItem(value, JsonReaderStackItem::BeforeStart)); + } + else + TOP.state = JsonReaderStackItem::Closed; + } + else + mError = true; + } + } +} + +#undef DOCUMENT +#undef STACK +#undef TOP +#undef CURRENT + +//////////////////////////////////////////////////////////////////////////////// +// JsonWriter + +#define WRITER reinterpret_cast*>(mWriter) +#define STREAM reinterpret_cast(mStream) + +JsonWriter::JsonWriter() : mWriter(), mStream() { + mStream = new StringBuffer; + mWriter = new PrettyWriter(*STREAM); +} + +JsonWriter::~JsonWriter() { + delete WRITER; + delete STREAM; +} + +const char* JsonWriter::GetString() const { + return STREAM->GetString(); +} + +JsonWriter& JsonWriter::StartObject() { + WRITER->StartObject(); + return *this; +} + +JsonWriter& JsonWriter::EndObject() { + WRITER->EndObject(); + return *this; +} + +JsonWriter& JsonWriter::Member(const char* name) { + WRITER->String(name, static_cast(strlen(name))); + return *this; +} + +bool JsonWriter::HasMember(const char*) const { + // This function is for JsonReader only. + assert(false); + return false; +} + +JsonWriter& JsonWriter::StartArray(size_t*) { + WRITER->StartArray(); + return *this; +} + +JsonWriter& JsonWriter::EndArray() { + WRITER->EndArray(); + return *this; +} + +JsonWriter& JsonWriter::operator&(bool& b) { + WRITER->Bool(b); + return *this; +} + +JsonWriter& JsonWriter::operator&(unsigned& u) { + WRITER->Uint(u); + return *this; +} + +JsonWriter& JsonWriter::operator&(int& i) { + WRITER->Int(i); + return *this; +} + +JsonWriter& JsonWriter::operator&(double& d) { + WRITER->Double(d); + return *this; +} + +JsonWriter& JsonWriter::operator&(std::string& s) { + WRITER->String(s.c_str(), static_cast(s.size())); + return *this; +} + +JsonWriter& JsonWriter::SetNull() { + WRITER->Null(); + return *this; +} + +#undef STREAM +#undef WRITER diff --git a/example/archiver/archiver.h b/example/archiver/archiver.h new file mode 100644 index 0000000..c7e74f0 --- /dev/null +++ b/example/archiver/archiver.h @@ -0,0 +1,139 @@ +#ifndef ARCHIVER_H_ +#define ARCHIVER_H_ + +#include +#include + +/** +\class Archiver +\brief Archiver concept + +Archiver can be a reader or writer for serialization or deserialization respectively. + +class Archiver { +public: + /// \returns true if the archiver is in normal state. false if it has errors. + operator bool() const; + + /// Starts an object + Archiver& StartObject(); + + /// After calling StartObject(), assign a member with a name + Archiver& Member(const char* name); + + /// After calling StartObject(), check if a member presents + bool HasMember(const char* name) const; + + /// Ends an object + Archiver& EndObject(); + + /// Starts an array + /// \param size If Archiver::IsReader is true, the size of array is written. + Archiver& StartArray(size_t* size = 0); + + /// Ends an array + Archiver& EndArray(); + + /// Read/Write primitive types. + Archiver& operator&(bool& b); + Archiver& operator&(unsigned& u); + Archiver& operator&(int& i); + Archiver& operator&(double& d); + Archiver& operator&(std::string& s); + + /// Write primitive types. + Archiver& SetNull(); + + //! Whether it is a reader. + static const bool IsReader; + + //! Whether it is a writer. + static const bool IsWriter; +}; +*/ + +/// Represents a JSON reader which implements Archiver concept. +class JsonReader { +public: + /// Constructor. + /** + \param json A non-const source json string for in-situ parsing. + \note in-situ means the source JSON string will be modified after parsing. + */ + JsonReader(const char* json); + + /// Destructor. + ~JsonReader(); + + // Archive concept + + operator bool() const { return !mError; } + + JsonReader& StartObject(); + JsonReader& Member(const char* name); + bool HasMember(const char* name) const; + JsonReader& EndObject(); + + JsonReader& StartArray(size_t* size = nullptr); + JsonReader& EndArray(); + + JsonReader& operator&(bool& b); + JsonReader& operator&(unsigned& u); + JsonReader& operator&(int& i); + JsonReader& operator&(double& d); + JsonReader& operator&(std::string& s); + + JsonReader& SetNull(); + + static const bool IsReader = true; + static const bool IsWriter = !IsReader; + +private: + void Next(); + + // PIMPL + void* mDocument; ///< DOM result of parsing. + void* mStack; ///< Stack for iterating the DOM + bool mError; ///< Whether an error is occured. +}; + +class JsonWriter { +public: + /// Constructor. + JsonWriter(); + + /// Destructor. + ~JsonWriter(); + + /// Obtains the serialized JSON string. + const char* GetString() const; + + // Archive concept + + operator bool() const { return true; } + + JsonWriter& StartObject(); + JsonWriter& Member(const char* name); + bool HasMember(const char* name) const; + JsonWriter& EndObject(); + + JsonWriter& StartArray(size_t* size = 0); + JsonWriter& EndArray(); + + JsonWriter& operator&(bool& b); + JsonWriter& operator&(unsigned& u); + JsonWriter& operator&(int& i); + JsonWriter& operator&(double& d); + JsonWriter& operator&(std::string& s); + JsonWriter& SetNull(); + + static const bool IsReader = false; + static const bool IsWriter = !IsReader; + +private: + // PIMPL idiom + void* mWriter; ///< JSON writer. + void* mStream; ///< Stream buffer. +}; + +#endif // ARCHIVER_H__ diff --git a/example/archiver/archivertest.cpp b/example/archiver/archivertest.cpp new file mode 100644 index 0000000..788db36 --- /dev/null +++ b/example/archiver/archivertest.cpp @@ -0,0 +1,281 @@ +#include "archiver.h" +#include +#include + +////////////////////////////////////////////////////////////////////////////// +// Test1: simple object + +struct Student { + std::string name; + unsigned age; + double height; + bool canSwim; +}; + +template +Archiver& operator&(Archiver& ar, Student& s) { + ar.StartObject(); + ar.Member("name") & s.name; + ar.Member("age") & s.age; + ar.Member("height") & s.height; + ar.Member("canSwim") & s.canSwim; + return ar.EndObject(); +} + +std::ostream& operator<<(std::ostream& os, const Student& s) { + return os << s.name << " " << s.age << " " << s.height << " " << s.canSwim; +} + +void test1() { + std::string json; + + // Serialize + { + Student s = { "Lua", 9, 150.5, true }; + + JsonWriter writer; + writer & s; + json = writer.GetString(); + std::cout << json << std::endl; + } + + // Deserialize + { + Student s; + JsonReader reader(json.c_str()); + reader & s; + std::cout << s << std::endl; + } +} + +////////////////////////////////////////////////////////////////////////////// +// Test2: std::vector <=> JSON array +// +// You can map a JSON array to other data structures as well + +struct Group { + std::string groupName; + std::vector students; +}; + +template +Archiver& operator&(Archiver& ar, Group& g) { + ar.StartObject(); + + ar.Member("groupName"); + ar & g.groupName; + + ar.Member("students"); + size_t studentCount = g.students.size(); + ar.StartArray(&studentCount); + if (ar.IsReader) + g.students.resize(studentCount); + for (size_t i = 0; i < studentCount; i++) + ar & g.students[i]; + ar.EndArray(); + + return ar.EndObject(); +} + +std::ostream& operator<<(std::ostream& os, const Group& g) { + os << g.groupName << std::endl; + for (std::vector::const_iterator itr = g.students.begin(); itr != g.students.end(); ++itr) + os << *itr << std::endl; + return os; +} + +void test2() { + std::string json; + + // Serialize + { + Group g; + g.groupName = "Rainbow"; + + Student s1 = { "Lua", 9, 150.5, true }; + Student s2 = { "Mio", 7, 120.0, false }; + g.students.push_back(s1); + g.students.push_back(s2); + + JsonWriter writer; + writer & g; + json = writer.GetString(); + std::cout << json << std::endl; + } + + // Deserialize + { + Group g; + JsonReader reader(json.c_str()); + reader & g; + std::cout << g << std::endl; + } +} + +////////////////////////////////////////////////////////////////////////////// +// Test3: polymorphism & friend +// +// Note that friendship is not necessary but make things simpler. + +class Shape { +public: + virtual ~Shape() {} + virtual const char* GetType() const = 0; + virtual void Print(std::ostream& os) const = 0; + +protected: + Shape() {} + Shape(double x, double y) : x_(x), y_(y) {} + + template + friend Archiver& operator&(Archiver& ar, Shape& s); + + double x_, y_; +}; + +template +Archiver& operator&(Archiver& ar, Shape& s) { + ar.Member("x") & s.x_; + ar.Member("y") & s.y_; + return ar; +} + +class Circle : public Shape { +public: + Circle() {} + Circle(double x, double y, double radius) : Shape(x, y), radius_(radius) {} + ~Circle() {} + + const char* GetType() const { return "Circle"; } + + void Print(std::ostream& os) const { + os << "Circle (" << x_ << ", " << y_ << ")" << " radius = " << radius_; + } + +private: + template + friend Archiver& operator&(Archiver& ar, Circle& c); + + double radius_; +}; + +template +Archiver& operator&(Archiver& ar, Circle& c) { + ar & static_cast(c); + ar.Member("radius") & c.radius_; + return ar; +} + +class Box : public Shape { +public: + Box() {} + Box(double x, double y, double width, double height) : Shape(x, y), width_(width), height_(height) {} + ~Box() {} + + const char* GetType() const { return "Box"; } + + void Print(std::ostream& os) const { + os << "Box (" << x_ << ", " << y_ << ")" << " width = " << width_ << " height = " << height_; + } + +private: + template + friend Archiver& operator&(Archiver& ar, Box& b); + + double width_, height_; +}; + +template +Archiver& operator&(Archiver& ar, Box& b) { + ar & static_cast(b); + ar.Member("width") & b.width_; + ar.Member("height") & b.height_; + return ar; +} + +class Canvas { +public: + Canvas() {} + ~Canvas() { Clear(); } + + void Clear() { + for (std::vector::iterator itr = shapes_.begin(); itr != shapes_.end(); ++itr) + delete *itr; + } + + void AddShape(Shape* shape) { shapes_.push_back(shape); } + + void Print(std::ostream& os) { + for (std::vector::iterator itr = shapes_.begin(); itr != shapes_.end(); ++itr) { + (*itr)->Print(os); + std::cout << std::endl; + } + } + +private: + template + friend Archiver& operator&(Archiver& ar, Canvas& c); + + std::vector shapes_; +}; + +template +Archiver& operator&(Archiver& ar, Shape*& shape) { + std::string type = ar.IsReader ? "" : shape->GetType(); + ar.StartObject(); + ar.Member("type") & type; + if (type == "Circle") { + if (ar.IsReader) shape = new Circle; + ar & static_cast(*shape); + } + else if (type == "Box") { + if (ar.IsReader) shape = new Box; + ar & static_cast(*shape); + } + return ar.EndObject(); +} + +template +Archiver& operator&(Archiver& ar, Canvas& c) { + size_t shapeCount = c.shapes_.size(); + ar.StartArray(&shapeCount); + if (ar.IsReader) { + c.Clear(); + c.shapes_.resize(shapeCount); + } + for (size_t i = 0; i < shapeCount; i++) + ar & c.shapes_[i]; + return ar.EndArray(); +} + +void test3() { + std::string json; + + // Serialize + { + Canvas c; + c.AddShape(new Circle(1.0, 2.0, 3.0)); + c.AddShape(new Box(4.0, 5.0, 6.0, 7.0)); + + JsonWriter writer; + writer & c; + json = writer.GetString(); + std::cout << json << std::endl; + } + + // Deserialize + { + Canvas c; + JsonReader reader(json.c_str()); + reader & c; + c.Print(std::cout); + } +} + +////////////////////////////////////////////////////////////////////////////// + +int main() { + test1(); + test2(); + test3(); +} From 9bfa0bb567fd16f0fe7ea615e4b8139e8e64ce73 Mon Sep 17 00:00:00 2001 From: sjaques Date: Thu, 21 Dec 2017 13:40:28 +0100 Subject: [PATCH 301/305] Fix uninitilized member Reader::state_ --- include/rapidjson/reader.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/rapidjson/reader.h b/include/rapidjson/reader.h index 120c311..681dec2 100644 --- a/include/rapidjson/reader.h +++ b/include/rapidjson/reader.h @@ -544,7 +544,8 @@ public: /*! \param stackAllocator Optional allocator for allocating stack memory. (Only use for non-destructive parsing) \param stackCapacity stack capacity in bytes for storing a single decoded string. (Only use for non-destructive parsing) */ - GenericReader(StackAllocator* stackAllocator = 0, size_t stackCapacity = kDefaultStackCapacity) : stack_(stackAllocator, stackCapacity), parseResult_() {} + GenericReader(StackAllocator* stackAllocator = 0, size_t stackCapacity = kDefaultStackCapacity) : + stack_(stackAllocator, stackCapacity), parseResult_(), state_(IterativeParsingStartState) {} //! Parse JSON text. /*! \tparam parseFlags Combination of \ref ParseFlag. From 20d44d9c443290bc77e720bd65850a401fd7e9f4 Mon Sep 17 00:00:00 2001 From: Lele Gaifax Date: Fri, 22 Dec 2017 19:24:15 +0100 Subject: [PATCH 302/305] Fix FileWriteStream doc --- include/rapidjson/filewritestream.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/rapidjson/filewritestream.h b/include/rapidjson/filewritestream.h index 3811f8b..8b48fee 100644 --- a/include/rapidjson/filewritestream.h +++ b/include/rapidjson/filewritestream.h @@ -25,7 +25,7 @@ RAPIDJSON_DIAG_OFF(unreachable-code) RAPIDJSON_NAMESPACE_BEGIN -//! Wrapper of C file stream for input using fread(). +//! Wrapper of C file stream for output using fwrite(). /*! \note implements Stream concept */ From 53eadd218d705bd2dd5354eae46dd20e01dcd4e9 Mon Sep 17 00:00:00 2001 From: Haffon <31226194+Haffon@users.noreply.github.com> Date: Thu, 28 Dec 2017 16:31:26 +0800 Subject: [PATCH 303/305] GetParseOffset to GetErrorOffset --- doc/dom.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/dom.md b/doc/dom.md index 25ffbd2..0079b64 100644 --- a/doc/dom.md +++ b/doc/dom.md @@ -128,7 +128,7 @@ And the `InputStream` is type of input stream. ## Parse Error {#ParseError} -When the parse processing succeeded, the `Document` contains the parse results. When there is an error, the original DOM is *unchanged*. And the error state of parsing can be obtained by `bool HasParseError()`, `ParseErrorCode GetParseError()` and `size_t GetParseOffset()`. +When the parse processing succeeded, the `Document` contains the parse results. When there is an error, the original DOM is *unchanged*. And the error state of parsing can be obtained by `bool HasParseError()`, `ParseErrorCode GetParseError()` and `size_t GetErrorOffset()`. Parse Error Code | Description --------------------------------------------|--------------------------------------------------- From 7dfeee862d3b47df7816a5b771a6523a61b62991 Mon Sep 17 00:00:00 2001 From: Haffon <31226194+Haffon@users.noreply.github.com> Date: Thu, 28 Dec 2017 16:32:26 +0800 Subject: [PATCH 304/305] GetParseOffset to GetErrorOffset --- doc/dom.zh-cn.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/dom.zh-cn.md b/doc/dom.zh-cn.md index b709485..9743b7a 100644 --- a/doc/dom.zh-cn.md +++ b/doc/dom.zh-cn.md @@ -128,7 +128,7 @@ GenericDocument& GenericDocument::Parse(const Ch* str); ## 解析错误 {#ParseError} -当解析过程顺利完成,`Document` 便会含有解析结果。当过程出现错误,原来的 DOM 会*维持不变*。可使用 `bool HasParseError()`、`ParseErrorCode GetParseError()` 及 `size_t GetParseOffset()` 获取解析的错误状态。 +当解析过程顺利完成,`Document` 便会含有解析结果。当过程出现错误,原来的 DOM 会*维持不变*。可使用 `bool HasParseError()`、`ParseErrorCode GetParseError()` 及 `size_t GetErrorOffset()` 获取解析的错误状态。 解析错误代号 | 描述 --------------------------------------------|--------------------------------------------------- From 0d95d58f8b6259e06e391ce962ec2062260bcdb8 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Sat, 13 Jan 2018 12:37:01 +0800 Subject: [PATCH 305/305] Try to fix travis build --- test/unittest/namespacetest.cpp | 4 ++++ test/unittest/unittest.h | 2 ++ 2 files changed, 6 insertions(+) diff --git a/test/unittest/namespacetest.cpp b/test/unittest/namespacetest.cpp index 1814724..9f5c9af 100644 --- a/test/unittest/namespacetest.cpp +++ b/test/unittest/namespacetest.cpp @@ -12,6 +12,10 @@ // CONDITIONS OF ANY KIND, either express or implied. See the License for the // specific language governing permissions and limitations under the License. +// Not throwing exception for this test +#include +#define RAPIDJSON_ASSERT(x) assert(x) + #include "unittest.h" // test another instantiation of RapidJSON in a different namespace diff --git a/test/unittest/unittest.h b/test/unittest/unittest.h index aa091aa..4b1c293 100644 --- a/test/unittest/unittest.h +++ b/test/unittest/unittest.h @@ -117,7 +117,9 @@ public: #pragma GCC diagnostic pop #endif +#ifndef RAPIDJSON_ASSERT #define RAPIDJSON_ASSERT(x) (!(x) ? throw AssertException(RAPIDJSON_STRINGIFY(x)) : (void)0u) +#endif class Random { public: