Skip to content

Commit

Permalink
add std::string ctor
Browse files Browse the repository at this point in the history
  • Loading branch information
matcool committed Nov 9, 2024
1 parent c62d44c commit 8c65f53
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/matjson.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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> value);
Expand Down
4 changes: 4 additions & 0 deletions src/value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<ValueImpl>(Type::String, std::move(value));
}

Value::Value(std::string_view value) {
m_impl = std::make_unique<ValueImpl>(Type::String, std::string(value));
}
Expand Down
32 changes: 32 additions & 0 deletions test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 8c65f53

Please sign in to comment.