Skip to content

[Repo Assist] Fix JsonConversions.AsDecimal to handle JsonValue.Float#1608

Merged
dsyme merged 4 commits intomainfrom
repo-assist/fix-json-decimal-float-1230-a945dd1e3b1536f6
Feb 22, 2026
Merged

[Repo Assist] Fix JsonConversions.AsDecimal to handle JsonValue.Float#1608
dsyme merged 4 commits intomainfrom
repo-assist/fix-json-decimal-float-1230-a945dd1e3b1536f6

Conversation

@github-actions
Copy link
Contributor

🤖 Repo Assist here — I'm an automated AI assistant for this repository.

Closes #1230

Problem

When the JSON parser encounters a number in exponential notation (e.g. 3.45678E5), Decimal.TryParse with NumberStyles.Currency fails because Currency does not include AllowExponent. The value is therefore stored as JsonValue.Float instead of JsonValue.Number.

JsonConversions.AsDecimal previously only handled JsonValue.Number (and JsonValue.String), so any field inferred as decimal that received an exponential-notation value at runtime threw "Expecting a Decimal at '…', got …".

Concrete reproduction (from #1230):

type ExponentialDataTypes =
    JsonProvider(Sample = """{ "mydata": [ 1, 2.34567E5, 3.14 ] }""")

// Fails with: Expecting a Decimal at '/mydata[1]', got 345678
ExponentialDataTypes.Parse("""{ "mydata": [2, 3.45678E5, 9.01] }""")

Root Cause

  1. Sample 2.34567E5Decimal.TryParse fails → stored as JsonValue.Float(234567.0)
  2. 234567.0 is an integer-valued float in Int32 range → inferred as Integer in JsonInference.fs
  3. 3.14JsonValue.Number(3.14M) → inferred as Decimal
  4. [Integer, Integer, Decimal] unifies to Decimal
  5. At runtime, 3.45678E5JsonValue.Float(345678.0)AsDecimal returns None → exception

Fix

Add JsonValue.Float handling to JsonConversions.AsDecimal, mirroring the existing pattern in AsInteger / AsInteger64:

| JsonValue.Float f when not (Double.IsInfinity f) && not (Double.IsNaN f) ->
    try Some (decimal f) with :? OverflowException -> None

Test Status

  • Build passes
  • All 248 FSharp.Data.Tests pass (including new regression test)
  • All 2841 FSharp.Data.Core.Tests pass (1 pre-existing network test fails due to environment proxy blocking `(www.google.com/redacted) — unrelated to this change)

Generated by Repo Assist

To install this workflow, run gh aw add githubnext/agentics/workflows/repo-assist.md@b0296681ad309e6455276244363810b1e7d98335. View source at https://github.com/githubnext/agentics/tree/b0296681ad309e6455276244363810b1e7d98335/workflows/repo-assist.md.

Warning

⚠️ Firewall blocked 1 domain

The following domain was blocked by the firewall during workflow execution:

  • www.google.com

When the JSON parser encounters a number in exponential notation such as
'3.45678E5', Decimal.TryParse with NumberStyles.Currency fails (because
Currency does not include AllowExponent), so the value is stored as
JsonValue.Float rather than JsonValue.Number.

JsonConversions.AsDecimal previously only handled JsonValue.Number, so
any field inferred as 'decimal' that received an exponential-notation value
at runtime threw 'Expecting a Decimal … got …'.

Fix: add a JsonValue.Float case to JsonConversions.AsDecimal that
converts the float to decimal (mirroring the existing AsInteger /
AsInteger64 patterns), guarding against Infinity/NaN and overflow.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@dsyme dsyme marked this pull request as ready for review February 22, 2026 09:02
@dsyme dsyme merged commit 77b8539 into main Feb 22, 2026
2 checks passed
@dsyme dsyme deleted the repo-assist/fix-json-decimal-float-1230-a945dd1e3b1536f6 branch February 22, 2026 10:14
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?

Copy link

@alex-piccione alex-piccione left a comment

Choose a reason for hiding this comment

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

Generated test has a huge comment that I'm not sure is required to stay in the code and is not following the code style of the other tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants