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 converter documentation: Converters is a readonly property #43178

Open
SeppPenner opened this issue Oct 23, 2024 · 0 comments · May be fixed by #43349
Open

System.Text.Json converter documentation: Converters is a readonly property #43178

SeppPenner opened this issue Oct 23, 2024 · 0 comments · May be fixed by #43349
Labels
dotnet-fundamentals/svc in-pr This issue will be closed (fixed) by an active pull request. Pri1 High priority, do before Pri2 and Pri3

Comments

@SeppPenner
Copy link

Type of issue

Outdated article

Description

This code:

var serializeOptions = new JsonSerializerOptions
{
    WriteIndented = true,
    Converters =
    {
        new DateTimeOffsetJsonConverter()
    }
};

jsonString = JsonSerializer.Serialize(weatherForecast, serializeOptions);

should be

var serializeOptions = new JsonSerializerOptions
{
    WriteIndented = true
};

serializeOptions.Converters.Add(DateTimeOffsetJsonConverter());
jsonString = JsonSerializer.Serialize(weatherForecast, serializeOptions);

and

var options = new JsonSerializerOptions
{
    Converters = { new JsonConverterFactoryForStackOfT() },
};

should be

var options = new JsonSerializerOptions();
options.Converters.Add(new JsonConverterFactoryForStackOfT());

The reason is that in Net8, Converters is now readonly. Converters is defined as following (Readonly):

/// <summary>
/// Provides options to be used with <see cref="JsonSerializer"/>.
/// </summary>
public sealed partial class JsonSerializerOptions
{
    /// <summary>
    /// The list of custom converters.
    /// </summary>
    /// <remarks>
    /// Once serialization or deserialization occurs, the list cannot be modified.
    /// </remarks>
    public IList<JsonConverter> Converters => _converters ??= new(this);

Page URL

https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/converters-how-to

Content source URL

https://github.com/dotnet/docs/blob/main/docs/standard/serialization/system-text-json/converters-how-to.md

Document Version Independent Id

678c4f23-764a-7ccd-0d6d-159a30957477

Article author

@gewarren

Metadata

  • ID: 5de37d79-751f-e372-e866-efde28f4908c
  • Service: dotnet-fundamentals
@issues-automation issues-automation bot added dotnet-fundamentals/svc Pri1 High priority, do before Pri2 and Pri3 labels Oct 23, 2024
SeppPenner added a commit to SeppPenner/docs that referenced this issue Oct 23, 2024
SeppPenner added a commit to SeppPenner/docs that referenced this issue Oct 23, 2024
SeppPenner added a commit to SeppPenner/docs that referenced this issue Oct 23, 2024
@dotnet-policy-service dotnet-policy-service bot added the in-pr This issue will be closed (fixed) by an active pull request. label Oct 23, 2024
SeppPenner added a commit to SeppPenner/docs that referenced this issue Nov 5, 2024
Changed the code samples with System.Text.Json Converters (Now readonly property).

Fixes dotnet#43178, makes dotnet#43179 obsolete.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dotnet-fundamentals/svc in-pr This issue will be closed (fixed) by an active pull request. Pri1 High priority, do before Pri2 and Pri3
Projects
None yet
1 participant