From 51c5b0690f830317a5f6bd55f7e227c803dbdb9e Mon Sep 17 00:00:00 2001
From: Timm Fitschen <t.fitschen@indiscale.com>
Date: Wed, 22 Jun 2022 21:33:34 +0200
Subject: [PATCH] More implementation for JsonValue

---
 include/caosdb/utility.h | 10 ++++++++--
 src/caosdb/utility.cpp   | 16 ++++++++++++++++
 test/test_utility.cpp    | 15 ---------------
 3 files changed, 24 insertions(+), 17 deletions(-)

diff --git a/include/caosdb/utility.h b/include/caosdb/utility.h
index a27ab64..43586da 100644
--- a/include/caosdb/utility.h
+++ b/include/caosdb/utility.h
@@ -98,6 +98,12 @@ inline auto get_env_fallback(const std::string &key, const std::string &fallback
  */
 class JsonValue {
 public:
+  /**
+   * Default Constructor.
+   *
+   * Creates an empty wrapper where `wrapped` is nullptr.
+   */
+  JsonValue() : JsonValue(nullptr) {}
   /**
    * Constructor.
    *
@@ -124,14 +130,14 @@ public:
    *
    * Also copies the `wrapped` object.
    */
-  auto operator=(const JsonValue &other) -> JsonValue & = default;
+  auto operator=(const JsonValue &other) -> JsonValue &;
 
   /**
    * Move Constructor.
    *
    * Also moves the `wrapped` object.
    */
-  JsonValue(JsonValue &&other);
+  JsonValue(JsonValue &&other) = default;
 
   /**
    * Move Assigment.
diff --git a/src/caosdb/utility.cpp b/src/caosdb/utility.cpp
index 6a9c8ec..7aed716 100644
--- a/src/caosdb/utility.cpp
+++ b/src/caosdb/utility.cpp
@@ -153,4 +153,20 @@ auto JsonValue::Reset() -> void {
   this->wrapped = nullptr;
 }
 
+JsonValue::JsonValue(const JsonValue &other) : wrapped(nullptr) {
+  if (!other.IsNull()) {
+    this->wrapped = new value(*static_cast<value *>(other.wrapped));
+  }
+}
+
+auto JsonValue::operator=(const JsonValue &other) -> JsonValue & {
+  if (this != &other) {
+    Reset();
+    if (!other.IsNull()) {
+      this->wrapped = new value(*static_cast<value *>(other.wrapped));
+    }
+  }
+  return *this;
+}
+
 } // namespace caosdb::utility
diff --git a/test/test_utility.cpp b/test/test_utility.cpp
index 3a9a420..74db29a 100644
--- a/test/test_utility.cpp
+++ b/test/test_utility.cpp
@@ -20,10 +20,7 @@
  *
  */
 
-#include "gmock/gmock-matchers.h"             // for ElementsAre, EXPECT_THAT
 #include "boost/beast/core/detail/base64.hpp" // for encoded_size
-#include "boost/json/object.hpp"              // for object
-#include "boost/json/value.hpp"               // for value
 #include "caosdb/data_type.h"                 // for atomicdatatype_names
 #include "caosdb/entity.h"                    // for importance_names, role...
 #include "caosdb/status_code.h"               // for get_status_description
@@ -38,7 +35,6 @@
 #include <utility>                            // for pair
 
 namespace caosdb::utility {
-using ::testing::ElementsAre;
 
 TEST(test_utility, base64_encode) {
   auto test_plain = std::string("admin:caosdb");
@@ -48,17 +44,6 @@ TEST(test_utility, base64_encode) {
   EXPECT_EQ(test_encoded, base64_encode(test_plain));
 }
 
-TEST(test_utility, test_load_json_file) {
-  auto json = load_json_file(TEST_DATA_DIR + "/test.json").as_object();
-
-  EXPECT_EQ(json["it"], "tests");
-  EXPECT_EQ(json["null values"], nullptr);
-  EXPECT_THAT(json["this"].as_array(), ElementsAre("is", "a", "test"));
-  EXPECT_THAT(json["numbers"].as_array(), ElementsAre(1, 2, 3.3));
-  auto sub = json["arrays and objects"].as_object();
-  EXPECT_THAT(sub["see?"].as_array(), ElementsAre(true, false));
-}
-
 TEST(test_utility, enum_names) {
   // All working enums
   for (const auto &entry : caosdb::entity::importance_names) {
-- 
GitLab