System.Text.Json: Providing default value for missing properties #96714
Unanswered
florianbader
asked this question in
Ideas
Replies: 2 comments 1 reply
-
|
Is the type owned by you? Can you set default values in its constructor? |
Beta Was this translation helpful? Give feedback.
1 reply
-
|
We're a couple of years past and there's still no good way to provide default values for missing JSON fields that I'm aware of. This is especially true with immutable F# records. Does .NET 10 or 11 have any new functionality that might help? |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I'm trying to implement handling of changes in my JSON schema. This includes for now adding a new property to the .NET type that is missing in an existing JSON by providing a default value.
While this would work fine with mutable types it's difficult to do with immutable records. Maybe I'm missing something but currently the only way I found is through reflection:
A
JsonConverterdoesn't work as theReadmethod is not called for properties that are missing in the JSON. There is also no way to use aTypeInfoResolveron myDocumentobject for theOptionsproperty as theSetdelegate is also not called for missing properties in JSON.Would be nice to have some way to actually provide a default value for missing properties through a JSON contract though something like
Any thoughts?
Beta Was this translation helpful? Give feedback.
All reactions