SerializableJsonEvent.cpp 2.2 KB

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