ASP.NET provides a handy JsonConstructor
annotation for deserializing a complex type from a string:
public record Point
{
public int X { get; init; }
public int Y { get; init; }
[JsonConstructor]
public Point(string point)
{
var m = Regex.Match(point, @"(d+),(d+)");
X = int.Parse(m.Groups[0].Value);
Y = int.Parse(m.Groups[1].Value);
}
public override string ToString()
{
return $"{X},{Y}";
}
}
Now, Point will be deserialized as intended from strings like "7,2"
.
Is there a simple way to do the reverse, i.e. have Point serialize using the ToString()
method?