Add skip function for glz::meta to ignore members during write#1846
Conversation
|
Thanks for looking into this! I'm really liking the new functional approach within Here is an example of the supported struct renamed_t {
std::string first_name{};
std::string last_name{};
int age{};
};
template <>
struct glz::meta<renamed_t> {
static constexpr std::string_view rename_key(const std::string_view key) {
if (key == "first_name") {
return "firstName";
}
else if (key == "last_name") {
return "lastName";
}
return key; // Return unchanged for other keys
}
};I tend to think this would be a better approach for your goal, and would look like: struct specify_only_skip_obj
{
int x{1};
int y{2};
};
template <>
struct glz::meta<specify_only_skip_obj > {
static constexpr bool skip(const std::string_view key) {
return key == "x"; // only skip the "x" key
}
};What do you think about this approach? It allows you to skip all keys that start with a specific character, or end with a particular extension, etc. It allows more customized skipping based on key names and keeps things in |
|
Hi, thanks for the feedback, this is indeed much more flexible, will update pr accordingly |
|
Cool, I would be happy to merge in a pull request that adds this |
b5d9455 to
26e6d7b
Compare
|
I cleaned up the code a little and added a check for whether the glz::meta has a skip within I also updated the code to use |
|
Thanks for the update, I think that skipping based on the rename provided by the user is more expected than not, and this is more user facing since it some cases it might be more beneficial for user to rename there members Looks like we can merge this one |
|
Sweet! I'll merge it in, thanks for your contributions and feedback! |
|
@stephenberry is there the possibility to hook into the skip call also from a 'full' to/from custom serialization? My problem is that I have fields that I can't mark optional and I need to inspect the field object before making the decision whether it can be skipped or not. And I also can't use the compile-time option skip_null_members. thx Michi |
|
@scalpel4k, I think you would have to implement checks for skipping within your custom to/from specializations. You can look how it is implemented in the JSON read.hpp and write.hpp. For reading, it looks like this: if constexpr (meta_has_skip<std::remove_cvref_t<T>>) {
if constexpr (meta<std::remove_cvref_t<T>>::skip(Key, {glz::operation::parse})) {
skip_value<JSON>::op<Opts>(ctx, it, end);
if (bool(ctx.error)) [[unlikely]]
return; // Propagate error from skip_value
return;
}
}The |
|
thanks for the hint, I'll have a look into it |
To customize writing of custom types it may be more user friendly to provide a list of members to ignore instead of specialization of
glz::metathis PR attempts to add such functionalityI marked it as a draft since implementation is not complete but would appreciate any feedback