serialize.cpp
Go to the documentation of this file.
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 
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 
104 };
105 
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 
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 }
bool Bool(bool b)
Definition: writer.h:184
std::string school_
Definition: serialize.cpp:68
bool EndArray(SizeType elementCount=0)
Definition: writer.h:247
bool EndObject(SizeType memberCount=0)
Definition: writer.h:232
RAPIDJSON_NAMESPACE_BEGIN typedef unsigned SizeType
Size type (for string lengths, array sizes, etc.)
Definition: rapidjson.h:389
bool married_
Definition: serialize.cpp:147
double GPA_
Definition: serialize.cpp:69
Education(const std::string &school, double GPA)
Definition: serialize.cpp:47
Person(const Person &rhs)
Definition: serialize.cpp:14
JSON writer.
Definition: fwd.h:95
std::vector< Dependent > dependents_
Definition: serialize.cpp:146
const Ch * GetString() const
Definition: stringbuffer.h:73
Dependent & operator=(const Dependent &rhs)
Definition: serialize.cpp:78
std::string name_
Definition: serialize.cpp:38
bool Double(double d)
Writes the given double value to the stream.
Definition: writer.h:195
virtual ~Employee()
Definition: serialize.cpp:150
bool String(const Ch *str, SizeType length, bool copy=false)
Definition: writer.h:204
int main(int, char *[])
Definition: serialize.cpp:153
bool Null()
Definition: writer.h:183
void Serialize(Writer &writer) const
Definition: serialize.cpp:25
Employee & operator=(const Employee &rhs)
Definition: serialize.cpp:116
Education * education_
Definition: serialize.cpp:103
Education(const Education &rhs)
Definition: serialize.cpp:48
void Serialize(Writer &writer) const
Definition: serialize.cpp:87
Person & operator=(const Person &rhs)
Definition: serialize.cpp:17
main RapidJSON namespace
void Serialize(Writer &writer) const
Definition: serialize.cpp:128
void AddDependent(const Dependent &dependent)
Definition: serialize.cpp:123
Dependent(const Dependent &rhs)
Definition: serialize.cpp:75
unsigned age_
Definition: serialize.cpp:39
void Serialize(Writer &writer) const
Definition: serialize.cpp:51
bool EndArray(SizeType memberCount=0)
Definition: prettywriter.h:163
Employee(const Employee &rhs)
Definition: serialize.cpp:113
Employee(const std::string &name, unsigned age, bool married)
Definition: serialize.cpp:112
Dependent(const std::string &name, unsigned age, Education *education=0)
Definition: serialize.cpp:74
bool StartObject()
Definition: writer.h:217
virtual ~Dependent()
Definition: serialize.cpp:106
bool StartArray()
Definition: writer.h:241
bool Uint(unsigned u)
Definition: writer.h:186
bool StartArray()
Definition: prettywriter.h:157
Person(const std::string &name, unsigned age)
Definition: serialize.cpp:13
Writer with indentation and spacing.
Definition: fwd.h:100
virtual ~Person()
Definition: serialize.cpp:42


choreo_rapidjson
Author(s):
autogenerated on Thu Jul 18 2019 03:59:09