20template <
typename Archiver>
23 ar.Member(
"name") &
s.name;
24 ar.Member(
"age") &
s.age;
25 ar.Member(
"height") &
s.height;
26 ar.Member(
"canSwim") &
s.canSwim;
27 return ar.EndObject();
31 return os <<
s.name <<
" " <<
s.age <<
" " <<
s.height <<
" " <<
s.canSwim;
44 std::cout << json << std::endl;
52 std::cout <<
s << std::endl;
67template <
typename Archiver>
71 ar.Member(
"groupName");
74 ar.Member(
"students");
75 size_t studentCount = g.
students.size();
76 ar.StartArray(&studentCount);
79 for (
size_t i = 0; i < studentCount; i++)
83 return ar.EndObject();
88 for (std::vector<Student>::const_iterator itr = g.
students.begin(); itr != g.
students.end(); ++itr)
89 os << *itr << std::endl;
101 Student s1(
"Lua", 9, 150.5,
true);
102 Student s2(
"Mio", 7, 120.0,
false);
109 std::cout << json << std::endl;
117 std::cout << g << std::endl;
130 virtual void Print(std::ostream&
os)
const = 0;
136 template <
typename Archiver>
142template <
typename Archiver>
144 ar.Member(
"x") &
s.x_;
145 ar.Member(
"y") &
s.y_;
152 Circle(
double x,
double y,
double radius) :
Shape(x, y), radius_(radius) {}
155 const char*
GetType()
const {
return "Circle"; }
158 os <<
"Circle (" <<
x_ <<
", " <<
y_ <<
")" <<
" radius = " << radius_;
162 template <
typename Archiver>
168template <
typename Archiver>
170 ar &
static_cast<Shape&
>(c);
171 ar.Member(
"radius") & c.radius_;
177 Box() : width_(), height_() {}
178 Box(
double x,
double y,
double width,
double height) :
Shape(x, y), width_(
width), height_(height) {}
184 os <<
"Box (" <<
x_ <<
", " <<
y_ <<
")" <<
" width = " << width_ <<
" height = " << height_;
188 template <
typename Archiver>
191 double width_, height_;
194template <
typename Archiver>
196 ar &
static_cast<Shape&
>(b);
197 ar.Member(
"width") & b.width_;
198 ar.Member(
"height") & b.height_;
208 for (std::vector<Shape*>::iterator itr = shapes_.begin(); itr != shapes_.end(); ++itr)
215 for (std::vector<Shape*>::iterator itr = shapes_.begin(); itr != shapes_.end(); ++itr) {
217 std::cout << std::endl;
222 template <
typename Archiver>
225 std::vector<Shape*> shapes_;
228template <
typename Archiver>
230 std::string type = ar.IsReader ?
"" : shape->
GetType();
232 ar.Member(
"type") &
type;
233 if (type ==
"Circle") {
234 if (ar.IsReader) shape =
new Circle;
235 ar &
static_cast<Circle&
>(*shape);
237 else if (type ==
"Box") {
238 if (ar.IsReader) shape =
new Box;
239 ar &
static_cast<Box&
>(*shape);
241 return ar.EndObject();
244template <
typename Archiver>
246 size_t shapeCount = c.shapes_.size();
247 ar.StartArray(&shapeCount);
250 c.shapes_.resize(shapeCount);
252 for (
size_t i = 0; i < shapeCount; i++)
254 return ar.EndArray();
269 std::cout << json << std::endl;
std::ostream & operator<<(std::ostream &os, const Student &s)
Archiver & operator&(Archiver &ar, Student &s)
Box(double x, double y, double width, double height)
void Print(std::ostream &os) const
const char * GetType() const
friend Archiver & operator&(Archiver &ar, Box &b)
void AddShape(Shape *shape)
friend Archiver & operator&(Archiver &ar, Canvas &c)
void Print(std::ostream &os)
void Print(std::ostream &os) const
Circle(double x, double y, double radius)
friend Archiver & operator&(Archiver &ar, Circle &c)
const char * GetType() const
Represents a JSON reader which implements Archiver concept.
const char * GetString() const
Obtains the serialized JSON string.
virtual void Print(std::ostream &os) const =0
virtual const char * GetType() const =0
friend Archiver & operator&(Archiver &ar, Shape &s)
Shape(double x, double y)
std::vector< Student > students
Student(const std::string name, unsigned age, double height, bool canSwim)