Skip to content

Added polymorphism test and virtual functions #2279

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions include/rapidjson/writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,56 +180,56 @@ class Writer {
*/
//@{

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)); }
virtual bool Null() { Prefix(kNullType); return EndValue(WriteNull()); }
virtual bool Bool(bool b) { Prefix(b ? kTrueType : kFalseType); return EndValue(WriteBool(b)); }
virtual bool Int(int i) { Prefix(kNumberType); return EndValue(WriteInt(i)); }
virtual bool Uint(unsigned u) { Prefix(kNumberType); return EndValue(WriteUint(u)); }
virtual bool Int64(int64_t i64) { Prefix(kNumberType); return EndValue(WriteInt64(i64)); }
virtual 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 EndValue(WriteDouble(d)); }
virtual bool Double(double d) { Prefix(kNumberType); return EndValue(WriteDouble(d)); }

bool RawNumber(const Ch* str, SizeType length, bool copy = false) {
virtual 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) {
virtual bool String(const Ch* str, SizeType length, bool copy = false) {
RAPIDJSON_ASSERT(str != 0);
(void)copy;
Prefix(kStringType);
return EndValue(WriteString(str, length));
}

#if RAPIDJSON_HAS_STDSTRING
bool String(const std::basic_string<Ch>& str) {
virtual bool String(const std::basic_string<Ch>& str) {
return String(str.data(), SizeType(str.size()));
}
#endif

bool StartObject() {
virtual bool StartObject() {
Prefix(kObjectType);
new (level_stack_.template Push<Level>()) Level(false);
return WriteStartObject();
}

bool Key(const Ch* str, SizeType length, bool copy = false) { return String(str, length, copy); }
virtual 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<Ch>& str)
virtual bool Key(const std::basic_string<Ch>& str)
{
return Key(str.data(), SizeType(str.size()));
}
#endif

bool EndObject(SizeType memberCount = 0) {
virtual bool EndObject(SizeType memberCount = 0) {
(void)memberCount;
RAPIDJSON_ASSERT(level_stack_.GetSize() >= sizeof(Level)); // not inside an Object
RAPIDJSON_ASSERT(!level_stack_.template Top<Level>()->inArray); // currently inside an Array, not Object
Expand All @@ -238,13 +238,13 @@ class Writer {
return EndValue(WriteEndObject());
}

bool StartArray() {
virtual bool StartArray() {
Prefix(kArrayType);
new (level_stack_.template Push<Level>()) Level(true);
return WriteStartArray();
}

bool EndArray(SizeType elementCount = 0) {
virtual bool EndArray(SizeType elementCount = 0) {
(void)elementCount;
RAPIDJSON_ASSERT(level_stack_.GetSize() >= sizeof(Level));
RAPIDJSON_ASSERT(level_stack_.template Top<Level>()->inArray);
Expand All @@ -257,8 +257,8 @@ class Writer {
//@{

//! Simpler but slower overload.
bool String(const Ch* const& str) { return String(str, internal::StrLen(str)); }
bool Key(const Ch* const& str) { return Key(str, internal::StrLen(str)); }
virtual bool String(const Ch* const& str) { return String(str, internal::StrLen(str)); }
virtual bool Key(const Ch* const& str) { return Key(str, internal::StrLen(str)); }

//@}

Expand All @@ -270,7 +270,7 @@ class Writer {
\param length Length of the json.
\param type Type of the root of json.
*/
bool RawValue(const Ch* json, size_t length, Type type) {
virtual bool RawValue(const Ch* json, size_t length, Type type) {
RAPIDJSON_ASSERT(json != 0);
Prefix(type);
return EndValue(WriteRawValue(json, length));
Expand Down
10 changes: 10 additions & 0 deletions test/unittest/prettywritertest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ TEST(PrettyWriter, Basic) {
EXPECT_STREQ(kPrettyJson, buffer.GetString());
}

TEST(PrettyWriter, Polymorphism) {
StringBuffer buffer;
PrettyWriter<StringBuffer> writer(buffer);
Writer<StringBuffer>* ugly_writer = dynamic_cast<Writer<StringBuffer>*>(&writer);
Reader reader;
StringStream s(kJson);
reader.Parse(s, *ugly_writer);
EXPECT_STREQ(kPrettyJson, buffer.GetString());
}

TEST(PrettyWriter, FormatOptions) {
StringBuffer buffer;
PrettyWriter<StringBuffer> writer(buffer);
Expand Down