serialize.cpp 4.6 KB

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