archivertest.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. #include "archiver.h"
  2. #include <iostream>
  3. #include <vector>
  4. //////////////////////////////////////////////////////////////////////////////
  5. // Test1: simple object
  6. struct Student {
  7. Student() : name(), age(), height(), canSwim() {}
  8. Student(const std::string name, unsigned age, double height, bool canSwim) :
  9. name(name), age(age), height(height), canSwim(canSwim)
  10. {}
  11. std::string name;
  12. unsigned age;
  13. double height;
  14. bool canSwim;
  15. };
  16. template <typename Archiver>
  17. Archiver& operator&(Archiver& ar, Student& s) {
  18. ar.StartObject();
  19. ar.Member("name") & s.name;
  20. ar.Member("age") & s.age;
  21. ar.Member("height") & s.height;
  22. ar.Member("canSwim") & s.canSwim;
  23. return ar.EndObject();
  24. }
  25. std::ostream& operator<<(std::ostream& os, const Student& s) {
  26. return os << s.name << " " << s.age << " " << s.height << " " << s.canSwim;
  27. }
  28. void test1() {
  29. std::string json;
  30. // Serialize
  31. {
  32. Student s("Lua", 9, 150.5, true);
  33. JsonWriter writer;
  34. writer & s;
  35. json = writer.GetString();
  36. std::cout << json << std::endl;
  37. }
  38. // Deserialize
  39. {
  40. Student s;
  41. JsonReader reader(json.c_str());
  42. reader & s;
  43. std::cout << s << std::endl;
  44. }
  45. }
  46. //////////////////////////////////////////////////////////////////////////////
  47. // Test2: std::vector <=> JSON array
  48. //
  49. // You can map a JSON array to other data structures as well
  50. struct Group {
  51. Group() : groupName(), students() {}
  52. std::string groupName;
  53. std::vector<Student> students;
  54. };
  55. template <typename Archiver>
  56. Archiver& operator&(Archiver& ar, Group& g) {
  57. ar.StartObject();
  58. ar.Member("groupName");
  59. ar & g.groupName;
  60. ar.Member("students");
  61. size_t studentCount = g.students.size();
  62. ar.StartArray(&studentCount);
  63. if (ar.IsReader)
  64. g.students.resize(studentCount);
  65. for (size_t i = 0; i < studentCount; i++)
  66. ar & g.students[i];
  67. ar.EndArray();
  68. return ar.EndObject();
  69. }
  70. std::ostream& operator<<(std::ostream& os, const Group& g) {
  71. os << g.groupName << std::endl;
  72. for (std::vector<Student>::const_iterator itr = g.students.begin(); itr != g.students.end(); ++itr)
  73. os << *itr << std::endl;
  74. return os;
  75. }
  76. void test2() {
  77. std::string json;
  78. // Serialize
  79. {
  80. Group g;
  81. g.groupName = "Rainbow";
  82. Student s1("Lua", 9, 150.5, true);
  83. Student s2("Mio", 7, 120.0, false);
  84. g.students.push_back(s1);
  85. g.students.push_back(s2);
  86. JsonWriter writer;
  87. writer & g;
  88. json = writer.GetString();
  89. std::cout << json << std::endl;
  90. }
  91. // Deserialize
  92. {
  93. Group g;
  94. JsonReader reader(json.c_str());
  95. reader & g;
  96. std::cout << g << std::endl;
  97. }
  98. }
  99. //////////////////////////////////////////////////////////////////////////////
  100. // Test3: polymorphism & friend
  101. //
  102. // Note that friendship is not necessary but make things simpler.
  103. class Shape {
  104. public:
  105. virtual ~Shape() {}
  106. virtual const char* GetType() const = 0;
  107. virtual void Print(std::ostream& os) const = 0;
  108. protected:
  109. Shape() : x_(), y_() {}
  110. Shape(double x, double y) : x_(x), y_(y) {}
  111. template <typename Archiver>
  112. friend Archiver& operator&(Archiver& ar, Shape& s);
  113. double x_, y_;
  114. };
  115. template <typename Archiver>
  116. Archiver& operator&(Archiver& ar, Shape& s) {
  117. ar.Member("x") & s.x_;
  118. ar.Member("y") & s.y_;
  119. return ar;
  120. }
  121. class Circle : public Shape {
  122. public:
  123. Circle() : radius_() {}
  124. Circle(double x, double y, double radius) : Shape(x, y), radius_(radius) {}
  125. ~Circle() {}
  126. const char* GetType() const { return "Circle"; }
  127. void Print(std::ostream& os) const {
  128. os << "Circle (" << x_ << ", " << y_ << ")" << " radius = " << radius_;
  129. }
  130. private:
  131. template <typename Archiver>
  132. friend Archiver& operator&(Archiver& ar, Circle& c);
  133. double radius_;
  134. };
  135. template <typename Archiver>
  136. Archiver& operator&(Archiver& ar, Circle& c) {
  137. ar & static_cast<Shape&>(c);
  138. ar.Member("radius") & c.radius_;
  139. return ar;
  140. }
  141. class Box : public Shape {
  142. public:
  143. Box() : width_(), height_() {}
  144. Box(double x, double y, double width, double height) : Shape(x, y), width_(width), height_(height) {}
  145. ~Box() {}
  146. const char* GetType() const { return "Box"; }
  147. void Print(std::ostream& os) const {
  148. os << "Box (" << x_ << ", " << y_ << ")" << " width = " << width_ << " height = " << height_;
  149. }
  150. private:
  151. template <typename Archiver>
  152. friend Archiver& operator&(Archiver& ar, Box& b);
  153. double width_, height_;
  154. };
  155. template <typename Archiver>
  156. Archiver& operator&(Archiver& ar, Box& b) {
  157. ar & static_cast<Shape&>(b);
  158. ar.Member("width") & b.width_;
  159. ar.Member("height") & b.height_;
  160. return ar;
  161. }
  162. class Canvas {
  163. public:
  164. Canvas() : shapes_() {}
  165. ~Canvas() { Clear(); }
  166. void Clear() {
  167. for (std::vector<Shape*>::iterator itr = shapes_.begin(); itr != shapes_.end(); ++itr)
  168. delete *itr;
  169. }
  170. void AddShape(Shape* shape) { shapes_.push_back(shape); }
  171. void Print(std::ostream& os) {
  172. for (std::vector<Shape*>::iterator itr = shapes_.begin(); itr != shapes_.end(); ++itr) {
  173. (*itr)->Print(os);
  174. std::cout << std::endl;
  175. }
  176. }
  177. private:
  178. template <typename Archiver>
  179. friend Archiver& operator&(Archiver& ar, Canvas& c);
  180. std::vector<Shape*> shapes_;
  181. };
  182. template <typename Archiver>
  183. Archiver& operator&(Archiver& ar, Shape*& shape) {
  184. std::string type = ar.IsReader ? "" : shape->GetType();
  185. ar.StartObject();
  186. ar.Member("type") & type;
  187. if (type == "Circle") {
  188. if (ar.IsReader) shape = new Circle;
  189. ar & static_cast<Circle&>(*shape);
  190. }
  191. else if (type == "Box") {
  192. if (ar.IsReader) shape = new Box;
  193. ar & static_cast<Box&>(*shape);
  194. }
  195. return ar.EndObject();
  196. }
  197. template <typename Archiver>
  198. Archiver& operator&(Archiver& ar, Canvas& c) {
  199. size_t shapeCount = c.shapes_.size();
  200. ar.StartArray(&shapeCount);
  201. if (ar.IsReader) {
  202. c.Clear();
  203. c.shapes_.resize(shapeCount);
  204. }
  205. for (size_t i = 0; i < shapeCount; i++)
  206. ar & c.shapes_[i];
  207. return ar.EndArray();
  208. }
  209. void test3() {
  210. std::string json;
  211. // Serialize
  212. {
  213. Canvas c;
  214. c.AddShape(new Circle(1.0, 2.0, 3.0));
  215. c.AddShape(new Box(4.0, 5.0, 6.0, 7.0));
  216. JsonWriter writer;
  217. writer & c;
  218. json = writer.GetString();
  219. std::cout << json << std::endl;
  220. }
  221. // Deserialize
  222. {
  223. Canvas c;
  224. JsonReader reader(json.c_str());
  225. reader & c;
  226. c.Print(std::cout);
  227. }
  228. }
  229. //////////////////////////////////////////////////////////////////////////////
  230. int main() {
  231. test1();
  232. test2();
  233. test3();
  234. }