I have implemented a EANCode13 class with a constructor with a parameter like this:
public class EAN13Code {
public EAN13Code(string value) {
// validate it
}
[JsonConstructor]
public EAN13Code(long value) {
// validate it
}
}
public class ApiResponse
{
public EAN13Code Code { get; }
[JsonConstructor]
public ApiResponse(EAN13Code code) {
Code = code;
}
}
and a JSON like this:
{
"code" : 978020137962
}
Trying to deserialize simply like this doesn't work:
var reponse = JsonSerializer.Deserialize<ApiResponse>(json);
What do I need to do make it work?
Do I need a custom Json Converter? Writing one is easy but isn't there a simpler or better way?
Is there a library which can help?