serialize.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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_ = (rhs.education_ == 0) ? 0 : new Education(*rhs.education_); }
  48. virtual ~Dependent();
  49. template <typename Writer>
  50. void Serialize(Writer& writer) const {
  51. writer.StartObject();
  52. Person::Serialize(writer);
  53. writer.String("education");
  54. if (education_)
  55. education_->Serialize(writer);
  56. else
  57. writer.Null();
  58. writer.EndObject();
  59. }
  60. private:
  61. Education *education_;
  62. };
  63. Dependent::~Dependent() {
  64. delete education_;
  65. }
  66. class Employee : public Person {
  67. public:
  68. Employee(const std::string& name, unsigned age, bool married) : Person(name, age), married_(married) {}
  69. virtual ~Employee();
  70. void AddDependent(const Dependent& dependent) {
  71. dependents_.push_back(dependent);
  72. }
  73. template <typename Writer>
  74. void Serialize(Writer& writer) const {
  75. writer.StartObject();
  76. Person::Serialize(writer);
  77. writer.String("married");
  78. writer.Bool(married_);
  79. writer.String(("dependents"));
  80. writer.StartArray();
  81. for (std::vector<Dependent>::const_iterator dependentItr = dependents_.begin(); dependentItr != dependents_.end(); ++dependentItr)
  82. dependentItr->Serialize(writer);
  83. writer.EndArray();
  84. writer.EndObject();
  85. }
  86. private:
  87. std::vector<Dependent> dependents_;
  88. bool married_;
  89. };
  90. Employee::~Employee() {
  91. }
  92. int main(int, char*[]) {
  93. std::vector<Employee> employees;
  94. employees.push_back(Employee("Milo YIP", 34, true));
  95. employees.back().AddDependent(Dependent("Lua YIP", 3, new Education("Happy Kindergarten", 3.5)));
  96. employees.back().AddDependent(Dependent("Mio YIP", 1));
  97. employees.push_back(Employee("Percy TSE", 30, false));
  98. FileStream s(stdout);
  99. PrettyWriter<FileStream> writer(s); // Can also use Writer for condensed formatting
  100. writer.StartArray();
  101. for (std::vector<Employee>::const_iterator employeeItr = employees.begin(); employeeItr != employees.end(); ++employeeItr)
  102. employeeItr->Serialize(writer);
  103. writer.EndArray();
  104. return 0;
  105. }