Bladeren bron

Added SerializableJSONBoolean class

- added SerializableJSONBoolean class to offer possibility to serialize Double
class
- added tests for SerializableJSONBoolean class
pcmattulat 4 jaren geleden
bovenliggende
commit
bc4ec42103

+ 5 - 2
CMakeLists.txt

@@ -89,7 +89,9 @@ set(SOURCE_FILES
         ${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/SerializableJSONDouble.hpp
-        ${CMAKE_CURRENT_SOURCE_DIR}/source/serialization/boxing/SerializableJSONDouble.cpp)
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/serialization/boxing/SerializableJSONDouble.cpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/serialization/boxing/SerializableJSONBoolean.hpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/serialization/boxing/SerializableJSONBoolean.cpp)
 
 set(TEST_FILES
         ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/boxing/IntegerTest.cpp
@@ -114,7 +116,8 @@ set(TEST_FILES
         ${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/SerializableJSONDoubleTest.cpp)
+        ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/serialization/boxing/SerializableJSONDoubleTest.cpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/serialization/boxing/SerializableJSONBooleanTest.cpp)
 
 ##########################################################
 # Build

+ 36 - 0
source/serialization/boxing/SerializableJSONBoolean.cpp

@@ -0,0 +1,36 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2020-09-04
+ * Changed:         2020-09-04
+ *
+ * */
+
+#include "SerializableJSONBoolean.hpp"
+
+ls_std::SerializableJSONBoolean::SerializableJSONBoolean(std::shared_ptr<ls_std::Boolean> _value) : Class("SerializableJSONBoolean"),
+value(std::move(_value))
+{}
+
+ls_std::byte_field ls_std::SerializableJSONBoolean::marshal()
+{
+  this->_update();
+  return this->jsonObject.dump();
+}
+
+void ls_std::SerializableJSONBoolean::unmarshal(const ls_std::byte_field& _data)
+{
+  this->jsonObject = nlohmann::json::parse(_data);
+
+  if(this->jsonObject.contains("value")) {
+    *this->value = (bool) this->jsonObject["value"];
+  }
+}
+
+void ls_std::SerializableJSONBoolean::_update()
+{
+  this->jsonObject = {
+      {"value", this->value->getValue()}
+  };
+}

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

+ 46 - 0
test/cases/serialization/boxing/SerializableJSONBooleanTest.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/Boolean.hpp"
+#include "../../../../source/serialization/boxing/SerializableJSONBoolean.hpp"
+#include "../../../../source/boxing/String.hpp"
+
+namespace {
+  class SerializableJSONBooleanTest : public ::testing::Test {
+    protected:
+
+      SerializableJSONBooleanTest() = default;
+      ~SerializableJSONBooleanTest() override = default;
+
+      void SetUp() override {}
+      void TearDown() override {}
+  };
+
+  TEST_F(SerializableJSONBooleanTest, marshal)
+  {
+    ls_std::Boolean x {true};
+    ls_std::SerializableJSONBoolean serializable {std::make_shared<ls_std::Boolean>(x)};
+    ls_std::String jsonString {serializable.marshal()};
+
+    ASSERT_TRUE(jsonString.contains(R"({"value":true)"));
+  }
+
+  TEST_F(SerializableJSONBooleanTest, unmarshal)
+  {
+    std::shared_ptr<ls_std::Boolean> x = std::make_shared<ls_std::Boolean>(false);
+
+    ASSERT_FALSE( *x);
+
+    ls_std::SerializableJSONBoolean serializable {x};
+    serializable.unmarshal(R"({"value":true})");
+
+    ASSERT_TRUE(*x);
+  }
+}