SerializableJSONStateConnection.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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-26
  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. ls_std::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. std::shared_ptr<ls_std::StateConnection> ls_std::SerializableJSONStateConnection::getValue()
  30. {
  31. return this->value;
  32. }
  33. void ls_std::SerializableJSONStateConnection::setValue(const std::shared_ptr<ls_std::StateConnection>& _value)
  34. {
  35. this->_assignValue(_value);
  36. this->_clear();
  37. }
  38. void ls_std::SerializableJSONStateConnection::_assignValue(const std::shared_ptr<ls_std::StateConnection> &_value)
  39. {
  40. if(_value == nullptr) {
  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 = {
  52. {"condition", this->value->isPassable()},
  53. {"connectionId", this->value->getConnectionId()},
  54. {"stateId", this->value->getStateId()}
  55. };
  56. }