Procházet zdrojové kódy

Added SerializableJSONDouble class

- added SerializableJSONDouble class to offer possibility to serialize Double class
- added tests for SerializableJSONDouble class
pcmattulat před 4 roky
rodič
revize
ffc4de1b6f

+ 5 - 2
CMakeLists.txt

@@ -87,7 +87,9 @@ set(SOURCE_FILES
         ${CMAKE_CURRENT_SOURCE_DIR}/source/serialization/boxing/SerializableJSONString.hpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/serialization/boxing/SerializableJSONString.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/serialization/boxing/SerializableJSONFloat.hpp
-        ${CMAKE_CURRENT_SOURCE_DIR}/source/serialization/boxing/SerializableJSONFloat.cpp)
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/serialization/boxing/SerializableJSONFloat.cpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/serialization/boxing/SerializableJSONDouble.hpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/serialization/boxing/SerializableJSONDouble.cpp)
 
 set(TEST_FILES
         ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/boxing/IntegerTest.cpp
@@ -111,7 +113,8 @@ set(TEST_FILES
         ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/serialization/boxing/SerializableJSONIntegerTest.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/serialization/boxing/SerializableJSONLongTest.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/serialization/boxing/SerializableJSONStringTest.cpp
-        ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/serialization/boxing/SerializableJSONFloatTest.cpp)
+        ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/serialization/boxing/SerializableJSONFloatTest.cpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/serialization/boxing/SerializableJSONDoubleTest.cpp)
 
 ##########################################################
 # Build

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

@@ -0,0 +1,38 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2020-09-04
+ * Changed:         2020-09-04
+ *
+ * */
+
+#include "SerializableJSONDouble.hpp"
+
+ls_std::SerializableJSONDouble::SerializableJSONDouble(std::shared_ptr<ls_std::Double> _doubleValue) : Class("SerializableJSONDouble"),
+doubleValue(std::move(_doubleValue))
+{}
+
+ls_std::byte_field ls_std::SerializableJSONDouble::marshal()
+{
+  this->_update();
+  return this->jsonObject.dump();
+}
+
+void ls_std::SerializableJSONDouble::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->doubleValue = this->jsonObject["value"];
+  }
+}
+
+void ls_std::SerializableJSONDouble::_update()
+{
+  this->jsonObject = {
+      {"class", this->doubleValue->getClassName()},
+      {"value", this->doubleValue->getValue()}
+  };
+}

+ 38 - 0
source/serialization/boxing/SerializableJSONDouble.hpp

@@ -0,0 +1,38 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2020-09-04
+ * Changed:         2020-09-04
+ *
+ * */
+
+#ifndef SERIALIZABLE_JSON_DOUBLE_HPP
+#define SERIALIZABLE_JSON_DOUBLE_HPP
+
+#include <memory>
+#include <json.hpp>
+#include "../../base/Class.hpp"
+#include "../ISerializable.hpp"
+#include "../../boxing/Double.hpp"
+
+namespace ls_std {
+  class SerializableJSONDouble : public Class, public ISerializable {
+    public:
+
+      explicit SerializableJSONDouble(std::shared_ptr<ls_std::Double> _doubleValue);
+      ~SerializableJSONDouble() = default;
+
+      ls_std::byte_field marshal() override;
+      void unmarshal(const ls_std::byte_field& _data) override;
+
+    private:
+
+      std::shared_ptr<ls_std::Double> doubleValue {};
+      nlohmann::json jsonObject {};
+
+      void _update();
+  };
+}
+
+#endif

+ 46 - 0
test/cases/serialization/boxing/SerializableJSONDoubleTest.cpp

@@ -0,0 +1,46 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2020-09-04
+ * Changed:         2020-09-04
+ *
+ * */
+
+#include <gtest/gtest.h>
+#include "../../../../source/boxing/Double.hpp"
+#include "../../../../source/serialization/boxing/SerializableJSONDouble.hpp"
+#include "../../../../source/boxing/String.hpp"
+
+namespace {
+  class SerializableJSONDoubleTest : public ::testing::Test {
+    protected:
+
+      SerializableJSONDoubleTest() = default;
+      ~SerializableJSONDoubleTest() override = default;
+
+      void SetUp() override {}
+      void TearDown() override {}
+  };
+
+  TEST_F(SerializableJSONDoubleTest, marshal)
+  {
+    ls_std::Double x {3.14159};
+    ls_std::SerializableJSONDouble serializable {std::make_shared<ls_std::Double>(x)};
+    ls_std::String jsonString {serializable.marshal()};
+
+    ASSERT_TRUE(jsonString.contains(R"({"class":"Double","value":3.14159)"));
+  }
+
+  TEST_F(SerializableJSONDoubleTest, unmarshal)
+  {
+    std::shared_ptr<ls_std::Double> x = std::make_shared<ls_std::Double>(14.2234);
+
+    ASSERT_DOUBLE_EQ(14.2234, *x);
+
+    ls_std::SerializableJSONDouble serializable {x};
+    serializable.unmarshal(R"({"class":"Double","value":3.14159})");
+
+    ASSERT_DOUBLE_EQ(3.14159, *x);
+  }
+}