SerializableJsonEvent.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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-23
  7. *
  8. * */
  9. #include <ls-std/core/evaluator/NullPointerArgumentEvaluator.hpp>
  10. #include <ls-std/event/serialization/SerializableJsonEvent.hpp>
  11. using ls::std::core::Class;
  12. using ls::std::core::NullPointerArgumentEvaluator;
  13. using ls::std::core::type::byte_field;
  14. using ls::std::core::type::event_parameter;
  15. using ls::std::core::type::json;
  16. using ls::std::event::Event;
  17. using ls::std::event::SerializableJsonEvent;
  18. using std::shared_ptr;
  19. using std::string;
  20. SerializableJsonEvent::SerializableJsonEvent(const shared_ptr<Event> &_value) : Class("SerializableJsonEvent")
  21. {
  22. this->_assignValue(_value);
  23. }
  24. SerializableJsonEvent::~SerializableJsonEvent() noexcept = default;
  25. byte_field SerializableJsonEvent::marshal()
  26. {
  27. this->_update();
  28. return this->jsonObject.dump();
  29. }
  30. void SerializableJsonEvent::unmarshal(const byte_field &_data)
  31. {
  32. this->jsonObject = json::parse(_data);
  33. this->value->setId(this->jsonObject["id"]);
  34. this->_unmarshalParameterList();
  35. }
  36. shared_ptr<Event> SerializableJsonEvent::getValue()
  37. {
  38. return this->value;
  39. }
  40. void SerializableJsonEvent::setValue(const shared_ptr<Event> &_value)
  41. {
  42. this->_assignValue(_value);
  43. }
  44. void SerializableJsonEvent::_assignValue(const shared_ptr<Event> &_value)
  45. {
  46. NullPointerArgumentEvaluator{_value, "event reference for serialization attempt is null!"}.evaluate();
  47. this->value = _value;
  48. }
  49. void SerializableJsonEvent::_unmarshalParameterList()
  50. {
  51. this->value->clearParameterList();
  52. for (const auto &parameterJson : this->jsonObject["parameterList"])
  53. {
  54. event_parameter parameter = {parameterJson.at(0), parameterJson.at(1)};
  55. this->value->addParameter(parameter);
  56. }
  57. }
  58. void SerializableJsonEvent::_update()
  59. {
  60. this->jsonObject = {{"id", this->value->getId()}};
  61. this->_updateEventParameterList();
  62. }
  63. void SerializableJsonEvent::_updateEventParameterList()
  64. {
  65. string jsonString{};
  66. for (const auto &eventParameter : this->value->getParameterList())
  67. {
  68. json parameterJson = {eventParameter.first, eventParameter.second};
  69. this->jsonObject["parameterList"][eventParameter.first] = parameterJson;
  70. }
  71. }