Description
Expected behavior: If a read-only property has [JsonInclude] attribute, it should get serialized via JsonSerializer.Serialize even if following serialization options are used:
new JsonSerializerOptions { IgnoreReadOnlyProperties = true })
but this is not the case.
Actual behavior: the property is ignored by serialization.
Note that [JsonIgnore] will correctly override options that have IgnoreReadOnlyProperties = false and ignore a property. Hence I would expect an analogous "mirrored" behavior from [JsonInclude].
As a side note, I was unable to find any documentation explaining the precedence of attributes vs serializer options.
Reproduction Steps
Given in this gist:
https://gist.github.com/konrad-jamrozik/721a9c7624560fae0d1582353ef0708e
To repro, run JsonIncludeReproTests.JsonIgnoreOverridesOptionsButJsonIncludeDoesNot
Expected behavior
See the description above.
Actual behavior
See the description above.
Regression?
I don't know.
Known Workarounds
None
Configuration
.NET 8 preview. Details in the gist:
https://gist.github.com/konrad-jamrozik/721a9c7624560fae0d1582353ef0708e
Other information
No response
Description
Expected behavior: If a read-only property has
[JsonInclude]attribute, it should get serialized viaJsonSerializer.Serializeeven if following serialization options are used:but this is not the case.
Actual behavior: the property is ignored by serialization.
Note that
[JsonIgnore]will correctly override options that haveIgnoreReadOnlyProperties = falseand ignore a property. Hence I would expect an analogous "mirrored" behavior from[JsonInclude].As a side note, I was unable to find any documentation explaining the precedence of attributes vs serializer options.
Reproduction Steps
Given in this gist:
https://gist.github.com/konrad-jamrozik/721a9c7624560fae0d1582353ef0708e
To repro, run
JsonIncludeReproTests.JsonIgnoreOverridesOptionsButJsonIncludeDoesNotExpected behavior
See the description above.
Actual behavior
See the description above.
Regression?
I don't know.
Known Workarounds
None
Configuration
.NET 8 preview. Details in the gist:
https://gist.github.com/konrad-jamrozik/721a9c7624560fae0d1582353ef0708e
Other information
No response