archivertest.cpp 6.4 KB

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