20 template <
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();
39 Student s(
"Lua", 9, 150.5,
true);
44 std::cout <<
json << std::endl;
52 std::cout << s << std::endl;
67 template <
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;
129 virtual const char*
GetType()
const = 0;
130 virtual void Print(std::ostream& os)
const = 0;
136 template <
typename Archiver>
142 template <
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"; }
157 void Print(std::ostream& os)
const {
158 os <<
"Circle (" <<
x_ <<
", " <<
y_ <<
")" <<
" radius = " << radius_;
162 template <
typename Archiver>
168 template <
typename Archiver>
170 ar &
static_cast<Shape&
>(c);
171 ar.Member(
"radius") & c.radius_;
177 Box() : width_(), height_() {}
183 void Print(std::ostream& os)
const {
184 os <<
"Box (" <<
x_ <<
", " <<
y_ <<
")" <<
" width = " << width_ <<
" height = " << height_;
188 template <
typename Archiver>
191 double width_, height_;
194 template <
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_;
228 template <
typename Archiver>
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();
244 template <
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;
const char * GetString() const
Obtains the serialized JSON string.
friend Archiver & operator &(Archiver &ar, Shape &s)
void AddShape(Shape *shape)
const char * GetType() const
Archiver & operator &(Archiver &ar, Student &s)
friend Archiver & operator &(Archiver &ar, Canvas &c)
Shape(double x, double y)
Represents a JSON reader which implements Archiver concept.
friend Archiver & operator &(Archiver &ar, Box &b)
const char * GetType() const
Circle(double x, double y, double radius)
void Print(std::ostream &os)
Box(double x, double y, double width, double height)
Student(const std::string name, unsigned age, double height, bool canSwim)
virtual void Print(std::ostream &os) const =0
friend Archiver & operator &(Archiver &ar, Circle &c)
virtual const char * GetType() const =0
std::ostream & operator<<(std::ostream &os, const Student &s)
void Print(std::ostream &os) const
void Print(std::ostream &os) const
std::vector< Student > students