Closed
Description
Updated by @layomia: I'm pasting the API approved in #34558. Please see that issue for review notes and further discussion.
namespace System.Text.Json
{
public partial class JsonSerializerOptions
{
public bool IgnoreReadOnlyFields { get; set; }
public bool IncludeFields { get; set; }
}
}
namespace System.Text.Json.Serialization
{
[AttributeUsage(AttributeTargets.Property |
Attributes.Field, AllowMultiple = false)]
public sealed class JsonIncludeAttribute : JsonAttribute
{
public JsonIncludeAttribute();
}
}
Original proposal (click to view)
There is no way to serialize and deserialize fields using JsonSerializer
.
While public fields are generally not recommended, they are used in .NET itself (see value tuples) and by users.
This feature was scoped out of v1 (3.x) due to lack of time, as we prioritized supporting simple POCOs with public properties.
We elected to have an opt-in model for field support because it would be a breaking change to support them by default, and also because public fields are generally not recommended. Other serializers, including Newtonsoft.Json
, Utf8Json
, and Jil
, support this feature by default.