Repository navigation
Replies: 1 comment
|
@alex-tee How about this? struct Position
{
double ticks = 0.0;
long frames = 0;
};
// deserialize
auto json = yyjson::read(R"({"ticks": 0.1, "frames": 1})");
if (auto json_obj = json.as_object(); json_obj)
{
auto position = yyjson::cast<Position>(*json_obj);
std::cout << "ticks: " << position.ticks << std::endl;
std::cout << "frames: " << position.frames << std::endl;
// -> ticks: 0.1
// frames: 1
}
else
{
throw std::runtime_error("invalid JSON object");
}
// serialize
Position my_position = {0.1, 1};
auto json_obj = yyjson::object(my_position);
std::cout << json_obj.write() << std::endl;
// -> {"ticks":0.1,"frames":1}We do not need to write the caster for your case since the compile-time reflection can be used. Please see here about casters. |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Thanks for this library! It looks promising, but I have no idea how to use it and I can't find proper examples to do what I was able to do with C yyjson (I am trying to port my code from the C yyjson API).
How would I do this with cpp-yjson?
All reactions