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