Skip to content

Reading parts of JSON (several times) #426

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

You must be logged in to vote

Hi! I would not use generic JSON for this problem, because that will load the entire structure into memory and be slow.

You can use glz::skip to indicate values that you do not want to read in. This will acknowledge the keys existence and simply skip the value. But, if may be annoying to add a bunch of skips. You can also set error_on_unknown_keys to false, which will then skip any keys that you have not added to your struct. This might be your best option, because then you can simply register the keys that you want to decode.

struct S {
  int i{};
};

template <>
struct glz::meta<S> {
  static constexpr auto value = object("key_to_skip", skip{}, "x", &S::i);
};

So, in short, look into us…

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Answer selected by sam20908
Comment options

You must be logged in to vote
1 reply
@stephenberry
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
Converted from issue

This discussion was converted from issue #425 on September 13, 2023 15:20.