Skip to content

Commit

Permalink
tmp: JOBJECT
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO committed Jan 23, 2024
1 parent 2db2aae commit 45d40fd
Showing 1 changed file with 57 additions and 8 deletions.
65 changes: 57 additions & 8 deletions sample/sample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,23 @@
bool parsing();
bool parsing_width();
bool serializing();
void test_JOBJECT();

int main()
{
std::cout << "\n****** Parsing ******\n" << std::endl;
// std::cout << "\n****** Parsing ******\n" << std::endl;

if (!parsing()) {
return -1;
}
// if (!parsing()) {
// return -1;
// }

std::cout << "\n****** Serializing ******\n" << std::endl;
// std::cout << "\n****** Serializing ******\n" << std::endl;

if (!serializing()) {
return -1;
}
// if (!serializing()) {
// return -1;
// }

test_JOBJECT();

return 0;
}
Expand Down Expand Up @@ -293,3 +296,49 @@ bool serializing()

return true;
}

void test_JOBJECT()
{

#define DEF_DUMP_TO_JSON(x) \
json::object dump_to_json() const \
{ \
return { \
{ #x, json::serialize<false>(x) }, \
}; \
}

#define DEF_LOAD_FROM_JSON(x) \
bool load_from_json(const json::object& raw) \
{ \
auto x##opt = raw.find<decltype(x)>(#x); \
if (!x##opt) { \
return false; \
} \
x = *std::move(x##opt); \
return true; \
}

struct MyStruct
{
std::vector<int> x;
int y = 0;

double a = 0;
double b = 0;

DEF_DUMP_TO_JSON(x);
DEF_LOAD_FROM_JSON(x);
};

MyStruct a;
a.x = { 1, 2, 3, 4, 5 };

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

MyStruct b;
b.load_from_json(j);

std::cout << b.dump_to_json() << std::endl;
}

0 comments on commit 45d40fd

Please sign in to comment.