Mercurial > hg > orthanc
comparison Core/SerializationToolbox.cpp @ 3991:5d2348b39392
turning toolbox namespaces into classes to control visibility in shared libraries
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 29 May 2020 21:23:57 +0200 |
parents | 09798f2b985f |
children |
comparison
equal
deleted
inserted
replaced
3990:226ab94a33cd | 3991:5d2348b39392 |
---|---|
40 # include "DicomParsing/FromDcmtkBridge.h" | 40 # include "DicomParsing/FromDcmtkBridge.h" |
41 #endif | 41 #endif |
42 | 42 |
43 namespace Orthanc | 43 namespace Orthanc |
44 { | 44 { |
45 namespace SerializationToolbox | 45 static bool ParseTagInternal(DicomTag& tag, |
46 { | 46 const char* name) |
47 static bool ParseTagInternal(DicomTag& tag, | 47 { |
48 const char* name) | |
49 { | |
50 #if ORTHANC_ENABLE_DCMTK == 1 | 48 #if ORTHANC_ENABLE_DCMTK == 1 |
51 try | 49 try |
52 { | 50 { |
53 tag = FromDcmtkBridge::ParseTag(name); | 51 tag = FromDcmtkBridge::ParseTag(name); |
54 return true; | 52 return true; |
55 } | 53 } |
56 catch (OrthancException&) | 54 catch (OrthancException&) |
57 { | 55 { |
58 return false; | 56 return false; |
59 } | 57 } |
60 #else | 58 #else |
61 return DicomTag::ParseHexadecimal(tag, name); | 59 return DicomTag::ParseHexadecimal(tag, name); |
62 #endif | 60 #endif |
63 } | 61 } |
64 | 62 |
65 | 63 |
66 std::string ReadString(const Json::Value& value, | 64 std::string SerializationToolbox::ReadString(const Json::Value& value, |
67 const std::string& field) | 65 const std::string& field) |
68 { | 66 { |
69 if (value.type() != Json::objectValue || | 67 if (value.type() != Json::objectValue || |
70 !value.isMember(field.c_str()) || | 68 !value.isMember(field.c_str()) || |
71 value[field.c_str()].type() != Json::stringValue) | 69 value[field.c_str()].type() != Json::stringValue) |
72 { | 70 { |
73 throw OrthancException(ErrorCode_BadFileFormat, | 71 throw OrthancException(ErrorCode_BadFileFormat, |
74 "String value expected in field: " + field); | 72 "String value expected in field: " + field); |
75 } | 73 } |
76 else | 74 else |
77 { | 75 { |
78 return value[field.c_str()].asString(); | 76 return value[field.c_str()].asString(); |
79 } | 77 } |
80 } | 78 } |
81 | 79 |
82 | 80 |
83 int ReadInteger(const Json::Value& value, | 81 int SerializationToolbox::ReadInteger(const Json::Value& value, |
84 const std::string& field) | 82 const std::string& field) |
85 { | 83 { |
86 if (value.type() != Json::objectValue || | 84 if (value.type() != Json::objectValue || |
87 !value.isMember(field.c_str()) || | 85 !value.isMember(field.c_str()) || |
88 (value[field.c_str()].type() != Json::intValue && | 86 (value[field.c_str()].type() != Json::intValue && |
89 value[field.c_str()].type() != Json::uintValue)) | 87 value[field.c_str()].type() != Json::uintValue)) |
90 { | 88 { |
91 throw OrthancException(ErrorCode_BadFileFormat, | 89 throw OrthancException(ErrorCode_BadFileFormat, |
92 "Integer value expected in field: " + field); | 90 "Integer value expected in field: " + field); |
93 } | 91 } |
94 else | 92 else |
95 { | 93 { |
96 return value[field.c_str()].asInt(); | 94 return value[field.c_str()].asInt(); |
97 } | 95 } |
98 } | 96 } |
99 | 97 |
100 | 98 |
101 int ReadInteger(const Json::Value& value, | 99 int SerializationToolbox::ReadInteger(const Json::Value& value, |
102 const std::string& field, | 100 const std::string& field, |
103 int defaultValue) | 101 int defaultValue) |
104 { | 102 { |
105 if (value.isMember(field.c_str())) | 103 if (value.isMember(field.c_str())) |
106 { | 104 { |
107 return ReadInteger(value, field); | 105 return ReadInteger(value, field); |
108 } | 106 } |
109 else | 107 else |
110 { | 108 { |
111 return defaultValue; | 109 return defaultValue; |
112 } | 110 } |
113 } | 111 } |
114 | 112 |
115 | 113 |
116 unsigned int ReadUnsignedInteger(const Json::Value& value, | 114 unsigned int SerializationToolbox::ReadUnsignedInteger(const Json::Value& value, |
117 const std::string& field) | 115 const std::string& field) |
118 { | 116 { |
119 int tmp = ReadInteger(value, field); | 117 int tmp = ReadInteger(value, field); |
120 | 118 |
121 if (tmp < 0) | 119 if (tmp < 0) |
122 { | 120 { |
123 throw OrthancException(ErrorCode_BadFileFormat, | 121 throw OrthancException(ErrorCode_BadFileFormat, |
124 "Unsigned integer value expected in field: " + field); | 122 "Unsigned integer value expected in field: " + field); |
125 } | 123 } |
126 else | 124 else |
127 { | 125 { |
128 return static_cast<unsigned int>(tmp); | 126 return static_cast<unsigned int>(tmp); |
129 } | 127 } |
130 } | 128 } |
131 | 129 |
132 | 130 |
133 bool ReadBoolean(const Json::Value& value, | 131 bool SerializationToolbox::ReadBoolean(const Json::Value& value, |
134 const std::string& field) | 132 const std::string& field) |
135 { | 133 { |
136 if (value.type() != Json::objectValue || | 134 if (value.type() != Json::objectValue || |
137 !value.isMember(field.c_str()) || | 135 !value.isMember(field.c_str()) || |
138 value[field.c_str()].type() != Json::booleanValue) | 136 value[field.c_str()].type() != Json::booleanValue) |
139 { | 137 { |
140 throw OrthancException(ErrorCode_BadFileFormat, | 138 throw OrthancException(ErrorCode_BadFileFormat, |
141 "Boolean value expected in field: " + field); | 139 "Boolean value expected in field: " + field); |
142 } | 140 } |
143 else | 141 else |
144 { | 142 { |
145 return value[field.c_str()].asBool(); | 143 return value[field.c_str()].asBool(); |
146 } | 144 } |
147 } | 145 } |
148 | 146 |
149 | 147 |
150 void ReadArrayOfStrings(std::vector<std::string>& target, | 148 void SerializationToolbox::ReadArrayOfStrings(std::vector<std::string>& target, |
151 const Json::Value& value, | 149 const Json::Value& value, |
152 const std::string& field) | 150 const std::string& field) |
153 { | 151 { |
154 if (value.type() != Json::objectValue || | 152 if (value.type() != Json::objectValue || |
155 !value.isMember(field.c_str()) || | 153 !value.isMember(field.c_str()) || |
156 value[field.c_str()].type() != Json::arrayValue) | 154 value[field.c_str()].type() != Json::arrayValue) |
155 { | |
156 throw OrthancException(ErrorCode_BadFileFormat, | |
157 "List of strings expected in field: " + field); | |
158 } | |
159 | |
160 const Json::Value& arr = value[field.c_str()]; | |
161 | |
162 target.resize(arr.size()); | |
163 | |
164 for (Json::Value::ArrayIndex i = 0; i < arr.size(); i++) | |
165 { | |
166 if (arr[i].type() != Json::stringValue) | |
157 { | 167 { |
158 throw OrthancException(ErrorCode_BadFileFormat, | 168 throw OrthancException(ErrorCode_BadFileFormat, |
159 "List of strings expected in field: " + field); | 169 "List of strings expected in field: " + field); |
160 } | 170 } |
161 | 171 else |
162 const Json::Value& arr = value[field.c_str()]; | 172 { |
163 | 173 target[i] = arr[i].asString(); |
164 target.resize(arr.size()); | 174 } |
165 | 175 } |
166 for (Json::Value::ArrayIndex i = 0; i < arr.size(); i++) | 176 } |
167 { | 177 |
168 if (arr[i].type() != Json::stringValue) | 178 |
169 { | 179 void SerializationToolbox::ReadListOfStrings(std::list<std::string>& target, |
170 throw OrthancException(ErrorCode_BadFileFormat, | 180 const Json::Value& value, |
171 "List of strings expected in field: " + field); | 181 const std::string& field) |
172 } | 182 { |
173 else | 183 std::vector<std::string> tmp; |
174 { | 184 ReadArrayOfStrings(tmp, value, field); |
175 target[i] = arr[i].asString(); | 185 |
176 } | 186 target.clear(); |
177 } | 187 for (size_t i = 0; i < tmp.size(); i++) |
178 } | 188 { |
179 | 189 target.push_back(tmp[i]); |
180 | 190 } |
181 void ReadListOfStrings(std::list<std::string>& target, | 191 } |
182 const Json::Value& value, | |
183 const std::string& field) | |
184 { | |
185 std::vector<std::string> tmp; | |
186 ReadArrayOfStrings(tmp, value, field); | |
187 | |
188 target.clear(); | |
189 for (size_t i = 0; i < tmp.size(); i++) | |
190 { | |
191 target.push_back(tmp[i]); | |
192 } | |
193 } | |
194 | 192 |
195 | 193 |
196 void ReadSetOfStrings(std::set<std::string>& target, | 194 void SerializationToolbox::ReadSetOfStrings(std::set<std::string>& target, |
197 const Json::Value& value, | 195 const Json::Value& value, |
198 const std::string& field) | 196 const std::string& field) |
199 { | 197 { |
200 std::vector<std::string> tmp; | 198 std::vector<std::string> tmp; |
201 ReadArrayOfStrings(tmp, value, field); | 199 ReadArrayOfStrings(tmp, value, field); |
202 | 200 |
203 target.clear(); | 201 target.clear(); |
204 for (size_t i = 0; i < tmp.size(); i++) | 202 for (size_t i = 0; i < tmp.size(); i++) |
205 { | 203 { |
206 target.insert(tmp[i]); | 204 target.insert(tmp[i]); |
207 } | 205 } |
208 } | 206 } |
209 | 207 |
210 | 208 |
211 void ReadSetOfTags(std::set<DicomTag>& target, | 209 void SerializationToolbox::ReadSetOfTags(std::set<DicomTag>& target, |
212 const Json::Value& value, | 210 const Json::Value& value, |
213 const std::string& field) | 211 const std::string& field) |
214 { | 212 { |
215 if (value.type() != Json::objectValue || | 213 if (value.type() != Json::objectValue || |
216 !value.isMember(field.c_str()) || | 214 !value.isMember(field.c_str()) || |
217 value[field.c_str()].type() != Json::arrayValue) | 215 value[field.c_str()].type() != Json::arrayValue) |
216 { | |
217 throw OrthancException(ErrorCode_BadFileFormat, | |
218 "Set of DICOM tags expected in field: " + field); | |
219 } | |
220 | |
221 const Json::Value& arr = value[field.c_str()]; | |
222 | |
223 target.clear(); | |
224 | |
225 for (Json::Value::ArrayIndex i = 0; i < arr.size(); i++) | |
226 { | |
227 DicomTag tag(0, 0); | |
228 | |
229 if (arr[i].type() != Json::stringValue || | |
230 !ParseTagInternal(tag, arr[i].asCString())) | |
218 { | 231 { |
219 throw OrthancException(ErrorCode_BadFileFormat, | 232 throw OrthancException(ErrorCode_BadFileFormat, |
220 "Set of DICOM tags expected in field: " + field); | 233 "Set of DICOM tags expected in field: " + field); |
221 } | 234 } |
222 | 235 else |
223 const Json::Value& arr = value[field.c_str()]; | 236 { |
224 | 237 target.insert(tag); |
225 target.clear(); | 238 } |
226 | 239 } |
227 for (Json::Value::ArrayIndex i = 0; i < arr.size(); i++) | 240 } |
228 { | 241 |
229 DicomTag tag(0, 0); | 242 |
230 | 243 void SerializationToolbox::ReadMapOfStrings(std::map<std::string, std::string>& target, |
231 if (arr[i].type() != Json::stringValue || | 244 const Json::Value& value, |
232 !ParseTagInternal(tag, arr[i].asCString())) | 245 const std::string& field) |
233 { | 246 { |
234 throw OrthancException(ErrorCode_BadFileFormat, | 247 if (value.type() != Json::objectValue || |
235 "Set of DICOM tags expected in field: " + field); | 248 !value.isMember(field.c_str()) || |
236 } | 249 value[field.c_str()].type() != Json::objectValue) |
237 else | 250 { |
238 { | 251 throw OrthancException(ErrorCode_BadFileFormat, |
239 target.insert(tag); | 252 "Associative array of strings to strings expected in field: " + field); |
240 } | 253 } |
241 } | 254 |
242 } | 255 const Json::Value& source = value[field.c_str()]; |
243 | 256 |
244 | 257 target.clear(); |
245 void ReadMapOfStrings(std::map<std::string, std::string>& target, | 258 |
246 const Json::Value& value, | 259 Json::Value::Members members = source.getMemberNames(); |
247 const std::string& field) | 260 |
248 { | 261 for (size_t i = 0; i < members.size(); i++) |
249 if (value.type() != Json::objectValue || | 262 { |
250 !value.isMember(field.c_str()) || | 263 const Json::Value& tmp = source[members[i]]; |
251 value[field.c_str()].type() != Json::objectValue) | 264 |
265 if (tmp.type() != Json::stringValue) | |
252 { | 266 { |
253 throw OrthancException(ErrorCode_BadFileFormat, | 267 throw OrthancException(ErrorCode_BadFileFormat, |
254 "Associative array of strings to strings expected in field: " + field); | 268 "Associative array of string to strings expected in field: " + field); |
255 } | 269 } |
256 | 270 else |
257 const Json::Value& source = value[field.c_str()]; | 271 { |
258 | 272 target[members[i]] = tmp.asString(); |
259 target.clear(); | 273 } |
260 | 274 } |
261 Json::Value::Members members = source.getMemberNames(); | 275 } |
262 | 276 |
263 for (size_t i = 0; i < members.size(); i++) | 277 |
264 { | 278 void SerializationToolbox::ReadMapOfTags(std::map<DicomTag, std::string>& target, |
265 const Json::Value& tmp = source[members[i]]; | 279 const Json::Value& value, |
266 | 280 const std::string& field) |
267 if (tmp.type() != Json::stringValue) | 281 { |
268 { | 282 if (value.type() != Json::objectValue || |
269 throw OrthancException(ErrorCode_BadFileFormat, | 283 !value.isMember(field.c_str()) || |
270 "Associative array of string to strings expected in field: " + field); | 284 value[field.c_str()].type() != Json::objectValue) |
271 } | 285 { |
272 else | 286 throw OrthancException(ErrorCode_BadFileFormat, |
273 { | 287 "Associative array of DICOM tags to strings expected in field: " + field); |
274 target[members[i]] = tmp.asString(); | 288 } |
275 } | 289 |
276 } | 290 const Json::Value& source = value[field.c_str()]; |
277 } | 291 |
278 | 292 target.clear(); |
279 | 293 |
280 void ReadMapOfTags(std::map<DicomTag, std::string>& target, | 294 Json::Value::Members members = source.getMemberNames(); |
281 const Json::Value& value, | 295 |
282 const std::string& field) | 296 for (size_t i = 0; i < members.size(); i++) |
283 { | 297 { |
284 if (value.type() != Json::objectValue || | 298 const Json::Value& tmp = source[members[i]]; |
285 !value.isMember(field.c_str()) || | 299 |
286 value[field.c_str()].type() != Json::objectValue) | 300 DicomTag tag(0, 0); |
301 | |
302 if (!ParseTagInternal(tag, members[i].c_str()) || | |
303 tmp.type() != Json::stringValue) | |
287 { | 304 { |
288 throw OrthancException(ErrorCode_BadFileFormat, | 305 throw OrthancException(ErrorCode_BadFileFormat, |
289 "Associative array of DICOM tags to strings expected in field: " + field); | 306 "Associative array of DICOM tags to strings expected in field: " + field); |
290 } | 307 } |
291 | 308 else |
292 const Json::Value& source = value[field.c_str()]; | 309 { |
293 | 310 target[tag] = tmp.asString(); |
294 target.clear(); | 311 } |
295 | 312 } |
296 Json::Value::Members members = source.getMemberNames(); | 313 } |
297 | 314 |
298 for (size_t i = 0; i < members.size(); i++) | 315 |
299 { | 316 void SerializationToolbox::WriteArrayOfStrings(Json::Value& target, |
300 const Json::Value& tmp = source[members[i]]; | 317 const std::vector<std::string>& values, |
301 | 318 const std::string& field) |
302 DicomTag tag(0, 0); | 319 { |
303 | 320 if (target.type() != Json::objectValue || |
304 if (!ParseTagInternal(tag, members[i].c_str()) || | 321 target.isMember(field.c_str())) |
305 tmp.type() != Json::stringValue) | 322 { |
306 { | 323 throw OrthancException(ErrorCode_BadFileFormat); |
307 throw OrthancException(ErrorCode_BadFileFormat, | 324 } |
308 "Associative array of DICOM tags to strings expected in field: " + field); | 325 |
309 } | 326 Json::Value& value = target[field]; |
310 else | 327 |
311 { | 328 value = Json::arrayValue; |
312 target[tag] = tmp.asString(); | 329 for (size_t i = 0; i < values.size(); i++) |
313 } | 330 { |
314 } | 331 value.append(values[i]); |
315 } | 332 } |
316 | 333 } |
317 | 334 |
318 void WriteArrayOfStrings(Json::Value& target, | 335 |
319 const std::vector<std::string>& values, | 336 void SerializationToolbox::WriteListOfStrings(Json::Value& target, |
320 const std::string& field) | 337 const std::list<std::string>& values, |
321 { | 338 const std::string& field) |
322 if (target.type() != Json::objectValue || | 339 { |
323 target.isMember(field.c_str())) | 340 if (target.type() != Json::objectValue || |
324 { | 341 target.isMember(field.c_str())) |
325 throw OrthancException(ErrorCode_BadFileFormat); | 342 { |
326 } | 343 throw OrthancException(ErrorCode_BadFileFormat); |
327 | 344 } |
328 Json::Value& value = target[field]; | 345 |
329 | 346 Json::Value& value = target[field]; |
330 value = Json::arrayValue; | 347 |
331 for (size_t i = 0; i < values.size(); i++) | 348 value = Json::arrayValue; |
332 { | 349 |
333 value.append(values[i]); | 350 for (std::list<std::string>::const_iterator it = values.begin(); |
334 } | 351 it != values.end(); ++it) |
335 } | 352 { |
336 | 353 value.append(*it); |
337 | 354 } |
338 void WriteListOfStrings(Json::Value& target, | 355 } |
339 const std::list<std::string>& values, | 356 |
340 const std::string& field) | 357 |
341 { | 358 void SerializationToolbox::WriteSetOfStrings(Json::Value& target, |
342 if (target.type() != Json::objectValue || | 359 const std::set<std::string>& values, |
343 target.isMember(field.c_str())) | 360 const std::string& field) |
344 { | 361 { |
345 throw OrthancException(ErrorCode_BadFileFormat); | 362 if (target.type() != Json::objectValue || |
346 } | 363 target.isMember(field.c_str())) |
347 | 364 { |
348 Json::Value& value = target[field]; | 365 throw OrthancException(ErrorCode_BadFileFormat); |
349 | 366 } |
350 value = Json::arrayValue; | 367 |
351 | 368 Json::Value& value = target[field]; |
352 for (std::list<std::string>::const_iterator it = values.begin(); | 369 |
353 it != values.end(); ++it) | 370 value = Json::arrayValue; |
354 { | 371 |
355 value.append(*it); | 372 for (std::set<std::string>::const_iterator it = values.begin(); |
356 } | 373 it != values.end(); ++it) |
357 } | 374 { |
358 | 375 value.append(*it); |
359 | 376 } |
360 void WriteSetOfStrings(Json::Value& target, | 377 } |
361 const std::set<std::string>& values, | 378 |
362 const std::string& field) | 379 |
363 { | 380 void SerializationToolbox::WriteSetOfTags(Json::Value& target, |
364 if (target.type() != Json::objectValue || | 381 const std::set<DicomTag>& tags, |
365 target.isMember(field.c_str())) | 382 const std::string& field) |
366 { | 383 { |
367 throw OrthancException(ErrorCode_BadFileFormat); | 384 if (target.type() != Json::objectValue || |
368 } | 385 target.isMember(field.c_str())) |
369 | 386 { |
370 Json::Value& value = target[field]; | 387 throw OrthancException(ErrorCode_BadFileFormat); |
371 | 388 } |
372 value = Json::arrayValue; | 389 |
373 | 390 Json::Value& value = target[field]; |
374 for (std::set<std::string>::const_iterator it = values.begin(); | 391 |
375 it != values.end(); ++it) | 392 value = Json::arrayValue; |
376 { | 393 |
377 value.append(*it); | 394 for (std::set<DicomTag>::const_iterator it = tags.begin(); |
378 } | 395 it != tags.end(); ++it) |
379 } | 396 { |
380 | 397 value.append(it->Format()); |
381 | 398 } |
382 void WriteSetOfTags(Json::Value& target, | 399 } |
383 const std::set<DicomTag>& tags, | 400 |
384 const std::string& field) | 401 |
385 { | 402 void SerializationToolbox::WriteMapOfStrings(Json::Value& target, |
386 if (target.type() != Json::objectValue || | 403 const std::map<std::string, std::string>& values, |
387 target.isMember(field.c_str())) | 404 const std::string& field) |
388 { | 405 { |
389 throw OrthancException(ErrorCode_BadFileFormat); | 406 if (target.type() != Json::objectValue || |
390 } | 407 target.isMember(field.c_str())) |
391 | 408 { |
392 Json::Value& value = target[field]; | 409 throw OrthancException(ErrorCode_BadFileFormat); |
393 | 410 } |
394 value = Json::arrayValue; | 411 |
395 | 412 Json::Value& value = target[field]; |
396 for (std::set<DicomTag>::const_iterator it = tags.begin(); | 413 |
397 it != tags.end(); ++it) | 414 value = Json::objectValue; |
398 { | 415 |
399 value.append(it->Format()); | 416 for (std::map<std::string, std::string>::const_iterator |
400 } | 417 it = values.begin(); it != values.end(); ++it) |
401 } | 418 { |
402 | 419 value[it->first] = it->second; |
403 | 420 } |
404 void WriteMapOfStrings(Json::Value& target, | 421 } |
405 const std::map<std::string, std::string>& values, | 422 |
406 const std::string& field) | 423 |
407 { | 424 void SerializationToolbox::WriteMapOfTags(Json::Value& target, |
408 if (target.type() != Json::objectValue || | 425 const std::map<DicomTag, std::string>& values, |
409 target.isMember(field.c_str())) | 426 const std::string& field) |
410 { | 427 { |
411 throw OrthancException(ErrorCode_BadFileFormat); | 428 if (target.type() != Json::objectValue || |
412 } | 429 target.isMember(field.c_str())) |
413 | 430 { |
414 Json::Value& value = target[field]; | 431 throw OrthancException(ErrorCode_BadFileFormat); |
415 | 432 } |
416 value = Json::objectValue; | 433 |
417 | 434 Json::Value& value = target[field]; |
418 for (std::map<std::string, std::string>::const_iterator | 435 |
419 it = values.begin(); it != values.end(); ++it) | 436 value = Json::objectValue; |
420 { | 437 |
421 value[it->first] = it->second; | 438 for (std::map<DicomTag, std::string>::const_iterator |
422 } | 439 it = values.begin(); it != values.end(); ++it) |
423 } | 440 { |
424 | 441 value[it->first.Format()] = it->second; |
425 | |
426 void WriteMapOfTags(Json::Value& target, | |
427 const std::map<DicomTag, std::string>& values, | |
428 const std::string& field) | |
429 { | |
430 if (target.type() != Json::objectValue || | |
431 target.isMember(field.c_str())) | |
432 { | |
433 throw OrthancException(ErrorCode_BadFileFormat); | |
434 } | |
435 | |
436 Json::Value& value = target[field]; | |
437 | |
438 value = Json::objectValue; | |
439 | |
440 for (std::map<DicomTag, std::string>::const_iterator | |
441 it = values.begin(); it != values.end(); ++it) | |
442 { | |
443 value[it->first.Format()] = it->second; | |
444 } | |
445 } | 442 } |
446 } | 443 } |
447 } | 444 } |