Skip to content
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

System.Text.Json serialisation should include all public fields in a struct by default #44007

Closed
jessicah opened this issue Oct 29, 2020 · 3 comments

Comments

@jessicah
Copy link
Contributor

Description

Serializing a struct with the System.Text.Json serializer should serialize all public fields by default, and only exclude fields by default for classes.

Other information

I haven't tested out record types in C# 9, but would expect the behaviour there to be the same as serializing a struct.

@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added area-System.Text.Json untriaged New issue has not been triaged by the area owner labels Oct 29, 2020
@Symbai
Copy link

Symbai commented Oct 29, 2020

And can you tell us WHY it should do that?

@GrabYourPitchforks
Copy link
Member

Possible dupe of #876.

@layomia layomia removed the untriaged New issue has not been triaged by the area owner label Oct 29, 2020
@layomia layomia added this to the 6.0.0 milestone Oct 29, 2020
@layomia
Copy link
Contributor

layomia commented Oct 29, 2020

Closing as a dupe of #876. When designing field support in JsonSerializer, we decided to not include fields when serializing and deserializing by default to avoid a breaking change from .NET 3.1 where fields were ignored. See #2192 (comment) for more info. You can opt into field support by setting JsonSerializerOptions.IncludeFields to true or by annotating each field to include with JsonIncludeAttribute.


What's the rationale for including fields by default for structs, but not classes? I think this behavior would be very confusing.

@layomia layomia closed this as completed Oct 29, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 6, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants