SerializableJSONState.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-09-15
  6. * Changed: 2020-11-06
  7. *
  8. * */
  9. #include "../../../../../include/ls_std/serialization/logic/SerializableJSONState.hpp"
  10. #include "../../../../../include/ls_std/serialization/logic/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::setValue(std::shared_ptr<State> _value)
  27. {
  28. this->value = std::move(_value);
  29. this->_clear();
  30. }
  31. void ls_std::SerializableJSONState::_clear()
  32. {
  33. this->jsonObject.clear();
  34. }
  35. void ls_std::SerializableJSONState::_unmarshalExistingStateConnection(nlohmann::json _jsonObject)
  36. {
  37. std::shared_ptr<ls_std::StateConnection> stateConnection = this->value->getConnectedStates().at(_jsonObject["connectionId"]);
  38. stateConnection->updatePassCondition(_jsonObject["condition"]);
  39. stateConnection->setStateId(_jsonObject["stateId"]);
  40. }
  41. void ls_std::SerializableJSONState::_unmarshalNewStateConnection(nlohmann::json _jsonObject)
  42. {
  43. ls_std::StateConnectionId connectionId = _jsonObject["connectionId"];
  44. ls_std::StateId stateId = _jsonObject["stateId"];
  45. std::shared_ptr<ls_std::StateConnection> stateConnection = std::make_shared<ls_std::StateConnection>(connectionId, stateId);
  46. stateConnection->updatePassCondition(_jsonObject["condition"]);
  47. this->value->addStateConnection(stateConnection);
  48. }
  49. void ls_std::SerializableJSONState::_unmarshalStateConnections()
  50. {
  51. for(const auto& connectedState : this->jsonObject["connectedStates"]) {
  52. if(this->value->hasConnection(connectedState["connectionId"])) {
  53. this->_unmarshalExistingStateConnection(connectedState);
  54. }
  55. else {
  56. this->_unmarshalNewStateConnection(connectedState);
  57. }
  58. }
  59. }
  60. void ls_std::SerializableJSONState::_update()
  61. {
  62. this->_updateStateConnections();
  63. this->jsonObject["id"] = this->value->getId();
  64. }
  65. void ls_std::SerializableJSONState::_updateStateConnections()
  66. {
  67. std::string jsonString {};
  68. for(const auto& connection : this->value->getConnectedStates()) {
  69. jsonString = ls_std::SerializableJSONStateConnection{connection.second}.marshal();
  70. this->jsonObject["connectedStates"][connection.first] = nlohmann::json::parse(jsonString);
  71. }
  72. }