serialize.cpp 4.5 KB

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