2 // This example shows writing JSON string with writer directly.
4 #include "rapidjson/prettywriter.h" // for stringify JSON
9 using namespace rapidjson;
13 Person(const std::string& name, unsigned age) : name_(name), age_(age) {}
14 Person(const Person& rhs) : name_(rhs.name_), age_(rhs.age_) {}
17 Person& operator=(const Person& rhs) {
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
31 writer.String(name_.c_str(), static_cast<SizeType>(name_.length())); // Supplying length of string is faster.
47 Education(const std::string& school, double GPA) : school_(school), GPA_(GPA) {}
48 Education(const Education& rhs) : school_(rhs.school_), GPA_(rhs.GPA_) {}
50 template <typename Writer>
51 void Serialize(Writer& writer) const {
54 writer.String("school");
55 #if RAPIDJSON_HAS_STDSTRING
56 writer.String(school_);
58 writer.String(school_.c_str(), static_cast<SizeType>(school_.length()));
72 class Dependent : public Person {
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_); }
78 Dependent& operator=(const Dependent& rhs) {
82 education_ = (rhs.education_ == 0) ? 0 : new Education(*rhs.education_);
86 template <typename Writer>
87 void Serialize(Writer& writer) const {
90 Person::Serialize(writer);
92 writer.String("education");
94 education_->Serialize(writer);
103 Education *education_;
106 Dependent::~Dependent() {
110 class Employee : public Person {
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_) {}
116 Employee& operator=(const Employee& rhs) {
117 static_cast<Person&>(*this) = rhs;
118 dependents_ = rhs.dependents_;
119 married_ = rhs.married_;
123 void AddDependent(const Dependent& dependent) {
124 dependents_.push_back(dependent);
127 template <typename Writer>
128 void Serialize(Writer& writer) const {
129 writer.StartObject();
131 Person::Serialize(writer);
133 writer.String("married");
134 writer.Bool(married_);
136 writer.String(("dependents"));
138 for (std::vector<Dependent>::const_iterator dependentItr = dependents_.begin(); dependentItr != dependents_.end(); ++dependentItr)
139 dependentItr->Serialize(writer);
146 std::vector<Dependent> dependents_;
150 Employee::~Employee() {
153 int main(int, char*[]) {
154 std::vector<Employee> employees;
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));
160 employees.push_back(Employee("Percy TSE", 30, false));
163 PrettyWriter<StringBuffer> writer(sb);
166 for (std::vector<Employee>::const_iterator employeeItr = employees.begin(); employeeItr != employees.end(); ++employeeItr)
167 employeeItr->Serialize(writer);
170 puts(sb.GetString());