Open aubergemediale opened 4 years ago
Deserializer is expecting a number long (number) primitive type
but you are giving it a null
. You should make Amount nullable
as in public long? Amount { get; set; }
Also, deserializing a null to 0 doesn't make sense since 0 and null are essentially 2 different values when dealing with primitive types hence ? nullable
feature.
If there is not any Amount
, just don't pass it in the payload and it'll default to 0
. However, if you make it ? nullable
it will be null
when you don't pass it.
@tandasima I can't agree with you here. Sure, I could work around this with making the long a Nullable
Also, I cannot influence what the api is getting as Json. I am getting:
JSON null means: also undefined, NaN, +/- infinity, it doen't strictly equal C# null.
So, imo, the C# property should basically 'stay' it's default value of 0, because no better value can be assigned.
Also, Newtonsoft's and Microsofts new serializers do it this way.
So I hope I find a way with Utf8Json to have the null resolve to 0. Maybe there is a way to override or customize this behavior, but I can't find that in the documentation...
When I deserialize json that contains
{"address":"an address","amount":null}
to a target Type like
I get an Exception: expected:'Number Token', actual:'null', at offset:107
My expectation would be, that the long gets initialized to its default value of 0;
Afaik, in json or javascript, it is not wrong to have a number be null. It's just a falsy value.
And I think the least surprise would be if this would not throw and just set the number to 0.
Is there maybe any option I can use to achieve this? This is not so easy to fix if you don't have control of the production of the json you are getting.