comparison OrthancFramework/Sources/SerializationToolbox.cpp @ 5807:8279eaab0d1d attach-custom-data

merged default -> attach-custom-data
author Alain Mazy <am@orthanc.team>
date Tue, 24 Sep 2024 11:39:52 +0200
parents f7adfb22e20e
children
comparison
equal deleted inserted replaced
5085:79f98ee4f04b 5807:8279eaab0d1d
1 /** 1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store 2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics 3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium 4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2022 Osimis S.A., Belgium 5 * Copyright (C) 2017-2023 Osimis S.A., Belgium
6 * Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium 6 * Copyright (C) 2024-2024 Orthanc Team SRL, Belgium
7 * Copyright (C) 2021-2024 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
7 * 8 *
8 * This program is free software: you can redistribute it and/or 9 * This program is free software: you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License 10 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation, either version 3 of 11 * as published by the Free Software Foundation, either version 3 of
11 * the License, or (at your option) any later version. 12 * the License, or (at your option) any later version.
70 return value[field.c_str()].asString(); 71 return value[field.c_str()].asString();
71 } 72 }
72 } 73 }
73 74
74 75
76 std::string SerializationToolbox::ReadString(const Json::Value& value,
77 const std::string& field,
78 const std::string& defaultValue)
79 {
80 if (value.isMember(field.c_str()))
81 {
82 return ReadString(value, field);
83 }
84 else
85 {
86 return defaultValue;
87 }
88 }
89
90
75 int SerializationToolbox::ReadInteger(const Json::Value& value, 91 int SerializationToolbox::ReadInteger(const Json::Value& value,
76 const std::string& field) 92 const std::string& field)
77 { 93 {
78 if (value.type() != Json::objectValue || 94 if (value.type() != Json::objectValue ||
79 !value.isMember(field.c_str()) || 95 !value.isMember(field.c_str()) ||
153 } 169 }
154 } 170 }
155 171
156 172
157 void SerializationToolbox::ReadArrayOfStrings(std::vector<std::string>& target, 173 void SerializationToolbox::ReadArrayOfStrings(std::vector<std::string>& target,
158 const Json::Value& value, 174 const Json::Value& valueObject,
159 const std::string& field) 175 const std::string& field)
176 {
177 if (valueObject.type() != Json::objectValue ||
178 !valueObject.isMember(field.c_str()) ||
179 valueObject[field.c_str()].type() != Json::arrayValue)
180 {
181 throw OrthancException(ErrorCode_BadFileFormat,
182 "List of strings expected in field: " + field);
183 }
184
185 const Json::Value& arr = valueObject[field.c_str()];
186
187 try
188 {
189 ReadArrayOfStrings(target, arr);
190 }
191 catch (OrthancException& ex)
192 { // more detailed error
193 throw OrthancException(ErrorCode_BadFileFormat,
194 "List of strings expected in field: " + field);
195 }
196 }
197
198
199 void SerializationToolbox::ReadArrayOfStrings(std::vector<std::string>& target,
200 const Json::Value& valueArray)
201 {
202 if (valueArray.type() != Json::arrayValue)
203 {
204 throw OrthancException(ErrorCode_BadFileFormat,
205 "List of strings expected");
206 }
207
208 target.resize(valueArray.size());
209
210 for (Json::Value::ArrayIndex i = 0; i < valueArray.size(); i++)
211 {
212 if (valueArray[i].type() != Json::stringValue)
213 {
214 throw OrthancException(ErrorCode_BadFileFormat,
215 "List of strings expected");
216 }
217 else
218 {
219 target[i] = valueArray[i].asString();
220 }
221 }
222 }
223
224
225 void SerializationToolbox::ReadListOfStrings(std::list<std::string>& target,
226 const Json::Value& value,
227 const std::string& field)
228 {
229 std::vector<std::string> tmp;
230 ReadArrayOfStrings(tmp, value, field);
231
232 target.clear();
233 for (size_t i = 0; i < tmp.size(); i++)
234 {
235 target.push_back(tmp[i]);
236 }
237 }
238
239
240 void SerializationToolbox::ReadSetOfStrings(std::set<std::string>& target,
241 const Json::Value& valueObject,
242 const std::string& field)
243 {
244 std::vector<std::string> tmp;
245 ReadArrayOfStrings(tmp, valueObject, field);
246
247 target.clear();
248 for (size_t i = 0; i < tmp.size(); i++)
249 {
250 target.insert(tmp[i]);
251 }
252 }
253
254
255 void SerializationToolbox::ReadSetOfStrings(std::set<std::string>& target,
256 const Json::Value& valueArray)
257 {
258 std::vector<std::string> tmp;
259 ReadArrayOfStrings(tmp, valueArray);
260
261 target.clear();
262 for (size_t i = 0; i < tmp.size(); i++)
263 {
264 target.insert(tmp[i]);
265 }
266 }
267
268
269 void SerializationToolbox::ReadSetOfTags(std::set<DicomTag>& target,
270 const Json::Value& value,
271 const std::string& field)
160 { 272 {
161 if (value.type() != Json::objectValue || 273 if (value.type() != Json::objectValue ||
162 !value.isMember(field.c_str()) || 274 !value.isMember(field.c_str()) ||
163 value[field.c_str()].type() != Json::arrayValue) 275 value[field.c_str()].type() != Json::arrayValue)
164 { 276 {
165 throw OrthancException(ErrorCode_BadFileFormat, 277 throw OrthancException(ErrorCode_BadFileFormat,
166 "List of strings expected in field: " + field);
167 }
168
169 const Json::Value& arr = value[field.c_str()];
170
171 target.resize(arr.size());
172
173 for (Json::Value::ArrayIndex i = 0; i < arr.size(); i++)
174 {
175 if (arr[i].type() != Json::stringValue)
176 {
177 throw OrthancException(ErrorCode_BadFileFormat,
178 "List of strings expected in field: " + field);
179 }
180 else
181 {
182 target[i] = arr[i].asString();
183 }
184 }
185 }
186
187
188 void SerializationToolbox::ReadListOfStrings(std::list<std::string>& target,
189 const Json::Value& value,
190 const std::string& field)
191 {
192 std::vector<std::string> tmp;
193 ReadArrayOfStrings(tmp, value, field);
194
195 target.clear();
196 for (size_t i = 0; i < tmp.size(); i++)
197 {
198 target.push_back(tmp[i]);
199 }
200 }
201
202
203 void SerializationToolbox::ReadSetOfStrings(std::set<std::string>& target,
204 const Json::Value& value,
205 const std::string& field)
206 {
207 std::vector<std::string> tmp;
208 ReadArrayOfStrings(tmp, value, field);
209
210 target.clear();
211 for (size_t i = 0; i < tmp.size(); i++)
212 {
213 target.insert(tmp[i]);
214 }
215 }
216
217
218 void SerializationToolbox::ReadSetOfTags(std::set<DicomTag>& target,
219 const Json::Value& value,
220 const std::string& field)
221 {
222 if (value.type() != Json::objectValue ||
223 !value.isMember(field.c_str()) ||
224 value[field.c_str()].type() != Json::arrayValue)
225 {
226 throw OrthancException(ErrorCode_BadFileFormat,
227 "Set of DICOM tags expected in field: " + field); 278 "Set of DICOM tags expected in field: " + field);
228 } 279 }
229 280
230 const Json::Value& arr = value[field.c_str()]; 281 const Json::Value& arr = value[field.c_str()];
231 282
362 value.append(*it); 413 value.append(*it);
363 } 414 }
364 } 415 }
365 416
366 417
367 void SerializationToolbox::WriteSetOfStrings(Json::Value& target, 418 void SerializationToolbox::WriteSetOfStrings(Json::Value& targetObject,
368 const std::set<std::string>& values, 419 const std::set<std::string>& values,
369 const std::string& field) 420 const std::string& field)
370 { 421 {
371 if (target.type() != Json::objectValue || 422 if (targetObject.type() != Json::objectValue ||
372 target.isMember(field.c_str())) 423 targetObject.isMember(field.c_str()))
373 { 424 {
374 throw OrthancException(ErrorCode_BadFileFormat); 425 throw OrthancException(ErrorCode_BadFileFormat);
375 } 426 }
376 427
377 Json::Value& value = target[field]; 428 Json::Value& targetArray = targetObject[field];
378 429
379 value = Json::arrayValue; 430 targetArray = Json::arrayValue;
431
432 WriteSetOfStrings(targetArray, values);
433 }
434
435
436 void SerializationToolbox::WriteSetOfStrings(Json::Value& targetArray,
437 const std::set<std::string>& values)
438 {
439 if (targetArray.type() != Json::arrayValue)
440 {
441 throw OrthancException(ErrorCode_BadFileFormat);
442 }
443
444 targetArray.clear();
380 445
381 for (std::set<std::string>::const_iterator it = values.begin(); 446 for (std::set<std::string>::const_iterator it = values.begin();
382 it != values.end(); ++it) 447 it != values.end(); ++it)
383 { 448 {
384 value.append(*it); 449 targetArray.append(*it);
385 } 450 }
386 } 451 }
387 452
388 453
389 void SerializationToolbox::WriteSetOfTags(Json::Value& target, 454 void SerializationToolbox::WriteSetOfTags(Json::Value& target,