SerializableJSONString.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-30
  6. * Changed: 2020-11-26
  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. std::string jsonString = std::string(_data);
  24. this->jsonObject = nlohmann::json::parse(jsonString);
  25. if(this->jsonObject.contains("value")) {
  26. *this->value = this->jsonObject["value"];
  27. }
  28. }
  29. std::shared_ptr<ls_std::String> ls_std::SerializableJSONString::getValue()
  30. {
  31. return this->value;
  32. }
  33. void ls_std::SerializableJSONString::setValue(const std::shared_ptr<ls_std::String> &_value)
  34. {
  35. this->_assignValue(_value);
  36. }
  37. void ls_std::SerializableJSONString::_assignValue(const std::shared_ptr<ls_std::String> &_value)
  38. {
  39. if(_value == nullptr) {
  40. throw ls_std::IllegalArgumentException {};
  41. }
  42. this->value = _value;
  43. }
  44. void ls_std::SerializableJSONString::_update()
  45. {
  46. this->jsonObject = {
  47. {"value", this->value->toString()}
  48. };
  49. }