Преглед на файлове

Added SerializableJSONInteger class

- added SerializableJSONInteger class to serialize
Integer class (marshal, unmarshal)
- added tests for SerializableJSONInteger class
Patrick преди 4 години
родител
ревизия
5b57c57052

+ 5 - 2
CMakeLists.txt

@@ -79,7 +79,9 @@ set(SOURCE_FILES
         ${CMAKE_CURRENT_SOURCE_DIR}/source/io/logging/Logger.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/io/logging/LogLevel.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/io/logging/LogLevelValue.hpp
-        ${CMAKE_CURRENT_SOURCE_DIR}/source/serialization/ISerializable.hpp)
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/serialization/ISerializable.hpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/serialization/boxing/SerializableJSONInteger.hpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/serialization/boxing/SerializableJSONInteger.cpp)
 
 set(TEST_FILES
         ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/boxing/IntegerTest.cpp
@@ -99,7 +101,8 @@ set(TEST_FILES
         ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/StorableFileTest.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/FileOutputStreamTest.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/logging/LoggerTest.cpp
-        ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/json/JSONTest.cpp)
+        ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/serialization/JSONTest.cpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/serialization/boxing/SerializableJSONIntegerTest.cpp)
 
 ##########################################################
 # Build

+ 41 - 0
source/serialization/boxing/SerializableJSONInteger.cpp

@@ -0,0 +1,41 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2020-08-21
+ * Changed:         2020-08-21
+ *
+ * */
+
+#include "SerializableJSONInteger.hpp"
+
+ls_std::SerializableJSONInteger::SerializableJSONInteger(std::shared_ptr<ls_std::Integer> _integer) : Class("SerializableJSONInteger"),
+integer(std::move(_integer))
+{}
+
+const ls_std::byte * ls_std::SerializableJSONInteger::marshal()
+{
+  this->_update();
+  auto* data = new char[this->jsonObject.dump().size() + 1];
+  strcpy(data, this->jsonObject.dump().c_str());
+
+  return data;
+}
+
+void ls_std::SerializableJSONInteger::unmarshal(const ls_std::byte *_data)
+{
+  std::string jsonString = std::string(_data);
+  this->jsonObject = nlohmann::json::parse(jsonString);
+
+  if(this->jsonObject.contains("value")) {
+    *this->integer = this->jsonObject["value"];
+  }
+}
+
+void ls_std::SerializableJSONInteger::_update()
+{
+  this->jsonObject = {
+      {"class", this->integer->getClassName()},
+      {"value", this->integer->getValue()}
+  };
+}

+ 37 - 0
source/serialization/boxing/SerializableJSONInteger.hpp

@@ -0,0 +1,37 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2020-08-21
+ * Changed:         2020-08-21
+ *
+ * */
+
+#ifndef SERIALIZABLE_JSON_INTEGER_HPP
+#define SERIALIZABLE_JSON_INTEGER_HPP
+
+#include "../../base/Class.hpp"
+#include "../ISerializable.hpp"
+#include "../../boxing/Integer.hpp"
+#include <json.hpp>
+
+namespace ls_std {
+  class SerializableJSONInteger : public Class, ISerializable {
+    public:
+
+      explicit SerializableJSONInteger(std::shared_ptr<ls_std::Integer> _integer);
+      ~SerializableJSONInteger() = default;
+
+      const ls_std::byte* marshal() override;
+      void unmarshal(const ls_std::byte* _data) override;
+
+    private:
+
+      std::shared_ptr<ls_std::Integer> integer {};
+      nlohmann::json jsonObject {};
+
+      void _update();
+  };
+}
+
+#endif

+ 44 - 0
test/cases/serialization/boxing/SerializableJSONIntegerTest.cpp

@@ -0,0 +1,44 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2020-08-21
+ * Changed:         2020-08-21
+ *
+ * */
+
+#include <gtest/gtest.h>
+#include "../../../../source/boxing/Integer.hpp"
+#include "../../../../source/serialization/boxing/SerializableJSONInteger.hpp"
+
+namespace {
+  class SerializableJSONIntegerTest : public ::testing::Test {
+    protected:
+
+      SerializableJSONIntegerTest() = default;
+      ~SerializableJSONIntegerTest() override = default;
+
+      void SetUp() override {}
+      void TearDown() override {}
+  };
+
+  TEST_F(SerializableJSONIntegerTest, marshal)
+  {
+    ls_std::Integer x {3};
+    ls_std::SerializableJSONInteger serializable {std::make_shared<ls_std::Integer>(x)};
+    std::string content = serializable.marshal();
+
+    ASSERT_STREQ(R"({"class":"Integer","value":3})", content.c_str());
+  }
+
+  TEST_F(SerializableJSONIntegerTest, unmarshal)
+  {
+    std::shared_ptr<ls_std::Integer> x = std::make_shared<ls_std::Integer>(13);
+    ASSERT_EQ(13, *x);
+
+    ls_std::SerializableJSONInteger serializable {x};
+    serializable.unmarshal(R"({"class":"Integer","value":1989})");
+
+    ASSERT_EQ(1989, *x);
+  }
+}