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

Added SerializableJSONLong class

- added SerializableJSONLong class to serialize
ls_std::Long class by using JSON
- added tests for SerializableJSONLong class
Patrick преди 4 години
родител
ревизия
66b968656b

+ 5 - 2
CMakeLists.txt

@@ -81,7 +81,9 @@ set(SOURCE_FILES
         ${CMAKE_CURRENT_SOURCE_DIR}/source/io/logging/LogLevelValue.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)
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/serialization/boxing/SerializableJSONInteger.cpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/serialization/boxing/SerializableJSONLong.hpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/serialization/boxing/SerializableJSONLong.cpp)
 
 set(TEST_FILES
         ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/boxing/IntegerTest.cpp
@@ -102,7 +104,8 @@ set(TEST_FILES
         ${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/serialization/JSONTest.cpp
-        ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/serialization/boxing/SerializableJSONIntegerTest.cpp)
+        ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/serialization/boxing/SerializableJSONIntegerTest.cpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/serialization/boxing/SerializableJSONLongTest.cpp)
 
 ##########################################################
 # Build

+ 38 - 0
source/serialization/boxing/SerializableJSONLong.cpp

@@ -0,0 +1,38 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2020-08-25
+ * Changed:         2020-08-26
+ *
+ * */
+
+#include "SerializableJSONLong.hpp"
+
+ls_std::SerializableJSONLong::SerializableJSONLong(std::shared_ptr<ls_std::Long> _longValue) : Class("SerializableJSONLong"),
+longValue(std::move(_longValue))
+{}
+
+ls_std::byte_field ls_std::SerializableJSONLong::marshal()
+{
+  this->_update();
+  return this->jsonObject.dump();
+}
+
+void ls_std::SerializableJSONLong::unmarshal(const ls_std::byte_field& _data)
+{
+  std::string jsonString = std::string(_data);
+  this->jsonObject = nlohmann::json::parse(jsonString);
+
+  if(this->jsonObject.contains("value")) {
+    *this->longValue = this->jsonObject["value"];
+  }
+}
+
+void ls_std::SerializableJSONLong::_update()
+{
+  this->jsonObject = {
+      {"class", this->longValue->getClassName()},
+      {"value", (ls_std::long_type) this->longValue->getValue()}
+  };
+}

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

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

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

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