Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Easier Initialization of glz::json #133

Merged
merged 4 commits into from
Jan 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/glaze/core/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ namespace glz
concept complex_t = glaze_t<std::decay_t<T>>;

template <class T>
concept str_t = !complex_t<T> && std::convertible_to<std::decay_t<T>, std::string_view>;
concept str_t = !complex_t<T> && !std::same_as<std::nullptr_t, T> && std::convertible_to<std::decay_t<T>, std::string_view>;

template <class T>
concept pair_t = requires(T pair)
Expand Down
21 changes: 20 additions & 1 deletion include/glaze/json/json_t.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ namespace glz
// Generic json type.
struct json_t
{
using null_t = std::monostate;
using array_t = std::vector<json_t>;
using object_t = std::map<std::string, json_t, std::less<>>;
using null_t = double*;
using val_t = std::variant<null_t, double, std::string, bool, array_t, object_t>;
val_t data{};

Expand All @@ -44,6 +44,7 @@ namespace glz
json_t& operator[](std::convertible_to<std::string_view> auto&& key)
{
//[] operator for maps does not support heterogeneous lookups yet
if (holds<null_t>()) data = object_t{};
auto& object = std::get<object_t>(data);
auto iter = object.find(key);
if (iter == object.end()) {
Expand All @@ -70,5 +71,23 @@ namespace glz
val_t& operator*() { return data; }

const val_t& operator*() const { return data; }

json_t() = default;

template <class T>
requires std::convertible_to<T, val_t> && (!std::same_as<json_t, std::decay_t<T>>)
json_t(T&& val)
{
data = val;
}

json_t(std::initializer_list<std::pair<const std::string, json_t>>&& obj) { data = object_t(obj); }

// Prevent conflict with object initializer list
template <bool deprioritize = true>
json_t(std::initializer_list<json_t>&& arr)
{
data = array_t(arr);
}
};
}
15 changes: 13 additions & 2 deletions tests/json_test/json_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2264,10 +2264,21 @@ suite variant_tests = [] {

suite generic_json_tests = [] {
"generic_json_write"_test = [] {
glz::json_t json = {glz::json_t::array_t{{5.0}, {"Hello World"}, {glz::json_t::object_t{{"pi", {3.14}}}}}};
glz::json_t json = {
{"pi", 3.141},
{"happy", true},
{"name", "Niels"},
{"nothing", nullptr},
{"answer", {{"everything", 42.0}}},
{"list", {1.0, 0.0, 2.0}},
{"object", {
{"currency", "USD"},
{"value", 42.99}
}}
};
std::string buffer{};
glz::write_json(json, buffer);
expect(buffer == R"([5,"Hello World",{"pi":3.14}])");
expect(buffer == R"({"answer":{"everything":42},"happy":true,"list":[1,0,2],"name":"Niels","object":{"currency":"USD","value":42.99},"pi":3.141})") << buffer;
};

"generic_json_read"_test = [] {
Expand Down