-
Notifications
You must be signed in to change notification settings - Fork 121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow map/transform JSON fields into struct fields via projection function #1437
Comments
The glz::meta API allows you to rename your fields however you like. If you look at the README it will explain a bit. Here's an example: template <>
struct glz::meta<file>
{
using T = file;
static constexpr auto value = object(“languageId”, &T::language_id, &T::uri, &T::version);
}; This will parse languageId in camel case into the C++ variable name language_id. You can customize any of your fields in this manner. Does this support your needs? |
I thought about it, but don't exactly know, is it possible to generalize to all identifiers, and another question: will another fields be read from JSON and deserialized into C++ struct? And I see an issue with scalability - we have to (re)write all fields likewise instead of transform() logic. I also looked into the code and began to work on my issue locally, I could try to make a PR with this feature, if you don't mind. |
I agree with complexities in scalability and transform logic would be cleaner. This is achievable at compile time, but would be somewhat complicated. The transformation would have to happen when populating the One tricky aspect is that these lambda functions like One last thought: I'm trying to avoid making top level APIs that are esoteric and complex, when we have reflection in C++26 coming soon. I don't want to add features that will be superseded by C++26 reflection, but rather add features that will complement it. I feel like this transformation might be better achieved with the coming reflection features since the transformation logic has to be implemented at the top level. |
Current
glz
API seems doesn't support JSON field name customizations like one of these cases:In this case glz would apply some transform function "
to_snake
" which "projects" the json field "languageId" to another string literal "language_id" and map it into file::language_id.It would be good to define the consteval(constexpr?) function with string fields names projection like:
For this case it may compare the input JSON keys with reserved C++ identifiers:
vs
auto location = glz::read_json<location, project_field_names_using<adapt_lat_lon_names>(in_json);
The reverse operation can also be projected from reflected field name to JSON field name.
The
glz::meta
API may also looks like:These proposed operations are conceptually same as projections in C++ stdlib ranged algorithms, but for JSON field names <-> native struct field names.
Sorry if describe it vaguely, and thanks for your lib.
The text was updated successfully, but these errors were encountered: