Explorar o código

Added SerializableJSONFloat class

- added SerializableJSONFloat class to offer possibility to serialize Float class
- added tests for SerializableJSONFloat class
pcmattulat %!s(int64=4) %!d(string=hai) anos
pai
achega
8d39a15d9e

+ 5 - 2
CMakeLists.txt

@@ -85,7 +85,9 @@ set(SOURCE_FILES
         ${CMAKE_CURRENT_SOURCE_DIR}/source/serialization/boxing/SerializableJSONLong.hpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/serialization/boxing/SerializableJSONLong.cpp
         ${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/SerializableJSONString.cpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/serialization/boxing/SerializableJSONFloat.hpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/serialization/boxing/SerializableJSONFloat.cpp)
 
 set(TEST_FILES
         ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/boxing/IntegerTest.cpp
@@ -108,7 +110,8 @@ set(TEST_FILES
         ${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/SerializableJSONLongTest.cpp
-        ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/serialization/boxing/SerializableJSONStringTest.cpp)
+        ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/serialization/boxing/SerializableJSONStringTest.cpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/serialization/boxing/SerializableJSONFloatTest.cpp)
 
 ##########################################################
 # Build

+ 38 - 0
source/serialization/boxing/SerializableJSONFloat.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 "SerializableJSONFloat.hpp"
+
+ls_std::SerializableJSONFloat::SerializableJSONFloat(std::shared_ptr<ls_std::Float> _floatValue) : Class("SerializableJSONFloat"),
+floatValue(std::move(_floatValue))
+{}
+
+ls_std::byte_field ls_std::SerializableJSONFloat::marshal()
+{
+  this->_update();
+  return this->jsonObject.dump();
+}
+
+void ls_std::SerializableJSONFloat::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->floatValue = this->jsonObject["value"];
+  }
+}
+
+void ls_std::SerializableJSONFloat::_update()
+{
+  this->jsonObject = {
+      {"class", this->floatValue->getClassName()},
+      {"value", this->floatValue->getValue()}
+  };
+}

+ 38 - 0
source/serialization/boxing/SerializableJSONFloat.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_FLOAT_HPP
+#define SERIALIZABLE_JSON_FLOAT_HPP
+
+#include <memory>
+#include <json.hpp>
+#include "../../base/Class.hpp"
+#include "../ISerializable.hpp"
+#include "../../boxing/Float.hpp"
+
+namespace ls_std {
+  class SerializableJSONFloat : public Class, public ISerializable {
+    public:
+
+      explicit SerializableJSONFloat(std::shared_ptr<ls_std::Float> _floatValue);
+      ~SerializableJSONFloat() = default;
+
+      ls_std::byte_field marshal() override;
+      void unmarshal(const ls_std::byte_field& _data) override;
+
+    private:
+
+      std::shared_ptr<ls_std::Float> floatValue {};
+      nlohmann::json jsonObject {};
+
+      void _update();
+  };
+}
+
+#endif

+ 45 - 0
test/cases/serialization/boxing/SerializableJSONFloatTest.cpp

@@ -0,0 +1,45 @@
+/*
+ * 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/Float.hpp"
+#include "../../../../source/serialization/boxing/SerializableJSONFloat.hpp"
+#include "../../../../source/boxing/String.hpp"
+
+namespace {
+  class SerializableJSONFloatTest : public ::testing::Test {
+    protected:
+
+      SerializableJSONFloatTest() = default;
+      ~SerializableJSONFloatTest() override = default;
+
+      void SetUp() override {}
+      void TearDown() override {}
+  };
+
+  TEST_F(SerializableJSONFloatTest, marshal)
+  {
+    ls_std::Float x {3.14159f};
+    ls_std::SerializableJSONFloat serializable {std::make_shared<ls_std::Float>(x)};
+    ls_std::String jsonString {serializable.marshal()};
+
+    ASSERT_TRUE(jsonString.contains(R"({"class":"Float","value":3.14159)"));
+  }
+
+  TEST_F(SerializableJSONFloatTest, unmarshal)
+  {
+    std::shared_ptr<ls_std::Float> x = std::make_shared<ls_std::Float>(14.2234f);
+    ASSERT_FLOAT_EQ(14.2234f, *x);
+
+    ls_std::SerializableJSONFloat serializable {x};
+    serializable.unmarshal(R"({"class":"Float","value":3.14159})");
+
+    ASSERT_FLOAT_EQ(3.14159f, *x);
+  }
+}