comparison OrthancFramework/Sources/SerializationToolbox.cpp @ 5379:b31c73bc7cb6

Toolbox : more set functions
author Alain Mazy <am@osimis.io>
date Thu, 31 Aug 2023 15:20:41 +0200
parents f783b99e4738
children 97004471a5c5
comparison
equal deleted inserted replaced
5378:d857c6210c50 5379:b31c73bc7cb6
176 if (value.type() != Json::objectValue || 176 if (value.type() != Json::objectValue ||
177 !value.isMember(field.c_str()) || 177 !value.isMember(field.c_str()) ||
178 value[field.c_str()].type() != Json::arrayValue) 178 value[field.c_str()].type() != Json::arrayValue)
179 { 179 {
180 throw OrthancException(ErrorCode_BadFileFormat, 180 throw OrthancException(ErrorCode_BadFileFormat,
181 "List of strings expected in field: " + field); 181 "List of strings expected in field: " + field);
182 } 182 }
183 183
184 const Json::Value& arr = value[field.c_str()]; 184 const Json::Value& arr = value[field.c_str()];
185 185
186 target.resize(arr.size()); 186 try
187 187 {
188 for (Json::Value::ArrayIndex i = 0; i < arr.size(); i++) 188 ReadArrayOfStrings(target, arr);
189 { 189 }
190 if (arr[i].type() != Json::stringValue) 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)
191 { 212 {
192 throw OrthancException(ErrorCode_BadFileFormat, 213 throw OrthancException(ErrorCode_BadFileFormat,
193 "List of strings expected in field: " + field); 214 "List of strings expected");
194 } 215 }
195 else 216 else
196 { 217 {
197 target[i] = arr[i].asString(); 218 target[i] = array[i].asString();
198 } 219 }
199 } 220 }
200 } 221 }
201 222
202 223
219 const Json::Value& value, 240 const Json::Value& value,
220 const std::string& field) 241 const std::string& field)
221 { 242 {
222 std::vector<std::string> tmp; 243 std::vector<std::string> tmp;
223 ReadArrayOfStrings(tmp, value, field); 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);
224 259
225 target.clear(); 260 target.clear();
226 for (size_t i = 0; i < tmp.size(); i++) 261 for (size_t i = 0; i < tmp.size(); i++)
227 { 262 {
228 target.insert(tmp[i]); 263 target.insert(tmp[i]);