У нас есть пользовательский тип строки в библиотеке моделей. Неявные преобразования этого типа строк существуют как для byte [], так и для строк.
Есть ли способ поддержки неявных преобразований с помощью JsonSerializer, не заставляя потребителей реализовывать свой собственный JsonConverter?
public class CustomString
{
private readonly string _str;
public CustomString(string str)
{
_str = str;
}
public static implicit operator CustomString(string str)
{
return str == null ? null : new CustomString(str);
}
public static implicit operator CustomString(byte[] byteArr)
{
return byteArr == null ? null : new CustomString(System.Text.Encoding.UTF8.GetString(byteArr));
}
public override string ToString()
{
return _str;
}
}
И исключение:
System.Text.Json.JsonException : The JSON value could not be converted to Namespace.CustomString. Path: ... | LineNumber: xxx | BytePositionInLine: xxx.