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 deserialization for positional record throws exception w/ Source Generator #56182

Closed
avdv opened this issue Jul 22, 2021 · 2 comments · Fixed by #56354
Closed

JSON deserialization for positional record throws exception w/ Source Generator #56182

avdv opened this issue Jul 22, 2021 · 2 comments · Fixed by #56354
Assignees
Milestone

Comments

@avdv
Copy link

avdv commented Jul 22, 2021

Description

Following the instructions here: https://devblogs.microsoft.com/dotnet/try-the-new-system-text-json-source-generator/ I created a new project, installed the latest build for the System.Text.Json package:

  1. dotnet new console
  2. dotnet add package System.Text.Json --version 6.0.0-rc.1.21372.4

Code:

internal record Person(string FirstName, string LastName);

[JsonSourceGenerationOptions(
    PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase)]
[JsonSerializable(typeof(Person))]
internal partial class MyJsonContext : JsonSerializerContext
{
}

Person person = new(FirstName: "Jane", LastName: "Doe");
byte[] utf8Json = JsonSerializer.SerializeToUtf8Bytes(person, MyJsonContext.Default.Person);
person = JsonSerializer.Deserialize<Person>(utf8Json); // works
person = JsonSerializer.Deserialize<Person>(utf8Json, MyJsonContext.Default.Person); // fails

This works when using the reflection based deserialization.

But throws a NotSupportedException: "Deserialization of types without a parameterless constructor, a singular parameterized constructor, or a parameterized constructor annotated with 'JsonConstructorAttribute' is not supported." although the record only has a single parameterized constructor, AFAIK.

Configuration

.NET SDK (reflecting any global.json):
 Version:   5.0.202
 Commit:    db7cc87d51

Runtime Environment:
 OS Name:     nixos
 OS Version:  21.05.1486.2a96414d7e3
 OS Platform: Linux
 RID:         linux-x64
 Base Path:   /nix/store/9q8cavv72f5z71w6rddfqk82q2waa4s0-dotnet-sdk-5.0.202/sdk/5.0.202/

Host (useful for support):
  Version: 5.0.5
  Commit:  2f740adc14

.NET SDKs installed:
  5.0.202 [/nix/store/9q8cavv72f5z71w6rddfqk82q2waa4s0-dotnet-sdk-5.0.202/sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 5.0.5 [/nix/store/9q8cavv72f5z71w6rddfqk82q2waa4s0-dotnet-sdk-5.0.202/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 5.0.5 [/nix/store/9q8cavv72f5z71w6rddfqk82q2waa4s0-dotnet-sdk-5.0.202/shared/Microsoft.NETCore.App]

To install additional .NET runtimes or SDKs:
  https://aka.ms/dotnet-download

Regression?

no.

Other information

Unhandled exception. System.NotSupportedException: Deserialization of types without a parameterless constructor, a singular parameterized constructor, or a parameterized constructor annotated with 'JsonConstructorAttribute' is not supported. Type 'Test.Person'. Path: $ | LineNumber: 0 | BytePositionInLine: 1.
 ---> System.NotSupportedException: Deserialization of types without a parameterless constructor, a singular parameterized constructor, or a parameterized constructor annotated with 'JsonConstructorAttribute' is not supported. Type 'Test.Person'.
   --- End of inner exception stack trace ---
   at System.Text.Json.ThrowHelper.ThrowNotSupportedException(ReadStack& state, Utf8JsonReader& reader, NotSupportedException ex)
   at System.Text.Json.ThrowHelper.ThrowNotSupportedException_DeserializeNoConstructor(Type type, Utf8JsonReader& reader, ReadStack& state)
   at System.Text.Json.Serialization.Converters.ObjectDefaultConverter`1.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
   at System.Text.Json.Serialization.Converters.JsonMetadataServicesConverter`1.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
   at System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
   at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
   at System.Text.Json.JsonSerializer.ReadCore[TValue](JsonConverter jsonConverter, Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
   at System.Text.Json.JsonSerializer.ReadUsingMetadata[TValue](ReadOnlySpan`1 utf8Json, JsonTypeInfo jsonTypeInfo, Nullable`1 actualByteCount)
   at System.Text.Json.JsonSerializer.ReadUsingMetadata[TValue](ReadOnlySpan`1 json, JsonTypeInfo jsonTypeInfo)
   at System.Text.Json.JsonSerializer.Deserialize[TValue](String json, JsonTypeInfo`1 jsonTypeInfo)
   at sourcegen.Program.Main(String[] args) in /home/claudio/src/sourcegen/Program.cs:line 31
@dotnet-issue-labeler dotnet-issue-labeler bot added area-System.Text.Json untriaged New issue has not been triaged by the area owner labels Jul 22, 2021
@ghost
Copy link

ghost commented Jul 22, 2021

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

Issue Details

Description

Following the instructions here: https://devblogs.microsoft.com/dotnet/try-the-new-system-text-json-source-generator/ I created a new project, installed the latest build for the System.Text.Json package:

  1. dotnet new console
  2. dotnet add package System.Text.Json --version 6.0.0-rc.1.21372.4

Code:

internal record Person(string FirstName, string LastName);

[JsonSourceGenerationOptions(
    PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase)]
[JsonSerializable(typeof(Person))]
internal partial class MyJsonContext : JsonSerializerContext
{
}

Person person = new(FirstName: "Jane", LastName: "Doe");
byte[] utf8Json = JsonSerializer.SerializeToUtf8Bytes(person, MyJsonContext.Default.Person);
person = JsonSerializer.Deserialize<Person>(utf8Json); // works
person = JsonSerializer.Deserialize<Person>(utf8Json, MyJsonContext.Default.Person); // fails

This works when using the reflection based deserialization.

But throws a NotSupportedException: "Deserialization of types without a parameterless constructor, a singular parameterized constructor, or a parameterized constructor annotated with 'JsonConstructorAttribute' is not supported." although the record only has a single parameterized constructor, AFAIK.

Configuration

.NET SDK (reflecting any global.json):
 Version:   5.0.202
 Commit:    db7cc87d51

Runtime Environment:
 OS Name:     nixos
 OS Version:  21.05.1486.2a96414d7e3
 OS Platform: Linux
 RID:         linux-x64
 Base Path:   /nix/store/9q8cavv72f5z71w6rddfqk82q2waa4s0-dotnet-sdk-5.0.202/sdk/5.0.202/

Host (useful for support):
  Version: 5.0.5
  Commit:  2f740adc14

.NET SDKs installed:
  5.0.202 [/nix/store/9q8cavv72f5z71w6rddfqk82q2waa4s0-dotnet-sdk-5.0.202/sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 5.0.5 [/nix/store/9q8cavv72f5z71w6rddfqk82q2waa4s0-dotnet-sdk-5.0.202/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 5.0.5 [/nix/store/9q8cavv72f5z71w6rddfqk82q2waa4s0-dotnet-sdk-5.0.202/shared/Microsoft.NETCore.App]

To install additional .NET runtimes or SDKs:
  https://aka.ms/dotnet-download

Regression?

no.

Other information

Unhandled exception. System.NotSupportedException: Deserialization of types without a parameterless constructor, a singular parameterized constructor, or a parameterized constructor annotated with 'JsonConstructorAttribute' is not supported. Type 'Test.Person'. Path: $ | LineNumber: 0 | BytePositionInLine: 1.
 ---> System.NotSupportedException: Deserialization of types without a parameterless constructor, a singular parameterized constructor, or a parameterized constructor annotated with 'JsonConstructorAttribute' is not supported. Type 'Test.Person'.
   --- End of inner exception stack trace ---
   at System.Text.Json.ThrowHelper.ThrowNotSupportedException(ReadStack& state, Utf8JsonReader& reader, NotSupportedException ex)
   at System.Text.Json.ThrowHelper.ThrowNotSupportedException_DeserializeNoConstructor(Type type, Utf8JsonReader& reader, ReadStack& state)
   at System.Text.Json.Serialization.Converters.ObjectDefaultConverter`1.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
   at System.Text.Json.Serialization.Converters.JsonMetadataServicesConverter`1.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
   at System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
   at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
   at System.Text.Json.JsonSerializer.ReadCore[TValue](JsonConverter jsonConverter, Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
   at System.Text.Json.JsonSerializer.ReadUsingMetadata[TValue](ReadOnlySpan`1 utf8Json, JsonTypeInfo jsonTypeInfo, Nullable`1 actualByteCount)
   at System.Text.Json.JsonSerializer.ReadUsingMetadata[TValue](ReadOnlySpan`1 json, JsonTypeInfo jsonTypeInfo)
   at System.Text.Json.JsonSerializer.Deserialize[TValue](String json, JsonTypeInfo`1 jsonTypeInfo)
   at sourcegen.Program.Main(String[] args) in /home/claudio/src/sourcegen/Program.cs:line 31
Author: avdv
Assignees: -
Labels:

area-System.Text.Json, untriaged

Milestone: -

@ericstj ericstj added bug and removed untriaged New issue has not been triaged by the area owner labels Jul 22, 2021
@ericstj ericstj added this to the 6.0.0 milestone Jul 22, 2021
@layomia
Copy link
Contributor

layomia commented Jul 22, 2021

Thanks for the feedback! I'm currently working on a PR fixing this and will tag you once it's ready.

@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Jul 27, 2021
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Aug 6, 2021
@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.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants