Mercurial > hg > orthanc
comparison OrthancFramework/Sources/SerializationToolbox.cpp @ 4044:d25f4c0fa160 framework
splitting code into OrthancFramework and OrthancServer
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 10 Jun 2020 20:30:34 +0200 |
parents | Core/SerializationToolbox.cpp@5d2348b39392 |
children | bf7b9edf6b81 |
comparison
equal
deleted
inserted
replaced
4043:6c6239aec462 | 4044:d25f4c0fa160 |
---|---|
1 /** | |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium | |
6 * | |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU General Public License as | |
9 * published by the Free Software Foundation, either version 3 of the | |
10 * License, or (at your option) any later version. | |
11 * | |
12 * In addition, as a special exception, the copyright holders of this | |
13 * program give permission to link the code of its release with the | |
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
15 * that use the same license as the "OpenSSL" library), and distribute | |
16 * the linked executables. You must obey the GNU General Public License | |
17 * in all respects for all of the code used other than "OpenSSL". If you | |
18 * modify file(s) with this exception, you may extend this exception to | |
19 * your version of the file(s), but you are not obligated to do so. If | |
20 * you do not wish to do so, delete this exception statement from your | |
21 * version. If you delete this exception statement from all source files | |
22 * in the program, then also delete it here. | |
23 * | |
24 * This program is distributed in the hope that it will be useful, but | |
25 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
27 * General Public License for more details. | |
28 * | |
29 * You should have received a copy of the GNU General Public License | |
30 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
31 **/ | |
32 | |
33 | |
34 #include "PrecompiledHeaders.h" | |
35 #include "SerializationToolbox.h" | |
36 | |
37 #include "OrthancException.h" | |
38 | |
39 #if ORTHANC_ENABLE_DCMTK == 1 | |
40 # include "DicomParsing/FromDcmtkBridge.h" | |
41 #endif | |
42 | |
43 namespace Orthanc | |
44 { | |
45 static bool ParseTagInternal(DicomTag& tag, | |
46 const char* name) | |
47 { | |
48 #if ORTHANC_ENABLE_DCMTK == 1 | |
49 try | |
50 { | |
51 tag = FromDcmtkBridge::ParseTag(name); | |
52 return true; | |
53 } | |
54 catch (OrthancException&) | |
55 { | |
56 return false; | |
57 } | |
58 #else | |
59 return DicomTag::ParseHexadecimal(tag, name); | |
60 #endif | |
61 } | |
62 | |
63 | |
64 std::string SerializationToolbox::ReadString(const Json::Value& value, | |
65 const std::string& field) | |
66 { | |
67 if (value.type() != Json::objectValue || | |
68 !value.isMember(field.c_str()) || | |
69 value[field.c_str()].type() != Json::stringValue) | |
70 { | |
71 throw OrthancException(ErrorCode_BadFileFormat, | |
72 "String value expected in field: " + field); | |
73 } | |
74 else | |
75 { | |
76 return value[field.c_str()].asString(); | |
77 } | |
78 } | |
79 | |
80 | |
81 int SerializationToolbox::ReadInteger(const Json::Value& value, | |
82 const std::string& field) | |
83 { | |
84 if (value.type() != Json::objectValue || | |
85 !value.isMember(field.c_str()) || | |
86 (value[field.c_str()].type() != Json::intValue && | |
87 value[field.c_str()].type() != Json::uintValue)) | |
88 { | |
89 throw OrthancException(ErrorCode_BadFileFormat, | |
90 "Integer value expected in field: " + field); | |
91 } | |
92 else | |
93 { | |
94 return value[field.c_str()].asInt(); | |
95 } | |
96 } | |
97 | |
98 | |
99 int SerializationToolbox::ReadInteger(const Json::Value& value, | |
100 const std::string& field, | |
101 int defaultValue) | |
102 { | |
103 if (value.isMember(field.c_str())) | |
104 { | |
105 return ReadInteger(value, field); | |
106 } | |
107 else | |
108 { | |
109 return defaultValue; | |
110 } | |
111 } | |
112 | |
113 | |
114 unsigned int SerializationToolbox::ReadUnsignedInteger(const Json::Value& value, | |
115 const std::string& field) | |
116 { | |
117 int tmp = ReadInteger(value, field); | |
118 | |
119 if (tmp < 0) | |
120 { | |
121 throw OrthancException(ErrorCode_BadFileFormat, | |
122 "Unsigned integer value expected in field: " + field); | |
123 } | |
124 else | |
125 { | |
126 return static_cast<unsigned int>(tmp); | |
127 } | |
128 } | |
129 | |
130 | |
131 bool SerializationToolbox::ReadBoolean(const Json::Value& value, | |
132 const std::string& field) | |
133 { | |
134 if (value.type() != Json::objectValue || | |
135 !value.isMember(field.c_str()) || | |
136 value[field.c_str()].type() != Json::booleanValue) | |
137 { | |
138 throw OrthancException(ErrorCode_BadFileFormat, | |
139 "Boolean value expected in field: " + field); | |
140 } | |
141 else | |
142 { | |
143 return value[field.c_str()].asBool(); | |
144 } | |
145 } | |
146 | |
147 | |
148 void SerializationToolbox::ReadArrayOfStrings(std::vector<std::string>& target, | |
149 const Json::Value& value, | |
150 const std::string& field) | |
151 { | |
152 if (value.type() != Json::objectValue || | |
153 !value.isMember(field.c_str()) || | |
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) | |
167 { | |
168 throw OrthancException(ErrorCode_BadFileFormat, | |
169 "List of strings expected in field: " + field); | |
170 } | |
171 else | |
172 { | |
173 target[i] = arr[i].asString(); | |
174 } | |
175 } | |
176 } | |
177 | |
178 | |
179 void SerializationToolbox::ReadListOfStrings(std::list<std::string>& target, | |
180 const Json::Value& value, | |
181 const std::string& field) | |
182 { | |
183 std::vector<std::string> tmp; | |
184 ReadArrayOfStrings(tmp, value, field); | |
185 | |
186 target.clear(); | |
187 for (size_t i = 0; i < tmp.size(); i++) | |
188 { | |
189 target.push_back(tmp[i]); | |
190 } | |
191 } | |
192 | |
193 | |
194 void SerializationToolbox::ReadSetOfStrings(std::set<std::string>& target, | |
195 const Json::Value& value, | |
196 const std::string& field) | |
197 { | |
198 std::vector<std::string> tmp; | |
199 ReadArrayOfStrings(tmp, value, field); | |
200 | |
201 target.clear(); | |
202 for (size_t i = 0; i < tmp.size(); i++) | |
203 { | |
204 target.insert(tmp[i]); | |
205 } | |
206 } | |
207 | |
208 | |
209 void SerializationToolbox::ReadSetOfTags(std::set<DicomTag>& target, | |
210 const Json::Value& value, | |
211 const std::string& field) | |
212 { | |
213 if (value.type() != Json::objectValue || | |
214 !value.isMember(field.c_str()) || | |
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())) | |
231 { | |
232 throw OrthancException(ErrorCode_BadFileFormat, | |
233 "Set of DICOM tags expected in field: " + field); | |
234 } | |
235 else | |
236 { | |
237 target.insert(tag); | |
238 } | |
239 } | |
240 } | |
241 | |
242 | |
243 void SerializationToolbox::ReadMapOfStrings(std::map<std::string, std::string>& target, | |
244 const Json::Value& value, | |
245 const std::string& field) | |
246 { | |
247 if (value.type() != Json::objectValue || | |
248 !value.isMember(field.c_str()) || | |
249 value[field.c_str()].type() != Json::objectValue) | |
250 { | |
251 throw OrthancException(ErrorCode_BadFileFormat, | |
252 "Associative array of strings to strings expected in field: " + field); | |
253 } | |
254 | |
255 const Json::Value& source = value[field.c_str()]; | |
256 | |
257 target.clear(); | |
258 | |
259 Json::Value::Members members = source.getMemberNames(); | |
260 | |
261 for (size_t i = 0; i < members.size(); i++) | |
262 { | |
263 const Json::Value& tmp = source[members[i]]; | |
264 | |
265 if (tmp.type() != Json::stringValue) | |
266 { | |
267 throw OrthancException(ErrorCode_BadFileFormat, | |
268 "Associative array of string to strings expected in field: " + field); | |
269 } | |
270 else | |
271 { | |
272 target[members[i]] = tmp.asString(); | |
273 } | |
274 } | |
275 } | |
276 | |
277 | |
278 void SerializationToolbox::ReadMapOfTags(std::map<DicomTag, std::string>& target, | |
279 const Json::Value& value, | |
280 const std::string& field) | |
281 { | |
282 if (value.type() != Json::objectValue || | |
283 !value.isMember(field.c_str()) || | |
284 value[field.c_str()].type() != Json::objectValue) | |
285 { | |
286 throw OrthancException(ErrorCode_BadFileFormat, | |
287 "Associative array of DICOM tags to strings expected in field: " + field); | |
288 } | |
289 | |
290 const Json::Value& source = value[field.c_str()]; | |
291 | |
292 target.clear(); | |
293 | |
294 Json::Value::Members members = source.getMemberNames(); | |
295 | |
296 for (size_t i = 0; i < members.size(); i++) | |
297 { | |
298 const Json::Value& tmp = source[members[i]]; | |
299 | |
300 DicomTag tag(0, 0); | |
301 | |
302 if (!ParseTagInternal(tag, members[i].c_str()) || | |
303 tmp.type() != Json::stringValue) | |
304 { | |
305 throw OrthancException(ErrorCode_BadFileFormat, | |
306 "Associative array of DICOM tags to strings expected in field: " + field); | |
307 } | |
308 else | |
309 { | |
310 target[tag] = tmp.asString(); | |
311 } | |
312 } | |
313 } | |
314 | |
315 | |
316 void SerializationToolbox::WriteArrayOfStrings(Json::Value& target, | |
317 const std::vector<std::string>& values, | |
318 const std::string& field) | |
319 { | |
320 if (target.type() != Json::objectValue || | |
321 target.isMember(field.c_str())) | |
322 { | |
323 throw OrthancException(ErrorCode_BadFileFormat); | |
324 } | |
325 | |
326 Json::Value& value = target[field]; | |
327 | |
328 value = Json::arrayValue; | |
329 for (size_t i = 0; i < values.size(); i++) | |
330 { | |
331 value.append(values[i]); | |
332 } | |
333 } | |
334 | |
335 | |
336 void SerializationToolbox::WriteListOfStrings(Json::Value& target, | |
337 const std::list<std::string>& values, | |
338 const std::string& field) | |
339 { | |
340 if (target.type() != Json::objectValue || | |
341 target.isMember(field.c_str())) | |
342 { | |
343 throw OrthancException(ErrorCode_BadFileFormat); | |
344 } | |
345 | |
346 Json::Value& value = target[field]; | |
347 | |
348 value = Json::arrayValue; | |
349 | |
350 for (std::list<std::string>::const_iterator it = values.begin(); | |
351 it != values.end(); ++it) | |
352 { | |
353 value.append(*it); | |
354 } | |
355 } | |
356 | |
357 | |
358 void SerializationToolbox::WriteSetOfStrings(Json::Value& target, | |
359 const std::set<std::string>& values, | |
360 const std::string& field) | |
361 { | |
362 if (target.type() != Json::objectValue || | |
363 target.isMember(field.c_str())) | |
364 { | |
365 throw OrthancException(ErrorCode_BadFileFormat); | |
366 } | |
367 | |
368 Json::Value& value = target[field]; | |
369 | |
370 value = Json::arrayValue; | |
371 | |
372 for (std::set<std::string>::const_iterator it = values.begin(); | |
373 it != values.end(); ++it) | |
374 { | |
375 value.append(*it); | |
376 } | |
377 } | |
378 | |
379 | |
380 void SerializationToolbox::WriteSetOfTags(Json::Value& target, | |
381 const std::set<DicomTag>& tags, | |
382 const std::string& field) | |
383 { | |
384 if (target.type() != Json::objectValue || | |
385 target.isMember(field.c_str())) | |
386 { | |
387 throw OrthancException(ErrorCode_BadFileFormat); | |
388 } | |
389 | |
390 Json::Value& value = target[field]; | |
391 | |
392 value = Json::arrayValue; | |
393 | |
394 for (std::set<DicomTag>::const_iterator it = tags.begin(); | |
395 it != tags.end(); ++it) | |
396 { | |
397 value.append(it->Format()); | |
398 } | |
399 } | |
400 | |
401 | |
402 void SerializationToolbox::WriteMapOfStrings(Json::Value& target, | |
403 const std::map<std::string, std::string>& values, | |
404 const std::string& field) | |
405 { | |
406 if (target.type() != Json::objectValue || | |
407 target.isMember(field.c_str())) | |
408 { | |
409 throw OrthancException(ErrorCode_BadFileFormat); | |
410 } | |
411 | |
412 Json::Value& value = target[field]; | |
413 | |
414 value = Json::objectValue; | |
415 | |
416 for (std::map<std::string, std::string>::const_iterator | |
417 it = values.begin(); it != values.end(); ++it) | |
418 { | |
419 value[it->first] = it->second; | |
420 } | |
421 } | |
422 | |
423 | |
424 void SerializationToolbox::WriteMapOfTags(Json::Value& target, | |
425 const std::map<DicomTag, std::string>& values, | |
426 const std::string& field) | |
427 { | |
428 if (target.type() != Json::objectValue || | |
429 target.isMember(field.c_str())) | |
430 { | |
431 throw OrthancException(ErrorCode_BadFileFormat); | |
432 } | |
433 | |
434 Json::Value& value = target[field]; | |
435 | |
436 value = Json::objectValue; | |
437 | |
438 for (std::map<DicomTag, std::string>::const_iterator | |
439 it = values.begin(); it != values.end(); ++it) | |
440 { | |
441 value[it->first.Format()] = it->second; | |
442 } | |
443 } | |
444 } |