| 1 |
// Serialize example |
| 2 |
// This example shows writing JSON string with writer directly. |
| 3 |
|
| 4 |
#include "rapidjson/prettywriter.h" // for stringify JSON |
| 5 |
#include <cstdio> |
| 6 |
#include <string> |
| 7 |
#include <vector> |
| 8 |
|
| 9 |
using namespace rapidjson; |
| 10 |
|
| 11 |
class Person { |
| 12 |
public: |
| 13 |
Person(const std::string& name, unsigned age) : name_(name), age_(age) {} |
| 14 |
Person(const Person& rhs) : name_(rhs.name_), age_(rhs.age_) {} |
| 15 |
virtual ~Person(); |
| 16 |
|
| 17 |
Person& operator=(const Person& rhs) { |
| 18 |
name_ = rhs.name_; |
| 19 |
age_ = rhs.age_; |
| 20 |
return *this; |
| 21 |
} |
| 22 |
|
| 23 |
protected: |
| 24 |
template <typename Writer> |
| 25 |
void Serialize(Writer& writer) const { |
| 26 |
// This base class just write out name-value pairs, without wrapping within an object. |
| 27 |
writer.String("name"); |
| 28 |
#if RAPIDJSON_HAS_STDSTRING |
| 29 |
writer.String(name_); |
| 30 |
#else |
| 31 |
writer.String(name_.c_str(), static_cast<SizeType>(name_.length())); // Supplying length of string is faster. |
| 32 |
#endif |
| 33 |
writer.String("age"); |
| 34 |
writer.Uint(age_); |
| 35 |
} |
| 36 |
|
| 37 |
private: |
| 38 |
std::string name_; |
| 39 |
unsigned age_; |
| 40 |
}; |
| 41 |
|
| 42 |
Person::~Person() { |
| 43 |
} |
| 44 |
|
| 45 |
class Education { |
| 46 |
public: |
| 47 |
Education(const std::string& school, double GPA) : school_(school), GPA_(GPA) {} |
| 48 |
Education(const Education& rhs) : school_(rhs.school_), GPA_(rhs.GPA_) {} |
| 49 |
|
| 50 |
template <typename Writer> |
| 51 |
void Serialize(Writer& writer) const { |
| 52 |
writer.StartObject(); |
| 53 |
|
| 54 |
writer.String("school"); |
| 55 |
#if RAPIDJSON_HAS_STDSTRING |
| 56 |
writer.String(school_); |
| 57 |
#else |
| 58 |
writer.String(school_.c_str(), static_cast<SizeType>(school_.length())); |
| 59 |
#endif |
| 60 |
|
| 61 |
writer.String("GPA"); |
| 62 |
writer.Double(GPA_); |
| 63 |
|
| 64 |
writer.EndObject(); |
| 65 |
} |
| 66 |
|
| 67 |
private: |
| 68 |
std::string school_; |
| 69 |
double GPA_; |
| 70 |
}; |
| 71 |
|
| 72 |
class Dependent : public Person { |
| 73 |
public: |
| 74 |
Dependent(const std::string& name, unsigned age, Education* education = 0) : Person(name, age), education_(education) {} |
| 75 |
Dependent(const Dependent& rhs) : Person(rhs), education_(0) { education_ = (rhs.education_ == 0) ? 0 : new Education(*rhs.education_); } |
| 76 |
virtual ~Dependent(); |
| 77 |
|
| 78 |
Dependent& operator=(const Dependent& rhs) { |
| 79 |
if (this == &rhs) |
| 80 |
return *this; |
| 81 |
delete education_; |
| 82 |
education_ = (rhs.education_ == 0) ? 0 : new Education(*rhs.education_); |
| 83 |
return *this; |
| 84 |
} |
| 85 |
|
| 86 |
template <typename Writer> |
| 87 |
void Serialize(Writer& writer) const { |
| 88 |
writer.StartObject(); |
| 89 |
|
| 90 |
Person::Serialize(writer); |
| 91 |
|
| 92 |
writer.String("education"); |
| 93 |
if (education_) |
| 94 |
education_->Serialize(writer); |
| 95 |
else |
| 96 |
writer.Null(); |
| 97 |
|
| 98 |
writer.EndObject(); |
| 99 |
} |
| 100 |
|
| 101 |
private: |
| 102 |
|
| 103 |
Education *education_; |
| 104 |
}; |
| 105 |
|
| 106 |
Dependent::~Dependent() { |
| 107 |
delete education_; |
| 108 |
} |
| 109 |
|
| 110 |
class Employee : public Person { |
| 111 |
public: |
| 112 |
Employee(const std::string& name, unsigned age, bool married) : Person(name, age), dependents_(), married_(married) {} |
| 113 |
Employee(const Employee& rhs) : Person(rhs), dependents_(rhs.dependents_), married_(rhs.married_) {} |
| 114 |
virtual ~Employee(); |
| 115 |
|
| 116 |
Employee& operator=(const Employee& rhs) { |
| 117 |
static_cast<Person&>(*this) = rhs; |
| 118 |
dependents_ = rhs.dependents_; |
| 119 |
married_ = rhs.married_; |
| 120 |
return *this; |
| 121 |
} |
| 122 |
|
| 123 |
void AddDependent(const Dependent& dependent) { |
| 124 |
dependents_.push_back(dependent); |
| 125 |
} |
| 126 |
|
| 127 |
template <typename Writer> |
| 128 |
void Serialize(Writer& writer) const { |
| 129 |
writer.StartObject(); |
| 130 |
|
| 131 |
Person::Serialize(writer); |
| 132 |
|
| 133 |
writer.String("married"); |
| 134 |
writer.Bool(married_); |
| 135 |
|
| 136 |
writer.String(("dependents")); |
| 137 |
writer.StartArray(); |
| 138 |
for (std::vector<Dependent>::const_iterator dependentItr = dependents_.begin(); dependentItr != dependents_.end(); ++dependentItr) |
| 139 |
dependentItr->Serialize(writer); |
| 140 |
writer.EndArray(); |
| 141 |
|
| 142 |
writer.EndObject(); |
| 143 |
} |
| 144 |
|
| 145 |
private: |
| 146 |
std::vector<Dependent> dependents_; |
| 147 |
bool married_; |
| 148 |
}; |
| 149 |
|
| 150 |
Employee::~Employee() { |
| 151 |
} |
| 152 |
|
| 153 |
int main(int, char*[]) { |
| 154 |
std::vector<Employee> employees; |
| 155 |
|
| 156 |
employees.push_back(Employee("Milo YIP", 34, true)); |
| 157 |
employees.back().AddDependent(Dependent("Lua YIP", 3, new Education("Happy Kindergarten", 3.5))); |
| 158 |
employees.back().AddDependent(Dependent("Mio YIP", 1)); |
| 159 |
|
| 160 |
employees.push_back(Employee("Percy TSE", 30, false)); |
| 161 |
|
| 162 |
StringBuffer sb; |
| 163 |
PrettyWriter<StringBuffer> writer(sb); |
| 164 |
|
| 165 |
writer.StartArray(); |
| 166 |
for (std::vector<Employee>::const_iterator employeeItr = employees.begin(); employeeItr != employees.end(); ++employeeItr) |
| 167 |
employeeItr->Serialize(writer); |
| 168 |
writer.EndArray(); |
| 169 |
|
| 170 |
puts(sb.GetString()); |
| 171 |
|
| 172 |
return 0; |
| 173 |
} |