You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As far as I can tell, createPerFieldToJson is currently mostly used by other code generators, and is rather impractical to use in other cases. As the name suggests, I would like to be able to only serialize some parts of a model, but the abstract class generated does not expose any methods that would make it easy to do this. If I were to use them in the way that the name suggests, my options are:
Copy-paste the generated code with the key strings, and manually create the methods I need to only serialize some fields of a model.
Mark most fields as nullable and not required, and simply copyWith(), with the unnecessary values nulled.
Create models that interface with the main ones, that have special json handling.
Create another library that does the heavy lifting, generating methods for me that only serialize the given values.
The first option is not great for obvious reasons. The second and third options are better, but it would lead to a lot of unnecessary null handling and boilerplate. The fourth option is ideal, but I honestly lack the expertise to achieve it.
For clarity, here's an example of what I'm trying to achieve. This is a model that we're using:
With how our API operates, always sending the entire model is undesirable. Trying to update the changelog, lastModified, or created fields for example will lead to errors. Also, during normal operation, there will be quite a few cases where multiple people will send requests that update the same models. Only sending the minimum of what was changed will help a lot avoiding possible data loss from multiple clients overwriting changes.
Is it possible/within reason to implement this functionality?
small note: Feel free to point me to a dependent library that achieves this, if there are any. 😅
The text was updated successfully, but these errors were encountered:
As far as I can tell,
createPerFieldToJson
is currently mostly used by other code generators, and is rather impractical to use in other cases. As the name suggests, I would like to be able to only serialize some parts of a model, but the abstract class generated does not expose any methods that would make it easy to do this. If I were to use them in the way that the name suggests, my options are:Copy-paste the generated code with the key strings, and manually create the methods I need to only serialize some fields of a model.
Mark most fields as nullable and not required, and simply copyWith(), with the unnecessary values nulled.
Create models that interface with the main ones, that have special json handling.
Create another library that does the heavy lifting, generating methods for me that only serialize the given values.
The first option is not great for obvious reasons. The second and third options are better, but it would lead to a lot of unnecessary null handling and boilerplate. The fourth option is ideal, but I honestly lack the expertise to achieve it.
For clarity, here's an example of what I'm trying to achieve. This is a model that we're using:
I would like to be able to make a method like this:
So I can do this:
Use case:
With how our API operates, always sending the entire model is undesirable. Trying to update the changelog, lastModified, or created fields for example will lead to errors. Also, during normal operation, there will be quite a few cases where multiple people will send requests that update the same models. Only sending the minimum of what was changed will help a lot avoiding possible data loss from multiple clients overwriting changes.
Is it possible/within reason to implement this functionality?
small note: Feel free to point me to a dependent library that achieves this, if there are any. 😅
The text was updated successfully, but these errors were encountered: