Electroneum
Toggle main menu visibility
Loading...
Searching...
No Matches
serialize.cpp
Go to the documentation of this file.
1
// Serialize example
2
// This example shows writing JSON string with writer directly.
3
4
#include "
rapidjson/prettywriter.h
"
// for stringify JSON
5
#include <cstdio>
6
#include <string>
7
#include <vector>
8
9
using namespace
rapidjson
;
10
11
class
Person
{
12
public
:
13
Person
(
const
std::string& name,
unsigned
age) : name_(name), age_(age) {}
14
Person
(
const
Person
& rhs) : name_(rhs.name_), age_(rhs.age_) {}
15
virtual
~Person
();
16
17
Person
&
operator=
(
const
Person
& rhs) {
18
name_ = rhs.name_;
19
age_ = rhs.age_;
20
return
*
this
;
21
}
22
23
protected
:
24
template
<
typename
Writer>
25
void
Serialize
(
Writer
& writer)
const
{
26
// This base class just write out name-value pairs, without wrapping within an object.
27
writer.
String
(
"name"
);
28
#if RAPIDJSON_HAS_STDSTRING
29
writer.
String
(name_);
30
#else
31
writer.
String
(name_.c_str(),
static_cast<
SizeType
>
(name_.length()));
// Supplying length of string is faster.
32
#endif
33
writer.
String
(
"age"
);
34
writer.
Uint
(age_);
35
}
36
37
private
:
38
std::string name_;
39
unsigned
age_;
40
};
41
42
Person::~Person
() {
43
}
44
45
class
Education
{
46
public
:
47
Education
(
const
std::string& school,
double
GPA) : school_(school), GPA_(GPA) {}
48
Education
(
const
Education
& rhs) : school_(rhs.school_), GPA_(rhs.GPA_) {}
49
50
template
<
typename
Writer>
51
void
Serialize
(
Writer
& writer)
const
{
52
writer.
StartObject
();
53
54
writer.
String
(
"school"
);
55
#if RAPIDJSON_HAS_STDSTRING
56
writer.
String
(school_);
57
#else
58
writer.
String
(school_.c_str(),
static_cast<
SizeType
>
(school_.length()));
59
#endif
60
61
writer.
String
(
"GPA"
);
62
writer.
Double
(GPA_);
63
64
writer.
EndObject
();
65
}
66
67
private
:
68
std::string school_;
69
double
GPA_;
70
};
71
72
class
Dependent
:
public
Person
{
73
public
:
74
Dependent
(
const
std::string& name,
unsigned
age,
Education
* education = 0) :
Person
(name, age), education_(education) {}
75
Dependent
(
const
Dependent
& rhs) :
Person
(rhs), education_(0) { education_ = (rhs.education_ == 0) ? 0 :
new
Education
(*rhs.education_); }
76
virtual
~Dependent
();
77
78
Dependent
&
operator=
(
const
Dependent
& rhs) {
79
if
(
this
== &rhs)
80
return
*
this
;
81
delete
education_;
82
education_ = (rhs.education_ == 0) ? 0 :
new
Education
(*rhs.education_);
83
return
*
this
;
84
}
85
86
template
<
typename
Writer>
87
void
Serialize
(
Writer
& writer)
const
{
88
writer.
StartObject
();
89
90
Person::Serialize
(writer);
91
92
writer.
String
(
"education"
);
93
if
(education_)
94
education_->Serialize(writer);
95
else
96
writer.
Null
();
97
98
writer.
EndObject
();
99
}
100
101
private
:
102
103
Education
*education_;
104
};
105
106
Dependent::~Dependent
() {
107
delete
education_;
108
}
109
110
class
Employee
:
public
Person
{
111
public
:
112
Employee
(
const
std::string& name,
unsigned
age,
bool
married) :
Person
(name, age), dependents_(), married_(married) {}
113
Employee
(
const
Employee
& rhs) :
Person
(rhs), dependents_(rhs.dependents_), married_(rhs.married_) {}
114
virtual
~Employee
();
115
116
Employee
&
operator=
(
const
Employee
& rhs) {
117
static_cast<
Person
&
>
(*this) = rhs;
118
dependents_ = rhs.dependents_;
119
married_ = rhs.married_;
120
return
*
this
;
121
}
122
123
void
AddDependent
(
const
Dependent
& dependent) {
124
dependents_.push_back(dependent);
125
}
126
127
template
<
typename
Writer>
128
void
Serialize
(
Writer
& writer)
const
{
129
writer.
StartObject
();
130
131
Person::Serialize
(writer);
132
133
writer.
String
(
"married"
);
134
writer.
Bool
(married_);
135
136
writer.
String
((
"dependents"
));
137
writer.
StartArray
();
138
for
(std::vector<Dependent>::const_iterator dependentItr = dependents_.begin(); dependentItr != dependents_.end(); ++dependentItr)
139
dependentItr->Serialize(writer);
140
writer.
EndArray
();
141
142
writer.
EndObject
();
143
}
144
145
private
:
146
std::vector<Dependent> dependents_;
147
bool
married_;
148
};
149
150
Employee::~Employee
() {
151
}
152
153
int
main
(
int
,
char
*[]) {
154
std::vector<Employee> employees;
155
156
employees.push_back(
Employee
(
"Milo YIP"
, 34,
true
));
157
employees.back().AddDependent(
Dependent
(
"Lua YIP"
, 3,
new
Education
(
"Happy Kindergarten"
, 3.5)));
158
employees.back().AddDependent(
Dependent
(
"Mio YIP"
, 1));
159
160
employees.push_back(
Employee
(
"Percy TSE"
, 30,
false
));
161
162
StringBuffer
sb;
163
PrettyWriter<StringBuffer>
writer(sb);
164
165
writer.
StartArray
();
166
for
(std::vector<Employee>::const_iterator employeeItr = employees.begin(); employeeItr != employees.end(); ++employeeItr)
167
employeeItr->Serialize(writer);
168
writer.
EndArray
();
169
170
puts(sb.
GetString
());
171
172
return
0;
173
}
main
int main()
Definition
archivertest.cpp:283
Dependent
Definition
serialize.cpp:72
Dependent::Dependent
Dependent(const Dependent &rhs)
Definition
serialize.cpp:75
Dependent::Serialize
void Serialize(Writer &writer) const
Definition
serialize.cpp:87
Dependent::~Dependent
virtual ~Dependent()
Definition
serialize.cpp:106
Dependent::operator=
Dependent & operator=(const Dependent &rhs)
Definition
serialize.cpp:78
Dependent::Dependent
Dependent(const std::string &name, unsigned age, Education *education=0)
Definition
serialize.cpp:74
Education
Definition
serialize.cpp:45
Education::Education
Education(const Education &rhs)
Definition
serialize.cpp:48
Education::Serialize
void Serialize(Writer &writer) const
Definition
serialize.cpp:51
Education::Education
Education(const std::string &school, double GPA)
Definition
serialize.cpp:47
Employee
Definition
serialize.cpp:110
Employee::Employee
Employee(const std::string &name, unsigned age, bool married)
Definition
serialize.cpp:112
Employee::operator=
Employee & operator=(const Employee &rhs)
Definition
serialize.cpp:116
Employee::Employee
Employee(const Employee &rhs)
Definition
serialize.cpp:113
Employee::Serialize
void Serialize(Writer &writer) const
Definition
serialize.cpp:128
Employee::~Employee
virtual ~Employee()
Definition
serialize.cpp:150
Employee::AddDependent
void AddDependent(const Dependent &dependent)
Definition
serialize.cpp:123
GenericStringBuffer::GetString
const Ch * GetString() const
Definition
stringbuffer.h:73
Person::operator=
Person & operator=(const Person &rhs)
Definition
serialize.cpp:17
Person::Serialize
void Serialize(Writer &writer) const
Definition
serialize.cpp:25
Person::Person
Person(const Person &rhs)
Definition
serialize.cpp:14
Person::Person
Person(const std::string &name, unsigned age)
Definition
serialize.cpp:13
Person::~Person
virtual ~Person()
Definition
serialize.cpp:42
PrettyWriter
Writer with indentation and spacing.
Definition
prettywriter.h:48
PrettyWriter::EndArray
bool EndArray(SizeType memberCount=0)
Definition
prettywriter.h:163
PrettyWriter::StartArray
bool StartArray()
Definition
prettywriter.h:157
Writer
JSON writer.
Definition
writer.h:89
Writer::EndObject
bool EndObject(SizeType memberCount=0)
Definition
writer.h:230
Writer::Double
bool Double(double d)
Writes the given double value to the stream.
Definition
writer.h:193
Writer::StartArray
bool StartArray()
Definition
writer.h:239
Writer::Uint
bool Uint(unsigned u)
Definition
writer.h:184
Writer::String
bool String(const Ch *str, SizeType length, bool copy=false)
Definition
writer.h:202
Writer::EndArray
bool EndArray(SizeType elementCount=0)
Definition
writer.h:245
Writer::Bool
bool Bool(bool b)
Definition
writer.h:182
Writer::StartObject
bool StartObject()
Definition
writer.h:215
Writer::Null
bool Null()
Definition
writer.h:181
StringBuffer
GenericStringBuffer< UTF8< char >, CrtAllocator > StringBuffer
Definition
fwd.h:61
rapidjson
main RapidJSON namespace
prettywriter.h
SizeType
RAPIDJSON_NAMESPACE_BEGIN typedef unsigned SizeType
Size type (for string lengths, array sizes, etc.).
Definition
rapidjson.h:389
external
rapidjson
example
serialize
serialize.cpp
Generated on
for Electroneum by
1.17.0