Sfoglia il codice sorgente

Added SerializableJSONString class

- added SerializableJSONString class to offer possibility to serialize
ls_std::String class
- added tests for SerializableJSONString class
pcmattulat 4 anni fa
parent
commit
b9fb195f73

+ 5 - 2
CMakeLists.txt

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

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

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

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

@@ -0,0 +1,37 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2020-08-30
+ * Changed:         2020-08-30
+ *
+ * */
+
+#ifndef SERIALIZABLE_JSON_STRING_HPP
+#define SERIALIZABLE_JSON_STRING_HPP
+
+#include <memory>
+#include <json.hpp>
+#include "../../serialization/ISerializable.hpp"
+#include "../../boxing/String.hpp"
+
+namespace ls_std {
+  class SerializableJSONString : public Class, public ISerializable {
+    public:
+
+      explicit SerializableJSONString(std::shared_ptr<ls_std::String> _value);
+      ~SerializableJSONString() = 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::String> value {};
+
+      void _update();
+  };
+}
+
+#endif

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

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