CppSerialization 1.0.5.0
C++ Serialization Library
Loading...
Searching...
No Matches
deserializer.inl
Go to the documentation of this file.
1
9namespace CppSerialization {
10namespace JSON {
11
12template<typename JSON>
13inline bool Deserializer::Find(const JSON& json, const char* key, bool& value)
14{
15 // Try to find a member with the given key
16 Value::ConstMemberIterator member = json.FindMember(key);
17 if ((member == json.MemberEnd()) || member->value.IsNull())
18 return false;
19
20 // Schema validation
21 if (!member->value.IsBool())
22 throwex SerializationException("Cannot deserialize JSON bool value!");
23
24 // Save the member value
25 value = member->value.GetBool();
26 return true;
27}
28
29template<typename JSON>
30inline bool Deserializer::Find(const JSON& json, const char* key, int& value)
31{
32 // Try to find a member with the given key
33 Value::ConstMemberIterator member = json.FindMember(key);
34 if ((member == json.MemberEnd()) || member->value.IsNull())
35 return false;
36
37 // Schema validation
38 if (!member->value.IsInt())
39 throwex SerializationException("Cannot deserialize JSON integer value!");
40
41 // Save the member value
42 value = member->value.GetInt();
43 return true;
44}
45
46template<typename JSON>
47inline bool Deserializer::Find(const JSON& json, const char* key, unsigned& value)
48{
49 // Try to find a member with the given key
50 Value::ConstMemberIterator member = json.FindMember(key);
51 if ((member == json.MemberEnd()) || member->value.IsNull())
52 return false;
53
54 // Schema validation
55 if (!member->value.IsUint())
56 throwex SerializationException("Cannot deserialize JSON unsigned value!");
57
58 // Save the member value
59 value = member->value.GetUint();
60 return true;
61}
62
63template<typename JSON>
64inline bool Deserializer::Find(const JSON& json, const char* key, int64_t& value)
65{
66 // Try to find a member with the given key
67 Value::ConstMemberIterator member = json.FindMember(key);
68 if ((member == json.MemberEnd()) || member->value.IsNull())
69 return false;
70
71 // Schema validation
72 if (!member->value.IsInt64())
73 throwex SerializationException("Cannot deserialize JSON 64-bit integer value!");
74
75 // Save the member value
76 value = member->value.GetInt64();
77 return true;
78}
79
80template<typename JSON>
81inline bool Deserializer::Find(const JSON& json, const char* key, uint64_t& value)
82{
83 // Try to find a member with the given key
84 Value::ConstMemberIterator member = json.FindMember(key);
85 if ((member == json.MemberEnd()) || member->value.IsNull())
86 return false;
87
88 // Schema validation
89 if (!member->value.IsUint64())
90 throwex SerializationException("Cannot deserialize JSON 64-bit unsigned value!");
91
92 // Save the member value
93 value = member->value.GetUint64();
94 return true;
95}
96
97template<typename JSON>
98inline bool Deserializer::Find(const JSON& json, const char* key, double& value)
99{
100 // Try to find a member with the given key
101 Value::ConstMemberIterator member = json.FindMember(key);
102 if ((member == json.MemberEnd()) || member->value.IsNull())
103 return false;
104
105 // Schema validation
106 if (!member->value.IsDouble())
107 throwex SerializationException("Cannot deserialize JSON double value!");
108
109 // Save the member value
110 value = member->value.GetDouble();
111 return true;
112}
113
114template<typename JSON>
115inline bool Deserializer::Find(const JSON& json, const char* key, char* value, size_t size)
116{
117 // Try to find a member with the given key
118 Value::ConstMemberIterator member = json.FindMember(key);
119 if ((member == json.MemberEnd()) || member->value.IsNull())
120 return false;
121
122 // Schema validation
123 if (!member->value.IsString())
124 throwex SerializationException("Cannot deserialize JSON C-string value with a given size!");
125
126 // Save the member value
127 size_t length = std::min((size_t)member->value.GetStringLength(), size);
128 std::memcpy(value, member->value.GetString(), length);
129 // Write the end of string character if possible
130 if (length < size)
131 value[length] = '\0';
132 return true;
133}
134
135template<typename JSON>
136inline bool Deserializer::Find(const JSON& json, const char* key, std::string& value)
137{
138 // Try to find a member with the given key
139 Value::ConstMemberIterator member = json.FindMember(key);
140 if ((member == json.MemberEnd()) || member->value.IsNull())
141 return false;
142
143 // Schema validation
144 if (!member->value.IsString())
145 throwex SerializationException("Cannot deserialize JSON string value!");
146
147 // Save the member value
148 value.assign(member->value.GetString(), (size_t)member->value.GetStringLength());
149 return true;
150}
151
152template <typename JSON, std::size_t N>
153inline bool Deserializer::Find(const JSON& json, const char* key, char (&value)[N])
154{
155 // Try to find a member with the given key
156 Value::ConstMemberIterator member = json.FindMember(key);
157 if ((member == json.MemberEnd()) || member->value.IsNull())
158 return false;
159
160 // Schema validation
161 if (!member->value.IsString())
162 throwex SerializationException("Cannot deserialize JSON string array value!");
163
164 // Save the member value
165 size_t length = std::min((size_t)member->value.GetStringLength(), N);
166 std::memcpy(value, member->value.GetString(), length);
167 // Write the end of string character if possible
168 if (length < N)
169 value[length] = '\0';
170 return true;
171}
172
173template<typename JSON>
174inline bool Deserializer::FindArray(const JSON& json, const char* key, const std::function<void(const Value&)>& handler)
175{
176 return FindArray(json, key, [](size_t){}, handler);
177}
178
179
180template<typename JSON>
181inline bool Deserializer::FindArray(const JSON& json, const char* key, const std::function<void(size_t)>& initialize, const std::function<void(const Value&)>& handler)
182{
183 assert((initialize) && "JSON initialize array handler must be valid!");
184 assert((handler) && "JSON array handler must be valid!");
185 if (!initialize || !handler)
186 return false;
187
188 // Try to find a member with the given key
189 Value::ConstMemberIterator member = json.FindMember(key);
190 if ((member == json.MemberEnd()) || member->value.IsNull())
191 return false;
192
193 // Schema validation
194 if (!member->value.IsArray())
195 throwex SerializationException("Cannot deserialize JSON array!");
196
197 // Handle array items
198 initialize(member->value.GetArray().Size());
199 for (const auto& item : member->value.GetArray())
200 handler(item);
201 return true;
202}
203
204template<typename JSON>
205inline bool Deserializer::FindObject(const JSON& json, const char* key, const std::function<void(const Value::ConstObject&)>& handler)
206{
207 assert((handler) && "JSON array handler must be valid!");
208 if (!handler)
209 return false;
210
211 // Try to find a member with the given key
212 Value::ConstMemberIterator member = json.FindMember(key);
213 if ((member == json.MemberEnd()) || member->value.IsNull())
214 return false;
215
216 // Schema validation
217 if (!member->value.IsObject())
218 throwex SerializationException("Cannot deserialize JSON object!");
219
220 // Handle object
221 handler(member->value.GetObj());
222 return true;
223}
224
225} // namespace JSON
226} // namespace CppSerialization
static bool FindArray(const JSON &json, const char *key, const std::function< void(const Value &)> &handler)
Try to get the array key/value pair.
static bool FindObject(const JSON &json, const char *key, const std::function< void(const Value::ConstObject &)> &handler)
Try to get the object key/value pair.
static bool Find(const JSON &json, const char *key, bool &value)
Try to get the boolean key/value pair.
C++ Serialization project definitions.
Definition exceptions.h:14