serialize.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. class Education {
  27. public:
  28. Education(const std::string& school, double GPA) : school_(school), GPA_(GPA) {}
  29. template <typename Writer>
  30. void Serialize(Writer& writer) const {
  31. writer.StartObject();
  32. writer.String("school");
  33. writer.String(school_.c_str(), (SizeType)school_.length());
  34. writer.String("GPA");
  35. writer.Double(GPA_);
  36. writer.EndObject();
  37. }
  38. private:
  39. std::string school_;
  40. double GPA_;
  41. };
  42. class Dependent : public Person {
  43. public:
  44. Dependent(const std::string& name, unsigned age, Education* education = 0) : Person(name, age), education_(education) {}
  45. Dependent(const Dependent& rhs) : Person(rhs) { education_ = (rhs.education_ == 0) ? 0 : new Education(*rhs.education_); }
  46. ~Dependent() { delete education_; }
  47. template <typename Writer>
  48. void Serialize(Writer& writer) const {
  49. writer.StartObject();
  50. Person::Serialize(writer);
  51. writer.String("education");
  52. if (education_)
  53. education_->Serialize(writer);
  54. else
  55. writer.Null();
  56. writer.EndObject();
  57. }
  58. private:
  59. Education *education_;
  60. };
  61. class Employee : public Person {
  62. public:
  63. Employee(const std::string& name, unsigned age, bool married) : Person(name, age), married_(married) {}
  64. void AddDependent(const Dependent& dependent) {
  65. dependents_.push_back(dependent);
  66. }
  67. template <typename Writer>
  68. void Serialize(Writer& writer) const {
  69. writer.StartObject();
  70. Person::Serialize(writer);
  71. writer.String("married");
  72. writer.Bool(married_);
  73. writer.String(("dependents"));
  74. writer.StartArray();
  75. for (std::vector<Dependent>::const_iterator dependentItr = dependents_.begin(); dependentItr != dependents_.end(); ++dependentItr)
  76. dependentItr->Serialize(writer);
  77. writer.EndArray();
  78. writer.EndObject();
  79. }
  80. private:
  81. bool married_;
  82. std::vector<Dependent> dependents_;
  83. };
  84. int main(int, char*[]) {
  85. std::vector<Employee> employees;
  86. employees.push_back(Employee("Milo YIP", 34, true));
  87. employees.back().AddDependent(Dependent("Lua YIP", 3, new Education("Happy Kindergarten", 3.5)));
  88. employees.back().AddDependent(Dependent("Mio YIP", 1));
  89. employees.push_back(Employee("Percy TSE", 30, false));
  90. FileStream s(stdout);
  91. PrettyWriter<FileStream> writer(s); // Can also use Writer for condensed formatting
  92. writer.StartArray();
  93. for (std::vector<Employee>::const_iterator employeeItr = employees.begin(); employeeItr != employees.end(); ++employeeItr)
  94. employeeItr->Serialize(writer);
  95. writer.EndArray();
  96. return 0;
  97. }