SerializableJsonDouble.cpp 1.4 KB

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