Skip to content

fix(csharp): serialize NaN and infinity in struct JSON - #4921

Merged
CurtHagenlocher merged 1 commit into
apache:mainfrom
CurtHagenlocher:fix-json-nonfinite
Oct 18, 2026
Merged

CurtHagenlocher merged 1 commit into
apache:mainfrom
CurtHagenlocher:fix-json-nonfinite

Conversation

@CurtHagenlocher

Copy link
Copy Markdown
Contributor

What's changed

When a struct is serialized to JSON (the default StructBehavior.JsonString), float and double fields are written with Utf8JsonWriter.WriteNumberValue. That throws ArgumentException for NaN and ±Infinity, so a single non-finite value made its whole row unreadable.

These values are now written as the strings "NaN", "Infinity" and "-Infinity". That's how System.Text.Json writes them when JsonNumberHandling.AllowNamedFloatingPointLiterals is set. StructBehavior.Strict is unchanged and still returns the actual float/double values.

This came up in review of #4919, where variants serialized to JSON use the same writer.

Tests

NonFiniteFloat and NonFiniteDouble check the JSON for NaN, Infinity and -Infinity. NonFiniteDouble also checks that the struct result keeps the actual NaN value. Without the fix, both throw the ArgumentException.

🤖 Generated with Claude Code

Utf8JsonWriter rejects non-finite numbers, so reading a struct with a NaN
or infinite float or double field threw ArgumentException under
StructBehavior.JsonString. Write these values as the strings "NaN",
"Infinity" and "-Infinity".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

No unresolved review issues were identified, and all approval assessments are positive.

No open issues 🎉

What changed in this PR

Fixes struct JSON serialization for NaN and infinite floating-point values by emitting named JSON strings instead of throwing exceptions.

Changes:

  • Serialize non-finite float and double values safely.
  • Add regression tests for JSON output and preserved values.
File Description
csharp/​src/​Apache.Arrow.Adbc/​Extensions/​IArrowArrayExtensions.cs Handles non-finite float and double JSON values.
csharp/​test/​Apache.Arrow.Adbc.Tests/​SerializeStructToJsonTests.cs Tests serialization of non-finite values.

Review effort: Lite

@CurtHagenlocher
CurtHagenlocher merged commit 52f7928 into apache:main Oct 18, 2026
9 checks passed
@CurtHagenlocher
CurtHagenlocher deleted the fix-json-nonfinite branch October 18, 2026 17:56
CurtHagenlocher added a commit that referenced this pull request Oct 18, 2026
## What's changed

Adds the variant test promised in #4919. Variants converted to JSON use
the same writer as structs, so they already get the behavior from #4921:
NaN and ±Infinity are written as the strings `"NaN"`, `"Infinity"` and
`"-Infinity"`. This test pins that down.

`VariantNonFiniteValues` reads a variant column with a non-finite double
and float inside an object and an array, and a top-level NaN:
- With `StructBehavior.JsonString`, it checks the exact JSON.
- With `StructBehavior.Strict`, it checks that the .NET values keep the
actual NaN and infinity values.

With the #4921 change reverted, the JSON case fails with the original
`ArgumentException`.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants