SerializableJsonEvent.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-12-07
  6. * Changed: 2023-02-08
  7. *
  8. * */
  9. #include <ls-std/core/evaluator/NullPointerArgumentEvaluator.hpp>
  10. #include <ls-std/event/serialization/SerializableJsonEvent.hpp>
  11. ls::std::event::SerializableJsonEvent::SerializableJsonEvent(const ::std::shared_ptr<ls::std::event::Event> &_value) : ls::std::core::Class("SerializableJsonEvent")
  12. {
  13. this->_assignValue(_value);
  14. }
  15. ls::std::event::SerializableJsonEvent::~SerializableJsonEvent() = default;
  16. ls::std::core::type::byte_field ls::std::event::SerializableJsonEvent::marshal()
  17. {
  18. this->_update();
  19. return this->jsonObject.dump();
  20. }
  21. void ls::std::event::SerializableJsonEvent::unmarshal(const ls::std::core::type::byte_field &_data)
  22. {
  23. this->jsonObject = ls::std::core::type::json::parse(_data);
  24. this->value->setId(this->jsonObject["id"]);
  25. this->_unmarshalParameterList();
  26. }
  27. ::std::shared_ptr<ls::std::event::Event> ls::std::event::SerializableJsonEvent::getValue()
  28. {
  29. return this->value;
  30. }
  31. void ls::std::event::SerializableJsonEvent::setValue(const ::std::shared_ptr<ls::std::event::Event> &_value)
  32. {
  33. this->_assignValue(_value);
  34. }
  35. void ls::std::event::SerializableJsonEvent::_assignValue(const ::std::shared_ptr<ls::std::event::Event> &_value)
  36. {
  37. ls::std::core::NullPointerArgumentEvaluator{_value, "event reference for serialization attempt is null!"}.evaluate();
  38. this->value = _value;
  39. }
  40. void ls::std::event::SerializableJsonEvent::_unmarshalParameterList()
  41. {
  42. this->value->clearParameterList();
  43. for (const auto &parameterJson : this->jsonObject["parameterList"])
  44. {
  45. ls::std::core::type::event_parameter parameter = {parameterJson.at(0), parameterJson.at(1)};
  46. this->value->addParameter(parameter);
  47. }
  48. }
  49. void ls::std::event::SerializableJsonEvent::_update()
  50. {
  51. this->jsonObject = {{"id", this->value->getId()}};
  52. this->_updateEventParameterList();
  53. }
  54. void ls::std::event::SerializableJsonEvent::_updateEventParameterList()
  55. {
  56. ::std::string jsonString{};
  57. for (const auto &eventParameter : this->value->getParameterList())
  58. {
  59. ls::std::core::type::json parameterJson = {eventParameter.first, eventParameter.second};
  60. this->jsonObject["parameterList"][eventParameter.first] = parameterJson;
  61. }
  62. }