Skip to content

Nested Structure Variant with Deduction of Tagged Object Types #1246

Answered by stephenberry
jimmylim0823 asked this question in Q&A
Discussion options

You must be logged in to vote

Thanks for the additional details.

Glaze cannot decode variants from sub-keys. Support may be added in the future, but I'm wary about adding too much complexity to variant handling, which is already complex. However, there are various solutions to this problem.

  1. Use JSON Pointer access for switching logic on your key
std::string s = R"({"info_common":{"key":"a","value":"val_a"},"info":[{"a":"foo_0", "b":"bar_0"},{"a":"foo_1","b":"bar_1"}]})";
std::string key = glz::get_as_json<std::string, "/info_common/key">(s);

// This lets you avoid variants if you don't want them
if (key == "a") {
   // read into data_a
}
else if (key == "b") {
   // read into data_b
}

Note that you can still use a …

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@jimmylim0823
Comment options

@stephenberry
Comment options

Answer selected by jimmylim0823
@jimmylim0823
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants