-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Closed
Closed
Copy link
Description
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:
dotnet new consoledotnet 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); // failsThis 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