Commit c2e2a4ce authored by Milo Yip's avatar Milo Yip

Merge pull request #234 from drewnoakes/pr/writer-string

Add std::string overload to Writer when RAPIDJSON_HAS_STDSTRING defined
parents 4cc36058 e54136d7
...@@ -19,8 +19,11 @@ protected: ...@@ -19,8 +19,11 @@ protected:
void Serialize(Writer& writer) const { void Serialize(Writer& writer) const {
// This base class just write out name-value pairs, without wrapping within an object. // This base class just write out name-value pairs, without wrapping within an object.
writer.String("name"); writer.String("name");
writer.String(name_.c_str(), (SizeType)name_.length()); // Suppling length of string is faster. #ifdef RAPIDJSON_HAS_STDSTRING
writer.String(name_);
#else
writer.String(name_.c_str(), (SizeType)name_.length()); // Supplying length of string is faster.
#endif
writer.String("age"); writer.String("age");
writer.Uint(age_); writer.Uint(age_);
} }
...@@ -42,7 +45,11 @@ public: ...@@ -42,7 +45,11 @@ public:
writer.StartObject(); writer.StartObject();
writer.String("school"); writer.String("school");
#ifdef RAPIDJSON_HAS_STDSTRING
writer.String(school_);
#else
writer.String(school_.c_str(), (SizeType)school_.length()); writer.String(school_.c_str(), (SizeType)school_.length());
#endif
writer.String("GPA"); writer.String("GPA");
writer.Double(GPA_); writer.Double(GPA_);
......
...@@ -82,6 +82,12 @@ public: ...@@ -82,6 +82,12 @@ public:
return Base::WriteString(str, length); return Base::WriteString(str, length);
} }
#if RAPIDJSON_HAS_STDSTRING
bool String(const std::basic_string<Ch>& str) {
return String(str.data(), SizeType(str.size()));
}
#endif
bool StartObject() { bool StartObject() {
PrettyPrefix(kObjectType); PrettyPrefix(kObjectType);
new (Base::level_stack_.template Push<typename Base::Level>()) typename Base::Level(false); new (Base::level_stack_.template Push<typename Base::Level>()) typename Base::Level(false);
......
...@@ -29,6 +29,10 @@ ...@@ -29,6 +29,10 @@
#include "stringbuffer.h" #include "stringbuffer.h"
#include <new> // placement new #include <new> // placement new
#if RAPIDJSON_HAS_STDSTRING
#include <string>
#endif
#ifdef _MSC_VER #ifdef _MSC_VER
RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_PUSH
RAPIDJSON_DIAG_OFF(4127) // conditional expression is constant RAPIDJSON_DIAG_OFF(4127) // conditional expression is constant
...@@ -127,6 +131,12 @@ public: ...@@ -127,6 +131,12 @@ public:
return WriteString(str, length); return WriteString(str, length);
} }
#if RAPIDJSON_HAS_STDSTRING
bool String(const std::basic_string<Ch>& str) {
return String(str.data(), SizeType(str.size()));
}
#endif
bool StartObject() { bool StartObject() {
Prefix(kObjectType); Prefix(kObjectType);
new (level_stack_.template Push<Level>()) Level(false); new (level_stack_.template Push<Level>()) Level(false);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment