------------------------------------------------------------------------------
| 198 | |
| 199 | //------------------------------------------------------------------------------ |
| 200 | std::vector<vtkTypeUInt32> vtkObjectManager::ImportFromJSON(const nlohmann::json& importJson) |
| 201 | { |
| 202 | std::vector<vtkTypeUInt32> strongObjectIds; |
| 203 | try |
| 204 | { |
| 205 | // Register all the states. |
| 206 | auto statesIter = importJson.find("States"); |
| 207 | if (statesIter != importJson.end()) |
| 208 | { |
| 209 | for (const auto& state : statesIter->items()) |
| 210 | { |
| 211 | this->Context->RegisterState(state.value()); |
| 212 | const auto identifier = state.value().at("Id").get<vtkTypeUInt32>(); |
| 213 | if (state.value().find("vtk-object-manager-kept-alive") != state.value().end() && |
| 214 | (state.value().at("vtk-object-manager-kept-alive").get<bool>() == true)) |
| 215 | { |
| 216 | strongObjectIds.emplace_back(identifier); |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | catch (nlohmann::json::exception& e) |
| 222 | { |
| 223 | vtkErrorMacro(<< "Failed to import states from byte array. message=" << e.what()); |
| 224 | } |
| 225 | try |
| 226 | { |
| 227 | // Register all the blobs. |
| 228 | auto blobsIter = importJson.find("Blobs"); |
| 229 | if (blobsIter != importJson.end()) |
| 230 | { |
| 231 | for (const auto& blob : blobsIter->items()) |
| 232 | { |
| 233 | auto hash = blob.key(); |
| 234 | auto byteArray = vtk::TakeSmartPointer(vtkTypeUInt8Array::New()); |
| 235 | if (blob.value().is_object()) |
| 236 | { |
| 237 | // when import json from a file, the type is object, and values must be copied into a |
| 238 | // vector. |
| 239 | const auto values = blob.value().at("bytes").get<std::vector<vtkTypeUInt8>>(); |
| 240 | if (!values.empty()) |
| 241 | { |
| 242 | auto* bytePtr = const_cast<vtkTypeUInt8*>(values.data()); |
| 243 | const vtkIdType numValues = static_cast<vtkIdType>(values.size()); |
| 244 | byteArray->SetArray(bytePtr, numValues, /*save=*/1); |
| 245 | } |
| 246 | } |
| 247 | else |
| 248 | { |
| 249 | const auto& values = blob.value().get_binary(); |
| 250 | if (!values.empty()) |
| 251 | { |
| 252 | auto* bytePtr = const_cast<vtkTypeUInt8*>(values.data()); |
| 253 | const vtkIdType numValues = static_cast<vtkIdType>(values.size()); |
| 254 | byteArray->SetArray(bytePtr, numValues, /*save=*/1); |
| 255 | } |
| 256 | } |
| 257 | this->Context->RegisterBlob(byteArray, hash); |
no test coverage detected