SerializableJSONStateConnection.cpp 1.7 KB

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