Skip to content

JsonProvider infers Decimal, but then cannot parse exponential notation #1230

@WallaceKelly

Description

@WallaceKelly

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>.$JsonRuntime.GetNonOptionalValue@143-5.Invoke(String message)
   at Program.main@11.Invoke(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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions