------------------------------------------------------------------------------
| 46 | |
| 47 | //------------------------------------------------------------------------------ |
| 48 | nlohmann::json vtkSerializer::SerializeJSON(vtkObjectBase* objectBase) |
| 49 | { |
| 50 | if ((this->Context == nullptr) || (objectBase == nullptr)) |
| 51 | { |
| 52 | return nlohmann::json::object(); |
| 53 | } |
| 54 | |
| 55 | vtkTypeUInt32 identifier = 0; |
| 56 | if (this->Context->HasId(objectBase, identifier)) |
| 57 | { |
| 58 | if (this->Context->IsProcessing(identifier) || this->Context->IsProcessed(identifier)) |
| 59 | { |
| 60 | vtkVLogF(this->GetSerializerLogVerbosity(), "Avoided serialization of %s", |
| 61 | objectBase->GetObjectDescription().c_str()); |
| 62 | this->Context->AddChild(identifier); |
| 63 | return { { "Id", identifier } }; |
| 64 | } |
| 65 | } |
| 66 | else if (!this->Context->RegisterObject(objectBase, identifier)) |
| 67 | { |
| 68 | vtkErrorMacro(<< "Failed to add object " << objectBase->GetObjectDescription()); |
| 69 | return nlohmann::json::object(); |
| 70 | } |
| 71 | nlohmann::json state = nlohmann::json::object(); |
| 72 | if (const auto& f = this->GetHandler(typeid(*objectBase))) |
| 73 | { |
| 74 | try |
| 75 | { |
| 76 | vtkMarshalContext::ScopedParentTracker parentTracker(this->Context, identifier); |
| 77 | vtkVLogScopeF(this->GetSerializerLogVerbosity(), "Serialize objectBase=%s at id=%u", |
| 78 | objectBase->GetObjectDescription().c_str(), identifier); |
| 79 | state = f(objectBase, this); |
| 80 | state["Id"] = identifier; |
| 81 | this->Context->UnRegisterState(identifier); |
| 82 | } |
| 83 | catch (std::exception& e) |
| 84 | { |
| 85 | vtkErrorMacro(<< "Failed to serialize objectBase=" << objectBase->GetObjectDescription() |
| 86 | << ". message=" << e.what()); |
| 87 | return nlohmann::json::object(); |
| 88 | } |
| 89 | } |
| 90 | if (this->Context->RegisterState(std::move(state))) |
| 91 | { |
| 92 | this->Context->AddChild(identifier); |
| 93 | return { { "Id", identifier } }; |
| 94 | } |
| 95 | vtkErrorMacro(<< "Failed to add state for object=" << objectBase->GetObjectDescription() |
| 96 | << " with id=" << identifier); |
| 97 | return nlohmann::json::object(); |
| 98 | } |
| 99 | |
| 100 | //------------------------------------------------------------------------------ |
| 101 | void vtkSerializer::RegisterHandler(const std::type_info& type, HandlerType handler) |