Skip to content

Json source generator compiler error using reserved property names and DefaultIgnoreCondition #98050

Closed
@vsfeedback

Description

@vsfeedback

This issue has been moved from a ticket on Developer Community.


[severity:I'm unable to use this version]
In .net8 using JSON source generator with property names that are preserved c# names and using JsonIgnoreCondition.WhenWritingNull, invalid sources will be generated.

SourceGenBug.zip

public class Model
{
  public string type { get; set; }
  public string alias { get; set; }
}

[JsonSourceGenerationOptions(DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
[JsonSerializable(typeof(Model))]
internal partial class ModelContext : JsonSerializerContext
{
}

This will generate source with a lot of compilation errors.
It wil generate

        string __value_@type = ((global::Model)value).@type;
        if (__value_@type != null)
        {
            writer.WriteString(PropName_type, __value_@type);
        }

In dotnet 7 it would generate:

            if (value.@type != null)
            {
                writer.WriteString(PropName_type, value.@type);
            }


Original Comments

Feedback Bot on 12/6/2023, 06:10 PM:

(private comment, text removed)


Original Solutions

(no solutions)

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions