SerializableJsonString.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-30
  6. * Changed: 2021-05-02
  7. *
  8. * */
  9. #include <ls_std/serialization/json/boxing/SerializableJsonString.hpp>
  10. #include <ls_std/exception/IllegalArgumentException.hpp>
  11. ls_std::SerializableJsonString::SerializableJsonString(const std::shared_ptr<ls_std::String> &_value) : ls_std::Class("SerializableJsonString")
  12. {
  13. this->_assignValue(_value);
  14. }
  15. ls_std::byte_field ls_std::SerializableJsonString::marshal()
  16. {
  17. this->_update();
  18. return this->jsonObject.dump();
  19. }
  20. void ls_std::SerializableJsonString::unmarshal(const ls_std::byte_field &_data)
  21. {
  22. this->jsonObject = nlohmann::json::parse(_data);
  23. if (this->jsonObject.contains("value"))
  24. {
  25. *this->value = this->jsonObject["value"];
  26. }
  27. }
  28. std::shared_ptr<ls_std::String> ls_std::SerializableJsonString::getValue()
  29. {
  30. return this->value;
  31. }
  32. void ls_std::SerializableJsonString::setValue(const std::shared_ptr<ls_std::String> &_value)
  33. {
  34. this->_assignValue(_value);
  35. }
  36. void ls_std::SerializableJsonString::_assignValue(const std::shared_ptr<ls_std::String> &_value)
  37. {
  38. if (_value == nullptr)
  39. {
  40. throw ls_std::IllegalArgumentException{};
  41. }
  42. this->value = _value;
  43. }
  44. void ls_std::SerializableJsonString::_update()
  45. {
  46. this->jsonObject = {{"value", this->value->toString()}};
  47. }