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

Can't apply a [JsonConstructor] attribute on a record class with more than one constructor #84544

Closed
zxopink opened this issue Apr 9, 2023 · 2 comments

Comments

@zxopink
Copy link

zxopink commented Apr 9, 2023

Description

my record:

public record class TargetScreen(PointF[] Locations) : Attack
{
    public TargetScreen(Point[] locations, Size size) 
    : this(locations.Select(p => new PointF(p.X / size.Width, p.Y / size.Height)).ToArray())
    { /*Empty*/ }

    public bool IsEmpty => Locations.Length == 0;
    public static readonly TargetScreen Empty = new TargetScreen(Array.Empty<PointF>());
}

I want to serialize the record using JsonSerializer but it throws an exception:
Deserialization of types without a parameterless constructor, a singular parameterized constructor, or a parameterized constructor annotated with 'JsonConstructorAttribute' is not supported

I want to add a [JsonConstructor] attribute to the TargetScreen(PointF[] Locations) but it won't allow me to put an attribute on it

Reproduction Steps

Copy the class and deserialize it:

using System.Drawing;
using System.Text.Json;

TargetScreen target = new(new PointF[] { new(1f, 2f), new(3f, 4f) });
string json = JsonSerializer.Serialize(target);
Console.WriteLine(json);

TargetScreen back = JsonSerializer.Deserialize<TargetScreen>(json);
Console.WriteLine(back);

Expected behavior

I expected to put a [JsonConstructor] attribute on the record and make the record's main constructor the json constructor.

Actual behavior

Gives an error:
Attribute 'JsonConstructor' is not valid on this declaration type. It is only valid on 'constructor' declarations.

Regression?

No response

Known Workarounds

switch to a normal class or play around with the constructors

Configuration

Visual Studio 2022, .NET 6, C# 9

Other information

No response

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Apr 9, 2023
@ghost
Copy link

ghost commented Apr 9, 2023

Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

my record:

public record class TargetScreen(PointF[] Locations) : Attack
{
    public TargetScreen(Point[] locations, Size size) 
    : this(locations.Select(p => new PointF(p.X / size.Width, p.Y / size.Height)).ToArray())
    { /*Empty*/ }

    public bool IsEmpty => Locations.Length == 0;
    public static readonly TargetScreen Empty = new TargetScreen(Array.Empty<PointF>());
}

I want to serialize the record using JsonSerializer but it throws an exception:
Deserialization of types without a parameterless constructor, a singular parameterized constructor, or a parameterized constructor annotated with 'JsonConstructorAttribute' is not supported

I want to add a [JsonConstructor] attribute to the TargetScreen(PointF[] Locations) but it won't allow me to put an attribute on it

Reproduction Steps

Copy the class and deserialize it:

using System.Drawing;
using System.Text.Json;

TargetScreen target = new(new PointF[] { new(1f, 2f), new(3f, 4f) });
string json = JsonSerializer.Serialize(target);
Console.WriteLine(json);

TargetScreen back = JsonSerializer.Deserialize<TargetScreen>(json);
Console.WriteLine(back);

Expected behavior

I expected to put a [JsonConstructor] attribute on the record and make the record's main constructor the json constructor.

Actual behavior

Gives an error:
Attribute 'JsonConstructor' is not valid on this declaration type. It is only valid on 'constructor' declarations.

Regression?

No response

Known Workarounds

switch to a normal class or play around with the constructors

Configuration

Visual Studio 2022, .NET 6, C# 9

Other information

No response

Author: zxopink
Assignees: -
Labels:

area-System.Text.Json

Milestone: -

@zxopink zxopink closed this as completed Apr 9, 2023
@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Apr 9, 2023
@eiriktsarpalis
Copy link
Member

This is a constraint of C# language itself, and is tracked by the following issue: dotnet/csharplang#7047

@ghost ghost locked as resolved and limited conversation to collaborators May 9, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants