123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- #include "archiver.h"
- #include <iostream>
- #include <vector>
- //////////////////////////////////////////////////////////////////////////////
- // Test1: simple object
- struct Student {
- Student() : name(), age(), height(), canSwim() {}
- Student(const std::string name, unsigned age, double height, bool canSwim) :
- name(name), age(age), height(height), canSwim(canSwim)
- {}
- std::string name;
- unsigned age;
- double height;
- bool canSwim;
- };
- template <typename Archiver>
- Archiver& operator&(Archiver& ar, Student& s) {
- ar.StartObject();
- ar.Member("name") & s.name;
- ar.Member("age") & s.age;
- ar.Member("height") & s.height;
- ar.Member("canSwim") & s.canSwim;
- return ar.EndObject();
- }
- std::ostream& operator<<(std::ostream& os, const Student& s) {
- return os << s.name << " " << s.age << " " << s.height << " " << s.canSwim;
- }
- void test1() {
- std::string json;
- // Serialize
- {
- Student s("Lua", 9, 150.5, true);
- JsonWriter writer;
- writer & s;
- json = writer.GetString();
- std::cout << json << std::endl;
- }
- // Deserialize
- {
- Student s;
- JsonReader reader(json.c_str());
- reader & s;
- std::cout << s << std::endl;
- }
- }
- //////////////////////////////////////////////////////////////////////////////
- // Test2: std::vector <=> JSON array
- //
- // You can map a JSON array to other data structures as well
- struct Group {
- Group() : groupName(), students() {}
- std::string groupName;
- std::vector<Student> students;
- };
- template <typename Archiver>
- Archiver& operator&(Archiver& ar, Group& g) {
- ar.StartObject();
-
- ar.Member("groupName");
- ar & g.groupName;
- ar.Member("students");
- size_t studentCount = g.students.size();
- ar.StartArray(&studentCount);
- if (ar.IsReader)
- g.students.resize(studentCount);
- for (size_t i = 0; i < studentCount; i++)
- ar & g.students[i];
- ar.EndArray();
- return ar.EndObject();
- }
- std::ostream& operator<<(std::ostream& os, const Group& g) {
- os << g.groupName << std::endl;
- for (std::vector<Student>::const_iterator itr = g.students.begin(); itr != g.students.end(); ++itr)
- os << *itr << std::endl;
- return os;
- }
- void test2() {
- std::string json;
- // Serialize
- {
- Group g;
- g.groupName = "Rainbow";
- Student s1("Lua", 9, 150.5, true);
- Student s2("Mio", 7, 120.0, false);
- g.students.push_back(s1);
- g.students.push_back(s2);
- JsonWriter writer;
- writer & g;
- json = writer.GetString();
- std::cout << json << std::endl;
- }
- // Deserialize
- {
- Group g;
- JsonReader reader(json.c_str());
- reader & g;
- std::cout << g << std::endl;
- }
- }
- //////////////////////////////////////////////////////////////////////////////
- // Test3: polymorphism & friend
- //
- // Note that friendship is not necessary but make things simpler.
- class Shape {
- public:
- virtual ~Shape() {}
- virtual const char* GetType() const = 0;
- virtual void Print(std::ostream& os) const = 0;
- protected:
- Shape() : x_(), y_() {}
- Shape(double x, double y) : x_(x), y_(y) {}
- template <typename Archiver>
- friend Archiver& operator&(Archiver& ar, Shape& s);
- double x_, y_;
- };
- template <typename Archiver>
- Archiver& operator&(Archiver& ar, Shape& s) {
- ar.Member("x") & s.x_;
- ar.Member("y") & s.y_;
- return ar;
- }
- class Circle : public Shape {
- public:
- Circle() : radius_() {}
- Circle(double x, double y, double radius) : Shape(x, y), radius_(radius) {}
- ~Circle() {}
- const char* GetType() const { return "Circle"; }
- void Print(std::ostream& os) const {
- os << "Circle (" << x_ << ", " << y_ << ")" << " radius = " << radius_;
- }
- private:
- template <typename Archiver>
- friend Archiver& operator&(Archiver& ar, Circle& c);
- double radius_;
- };
- template <typename Archiver>
- Archiver& operator&(Archiver& ar, Circle& c) {
- ar & static_cast<Shape&>(c);
- ar.Member("radius") & c.radius_;
- return ar;
- }
- class Box : public Shape {
- public:
- Box() : width_(), height_() {}
- Box(double x, double y, double width, double height) : Shape(x, y), width_(width), height_(height) {}
- ~Box() {}
- const char* GetType() const { return "Box"; }
- void Print(std::ostream& os) const {
- os << "Box (" << x_ << ", " << y_ << ")" << " width = " << width_ << " height = " << height_;
- }
- private:
- template <typename Archiver>
- friend Archiver& operator&(Archiver& ar, Box& b);
- double width_, height_;
- };
- template <typename Archiver>
- Archiver& operator&(Archiver& ar, Box& b) {
- ar & static_cast<Shape&>(b);
- ar.Member("width") & b.width_;
- ar.Member("height") & b.height_;
- return ar;
- }
- class Canvas {
- public:
- Canvas() : shapes_() {}
- ~Canvas() { Clear(); }
-
- void Clear() {
- for (std::vector<Shape*>::iterator itr = shapes_.begin(); itr != shapes_.end(); ++itr)
- delete *itr;
- }
- void AddShape(Shape* shape) { shapes_.push_back(shape); }
-
- void Print(std::ostream& os) {
- for (std::vector<Shape*>::iterator itr = shapes_.begin(); itr != shapes_.end(); ++itr) {
- (*itr)->Print(os);
- std::cout << std::endl;
- }
- }
- private:
- template <typename Archiver>
- friend Archiver& operator&(Archiver& ar, Canvas& c);
- std::vector<Shape*> shapes_;
- };
- template <typename Archiver>
- Archiver& operator&(Archiver& ar, Shape*& shape) {
- std::string type = ar.IsReader ? "" : shape->GetType();
- ar.StartObject();
- ar.Member("type") & type;
- if (type == "Circle") {
- if (ar.IsReader) shape = new Circle;
- ar & static_cast<Circle&>(*shape);
- }
- else if (type == "Box") {
- if (ar.IsReader) shape = new Box;
- ar & static_cast<Box&>(*shape);
- }
- return ar.EndObject();
- }
- template <typename Archiver>
- Archiver& operator&(Archiver& ar, Canvas& c) {
- size_t shapeCount = c.shapes_.size();
- ar.StartArray(&shapeCount);
- if (ar.IsReader) {
- c.Clear();
- c.shapes_.resize(shapeCount);
- }
- for (size_t i = 0; i < shapeCount; i++)
- ar & c.shapes_[i];
- return ar.EndArray();
- }
- void test3() {
- std::string json;
- // Serialize
- {
- Canvas c;
- c.AddShape(new Circle(1.0, 2.0, 3.0));
- c.AddShape(new Box(4.0, 5.0, 6.0, 7.0));
- JsonWriter writer;
- writer & c;
- json = writer.GetString();
- std::cout << json << std::endl;
- }
- // Deserialize
- {
- Canvas c;
- JsonReader reader(json.c_str());
- reader & c;
- c.Print(std::cout);
- }
- }
- //////////////////////////////////////////////////////////////////////////////
- int main() {
- test1();
- test2();
- test3();
- }
|