-
Notifications
You must be signed in to change notification settings - Fork 4.5k
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
[API Proposal]: Add JsonPropertyFlag to System.Text.Json.Serialization to signal Property existence in Deserialize objects. #102186
Comments
I haven’t worked with System.Text.Json, but couldn’t you have the setter of the Name property set HasName to true? In most serialization/deserialization frameworks I’ve worked with the property setter is not called if the json string doesn’t contain a value and it is called with null if it contains a null value. It is some boilerplate code but it seems like it keeps the solution at the level where it belongs (most users probably don’t care about this difference). |
It's probably doable but would involve quite a bit of extra typing and having backing fields for every property But this would be simple and would require no changes to existing json models, except adding the flag field where needed. |
Support for such an attribute would be possible to implement using contract customization. It would require a bit of reflection correlating the two properties and then using a composite I don't think it's something we could consider as a built-in functionality though. |
@eiriktsarpalis Thanks! I just implemented it on my side. |
Background and motivation
Problem
Given the following JSONs:
...and this C# object:
When using
JsonSerializer.Deserialize<NameModel>()
on the input JSONs, they yield the (euphemistically) same object even if they are NOT the same object. One of them has a clearly specifiedNULL
value while the other has it missing altogether, it's more likeNONE
.Solution
What I propose is the addition of a new Attribute called
JsonPropertyFlagAttribute
that would work like this:NULL
andNONE
don't need to be distinguished.JsonPropertyFlag
would be excluded from the actual (de)serialization process.JsonPropertyFlag
could be set to false.JsonPropertyName
but should also signal its presence by toggling theJsonPropertyFlag
totrue
. This would says: the Property is present. It may or may not have theNULL
value but the presence itself is important.NULL
value or just skip it. Just likeJsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)
.Inconveniences
Since this would be completely optional, it would change nothing in current JSON Api. But, when using this, the presence of the
JsonPropertyFlag
would also require of theJsonPropertyName
counterpart and their.Name
MUST match. This could be overcome by using the actual name of the linked property in the flag and resolving internally from there (and not the json literal property name), but it's an unneeded complication.My Use Case
I really need this to do partial Database updates on tables with nullable fields. So if a field accepts
NULL
, I only update it when its Property is not missing sinceNULL
is a valid value and I cannot differentiate whether it needs to be updated or not on its value alone.API Proposal
API Usage
Alternative Designs
This same thing can be achieved by using
JsonDocument
orJsonNode
but it's a VERY manual process in which you need to check for Property existence and also fetch it's Value, literally deserializing manually.This could also be implemented with custom objects (like a
JsonNullable<T>
but should also handle references; and would have 3 states:none
,null
andvalue
) types but more complicated than the provided minimal footprint solution.Risks
I don't see any risks. It's an optional addition, very easy to operate around: don't ever serialize it, toggle when a property name matches. The
JsonPropertyFlag
would require the presence of theJsonPropertyName
(so a little extra typing effort, only where needed) but it's optional.The text was updated successfully, but these errors were encountered: