SerializableJSONState.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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-25
  7. *
  8. * */
  9. #include "../../../../../include/ls_std/serialization/logic/SerializableJSONState.hpp"
  10. #include "../../../../../include/ls_std/serialization/logic/SerializableJSONStateConnection.hpp"
  11. #include <ls_std/exception/IllegalArgumentException.hpp>
  12. ls_std::SerializableJSONState::SerializableJSONState(const std::shared_ptr<State>& _value) :
  13. Class("SerializableJSONState")
  14. {
  15. this->_assignValue(_value);
  16. }
  17. ls_std::byte_field ls_std::SerializableJSONState::marshal()
  18. {
  19. this->_update();
  20. return this->jsonObject.dump();
  21. }
  22. void ls_std::SerializableJSONState::unmarshal(const ls_std::byte_field &_data)
  23. {
  24. this->jsonObject = nlohmann::json::parse(_data);
  25. this->_unmarshalStateConnections();
  26. this->value->setId(this->jsonObject["id"]);
  27. }
  28. void ls_std::SerializableJSONState::setValue(const std::shared_ptr<State>& _value)
  29. {
  30. this->_assignValue(_value);
  31. this->_clear();
  32. }
  33. void ls_std::SerializableJSONState::_assignValue(const std::shared_ptr<State> &_value)
  34. {
  35. if(_value == nullptr) {
  36. throw ls_std::IllegalArgumentException {};
  37. }
  38. this->value = _value;
  39. }
  40. void ls_std::SerializableJSONState::_clear()
  41. {
  42. this->jsonObject.clear();
  43. }
  44. void ls_std::SerializableJSONState::_unmarshalExistingStateConnection(nlohmann::json _jsonObject)
  45. {
  46. std::shared_ptr<ls_std::StateConnection> stateConnection = this->value->getConnectedStates().at(_jsonObject["connectionId"]);
  47. stateConnection->updatePassCondition(_jsonObject["condition"]);
  48. stateConnection->setStateId(_jsonObject["stateId"]);
  49. }
  50. void ls_std::SerializableJSONState::_unmarshalNewStateConnection(nlohmann::json _jsonObject)
  51. {
  52. ls_std::StateConnectionId connectionId = _jsonObject["connectionId"];
  53. ls_std::StateId stateId = _jsonObject["stateId"];
  54. std::shared_ptr<ls_std::StateConnection> stateConnection = std::make_shared<ls_std::StateConnection>(connectionId, stateId);
  55. stateConnection->updatePassCondition(_jsonObject["condition"]);
  56. this->value->addStateConnection(stateConnection);
  57. }
  58. void ls_std::SerializableJSONState::_unmarshalStateConnections()
  59. {
  60. for(const auto& connectedState : this->jsonObject["connectedStates"]) {
  61. if(this->value->hasConnection(connectedState["connectionId"])) {
  62. this->_unmarshalExistingStateConnection(connectedState);
  63. }
  64. else {
  65. this->_unmarshalNewStateConnection(connectedState);
  66. }
  67. }
  68. }
  69. void ls_std::SerializableJSONState::_update()
  70. {
  71. this->jsonObject = {
  72. {"id", this->value->getId()}
  73. };
  74. this->_updateStateConnections();
  75. }
  76. void ls_std::SerializableJSONState::_updateStateConnections()
  77. {
  78. std::string jsonString {};
  79. for(const auto& connection : this->value->getConnectedStates()) {
  80. jsonString = ls_std::SerializableJSONStateConnection{connection.second}.marshal();
  81. this->jsonObject["connectedStates"][connection.first] = nlohmann::json::parse(jsonString);
  82. }
  83. }