SerializableJSONState.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-09-15
  6. * Changed: 2020-09-15
  7. *
  8. * */
  9. #include "SerializableJSONState.hpp"
  10. #include "SerializableJSONStateConnection.hpp"
  11. ls_std::SerializableJSONState::SerializableJSONState(std::shared_ptr<State> _value) :
  12. Class("SerializableJSONState"),
  13. value(std::move(_value))
  14. {}
  15. ls_std::byte_field ls_std::SerializableJSONState::marshal()
  16. {
  17. this->_update();
  18. return this->jsonObject.dump();
  19. }
  20. void ls_std::SerializableJSONState::unmarshal(const ls_std::byte_field &_data)
  21. {
  22. this->jsonObject = nlohmann::json::parse(_data);
  23. this->_unmarshalStateConnections();
  24. this->value->setId(this->jsonObject["id"]);
  25. }
  26. void ls_std::SerializableJSONState::_unmarshalExistingStateConnection(nlohmann::json _jsonObject)
  27. {
  28. std::shared_ptr<ls_std::StateConnection> stateConnection = this->value->getConnectedStates().at(_jsonObject["connectionId"]);
  29. stateConnection->updatePassCondition(_jsonObject["condition"]);
  30. stateConnection->setStateId(_jsonObject["stateId"]);
  31. }
  32. void ls_std::SerializableJSONState::_unmarshalNewStateConnection(nlohmann::json _jsonObject)
  33. {
  34. std::shared_ptr<ls_std::StateConnection> stateConnection = std::make_shared<ls_std::StateConnection>(_jsonObject["connectionId"], _jsonObject["stateId"]);
  35. stateConnection->updatePassCondition(_jsonObject["condition"]);
  36. this->value->addStateConnection(stateConnection);
  37. }
  38. void ls_std::SerializableJSONState::_unmarshalStateConnections()
  39. {
  40. for(const auto& connectedState : this->jsonObject["connectedStates"]) {
  41. if(this->value->hasConnection(connectedState["connectionId"])) {
  42. this->_unmarshalExistingStateConnection(connectedState);
  43. }
  44. else {
  45. this->_unmarshalNewStateConnection(connectedState);
  46. }
  47. }
  48. }
  49. void ls_std::SerializableJSONState::_update()
  50. {
  51. this->_updateStateConnections();
  52. this->jsonObject["id"] = this->value->getId();
  53. }
  54. void ls_std::SerializableJSONState::_updateStateConnections()
  55. {
  56. ls_std::SerializableJSONStateConnection serializable {nullptr};
  57. for(const auto& connection : this->value->getConnectedStates()) {
  58. serializable.setValue(connection.second);
  59. this->jsonObject["connectedStates"][connection.first] = nlohmann::json::parse(serializable.marshal());
  60. }
  61. }