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 code generator: cannot convert from 'System.Text.Json.Utf8JsonWriter' to 'System.IO.Stream' #58118

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

Comments

@Jericho
Copy link

Jericho commented Aug 25, 2021

My library contains the following BouncedEvent class and BounceType enum:

public class BouncedEvent
{
    public BouncedEvent()
    {
    }

    [JsonPropertyName("ip")]
    public string IpAddress { get; set; }

    [JsonPropertyName("reason")]
    public string Reason { get; set; }

    [JsonPropertyName("status")]
    public string Status { get; set; }

    [JsonPropertyName("type")]
    public BounceType Type { get; set; }
}

[JsonConverter(typeof(StringEnumConverter<BounceType>))]
public enum BounceType
{
    [EnumMember(Value = "bounce")]
    HardBounce,

    [EnumMember(Value = "blocked")]
    Block
}

System.Text.Json code generator generates the following snippet:

private static void BouncedEventSerialize(global::System.Text.Json.Utf8JsonWriter writer, global::MyLibrary.Models.BouncedEvent value)
{
    if (value == null)
    {
        writer.WriteNullValue();
        return;
    }
        
    writer.WriteStartObject();
    writer.WriteString(ipPropName, value.IpAddress);
    writer.WriteString(reasonPropName, value.Reason);
    writer.WriteString(statusPropName, value.Status);
    writer.WritePropertyName(typePropName);

    // The following line is causing the errors described bellow 
    global::System.Text.Json.JsonSerializer.Serialize(writer, value.Type, global::MyLibary.Utilities.MyJsonSerializerContext.Default.BounceType);

    writer.WriteEndObject();
}

VS.NET refuses to compile and reports the following errors:

CS1503	Argument 1: cannot convert from 'System.Text.Json.Utf8JsonWriter' to 'System.IO.Stream'	MyLibrary (net5.0)	D:\_build\MyLibrary\Source\StrongGrid\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\MyJsonSerializerContext.BouncedEvent.g.cs	400	Active
CS1503	Argument 3: cannot convert from 'System.Text.Json.Serialization.Metadata.JsonTypeInfo<MyLibrary.Models.BounceType>' to 'System.Text.Json.JsonSerializerOptions?'	MyLibrary (net5.0)	D:\_build\MyLibrary\Source\MyLibrary\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\MyJsonSerializerContext.BouncedEvent.g.cs	400	Active

I'm using the latest daily build of System.Text.Json: 6.0.0-rc.2.21424.25

❯ dotnet --info
.NET SDK (reflecting any global.json):
Version: 5.0.400
Commit: ...snip...

Runtime Environment:
OS Name: Windows
OS Version: 10.0.22000
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\5.0.400\

Host (useful for support):
Version: 6.0.0-preview.7.21377.19
Commit: ...snip...

.NET SDKs installed:
2.1.816 [C:\Program Files\dotnet\sdk]
3.1.302 [C:\Program Files\dotnet\sdk]
3.1.400 [C:\Program Files\dotnet\sdk]
3.1.411 [C:\Program Files\dotnet\sdk]
5.0.103 [C:\Program Files\dotnet\sdk]
5.0.104 [C:\Program Files\dotnet\sdk]
5.0.205 [C:\Program Files\dotnet\sdk]
5.0.302 [C:\Program Files\dotnet\sdk]
5.0.400 [C:\Program Files\dotnet\sdk]
6.0.100-preview.7.21379.14 [C:\Program Files\dotnet\sdk]

@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 25, 2021
@ghost
Copy link

ghost commented Aug 25, 2021

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

Issue Details

My library contains the following BouncedEvent class and BounceType enum:

public class BouncedEvent
{
    public BouncedEvent()
    {
        EventType = EventType.Bounce;
    }

    [JsonPropertyName("ip")]
    public string IpAddress { get; set; }

    [JsonPropertyName("reason")]
    public string Reason { get; set; }

    [JsonPropertyName("status")]
    public string Status { get; set; }

    [JsonPropertyName("type")]
    public BounceType Type { get; set; }
}

[JsonConverter(typeof(StringEnumConverter<BounceType>))]
public enum BounceType
{
    [EnumMember(Value = "bounce")]
    HardBounce,

    [EnumMember(Value = "blocked")]
    Block
}

System.Text.Json code generator generates the following snippet:

private static void BouncedEventSerialize(global::System.Text.Json.Utf8JsonWriter writer, global::MyLibrary.Models.BouncedEvent value)
{
    if (value == null)
    {
        writer.WriteNullValue();
        return;
    }
        
    writer.WriteStartObject();
    writer.WriteString(ipPropName, value.IpAddress);
    writer.WriteString(reasonPropName, value.Reason);
    writer.WriteString(statusPropName, value.Status);
    writer.WritePropertyName(typePropName);

    // The following line is causing the errors described bellow 
    global::System.Text.Json.JsonSerializer.Serialize(writer, value.Type, global::MyLibary.Utilities.MyJsonSerializerContext.Default.BounceType);

    writer.WriteEndObject();
}

VS.NET refuses to compile and reports the following errors:

CS1503	Argument 1: cannot convert from 'System.Text.Json.Utf8JsonWriter' to 'System.IO.Stream'	MyLibrary (net5.0)	D:\_build\MyLibrary\Source\StrongGrid\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\MyJsonSerializerContext.BouncedEvent.g.cs	400	Active
CS1503	Argument 3: cannot convert from 'System.Text.Json.Serialization.Metadata.JsonTypeInfo<MyLibrary.Models.BounceType>' to 'System.Text.Json.JsonSerializerOptions?'	MyLibrary (net5.0)	D:\_build\MyLibrary\Source\MyLibrary\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\MyJsonSerializerContext.BouncedEvent.g.cs	400	Active

I'm using the latest daily build of System.Text.Json: 6.0.0-rc.2.21424.25

❯ dotnet --info
.NET SDK (reflecting any global.json):
Version: 5.0.400
Commit: ...snip...

Runtime Environment:
OS Name: Windows
OS Version: 10.0.22000
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\5.0.400\

Host (useful for support):
Version: 6.0.0-preview.7.21377.19
Commit: ...snip...

.NET SDKs installed:
2.1.816 [C:\Program Files\dotnet\sdk]
3.1.302 [C:\Program Files\dotnet\sdk]
3.1.400 [C:\Program Files\dotnet\sdk]
3.1.411 [C:\Program Files\dotnet\sdk]
5.0.103 [C:\Program Files\dotnet\sdk]
5.0.104 [C:\Program Files\dotnet\sdk]
5.0.205 [C:\Program Files\dotnet\sdk]
5.0.302 [C:\Program Files\dotnet\sdk]
5.0.400 [C:\Program Files\dotnet\sdk]
6.0.100-preview.7.21379.14 [C:\Program Files\dotnet\sdk]

Author: Jericho
Assignees: -
Labels:

area-System.Text.Json, untriaged

Milestone: -

@huoyaoyuan
Copy link
Member

huoyaoyuan commented Aug 25, 2021

The overload is introduced in .NET 6. You should upgrade your project's target framework to net6.0.

The JsonSerializer api should be upgraded by OOB package. I should be wrong here.

@eiriktsarpalis
Copy link
Member

Hi @Jericho, is this a console app targeting net5.0 that references a 6.0 System.Text.Json nuget package? I cannot reproduce the issue, would it be possible to share a standalone reproduction (like the one I shared here)? This would also need to include the csproj file you are defining.

@Jericho
Copy link
Author

Jericho commented Aug 26, 2021

@eiriktsarpalis I found more information and it turns out the problem has nothing to do with converting from 'System.Text.Json.Utf8JsonWriter' to 'System.IO.Stream' as the error message leads you to believe. This error message was simply misleading. The confusion come from the fact that Visual Studio 2019 obscures the underlying issue. Switching to VS.NET 2022 reveals more detailed information about the problem (it has to do with two classes sharing the same name). I raised a different issue with repro steps for the underlying problem.

This confusion could have been avoided if VS.NET 2019 displayed the same level of details that VS.NET 2022 does. It's also surprising that symptoms and the feedback provided in the "Error List" window for a given problem are so different depending on the version of VS

@eiriktsarpalis
Copy link
Member

Thanks @Jericho, in that case I'll close this in favor of #58198.

@ghost ghost locked as resolved and limited conversation to collaborators Sep 25, 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