SerializableJsonEvent.cpp 2.0 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: 2021-05-02
  7. *
  8. * */
  9. #include <ls_std/serialization/json/event/SerializableJsonEvent.hpp>
  10. #include <ls_std/exception/NullPointerException.hpp>
  11. ls_std::SerializableJsonEvent::SerializableJsonEvent(const std::shared_ptr<ls_std::Event> &_value) : ls_std::Class("SerializableJsonEvent")
  12. {
  13. this->_assignValue(_value);
  14. }
  15. ls_std::byte_field ls_std::SerializableJsonEvent::marshal()
  16. {
  17. this->_update();
  18. return this->jsonObject.dump();
  19. }
  20. void ls_std::SerializableJsonEvent::unmarshal(const ls_std::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> ls_std::SerializableJsonEvent::getValue()
  27. {
  28. return this->value;
  29. }
  30. void ls_std::SerializableJsonEvent::setValue(const std::shared_ptr<ls_std::Event> &_value)
  31. {
  32. this->_assignValue(_value);
  33. }
  34. void ls_std::SerializableJsonEvent::_assignValue(const std::shared_ptr<ls_std::Event> &_value)
  35. {
  36. if (_value == nullptr)
  37. {
  38. throw ls_std::NullPointerException{};
  39. }
  40. this->value = _value;
  41. }
  42. void ls_std::SerializableJsonEvent::_unmarshalParameterList()
  43. {
  44. this->value->clearParameterList();
  45. for (const auto &parameterJson : this->jsonObject["parameterList"])
  46. {
  47. ls_std::event_parameter parameter = {parameterJson.at(0), parameterJson.at(1)};
  48. this->value->addParameter(parameter);
  49. }
  50. }
  51. void ls_std::SerializableJsonEvent::_update()
  52. {
  53. this->jsonObject = {{"id", this->value->getId()}};
  54. this->_updateEventParameterList();
  55. }
  56. void ls_std::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. }