CSharp: JSON values with decimal point are generated as long
properties, should be double
/decimal
#2636
Labels
long
properties, should be double
/decimal
#2636
Minimal reproducible example:
JSON:
Resulting CSharp:
Options are default:
As you can see, QuickType results in type
long
, even though there is a.0
in the JSON.Yes, if I change it to
.1
, the resulting type will bedouble
ordecimal
, depending on the settings.But I want QuickType to detect that I have
.0
in the value, and generate a decimal number type, not integer.Current behavior:
123.0
(i.e. there is a decimal part, but it's zero), the resulting type islong
.123.1
(i.e. there is a decimal part, and it's not zero), the resulting type isdouble
ordecimal
.123
(i.e. there is no decimal part), the resulting type islong
.Expected/desirable behavior:
123.0
(i.e. there is a decimal part, but it's zero), the resulting type isdouble
ordecimal
.123.1
(i.e. there is a decimal part, and it's not zero), the resulting type isdouble
ordecimal
.123
(i.e. there is no decimal part), the resulting type islong
.Why?
Because otherwise C# throws an error like this:
the stack trace above is from my actual JSON that I needed to convert to CSharp classes in QuickType. but the point still stands - the System.Text.Json serializer cannot deserialize
123.0
into a long, therefore it should be a decimal or a double.This change can become a default behavior, or be toggleable with a checkbox in the options, doesn't matter.
The only workaround I see now is to replace
/(\d+)\.0+\b/
(or something) with$1.001
or something, e.g. turn123.0
into123.001
to force QuickType to turn numbers with.0
into decimals, while keeping integer values (e.g.123
) intact.The text was updated successfully, but these errors were encountered: