SerializableJSONInteger.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-21
  6. * Changed: 2020-08-23
  7. *
  8. * */
  9. #include "SerializableJSONInteger.hpp"
  10. ls_std::SerializableJSONInteger::SerializableJSONInteger(std::shared_ptr<ls_std::Integer> _integer) : Class("SerializableJSONInteger"),
  11. integer(std::move(_integer))
  12. {}
  13. ls_std::byte_field ls_std::SerializableJSONInteger::marshal()
  14. {
  15. this->_update();
  16. auto* data = new char[this->jsonObject.dump().size() + 1];
  17. strcpy(data, this->jsonObject.dump().c_str());
  18. return data;
  19. }
  20. void ls_std::SerializableJSONInteger::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. *this->integer = this->jsonObject["value"];
  26. }
  27. }
  28. void ls_std::SerializableJSONInteger::_update()
  29. {
  30. this->jsonObject = {
  31. {"class", this->integer->getClassName()},
  32. {"value", this->integer->getValue()}
  33. };
  34. }