Skip to content

Commit

Permalink
err: operator json::object
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO committed Jan 24, 2024
1 parent 41979bf commit b75b4a7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 38 deletions.
6 changes: 5 additions & 1 deletion include/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2924,7 +2924,7 @@ namespace json::_private_macro
if constexpr (std::is_same_v<std::decay_t<decltype(x)>, json::_jsonization_helper::next_is_optional>) { \
_MEOJSON_VARNAME(next_is_optional) = true; \
} \
else if (auto _MEOJSON_VARNAME(opt) = _MEOJSON_VARNAME(input).find(_MEOJSON_STRINGIZE(x))) { \
else if (auto _MEOJSON_VARNAME(opt) = _MEOJSON_VARNAME(input).find(_MEOJSON_STRINGIZE(x))) { \
if (!_MEOJSON_VARNAME(opt)->is<decltype(x)>()) { \
_MEOJSON_VARNAME(error_key) = _MEOJSON_STRINGIZE(x); \
return false; \
Expand Down Expand Up @@ -2957,6 +2957,10 @@ namespace json::_private_macro
[[maybe_unused]] bool _MEOJSON_VARNAME(next_is_optional) = false; \
_MEOJSON_FOR_EACH(_MEOJSON_LOAD_ARG, __VA_ARGS__) \
return true; \
} \
explicit operator json::object() const \
{ \
return dump_to_json(); \
}

#define MEO_OPTIONAL json::_jsonization_helper::next_is_optional(),
Expand Down
41 changes: 4 additions & 37 deletions sample/sample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,45 +301,12 @@ void test_jsonization()
{
struct MyStruct : json::bind_helper
{
std::vector<int> vec;
std::map<std::string, int> map;
int aaa = 100;

int i = 0;
double d = 0;

MEO_JSONIZATION(vec, map, MEO_OPTIONAL i, MEO_OPTIONAL d);
MEO_JSONBIND(MEO_REQ(vec), MEO_REQ(map), MEO_OPT(i), MEO_OPT(d))
MEO_JSONIZATION(aaa);
};

MyStruct a;
a.vec = { 1, 2, 3 };
a.map = { { "key", 5 } };
a.i = 100;
a.d = 0.5;

// json::object j = a.dump_to_json();
// std::cout << j << std::endl;

// // for test MEO_OPTIONAL
// j.erase("i");

// MyStruct b;
// bool loaded = b.load_from_json(j);

// std::cout << "loaded: " << loaded << std::endl;
// std::cout << b.dump_to_json() << std::endl;

{
json::object j = a.to_json();
std::cout << j << std::endl;

// for test MEO_OPTIONAL
j.erase("i");

MyStruct b;
bool loaded = b.from_json(j);

std::cout << "loaded: " << loaded << std::endl;
std::cout << b.to_json() << std::endl;
}
auto j1 = a.operator json::object();
auto j2 = (json::object)a; // error
}

0 comments on commit b75b4a7

Please sign in to comment.