SerializableJSONStateConnection.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-09-14
  6. * Changed: 2021-04-23
  7. *
  8. * */
  9. #include <ls_std/serialization/logic/SerializableJSONStateConnection.hpp>
  10. #include <ls_std/exception/IllegalArgumentException.hpp>
  11. ls_std::SerializableJSONStateConnection::SerializableJSONStateConnection(const std::shared_ptr<ls_std::StateConnection> &_value) : ls_std::Class("SerializableJSONStateConnection")
  12. {
  13. this->_assignValue(_value);
  14. }
  15. ls_std::byte_field ls_std::SerializableJSONStateConnection::marshal()
  16. {
  17. this->_update();
  18. return this->jsonObject.dump();
  19. }
  20. void ls_std::SerializableJSONStateConnection::unmarshal(const ls_std::byte_field &_data)
  21. {
  22. std::string jsonString = std::string(_data);
  23. this->jsonObject = nlohmann::json::parse(jsonString);
  24. this->value->setConnectionId(this->jsonObject["connectionId"]);
  25. this->value->setStateId(this->jsonObject["stateId"]);
  26. this->value->updatePassCondition(this->jsonObject["condition"]);
  27. }
  28. std::shared_ptr<ls_std::StateConnection> ls_std::SerializableJSONStateConnection::getValue()
  29. {
  30. return this->value;
  31. }
  32. void ls_std::SerializableJSONStateConnection::setValue(const std::shared_ptr<ls_std::StateConnection> &_value)
  33. {
  34. this->_assignValue(_value);
  35. this->_clear();
  36. }
  37. void ls_std::SerializableJSONStateConnection::_assignValue(const std::shared_ptr<ls_std::StateConnection> &_value)
  38. {
  39. if (_value == nullptr)
  40. {
  41. throw ls_std::IllegalArgumentException{};
  42. }
  43. this->value = _value;
  44. }
  45. void ls_std::SerializableJSONStateConnection::_clear()
  46. {
  47. this->jsonObject.clear();
  48. }
  49. void ls_std::SerializableJSONStateConnection::_update()
  50. {
  51. this->jsonObject = {{"condition", this->value->isPassable()},
  52. {"connectionId", this->value->getConnectionId()},
  53. {"stateId", this->value->getStateId()}};
  54. }