Skip to content

Json source generator generates uncompilable code for new .NET Framework project #83392

@stephentoub

Description

@stephentoub

The JSON source generator purports to support working in .NET Framework and .NET Standard projects. However, such projects default to using a language version of C# 7.3, which is the last version of the language officially supported on those platform targets. And the JSON source generator emits code that requires newer features, in particular nullable, which is only usable with a C# 8+ language version. Thus, out of the box in a new .NET Framework or .NET Standard project, attempts to use the JSON source generator result in a littany of errors.

Repro:

  1. Create a new .NET Framework 4.8 console app.
  2. Add a nuget reference to the latest stable System.Text.Json nuget package.
  3. Paste this code into your Program.cs, which is based on the docs for [JsonSerializable]:
using System.Collections.Generic;
using System.Text.Json.Serialization;

internal class Program
{
    static void Main() { }
}

public class WeatherForecast
{
    public object Data { get; set; }
    public List<object> DataList { get; set; }
}

[JsonSerializable(typeof(WeatherForecast))]
[JsonSerializable(typeof(bool))]
[JsonSerializable(typeof(int))]
public partial class WeatherForecastContext : JsonSerializerContext
{
}
  1. Compile. You'll get errors like this:
1>ConsoleApp7\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\WeatherForecastContext.g.cs(32,74,32,75): error CS8370: Feature 'nullable reference types' is not available in C# 7.3. Please use language version 8.0 or greater.
1>ConsoleApp7\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\WeatherForecastContext.g.cs(44,76,44,77): error CS8370: Feature 'nullable reference types' is not available in C# 7.3. Please use language version 8.0 or greater.
1>ConsoleApp7\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\WeatherForecastContext.GetJsonTypeInfo.g.cs(41,69,41,70): error CS8370: Feature 'nullable reference types' is not available in C# 7.3. Please use language version 8.0 or greater.
1>ConsoleApp7\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\WeatherForecastContext.Object.g.cs(10,100,10,101): error CS8370: Feature 'nullable reference types' is not available in C# 7.3. Please use language version 8.0 or greater.
1>ConsoleApp7\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\WeatherForecastContext.ListObject.g.cs(10,141,10,142): error CS8370: Feature 'nullable reference types' is not available in C# 7.3. Please use language version 8.0 or greater.
1>ConsoleApp7\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\WeatherForecastContext.WeatherForecast.g.cs(10,102,10,103): error CS8370: Feature 'nullable reference types' is not available in C# 7.3. Please use language version 8.0 or greater.
1>ConsoleApp7\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\WeatherForecastContext.Boolean.g.cs(10,101,10,102): error CS8370: Feature 'nullable reference types' is not available in C# 7.3. Please use language version 8.0 or greater.
1>ConsoleApp7\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\WeatherForecastContext.Int32.g.cs(10,99,10,100): error CS8370: Feature 'nullable reference types' is not available in C# 7.3. Please use language version 8.0 or greater.
1>ConsoleApp7\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\WeatherForecastContext.g.cs(22,54,22,55): error CS8370: Feature 'nullable reference types' is not available in C# 7.3. Please use language version 8.0 or greater.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

To my knowledge, the only fix is to change your project's language version to something >= 8.0, which is officially an unsupported configuration.

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions