| 269 | } |
| 270 | |
| 271 | void mitk::SegmentationTaskListIO::Write() |
| 272 | { |
| 273 | auto segmentationTaskList = dynamic_cast<const SegmentationTaskList*>(this->GetInput()); |
| 274 | |
| 275 | if (nullptr == segmentationTaskList) |
| 276 | mitkThrow() << "Invalid input for writing!"; |
| 277 | |
| 278 | if (segmentationTaskList->GetNumberOfTasks() == 0) |
| 279 | mitkThrow() << "No tasks found!"; |
| 280 | |
| 281 | auto* stream = this->GetOutputStream(); |
| 282 | std::ofstream fileStream; |
| 283 | |
| 284 | if (nullptr == stream) |
| 285 | { |
| 286 | auto filename = this->GetOutputLocation(); |
| 287 | |
| 288 | if (filename.empty()) |
| 289 | mitkThrow() << "Neither an output stream nor an output filename was specified!"; |
| 290 | |
| 291 | fileStream.open(filename); |
| 292 | |
| 293 | if (!fileStream.is_open()) |
| 294 | mitkThrow() << "Could not open file \"" << filename << "\" for writing!"; |
| 295 | |
| 296 | stream = &fileStream; |
| 297 | } |
| 298 | |
| 299 | if (!stream->good()) |
| 300 | mitkThrow() << "Stream for writing is not good!"; |
| 301 | |
| 302 | nlohmann::ordered_json json = { |
| 303 | { "FileFormat", "MITK Segmentation Task List" }, |
| 304 | { "Version", this->GetMinimumRequiredVersion(segmentationTaskList) }, |
| 305 | { "Name", segmentationTaskList->GetProperty("name")->GetValueAsString() } |
| 306 | }; |
| 307 | |
| 308 | nlohmann::json defaults = segmentationTaskList->GetDefaults(); |
| 309 | |
| 310 | if (!defaults.is_null()) |
| 311 | json["Defaults"] = defaults; |
| 312 | |
| 313 | nlohmann::json tasks; |
| 314 | |
| 315 | for (const auto& task : *segmentationTaskList) |
| 316 | tasks.push_back(task); |
| 317 | |
| 318 | json["Tasks"] = tasks; |
| 319 | |
| 320 | *stream << std::setw(2) << json << std::endl; |
| 321 | } |
| 322 | |
| 323 | int mitk::SegmentationTaskListIO::GetMinimumRequiredVersion(const SegmentationTaskList* segmentationTaskList) const |
| 324 | { |
nothing calls this directly
no test coverage detected