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

JSON source generator fails to compile if context type isn't in a namespace #56920

Closed
stephentoub opened this issue Aug 5, 2021 · 1 comment · Fixed by #57183
Closed

JSON source generator fails to compile if context type isn't in a namespace #56920

stephentoub opened this issue Aug 5, 2021 · 1 comment · Fixed by #57183
Assignees
Milestone

Comments

@stephentoub
Copy link
Member

This fails to compile:

using System;
using System.Text.Json.Serialization;

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

internal class Book
{
    public string? Name { get; set; }
}

[JsonSerializable(typeof(Book))]
internal partial class MyJsonContext : JsonSerializerContext { }

with the errors:

Error	CS0534	'MyJsonContext' does not implement inherited abstract member 'JsonSerializerContext.GetTypeInfo(Type)'
Error	CS7036	There is no argument given that corresponds to the required formal parameter 'instanceOptions' of 'JsonSerializerContext.JsonSerializerContext(JsonSerializerOptions?, JsonSerializerOptions?)'

but wrap both the type to be serialized and the context type in a namespace, and then it compiles fine:

using System;
using System.Text.Json.Serialization;

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

namespace Anything
{
    internal class Book
    {
        public string? Name { get; set; }
    }

    [JsonSerializable(typeof(Book))]
    internal partial class MyJsonContext : JsonSerializerContext { }
}
@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 5, 2021
@ghost
Copy link

ghost commented Aug 5, 2021

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

Issue Details

This fails to compile:

using System;
using System.Text.Json.Serialization;

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

internal class Book
{
    public string? Name { get; set; }
}

[JsonSerializable(typeof(Book))]
internal partial class MyJsonContext : JsonSerializerContext { }

with the errors:

Error	CS0534	'MyJsonContext' does not implement inherited abstract member 'JsonSerializerContext.GetTypeInfo(Type)'
Error	CS7036	There is no argument given that corresponds to the required formal parameter 'instanceOptions' of 'JsonSerializerContext.JsonSerializerContext(JsonSerializerOptions?, JsonSerializerOptions?)'

but wrap both the type to be serialized and the context type in a namespace, and then it compiles fine:

using System;
using System.Text.Json.Serialization;

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

namespace Anything
{
    internal class Book
    {
        public string? Name { get; set; }
    }

    [JsonSerializable(typeof(Book))]
    internal partial class MyJsonContext : JsonSerializerContext { }
}
Author: stephentoub
Assignees: -
Labels:

area-System.Text.Json, untriaged

Milestone: -

@stephentoub stephentoub added this to the 6.0.0 milestone Aug 5, 2021
@layomia layomia removed the untriaged New issue has not been triaged by the area owner label Aug 5, 2021
@layomia layomia self-assigned this Aug 5, 2021
@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Aug 11, 2021
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Aug 11, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Sep 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants