SerializableJsonStateConnection.cpp 2.0 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: 2022-05-20
  7. *
  8. * */
  9. #include <ls_std/logic/serialization/SerializableJsonStateConnection.hpp>
  10. #include <ls_std/core/exception/IllegalArgumentException.hpp>
  11. ls::std::logic::SerializableJsonStateConnection::SerializableJsonStateConnection(const ::std::shared_ptr<ls::std::logic::StateConnection> &_value) : ls::std::core::Class("SerializableJsonStateConnection")
  12. {
  13. this->_assignValue(_value);
  14. }
  15. ls::std::core::type::byte_field ls::std::logic::SerializableJsonStateConnection::marshal()
  16. {
  17. this->_update();
  18. return this->jsonObject.dump();
  19. }
  20. void ls::std::logic::SerializableJsonStateConnection::unmarshal(const ls::std::core::type::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::logic::StateConnection> ls::std::logic::SerializableJsonStateConnection::getValue()
  29. {
  30. return this->value;
  31. }
  32. void ls::std::logic::SerializableJsonStateConnection::setValue(const ::std::shared_ptr<ls::std::logic::StateConnection> &_value)
  33. {
  34. this->_assignValue(_value);
  35. this->_clear();
  36. }
  37. void ls::std::logic::SerializableJsonStateConnection::_assignValue(const ::std::shared_ptr<ls::std::logic::StateConnection> &_value)
  38. {
  39. if (_value == nullptr)
  40. {
  41. throw ls::std::core::IllegalArgumentException{};
  42. }
  43. this->value = _value;
  44. }
  45. void ls::std::logic::SerializableJsonStateConnection::_clear()
  46. {
  47. this->jsonObject.clear();
  48. }
  49. void ls::std::logic::SerializableJsonStateConnection::_update()
  50. {
  51. this->jsonObject = {{"condition", this->value->isPassable()},
  52. {"connectionId", this->value->getConnectionId()},
  53. {"stateId", this->value->getStateId()}};
  54. }