Parcourir la source

fix for `Reader::ParseString()` implementation plus some minor code cleanups and additions

Kosta il y a 11 ans
Parent
commit
e70494bc00

+ 1 - 2
example/capitalize/capitalize.cpp

@@ -14,7 +14,7 @@
 using namespace rapidjson;
 
 template<typename OutputHandler>
-struct CapitalizeFilter {
+struct CapitalizeFilter : public BaseReaderHandler<UTF8<>, OutputHandler> {
     CapitalizeFilter(OutputHandler& out) : out_(out), buffer_() {}
 
     bool Null() { return out_.Null(); }
@@ -31,7 +31,6 @@ struct CapitalizeFilter {
         return out_.String(&buffer_.front(), length, true); // true = output handler need to copy the string
     }
     bool StartObject() { return out_.StartObject(); }
-    bool Key(const char* str, SizeType length, bool copy) { return String(str, length, copy); }
     bool EndObject(SizeType memberCount) { return out_.EndObject(memberCount); }
     bool StartArray() { return out_.StartArray(); }
     bool EndArray(SizeType elementCount) { return out_.EndArray(elementCount); }

+ 5 - 2
example/simplereader/simplereader.cpp

@@ -4,7 +4,7 @@
 using namespace rapidjson;
 using namespace std;
 
-struct MyHandler {
+struct MyHandler : public BaseReaderHandler<UTF8<>, 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; }
@@ -17,7 +17,10 @@ struct MyHandler {
         return true;
     }
     bool StartObject() { cout << "StartObject()" << endl; return true; }
-    bool Key(const char* str, SizeType length, bool copy) { return String(str, length, copy); }
+    bool Key(const char* str, SizeType length, bool copy) {
+        cout << "Key(" << str << ", " << length << ", " << boolalpha << copy << ")" << endl;
+        return true;
+    }
     bool EndObject(SizeType memberCount) { cout << "EndObject(" << memberCount << ")" << endl; return true; }
     bool StartArray() { cout << "StartArray()" << endl; return true; }
     bool EndArray(SizeType elementCount) { cout << "EndArray(" << elementCount << ")" << endl; return true; }

+ 1 - 0
include/rapidjson/prettywriter.h

@@ -137,6 +137,7 @@ 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)); }
 
     //@}
 protected:

+ 6 - 8
include/rapidjson/reader.h

@@ -623,27 +623,25 @@ private:
         internal::StreamLocalCopy<InputStream> copy(is);
         InputStream& s(copy.s);
 
-        const typename TargetEncoding::Ch* str = NULL;
-        SizeType len = 0;
-
+        bool success = false;
         if (parseFlags & kParseInsituFlag) {
             typename InputStream::Ch *head = s.PutBegin();
             ParseStringToStream<parseFlags, SourceEncoding, SourceEncoding>(s, s);
             RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
             size_t length = s.PutEnd(head) - 1;
             RAPIDJSON_ASSERT(length <= 0xFFFFFFFF);
-            str = (const typename TargetEncoding::Ch*)head;
-            len = SizeType(length);
+            const typename TargetEncoding::Ch* const str = (const typename TargetEncoding::Ch*)head;
+            success = (isKey ? handler.Key(str, SizeType(length), false) : handler.String(str, SizeType(length), false));
         }
         else {
             StackStream stackStream(stack_);
             ParseStringToStream<parseFlags, SourceEncoding, TargetEncoding>(s, stackStream);
             RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID;
-            str = stack_.template Pop<typename TargetEncoding::Ch>(stackStream.length_);
-            len = stackStream.length_ - 1;
+            const typename TargetEncoding::Ch* const str = stack_.template Pop<typename TargetEncoding::Ch>(stackStream.length_);
+            success = (isKey ? handler.Key(str, stackStream.length_ - 1, false) : handler.String(str, stackStream.length_ - 1, false));
         }
 
-        if(!(isKey ? handler.Key(str, len, false) : handler.String(str, len, false)))
+        if(!success)
             RAPIDJSON_PARSE_ERROR(kParseErrorTermination, s.Tell());
     }
 

+ 1 - 0
include/rapidjson/writer.h

@@ -167,6 +167,7 @@ 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)); }
 
     //@}