diff --git a/include/caosdb/utility.h b/include/caosdb/utility.h index a27ab64f068d008ace914676cee675851313eef5..43586da27258311470241097d536bdeb7c58c772 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 6a9c8ec0bae052649e0c72ecc4b15bd08f163b95..7aed71621b7c8a04b332bd785e5aeee4a8dfa26b 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 3a9a420cee742c1001c727a6154452ee10e8d8fb..74db29afa798ccdbf9d4be45497e1a44762d630d 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) {