From 8c65f5375ae53a7b1535bb270991f7ae66ad43f0 Mon Sep 17 00:00:00 2001 From: matcool <26722564+matcool@users.noreply.github.com> Date: Sat, 9 Nov 2024 13:01:10 -0300 Subject: [PATCH] add std::string ctor --- include/matjson.hpp | 1 + src/value.cpp | 4 ++++ test/test.cpp | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+) diff --git a/include/matjson.hpp b/include/matjson.hpp index af59b4d..49ff409 100644 --- a/include/matjson.hpp +++ b/include/matjson.hpp @@ -76,6 +76,7 @@ namespace matjson { public: /// Defaults to a JSON object, for convenience Value(); + Value(std::string value); Value(std::string_view value); Value(char const* value); Value(std::vector value); diff --git a/src/value.cpp b/src/value.cpp index ca94a99..05b186f 100644 --- a/src/value.cpp +++ b/src/value.cpp @@ -11,6 +11,10 @@ Value::Value() { Value::Value(char const* str) : Value(std::string(str)) {} +Value::Value(std::string value) { + m_impl = std::make_unique(Type::String, std::move(value)); +} + Value::Value(std::string_view value) { m_impl = std::make_unique(Type::String, std::string(value)); } diff --git a/test/test.cpp b/test/test.cpp index 27e6051..9289e53 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -335,3 +335,35 @@ TEST_CASE("Special characters") { REQUIRE(obj["control"].asString().unwrap() == "\b\f\n\r\t\x12 "); } + +TEST_CASE("Implicit ctors") { + matjson::Value value; + + value["a"] = 123; + value["a"] = 123.0; + value["a"] = true; + value["a"] = false; + value["a"] = nullptr; + value["a"] = "Hello!"; + std::string foo; + value["a"] = foo; + char const* bar = "hi"; + value["a"] = bar; + std::string_view baz = "c"; + value["a"] = baz; + + struct Test { + operator std::string() { + return "hi"; + } + } t; + + value["a"] = t; + + value["a"] = CoolStruct{ + .name = "Hello!", + .value = 123, + }; + CoolStruct b{}; + value["a"] = b; +}