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 Source Generator not working with classes in global namespace #56970

Closed
Vake93 opened this issue Aug 6, 2021 · 2 comments
Closed
Labels
area-System.Text.Json untriaged New issue has not been triaged by the area owner

Comments

@Vake93
Copy link

Vake93 commented Aug 6, 2021

Description

If a class is defined in the global namespace, the JsonSerializerContext class code generation does not work.
Installed .NET SDK Version is: 6.0.100-preview.6.21355.2

Steps to re-create this is as follows:

  1. Create a new .NET 6 console application
  2. Add a reference to the latest preview of System.Text.Json NuGet (I tested with 6.0.0-rc.1.21405.12)
  3. Replace the contents of Program.cs with the following
using System.Text.Json;
using System.Text.Json.Serialization;

string SerializeSourceGen() => JsonSerializer.Serialize(new Person("Vishvaka", "Ranasinghe", 28));

SerializeSourceGen();

public class Person
{
    public Person(string firstName, string lastName, uint age)
    {
        FirstName = firstName;
        LastName = lastName;
        Age = age;
    }

    public string FirstName { get; set; }

    public string LastName { get; set; }

    public uint Age { get; set; }
}

[JsonSerializable(typeof(Person))]
internal partial class MyJsonContext : JsonSerializerContext
{
}
  1. Run with dotnet run command. Notice the build fails.
  2. Now replace the contents of Program.cs with the following
using Experiments;
using System.Text.Json;

string SerializeSourceGen() => JsonSerializer.Serialize(new Person("Vishvaka", "Ranasinghe", 28));
SerializeSourceGen();
  1. Create new Models.cs file with the contents as:
using System.Text.Json.Serialization;

namespace Experiments
{
    public class Person
    {
        public Person(string firstName, string lastName, uint age)
        {
            FirstName = firstName;
            LastName = lastName;
            Age = age;
        }

        public string FirstName { get; set; }

        public string LastName { get; set; }

        public uint Age { get; set; }
    }

    [JsonSerializable(typeof(Person))]
    internal partial class MyJsonContext : JsonSerializerContext
    {
    }
}
  1. Project build and run as expected.

Not having support for classes cause the need to create additional files with namespaces when trying to create a simple application using top-level statements.

@dotnet-issue-labeler dotnet-issue-labeler bot added area-System.Text.Json untriaged New issue has not been triaged by the area owner labels Aug 6, 2021
@ghost
Copy link

ghost commented Aug 6, 2021

Tagging subscribers to this area: @eiriktsarpalis, @layomia
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

If a class is defined in the global namespace, the JsonSerializerContext class code generation does not work.
Installed .NET SDK Version is: 6.0.100-preview.6.21355.2

Steps to re-create this is as follows:

  1. Create a new .NET 6 console application
  2. Add a reference to the latest preview of System.Text.Json NuGet (I tested with 6.0.0-rc.1.21405.12)
  3. Replace the contents of Program.cs with the following
using System.Text.Json;
using System.Text.Json.Serialization;

string SerializeSourceGen() => JsonSerializer.Serialize(new Person("Vishvaka", "Ranasinghe", 28));

SerializeSourceGen();

public class Person
{
    public Person(string firstName, string lastName, uint age)
    {
        FirstName = firstName;
        LastName = lastName;
        Age = age;
    }

    public string FirstName { get; set; }

    public string LastName { get; set; }

    public uint Age { get; set; }
}

[JsonSerializable(typeof(Person))]
internal partial class MyJsonContext : JsonSerializerContext
{
}
  1. Run with dotnet run command. Notice the build fails.
  2. Now replace the contents of Program.cs with the following
using Experiments;
using System.Text.Json;

string SerializeSourceGen() => JsonSerializer.Serialize(new Person("Vishvaka", "Ranasinghe", 28));
SerializeSourceGen();
  1. Create new Models.cs file with the contents as:
using System.Text.Json.Serialization;

namespace Experiments
{
    public class Person
    {
        public Person(string firstName, string lastName, uint age)
        {
            FirstName = firstName;
            LastName = lastName;
            Age = age;
        }

        public string FirstName { get; set; }

        public string LastName { get; set; }

        public uint Age { get; set; }
    }

    [JsonSerializable(typeof(Person))]
    internal partial class MyJsonContext : JsonSerializerContext
    {
    }
}
  1. Project build and run as expected.

Not having support for classes cause the need to create additional files with namespaces when trying to create a simple application using top-level statements.

Author: Vake93
Assignees: -
Labels:

area-System.Text.Json, untriaged

Milestone: -

@stephentoub
Copy link
Member

Thanks. Dup of #56920.

@ghost ghost locked as resolved and limited conversation to collaborators Sep 7, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Text.Json untriaged New issue has not been triaged by the area owner
Projects
None yet
Development

No branches or pull requests

3 participants