Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/FSharp.Data.Json.Core/JsonConversions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ type JsonConversions =
static member AsDecimal cultureInfo =
function
| JsonValue.Number n -> Some n
| JsonValue.Float f when not (Double.IsInfinity f) && not (Double.IsNaN f) ->
try
Some(decimal f)
with :? OverflowException ->
None
| JsonValue.String s -> TextConversions.AsDecimal cultureInfo s
| _ -> None

Expand Down
12 changes: 12 additions & 0 deletions tests/FSharp.Data.Tests/JsonProvider.fs
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,18 @@ let ``ParseList return result list`` () =
let prov = NumericFields.ParseList(""" [{"a":123}, {"a":987}] """)
prov |> Array.map (fun v -> v.A) |> Array.sort |> should equal [|123M; 987M|]

// Regression test for https://github.com/fsprojects/FSharp.Data/issues/1230
Copy link

@alex-piccione alex-piccione Feb 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Is this comment necessary?
  2. Why the JsonProvider is not created in-line like in the other tests?

// When a JSON array sample mixes decimal and exponential-notation numbers, the inferred
// type is decimal (because the exponential value is stored as JsonValue.Float and inferred
// as integer, which is then unified with decimal). At runtime, any exponential-notation
// number in the actual JSON must also be convertible to decimal.
type ExponentialDecimalProvider = JsonProvider<"""{"mydata": [1, 2.34567E5, 3.14]}""">

[<Test>]
let ``Decimal inferred from mixed-notation array can parse exponential notation at runtime`` () =
let result = ExponentialDecimalProvider.Parse("""{"mydata": [2, 3.45678E5, 9.01]}""")
result.Mydata |> should equal [| 2M; 345678M; 9.01M |]


type ServiceResponse = JsonProvider<"""[
{ "code": 0, "value": {"generic payload": "yes"}, "message": null},
Expand Down
Loading