serialize.cpp 3.43 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Serialize example
// This example shows writing JSON string with writer directly.

#include "rapidjson/prettywriter.h"	// for stringify JSON
#include "rapidjson/filestream.h"	// wrapper of C stream for prettywriter as output
#include <cstdio>
#include <string>
#include <vector>

using namespace rapidjson;

class Person {
public:
	Person(const std::string& name, unsigned age) : name_(name), age_(age) {}
15
	virtual ~Person();
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

protected:
	template <typename Writer>
	void Serialize(Writer& writer) const {
		// This base class just write out name-value pairs, without wrapping within an object.
		writer.String("name");
		writer.String(name_.c_str(), (SizeType)name_.length());	// Suppling length of string is faster.

		writer.String("age");
		writer.Uint(age_);
	}

private:
	std::string name_;
	unsigned age_;
};

33 34 35
Person::~Person() {
}

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
class Education {
public:
	Education(const std::string& school, double GPA) : school_(school), GPA_(GPA) {}

	template <typename Writer>
	void Serialize(Writer& writer) const {
		writer.StartObject();
		
		writer.String("school");
		writer.String(school_.c_str(), (SizeType)school_.length());

		writer.String("GPA");
		writer.Double(GPA_);

		writer.EndObject();
	}

private:
	std::string school_;
	double GPA_;
};

class Dependent : public Person {
public:
	Dependent(const std::string& name, unsigned age, Education* education = 0) : Person(name, age), education_(education) {}
	Dependent(const Dependent& rhs) : Person(rhs) { education_ = (rhs.education_ == 0) ? 0 : new Education(*rhs.education_); }
62
	virtual ~Dependent();
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82

	template <typename Writer>
	void Serialize(Writer& writer) const {
		writer.StartObject();

		Person::Serialize(writer);

		writer.String("education");
		if (education_)
			education_->Serialize(writer);
		else
			writer.Null();

		writer.EndObject();
	}

private:
	Education *education_;
};

83 84 85 86
Dependent::~Dependent() {
	delete education_; 
}

87 88 89
class Employee : public Person {
public:
	Employee(const std::string& name, unsigned age, bool married) : Person(name, age), married_(married) {}
90
	virtual ~Employee();
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114

	void AddDependent(const Dependent& dependent) {
		dependents_.push_back(dependent);
	}

	template <typename Writer>
	void Serialize(Writer& writer) const {
		writer.StartObject();

		Person::Serialize(writer);

		writer.String("married");
		writer.Bool(married_);

		writer.String(("dependents"));
		writer.StartArray();
		for (std::vector<Dependent>::const_iterator dependentItr = dependents_.begin(); dependentItr != dependents_.end(); ++dependentItr)
			dependentItr->Serialize(writer);
		writer.EndArray();

		writer.EndObject();
	}

private:
115
	std::vector<Dependent> dependents_;
116
	bool married_;
117 118
};

119 120 121
Employee::~Employee() {
}

122
int main(int, char*[]) {
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
	std::vector<Employee> employees;

	employees.push_back(Employee("Milo YIP", 34, true));
	employees.back().AddDependent(Dependent("Lua YIP", 3, new Education("Happy Kindergarten", 3.5)));
	employees.back().AddDependent(Dependent("Mio YIP", 1));

	employees.push_back(Employee("Percy TSE", 30, false));

	FileStream s(stdout);
	PrettyWriter<FileStream> writer(s);		// Can also use Writer for condensed formatting

	writer.StartArray();
	for (std::vector<Employee>::const_iterator employeeItr = employees.begin(); employeeItr != employees.end(); ++employeeItr)
		employeeItr->Serialize(writer);
	writer.EndArray();

	return 0;
}