JsonProvider infers Decimal, but then cannot parse exponential notation
The following code fails at runtime:
type ExponentialDataTypes =
FSharp.Data.JsonProvider< Sample = "{ \"mydata\": [ 1, 2.34567E5, 3.14 ] }" >
[<EntryPoint>]
let main argv =
let r = ExponentialDataTypes.Parse("{ \"mydata\": [2, 3.45678E5, 9.01 ] }")
printfn "An array of %s" (r.Mydata.[0].GetType().Name)
printfn "%A" r.Mydata
0 // return an integer exit code
It fails with this exception:
Unhandled Exception: System.Exception: Expecting a Decimal at '/mydata[1]', got 345678
at <StartupCode$FSharp-Data>[email protected](String message)
at [email protected](IJsonDocument t)
at Microsoft.FSharp.Collections.ArrayModule.MapIndexed[T,TResult](FSharpFunc`2 mapping, T[] array)
at Program.main(String[] argv)
The JSON type provider infers the array of numbers to be a Decimal array. Then, when presented with an array of numbers that has an exponential notation (e.g,. 3.45678E5), the generated type cannot parse the array of numbers. It fails to parse the number serialized in exponential notation.
I have not dug into the code yet, but I would expect it has something to do with the fact that Decimal.Parse(...) cannot parse exponential notation by default, as described here.
If you provide a sample with only exponential notation, the inferred type is Double array (instead of Decimal array). In that case, an exception is not thrown, presumably because of the fact that Double.Parse(...) can parse exponential notation.
Another symptom if when the sample is/has a list and the decimal element is missing on some items. It is correctly inferred as a decimal option, but when parsing a real json, will always return None for that field.
The workaround of using only exponential form in the sample works ok too. Many thanks