Reading parts of JSON (several times) #426
-
I have one giant JSON that contains some data for different "components" of my app, and I would like to separate out the variables that stores the data into each component (not one giant struct). Is it possible? Also, what's the most performant way to do it? I know generic JSON can help me do this, but I know my keys and hope that I can somehow take advantage of small dynamic allocations |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
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 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 using |
Beta Was this translation helpful? Give feedback.
-
That makes sense. I wonder what you mean by "skip" values, because does that mean it avoids parsing the skipped keys and values entirely (something like a linear scan of the string every time the string is parsed but it just goes over the skipped parts)? I might be misunderstanding so please help me out 👍 |
Beta Was this translation helpful? Give feedback.
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 seterror_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.So, in short, look into us…