Skip to content

Deserializing json into a general struct #130

Closed Answered by mwalcott3
klemenpeganc asked this question in Q&A
Discussion options

You must be logged in to vote

@klemenpeganc, Just added a very basic generic json type glz::json_t (glaze/json/json_t.hpp) in #132

glz::json_t json{};
std::string buffer = R"([5,"Hello World",{"pi":3.14}])";
glz::read_json(json, buffer);
assert(json[0].get<double>() == 5.0);
assert(json[1].get<std::string>() == "Hello World");
assert(json[2]["pi"].get<double>() == 3.14);

You can use it by itself or nested within one of your own meta objects or in a container. I would recommend using typed JSON like variants if possible but this offers a way to handle things when thats not possible.

As pointed out by @stephenberry your example use case of having 3 structs (structA, structB, structC) is probably better handled by using …

Replies: 5 comments 2 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@stephenberry
Comment options

@mwalcott3
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by mwalcott3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants