SerializableJSONString.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-30
  6. * Changed: 2020-12-20
  7. *
  8. * */
  9. #include <ls_std/serialization/boxing/SerializableJSONString.hpp>
  10. #include <ls_std/exception/IllegalArgumentException.hpp>
  11. ls_std::SerializableJSONString::SerializableJSONString(const std::shared_ptr<ls_std::String>& _value) :
  12. ls_std::Class("SerializableJSONString")
  13. {
  14. this->_assignValue(_value);
  15. }
  16. ls_std::byte_field ls_std::SerializableJSONString::marshal()
  17. {
  18. this->_update();
  19. return this->jsonObject.dump();
  20. }
  21. void ls_std::SerializableJSONString::unmarshal(const ls_std::byte_field& _data)
  22. {
  23. this->jsonObject = nlohmann::json::parse(_data);
  24. if(this->jsonObject.contains("value")) {
  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. throw ls_std::IllegalArgumentException {};
  40. }
  41. this->value = _value;
  42. }
  43. void ls_std::SerializableJSONString::_update()
  44. {
  45. this->jsonObject = {
  46. {"value", this->value->toString()}
  47. };
  48. }