serialize.cpp 3.7 KB

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