comparison OrthancFramework/Sources/SerializationToolbox.cpp @ 5380:97004471a5c5

Toolbox : more set functions (cont)
author Alain Mazy <am@osimis.io>
date Thu, 31 Aug 2023 16:34:53 +0200
parents b31c73bc7cb6
children 48b8dae6dc77
comparison
equal deleted inserted replaced
5379:b31c73bc7cb6 5380:97004471a5c5
168 } 168 }
169 } 169 }
170 170
171 171
172 void SerializationToolbox::ReadArrayOfStrings(std::vector<std::string>& target, 172 void SerializationToolbox::ReadArrayOfStrings(std::vector<std::string>& target,
173 const Json::Value& value, 173 const Json::Value& valueObject,
174 const std::string& field) 174 const std::string& field)
175 {
176 if (valueObject.type() != Json::objectValue ||
177 !valueObject.isMember(field.c_str()) ||
178 valueObject[field.c_str()].type() != Json::arrayValue)
179 {
180 throw OrthancException(ErrorCode_BadFileFormat,
181 "List of strings expected in field: " + field);
182 }
183
184 const Json::Value& arr = valueObject[field.c_str()];
185
186 try
187 {
188 ReadArrayOfStrings(target, arr);
189 }
190 catch (OrthancException& ex)
191 { // more detailed error
192 throw OrthancException(ErrorCode_BadFileFormat,
193 "List of strings expected in field: " + field);
194 }
195 }
196
197
198 void SerializationToolbox::ReadArrayOfStrings(std::vector<std::string>& target,
199 const Json::Value& valueArray)
200 {
201 if (valueArray.type() != Json::arrayValue)
202 {
203 throw OrthancException(ErrorCode_BadFileFormat,
204 "List of strings expected");
205 }
206
207 target.resize(valueArray.size());
208
209 for (Json::Value::ArrayIndex i = 0; i < valueArray.size(); i++)
210 {
211 if (valueArray[i].type() != Json::stringValue)
212 {
213 throw OrthancException(ErrorCode_BadFileFormat,
214 "List of strings expected");
215 }
216 else
217 {
218 target[i] = valueArray[i].asString();
219 }
220 }
221 }
222
223
224 void SerializationToolbox::ReadListOfStrings(std::list<std::string>& target,
225 const Json::Value& value,
226 const std::string& field)
227 {
228 std::vector<std::string> tmp;
229 ReadArrayOfStrings(tmp, value, field);
230
231 target.clear();
232 for (size_t i = 0; i < tmp.size(); i++)
233 {
234 target.push_back(tmp[i]);
235 }
236 }
237
238
239 void SerializationToolbox::ReadSetOfStrings(std::set<std::string>& target,
240 const Json::Value& valueObject,
241 const std::string& field)
242 {
243 std::vector<std::string> tmp;
244 ReadArrayOfStrings(tmp, valueObject, field);
245
246 target.clear();
247 for (size_t i = 0; i < tmp.size(); i++)
248 {
249 target.insert(tmp[i]);
250 }
251 }
252
253
254 void SerializationToolbox::ReadSetOfStrings(std::set<std::string>& target,
255 const Json::Value& valueArray)
256 {
257 std::vector<std::string> tmp;
258 ReadArrayOfStrings(tmp, valueArray);
259
260 target.clear();
261 for (size_t i = 0; i < tmp.size(); i++)
262 {
263 target.insert(tmp[i]);
264 }
265 }
266
267
268 void SerializationToolbox::ReadSetOfTags(std::set<DicomTag>& target,
269 const Json::Value& value,
270 const std::string& field)
175 { 271 {
176 if (value.type() != Json::objectValue || 272 if (value.type() != Json::objectValue ||
177 !value.isMember(field.c_str()) || 273 !value.isMember(field.c_str()) ||
178 value[field.c_str()].type() != Json::arrayValue) 274 value[field.c_str()].type() != Json::arrayValue)
179 { 275 {
180 throw OrthancException(ErrorCode_BadFileFormat, 276 throw OrthancException(ErrorCode_BadFileFormat,
181 "List of strings expected in field: " + field);
182 }
183
184 const Json::Value& arr = value[field.c_str()];
185
186 try
187 {
188 ReadArrayOfStrings(target, arr);
189 }
190 catch (OrthancException& ex)
191 { // more detailed error
192 throw OrthancException(ErrorCode_BadFileFormat,
193 "List of strings expected in field: " + field);
194 }
195 }
196
197
198 void SerializationToolbox::ReadArrayOfStrings(std::vector<std::string>& target,
199 const Json::Value& array)
200 {
201 if (array.type() != Json::arrayValue)
202 {
203 throw OrthancException(ErrorCode_BadFileFormat,
204 "List of strings expected");
205 }
206
207 target.resize(array.size());
208
209 for (Json::Value::ArrayIndex i = 0; i < array.size(); i++)
210 {
211 if (array[i].type() != Json::stringValue)
212 {
213 throw OrthancException(ErrorCode_BadFileFormat,
214 "List of strings expected");
215 }
216 else
217 {
218 target[i] = array[i].asString();
219 }
220 }
221 }
222
223
224 void SerializationToolbox::ReadListOfStrings(std::list<std::string>& target,
225 const Json::Value& value,
226 const std::string& field)
227 {
228 std::vector<std::string> tmp;
229 ReadArrayOfStrings(tmp, value, field);
230
231 target.clear();
232 for (size_t i = 0; i < tmp.size(); i++)
233 {
234 target.push_back(tmp[i]);
235 }
236 }
237
238
239 void SerializationToolbox::ReadSetOfStrings(std::set<std::string>& target,
240 const Json::Value& value,
241 const std::string& field)
242 {
243 std::vector<std::string> tmp;
244 ReadArrayOfStrings(tmp, value, field);
245
246 target.clear();
247 for (size_t i = 0; i < tmp.size(); i++)
248 {
249 target.insert(tmp[i]);
250 }
251 }
252
253
254 void SerializationToolbox::ReadSetOfStrings(std::set<std::string>& target,
255 const Json::Value& value)
256 {
257 std::vector<std::string> tmp;
258 ReadArrayOfStrings(tmp, value);
259
260 target.clear();
261 for (size_t i = 0; i < tmp.size(); i++)
262 {
263 target.insert(tmp[i]);
264 }
265 }
266
267
268 void SerializationToolbox::ReadSetOfTags(std::set<DicomTag>& target,
269 const Json::Value& value,
270 const std::string& field)
271 {
272 if (value.type() != Json::objectValue ||
273 !value.isMember(field.c_str()) ||
274 value[field.c_str()].type() != Json::arrayValue)
275 {
276 throw OrthancException(ErrorCode_BadFileFormat,
277 "Set of DICOM tags expected in field: " + field); 277 "Set of DICOM tags expected in field: " + field);
278 } 278 }
279 279
280 const Json::Value& arr = value[field.c_str()]; 280 const Json::Value& arr = value[field.c_str()];
281 281
412 value.append(*it); 412 value.append(*it);
413 } 413 }
414 } 414 }
415 415
416 416
417 void SerializationToolbox::WriteSetOfStrings(Json::Value& target, 417 void SerializationToolbox::WriteSetOfStrings(Json::Value& targetObject,
418 const std::set<std::string>& values, 418 const std::set<std::string>& values,
419 const std::string& field) 419 const std::string& field)
420 { 420 {
421 if (target.type() != Json::objectValue || 421 if (targetObject.type() != Json::objectValue ||
422 target.isMember(field.c_str())) 422 targetObject.isMember(field.c_str()))
423 { 423 {
424 throw OrthancException(ErrorCode_BadFileFormat); 424 throw OrthancException(ErrorCode_BadFileFormat);
425 } 425 }
426 426
427 Json::Value& value = target[field]; 427 Json::Value& targetArray = targetObject[field];
428 428
429 value = Json::arrayValue; 429 targetArray = Json::arrayValue;
430
431 WriteSetOfStrings(targetArray, values);
432 }
433
434
435 void SerializationToolbox::WriteSetOfStrings(Json::Value& targetArray,
436 const std::set<std::string>& values)
437 {
438 if (targetArray.type() != Json::arrayValue)
439 {
440 throw OrthancException(ErrorCode_BadFileFormat);
441 }
442
443 targetArray.clear();
430 444
431 for (std::set<std::string>::const_iterator it = values.begin(); 445 for (std::set<std::string>::const_iterator it = values.begin();
432 it != values.end(); ++it) 446 it != values.end(); ++it)
433 { 447 {
434 value.append(*it); 448 targetArray.append(*it);
435 } 449 }
436 } 450 }
437 451
438 452
439 void SerializationToolbox::WriteSetOfTags(Json::Value& target, 453 void SerializationToolbox::WriteSetOfTags(Json::Value& target,