paiden / Nett

.Net library for TOML
MIT License
223 stars 27 forks source link

Using System.Convert for type conversion. #52

Closed kzmi closed 6 years ago

kzmi commented 6 years ago

Currently, ConversionSets.NumercialType and ConversionSets.NumericalSize use cast expression for type conversions like:

new TomlConverter<TomlInt, int>((m, i) => (int)i.Value)

Such conversion leads unwanted result if overflow occurred.

For example,

X = 9999999999999

This TOML value will be parsed as TomlInt (Int64).
If this X was mapped to an Int32 field, 1316134911 will be set to the field.

Generally, such result would not be acceptable.
It is not easy to know whether the value on the field is an overflowed value or not.

This PR replaces cast expression with method of System.Convert.
It throws System.OverflowException if overflow will occur in the conversion.

I changed conversions Integer-to-Integer and Float-to-Integer.
Conversions Integer-to-Float and Float-to-Float still use implicit cast.

Note

System.Convert calls Math.Round() in Float-to-Integer conversion.
It may cause minor compatibility issue because the cast expression do truncation.